PDA

View Full Version : Exception in a very simple Quadrilateral program !



Aditi
01-29-2009, 03:46 PM
I have just started doing openGL programming a week back and came across a problem in the very beginning...I wanna make a simple flat surface(floor)in openGL for which i am using Visual C++ 2008 express edition...

But when i debug my program, the following exception occurs :

Unhandled exception at 0x1000fdac in first project.exe: 0xC0000005: Access violation reading location 0x00000000.

Blow is my code...Please tell me if the code is wrong

#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>

void drawFloor(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
glBegin(GL_QUADS);
glVertex3f(-18.0, 0.0, 27.0);
glVertex3f(27.0, 0.0, 27.0);
glVertex3f(27.0, 0.0, -18.0);
glVertex3f(-18.0, 0.0, -18.0);
glEnd();
glFlush();
}

void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}


int main(int argc,char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(20,60);
glutInitWindowSize(360, 360);
glutCreateWindow("Project");
init();
glutDisplayFunc(drawFloor);
glutMainLoop();
return (0);
}

dorbie
01-29-2009, 06:08 PM
I don't see anything, but that looks like a NULL pointer error.
There's nothing like that in your code.

Have you tried adding a keyboard callback?

MaxH
01-30-2009, 11:40 AM
Have you been able to compile and run ANY OpenGL programs using Visual C++? I ask because like 'dorbie' said, your error message doesn't seem to have anything to do with your code. I was able to copy your code into a C++ project, compile it, and run it without getting any error messages. But I'm using Visual C++ 9.0 on the computer I'm on right now. BTW, you should review what the parameters of glOrtho mean. You have set up a viewing volume with bounds from 0 to 1 in x and y, and -1 to 1 in Z. But the Quad you are trying to draw has vertices WAY outside these bounds. Even if you fix that error message and get your program to open a window, the window will be blank. I tried it. I had no problem getting a Quad to show up when I used the glVertex commands below ....

glVertex3f (0.2, 0.2, 0);
glVertex3f (0.8, 0.3, 0);
glVertex3f (0.7, 0.8, 0);
glVertex3f (0.2, 0.7, 0);

Aditi
01-30-2009, 01:23 PM
Hey thanks for your help..
I am able to run my program now...And i got your point regarding the glOrtho parameters..
I am trying to make a FLAT SURFACE i.e. a FLOOR using these functions and parameters...but i am unable to get the desired effect..

glVertex3f (0.2,0.2,0);
glVertex3f (0.8,0.3,0);
glVertex3f (0.7,0.8,0);
glVertex3f (0.2,0.7,0);

and glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);

Please correct me if any parameters needs to be changed in order to get a flat floor..
Thanks..

MaxH
02-02-2009, 11:42 AM
I'm gonna guess that what you mean by 'flat floor' would be generated with the coordinates below. If your mind works in 3D, this scene looks like an empty space except for a floor which recedes into the screen. If you think in 2D, it looks like a trapezoid in the plane of the screen.

glVertex3f (0.1, 0.1, 0);
glVertex3f (0.9, 0.1, 0);
glVertex3f (0.8, 0.2, 0);
glVertex3f (0.2, 0.2, 0);

If you want a realistic looking 3D scene with objects resting on a floor, you should not be using the glOrtho command. gluPerspective is the way to go.