Jump to content
  • Advertisement
Sign in to follow this  
billconan

OpenGL how come polygon doesn't appear

This topic is 4859 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi guys, i'm new to gl, i'm now following the opengl guide (redbook), i ran the first sample of the book, but no polygon was showed out as it is said in the book. the book's sample uses some head files which i can't find. the sgi ftp cannot be connected. so i modified the sample a bit. here is it:
#include <GL/glut.h>
#include <stdlib.h>

void init(void) 
{
   glClearColor (0.0, 0.0, 0.0, 0.0);
   glShadeModel (GL_FLAT);
}

void display(void)
{
	glClearColor(0.0, 0.0, 0.0, 0.0);
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(1.0, 1.0, 1.0);
	glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
	glBegin(GL_POLYGON);
	glVertex2f(-50, -50);
	glVertex2f(-50, 50);
	glVertex2f(50, 50);
	glVertex2f(50, -50);
	glEnd();
	glFlush();
}

void reshape (int w, int h)
{
   glViewport (0, 0, (GLsizei) w, (GLsizei) h); 
   glMatrixMode (GL_PROJECTION);
   glLoadIdentity ();
   glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
   glMatrixMode (GL_MODELVIEW);
}

void keyboard(unsigned char key, int x, int y)
{
   switch (key) {
      case 27:
         exit(0);
         break;
   }
}

int _tmain(int argc, _TCHAR* argv[])
{
   glutInit(&argc, argv);
   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
   glutInitWindowSize (500, 500); 
   glutInitWindowPosition (100, 100);
   glutCreateWindow (argv[0]);
   init ();
   glutDisplayFunc(display); 
   glutReshapeFunc(reshape);
   glutKeyboardFunc(keyboard);
   glutMainLoop();
   return 0;
}
there should be a white box in the middle of the screen, but what i got is only a solid black background. :( so what's the problem? thank you! screen shot:

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by billconan
the book's sample uses some head files which i can't find. the sgi ftp cannot be connected. so i modified the sample a bit.


Either that sample was completely borked or you modified it a bit too much.

display function:
glClearColor(0.0, 0.0, 0.0, 0.0);
No reason do do that every single frame

glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
See above, plus you didn't change to the projection matrix, plus you didn't load the identity matrix either, meaning you keep multiplying this to the modelview matrix again and again with every frame.

glVertex2f(-50, -50);
All coords are way outside the frustum you set up so in a best case you would see nothing but white. But you won't, as you don't specify a z coord it is set to 0, which is closer than the near clipping plane you set to in your call to glFrustum 1.5.

reshape function:

glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);

Isn't using the resolution/window size anyway, so no need to set it again. Doesn't hurt, though.

Share this post


Link to post
Share on other sites
if I'm not mistaken the glortho just makes an orthographic matrix and multiplies it to the current model view matrix. if this is the case your modelview matrix will constantly be multiplied over and over every frame. I don't see you loading the identity matrix so I'd expect whatever operations you're doing in the display func to accumulate. what you want is the same operation being applied every frame. not to accumulate your changes. try glLoadidentity() before your display func

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!