Saturday 21 October 2017

opengl - My display() function only displays when it enters it the first time. Then it shows a blank window

itemprop="text">



void
init(void)
{
glEnable(GL_DEPTH_TEST);

glPolygonMode(GL_FRONT_AND_BACK,
GL_LINE);
}

void
display(void)
{
glClearColor(1.0, 1.0, 1.0,
1.0);




glClear(GL_COLOR_BUFFER_BIT
|
GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glScalef(1.0,1.0,1.0);
glColor3f(0.0,0.0,0.0);

glBegin(GL_POLYGON);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5,
0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.5,
-0.5);
glEnd();


glutSwapBuffers();


}



void
reshape(int w, int h)
{
int height = h;
int width =
w;
glViewport(0, 0, (GLsizei)w, (GLsizei)h);


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60,
(GLfloat)w / (GLfloat)h, 1.0, 100.0);

glMatrixMode(GL_MODELVIEW);
}



int
main(int argc, char* argv[])
{
Complex c(0,0);

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_DOUBLE |
GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(512, 512);

glutInitWindowPosition(100, 100);
winID =
glutCreateWindow("Fractal");
init();

glutDisplayFunc(display);
glutIdleFunc(display);

glutReshapeFunc(reshape);
// Compute the update rate here...

glutMainLoop();

return
0;
}



I
get a square if I put the code in display(), except the glutSwapBuffers() in if
condition which checks whether the code has entered display the first time. If I remove
the if, I get a white window


itemprop="text">
class="normal">Answer



Put an
extra glTranslatef(0.0f, 0.0f, -5.0f) in your
display function, to push everything 5 units back into the
scene, otherwise you wont see what you're drawing at the
origin.



Btw, the line
glScalef(1.0,1.0,1.0) doesn't do anything, remove it to avoid
unnecessary
calculations.




EDIT:
There
is no "eye" in OpenGL. More specifically, it is always fixed at (0,0,0) looking down at
the negative z-axis. You have to move the whole scene with the inverse of the eye
transformations. You can either move the scene manually so your eye can see it, or use
gluLookAt, where you can specify the "eye", but in the background
it just transforms the scene so the fixed "eye" can see
it.



Further reading: href="http://www.opengl.org/archives/resources/faq/technical/viewing.htm"
rel="nofollow">http://www.opengl.org/archives/resources/faq/technical/viewing.htm



No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...