PDA

View Full Version : First OpenGL program...



mogget03
05-31-2010, 02:47 PM
Hi,

I'm trying to get started with OpenGL on OS X and I haven't been able to get this simple program to work. It's supposed to draw a triangle, but it isn't. I took some of it from NeHe's tutorial. I'd really appreciate if someone could tell me why it's not working! Here's the code:



#include <stdio.h>
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>

// Window width and height
#define width 800
#define height 600
// Window position
#define winXPos 720
#define winYPos 450

void draw(void) {
glEnable(GL_DEPTH_TEST);
// Clear screen and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Reset the view
glLoadIdentity();
// Make the view deeper so we can see the scene
glTranslatef(0.0f, 0.0f, 9.0f);

// Draw a rectangle
glNewList(1, GL_COMPILE_AND_EXECUTE);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_TRIANGLES);
glVertex3f(0.0f, 1000.0f, 0.0f);
glVertex3f(-1000.0f, -1000.0f, 0.0f);
glVertex3f(1000.0f, -1000.0f, 0.0f);
glEnd();
glEndList();

glFlush();
}

void initGL() {
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}

int main (int argc, char *argv[]) {
// Initialize opengl
glutInit(&amp;argc, argv);

// Set parameters: double buffer for smooth animation and RGB colors
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE | GLUT_DEPTH);

// Set window size
glutInitWindowSize(width, height);

// Set window title
glutCreateWindow("trebsim");

initGL();

// Set draw function
glutDisplayFunc(draw);

glutMainLoop();

return 0;
}

Jan
05-31-2010, 04:20 PM
It is very likely that your object lies somewhere outside the view-frustum. Try setting the glClearColor to different values each frame, then you will see whether your render-loop is actually properly executed. If so, play with glTranslatef to try to move your object into view. Try negative values for the z-axis, it's possible your object is rendered "behind" you.

Also:
1) You should CREATE the display list inside "initGL".
2) In "draw" you should only execute/render your display list, using glCallList.

At the moment you create (but never delete) one DL every frame, so you actually have a memory leak.

Hope that helps,
Jan.

Jan
05-31-2010, 04:21 PM
Also add glDisable (GL_CULL_FACE) to initGL, otherwise it could be that you look at the back-side of the triangle and it is therefore culled away (not rendered).

mogget03
06-01-2010, 11:11 AM
I did what you suggested: it turns out the render loop was not executing properly. I added a call to glutPostRedisplay() after the glFlush() call to fix that problem. Still working on getting the object to be visible.

Thanks for your help so far.

mhagain
06-01-2010, 02:31 PM
You should also create a doublebuffered format (glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);) and add a glutSwapBuffers at the end of each frame. Drop the display list code, you don't need it for this and it's only complicating things. Add a glViewport and set your projection matrix (via glOrtho, glFrustum or gluPerspective), then make your triangle smaller - 100s rather than 1000s, say, and see how you get on with that.

mogget03
06-01-2010, 06:03 PM
Things are working now, and I have a much better idea of what's going on. Here's my code:




#include <stdio.h>
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>

// Window width and height
#define width 800
#define height 600
// Window position
#define winXPos 720
#define winYPos 450

void draw(void) {
// Clear color and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Reset the view
glLoadIdentity();

// Draw the triangle
glTranslatef(0.0f, 0.0f, -9.0f);
glBegin(GL_TRIANGLES);
glVertex3f(0.0f, 3.0f, 0.0f);
glVertex3f(-3.0f, -3.0f, 0.0f);
glVertex3f(3.0f, -3.0f, 0.0f);
glEnd();

glutSwapBuffers();
}

void initGL() {
// Various initializations
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glDisable(GL_CULL_FACE);

// Make a viewport
glViewport(0, 0, width, height);
// Set the projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-5.0, 5.0, -5.0, 5.0, 1.0, 10.0);
// Switch to the modelview matrix
glMatrixMode(GL_MODELVIEW);
}

int main (int argc, char *argv[]) {
// Initialize opengl
glutInit(&amp;argc, argv);

// Set parameters: double buffer for smooth animation and RGB colors
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

// Set window size and position
glutInitWindowSize(width, height);
glutInitWindowPosition(winXPos, winYPos);

// Set window title
glutCreateWindow("window");

// Initialize the scene/window
initGL();

// Set draw function
glutDisplayFunc(draw);

glutMainLoop();

return 0;
}


Thanks!