PDA

View Full Version : New to OpenGL...looking for some help!



danh789
08-03-2011, 12:26 AM
Im just starting to learn OpenGL. I'm wondering if someone here could help me with a piece of code....

#include <gl/glut.h>
void drawStar() {
glBegin(GL_TRIANGLES);
glVertex2f(-0.5, -0.5);
glVertex2f(0.5, -0.5);
glVertex2f(0.0, 0.5);
glVertex2f(-0.5, 0.15);
glVertex2f(0.5, 0.15);
glVertex2f(0.0, -0.85);
glEnd();
}
void display(void) {
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluOrtho2D(-1, 1, -1, 1);
drawStar();
glFlush ();
}
int main(int argc, char** argv) {
glutInit(&amp;argc, argv);
glutCreateWindow ("Star");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}


How do you modify the display() function so that a 3-dimensional projection is used rather than an orthographic projection? Do you use gluPerspective(fov,aspect, near, far)?Its probably simple enough to solve but I just dont really have a clue what to do! Any help would be very much appreciated!

Aleksandar
08-03-2011, 01:00 AM
The code is not correct!
gluOrtho2D() and gluPerspective set projection matrix, so before calling them, you should set appropriate matrix-stack active:


glMatrixMode(GL_PROJECTION);
gluPerspective(...);

And then again set active model-view matrix-stack in order to do transformations:


glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
drawStar();

As I already said in a myriad posts before: Please, read OpenGL Programming Guide chapter 3. :(

MaxH
08-03-2011, 10:06 AM
What happens when you run this code (without any changes)?
What do you see on the screen?