PDA

View Full Version : trying to render one triangle.



mallyone
03-19-2009, 12:30 PM
I stole an opengl template from:
http://www.cprogramming.com/snippets/show.php?tip=7&count=30&page=0

I'll include my code with a couple modifications below. The problem is, I can't seem to get a triangle to render. Can someone take a quick look at the code and point me in the right direction.

Start code, sorry, I'm not familiar with ubbcode to be able to format it perdy like :).


#include <GLUT/glut.h>
#include <stdio.h>

/* process menu option 'op' */
void menu(int op) {
switch(op) {
case 'Q':
case 'q':
exit(0);
}
}

/* executed when a regular key is pressed */
void keyboardDown(unsigned char key, int x, int y) {

switch(key) {
case 'Q':
case 'q':
case 27: // ESC
exit(0);
}
}

/* executed when a regular key is released */
void keyboardUp(unsigned char key, int x, int y) {
exit(0);
}

/* executed when a special key is pressed */
void keyboardSpecialDown(int k, int x, int y) {

}

/* executed when a special key is released */
void keyboardSpecialUp(int k, int x, int y) {

}

/* reshaped window */
void reshape(int width, int height) {

GLfloat fieldOfView = 90.0f;
glViewport (0, 0, (GLsizei) width, (GLsizei) height);

glMatrixMode (GL_PROJECTION);
glLoadIdentity();
gluPerspective(fieldOfView, (GLfloat) width/(GLfloat) height, 0.1, 500.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

/* executed when button 'button' is put into state 'state' at screen position ('x', 'y') */
void mouseClick(int button, int state, int x, int y) {

}

/* executed when the mouse moves to position ('x', 'y') */
void mouseMotion(int x, int y) {

}

/* render the scene */
void draw() {

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

/* render the scene here */
glBegin(GL_TRIANGLES);
glColor3f( 0.0, 0.0, 0.0);
glVertex3f( 0.0, 0.0, 0.0); // Top
glVertex3f( 100.0, 100.0, 0.0); // Bottom Left
glVertex3f( 0.0, 100.0, 0.0); // Bottom Right
/*
glVertex3f( 0.0, 0.0, 0.0);
glVertex3f( 1.0, 1.0, 0.0);
glVertex3f( 0.0, 1.0, 0.0);
*/
glEnd();


glFlush();
glutSwapBuffers();
}

/* executed when program is idle */
void idle() {

}

/* initialize OpenGL settings */
void initGL(int width, int height) {

reshape(width, height);

glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClearDepth(1.0f);

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
}

/* initialize GLUT settings, register callbacks, enter main loop */
int main(int argc, char** argv) {

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow("Perspective's GLUT Template");

// register glut call backs
glutKeyboardFunc(keyboardDown);
glutKeyboardUpFunc(keyboardUp);
glutSpecialFunc(keyboardSpecialDown);
glutSpecialUpFunc(keyboardSpecialUp);
glutMouseFunc(mouseClick);
glutMotionFunc(mouseMotion);
glutReshapeFunc(reshape);
glutDisplayFunc(draw);
glutIdleFunc(idle);
// glutIgnoreKeyRepeat(true); // ignore keys held down

// create a sub menu
int subMenu = glutCreateMenu(menu);
glutAddMenuEntry("Do nothing", 0);
glutAddMenuEntry("Really Quit", 'q');

// create main "right click" menu
glutCreateMenu(menu);
glutAddSubMenu("Sub Menu", subMenu);
glutAddMenuEntry("Quit", 'q');
glutAttachMenu(GLUT_RIGHT_BUTTON);

initGL(800, 600);

glutMainLoop();
return 0;
}

randall
03-19-2009, 04:06 PM
z coordinate of your triangle has to be less then 0 to be visible, try this:
glVertex3f( 0.0, 0.0, -10.0); // Top
glVertex3f( 100.0, 100.0, -10.0); // Bottom Left
glVertex3f( 0.0, 100.0, -10.0); // Bottom Right

mallyone
03-19-2009, 04:21 PM
That was it :) Thank you Randall...

enunes
03-19-2009, 07:18 PM
Hi,

that happens because the default eye position is at 0.0, 0.0, 0.0; so as your triangle is at the z=0 plane you won't see it unless you change the eye position.

You can change the eye position/orientation using the utility function gluLookAt()
http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/glu/lookat.html

As an example, you could change the eye position to, for example, (0.0, 0.0, 10.0) and pointing to (0.0, 0.0, -1.0) as follows:
gluLookAt (0.0, 0.0, 10.0, 0.0, 0.0, -1.0, 0.0, 1.0, 0.0);
instead of drawing everything with z less than it is supposed to be :)