Show text in a scene with a loaded obj OpenGL

I'm trying to display some 2d text on a scene were I also load an obj. I'm using a perspective view, wich seems not to go well with what I want to do. I tried to create a 3d Buffer with z=0, but it does not work. Here is the code...someone could help me?

void* font = GLUT_BITMAP_TIMES_ROMAN_24;

void output(int x, int y, std::string str)

{

int len, i;

glRasterPos3f(x, y, 0.0);
len = str.length();

glPushMatrix();
for (i = 0; i < len; i++)
    glutBitmapCharacter(font, str[i]);

glPopMatrix();
glutPostRedisplay();

}

void reshape(int width, int height) {

const double aspectRatio = (float) width / height, fieldOfView = 45.0;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fieldOfView, aspectRatio,
    1.0, 1000.0);  // Znear and Zfar 
glViewport(0, 0, width, height);

}

void display(void) {

float tmp;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glColor3f(0.0, 1.0, 0.0);
std::string text_row_2 = "please work";
output(50, 145, text_row_2);


glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.f,0.f,3.f,0.f,0.f,-5.f,0.f,1.f,0.f);

// rotate it around the y axis
glRotatef(angle,0.f,1.f,0.f);

// scale the whole asset to fit into our view frustum 
tmp = scene_max.x-scene_min.x;
tmp = aisgl_max(scene_max.y - scene_min.y,tmp);
tmp = aisgl_max(scene_max.z - scene_min.z,tmp);
tmp = 1.f / tmp;
glScalef(tmp, tmp, tmp);

    // center the model
glTranslatef( -scene_center.x, -scene_center.y, -scene_center.z );  

    // if the display list has not been made yet, create a new one and
    // fill it with scene contents
if(scene_list == 0) {
    scene_list = glGenLists(1);
    glNewList(scene_list, GL_COMPILE);
        // now begin at the root node of the imported data and traverse
        // the scenegraph by multiplying subsequent local transforms
        // together on GL's matrix stack.
    recursive_render(scene, scene->mRootNode, 1.0);
    glEndList();
}

glCallList(scene_list);

glutSwapBuffers();

//do_motion();

}

int main(int argc, char **argv) {

struct aiLogStream stream;

glutInitWindowSize(900,600);
glutInitWindowPosition(100,100);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInit(&argc, argv);

glutCreateWindow("Assimp - Very simple OpenGL sample");
glutDisplayFunc(display);
glutReshapeFunc(reshape);

// get a handle to the predefined STDOUT log stream and attach
// it to the logging system. It will be active for all further
// calls to aiImportFile(Ex) and aiApplyPostProcessing.

stream = aiGetPredefinedLogStream(aiDefaultLogStream_STDOUT,NULL);
aiAttachLogStream(&stream);

// ... exactly the same, but this stream will now write the
// log file to assimp_log.txt
stream = aiGetPredefinedLogStream(aiDefaultLogStream_FILE,"assimp_log.txt");
aiAttachLogStream(&stream);

// the model name can be specified on the command line. 
if(argc>=2)
    loadasset( argv[1] );
else
{
    fprintf(stderr,"usage: >>SimpleOpenGLTexturedLoader <modelpath\\modelname>");
    exit(1);
}   

if (!InitGL())
{
    fprintf(stderr,"Initialization failed");
    return FALSE;
}

glutGet(GLUT_ELAPSED_TIME);
glutMainLoop();

// cleanup - calling 'aiReleaseImport' is important, as the library 
// keeps internal resources until the scene is freed again. Not 
// doing so can cause severe resource leaking.
aiReleaseImport(scene);

// We added a log stream to the library, it's our job to disable it
// again. This will definitely release the last resources allocated
// by Assimp.
aiDetachAllLogStreams();    

return 0;

}



Read more here: https://stackoverflow.com/questions/67380289/show-text-in-a-scene-with-a-loaded-obj-opengl

Content Attribution

This content was originally published by Faith97 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: