PDA

View Full Version : Xcode begginer issues



Ricardo Sanchez
09-21-2009, 12:29 PM
Hi, I am following the popular NeHe tutorials, currently in Lesson 5, where I have a problem, the Objects are not rendering properly, here is my code:
#include <stdio.h>
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>

GLfloat rtri;
GLfloat rquad;


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


void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

/* piramid */
glTranslatef(-1.5, 0.0, -6.0);
glRotatef(rtri, 0.0, 1.0, 0.0);

glBegin(GL_TRIANGLES);

glColor3f(1.0, 0.0, 0.0);
glVertex3f( 0.0, 1.0, 0.0);
glColor3f(0.0, 1.0, 0.0);
glVertex3f(-1.0, -1.0, 1.0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f( 1.0, -1.0, 1.0);

glColor3f(1.0, 0.0, 0.0);
glVertex3f( 0.0, 1.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f( 1.0, -1.0, 1.0);
glColor3f(0.0, 1.0, 0.0);
glVertex3f( 1.0, -1.0, -1.0);

glColor3f(1.0, 0.0, 0.0);
glVertex3f( 0.0, 1.0, 0.0);
glColor3f(0.0, 1.0, 0.0);
glVertex3f( 1.0, -1.0, -1.0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(-1.0, -1.0, -1.0);

glColor3f(1.0, 0.0, 0.0);
glVertex3f( 0.0, 1.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(-1.0, -1.0, -1.0);
glColor3f(0.0, 1.0, 0.0);
glVertex3f(-1.0, -1.0, 1.0);

glEnd();



glLoadIdentity();

/* box */
glTranslatef(1.5, 0.0, -6.0);
glRotatef(rquad, 0.0, 1.0, 0.0);

glBegin(GL_QUADS);

glColor3f(0.0, 1.0, 0.0);
glVertex3f( 1.0, 1.0, -1.0);
glVertex3f(-1.0, 1.0, -1.0);
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f( 1.0, 1.0, 1.0);

glColor3f(1.0, 0.5, 0.0);
glVertex3f( 1.0, -1.0, 1.0);
glVertex3f(-1.0, -1.0, 1.0);
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f( 1.0, -1.0, -1.0);

glColor3f(1.0, 0.0, 0.0);
glVertex3f( 1.0, 1.0, 1.0);
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f(-1.0, -1.0, 1.0);
glVertex3f( 1.0, -1.0, 1.0);

glColor3f(1.0, 1.0, 0.0);
glVertex3f( 1.0, -1.0, -1.0);
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f(-1.0, 1.0, -1.0);
glVertex3f( 1.0, 1.0, -1.0);

glColor3f(0.0, 0.0, 1.0);
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f(-1.0, 1.0, -1.0);
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f(-1.0, -1.0, 1.0);

glColor3f(1.0, 0.0, 1.0);
glVertex3f( 1.0, 1.0, -1.0);
glVertex3f( 1.0, 1.0, 1.0);
glVertex3f( 1.0, -1.0, 1.0);
glVertex3f( 1.0, -1.0, -1.0);

glEnd();

rtri += 0.02;
rquad -= 0.015;

glutSwapBuffers();
glutPostRedisplay();
}



void reshape(int w, int h) {
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (GLfloat)w / (GLfloat)h, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}



int main(int argc, char** argv) {
/* creates a new window and its attributes */
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(450, 350);
glutInitWindowPosition(400, 300);
//glutCreateWindow("learning opengl");
glutCreateWindow(argv[0]);

init ();
/* drawing */
glutDisplayFunc(display);
/* resize */
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}


for some reason the last vertex points appear always in front making the geometry to look odd, so what I need to do to fix this? also, If I comment the init() function it does not make any difference?

Cheers
rS

todayman
09-21-2009, 01:43 PM
You enabled depth testing, but that alone is not enough to ensure that the testing works. You also need a depth buffer in which to store depth values. add GLUT_DEPTH to the glutInitDisplayMode argument and then it should work fine.

Ricardo Sanchez
09-21-2009, 02:20 PM
It works!

Now is it the code for the animation the best way to do it or is it a better way?

Thanks
rS

todayman
09-21-2009, 06:18 PM
Well, this works, so this is fine. There is a glutTimerFunc that calls a function after a certain amount of time, so you can use that to control the framerate if you want. For large [projects, you'll probably use something other than GLUT, so you'll do it with that.