Jump to content
  • Advertisement
Sign in to follow this  
heefan

[SuperBible Code] Different result from the sample

This topic is 3470 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 all, I am new on OpenGL, and reading 'OpenGL Super Bible 4th' now. In the chapter 2 (Page 54), I run the sample code, but I got different result from the book. Actually I didn't get rectangle. The source code like this: #include "gltools.h" #include "math3d.h" void RenderScene(void) { // Clear the window with current clearing color glClear(GL_COLOR_BUFFER_BIT); // Set current drawing color to red // R G B glColor3f(1.0f, 0.0f, 0.0f); // Draw a filled rectangle with current color glRectf(-25.0f, 25.0f, 25.0f, -25.0f); // Flush drawing commands glFlush(); } /////////////////////////////////////////////////////////// // Set up the rendering state void SetupRC(void) { // Set clear color to blue glClearColor(0.0f, 0.0f, 1.0f, 1.0f); } /////////////////////////////////////////////////////////// // Called by GLUT library when the window has chanaged size void ChangeSize(GLsizei w, GLsizei h) { GLfloat aspectRatio; // Prevent a divide by zero if(h == 0) h = 1; // Set Viewport to window dimensions glViewport(0, 0, w, h); // Reset coordinate system glMatrixMode(GL_PROJECTION); glLoadIdentity(); CHAPTER 2 Using OpenGL 54LISTING 2.2 Continued // Establish clipping volume (left, right, bottom, top, near, far) aspectRatio = (GLfloat)w / (GLfloat)h; if (w <= h) glOrtho (-100.0, 100.0, -100 / aspectRatio, 100.0 / aspectRatio, 1.0, -1.0); else glOrtho (-100.0 * aspectRatio, 100.0 * aspectRatio, -100.0, 100.0, 1.0, -1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } /////////////////////////////////////////////////////////// // Main program entry point void main(void) int main(int argc, char* argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutCreateWindow(“GLRect”); glutDisplayFunc(RenderScene); glutReshapeFunc(ChangeSize); SetupRC(); glutMainLoop(); return 0; } The function of the program is to draw a rectangle. Is the code wrong?

Share this post


Link to post
Share on other sites
Advertisement
This is just a guess, but try throwing in the following at the beginning of your RenderScene function.

glLoadIdentity();
glTranslatef(0.0f, 0.0f, -0.5f);


If the rectangle is being drawn at 0 on the z axis and your camera's viewpoint is also at 0 on the z axis, you wont be able to see it. These two lines of code shift the draw position forward on the z axis.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!