GL_LINES Crashes my app.

Does anyone know why I get the error “Floating point division by zero” when I draw a line with GL_LINES?

Here’s my code:

//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include “Unit1.h”
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource “*.dfm”
TForm1 Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent
Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void TForm1::Error(char *s) {
MessageBox(NULL,s,“ERROR”,MB_OK|MB_ICONEXCLAMATION);
Close();
}

void TForm1::SetupPixelFormat() {
PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
16,
0,0,0,0,0,0,
0,0,
0,0,0,0,0,
16,
0,
0,
PFD_MAIN_PLANE,
0,
0,0,
};
if ((PixelFormat = ChoosePixelFormat(OpenGL1hDC, &pfd)) == 0)
Error(“ChoosePixelFormat failed”);

if (SetPixelFormat(OpenGL1hDC, PixelFormat, &pfd) == FALSE)
	Error("SetPixelFormat failed");

}

void TForm1::OpenGL1DrawScene()
{

glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLoadIdentity();

glBegin(GL_LINES);
glVertex3d(0.0f,0.0f,-3.0f);
glVertex3d (0.0f,0.0f,3.0f);
glEnd();

SwapBuffers(OpenGL1hDC);

}

void TForm1::OpenGL1Resize( GLsizei width, GLsizei height )
{
glViewport( 0, 0, width, height );

glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 10.0, (GLdouble) width / height, 1.0, 10.0 );

glMatrixMode( GL_MODELVIEW );
glLoadIdentity();

}

void TForm1::OpenGL1Initialize(GLsizei width, GLsizei height)
{
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glClearDepth(1.0);

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);								// The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	// Really Nice Perspective Calculations

}

void __fastcall TForm1::FormShow(TObject *Sender)
{
if (!(OpenGL1hDC = GetDC(Handle)))
Error(“Can’t Create A GL Device Context.”);
SetupPixelFormat();
if (!(OpenGL1hRC = wglCreateContext(OpenGL1hDC)))
Error(“Can’t Create A GL Rendering Context.”);

if(!(wglMakeCurrent(OpenGL1hDC, OpenGL1hRC)))
	Error("Could not MakeCurrent");

OpenGL1Resize(Panel1-&gt;Width, Panel1-&gt;Height);
OpenGL1Initialize(0, 0);

}
//---------------------------------------------------------------------------

void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
Timer1->Enabled = false;
OpenGL1DrawScene();
Timer1->Enabled = true;
}
//---------------------------------------------------------------------------

Been trying to figure this out for osme days now. Plz help me.

Thanks!
//wiggo

Are you sure it is because you’re drawing lines?
Check which values you throw into the Resize() function. You do not check height for 0 before dividing!
And you should cast both integers to double before your divide like this:
gluPerspective( 10.0, (GLdouble) width / (GLdouble) height, 1.0, 10.0 );
and not the result of the integer division Your aspect ratio is 0.0 if width < height.

If these are not the problems and your OpenGL is HW accelerated, try it with Microsofts GDI Generic implementation (PFD_GENERIC_FORMAT). If that works, the driver has a bug.

I changed the code and used a software driver.
But it keeps giving me the error “Floating point division by zero” when I draw lines. GL_TRIANGLES works without any problems.

If it crahses on two different implementations it’s probably your code.

Step through the debugger to see the exact point where the crash occurs.

What coordinates work?

Try the following suggestions step by step.

if (width == 0 | | height == 0)
dAspect = 1.0;
else
dAspect = (double) width / (double) height;
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 10.0, dAspect, 1.0, 10.0 );

With the line coordinates you specified you should get a single point in the center of the window.

Try this:

glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -5.5f); // Put origin in center of viewing frustum.
// Do not glLoadIdentity() afterwards.

glBegin(GL_LINES);
glVertex3d(0.0f,0.0f,-3.0f);
glVertex3d(0.0f,0.0f,3.0f);
glEnd();

No need to use floating point constants if you call the double type glVertex.
Try glVertex3f() instead.
Try coordinates other than 0.0 for x and y.

[This message has been edited by Relic (edited 01-06-2001).]

Hey!

It finally works, The problem was that the z (depth) coordinate was 0.0.

When I changed this it worked perfect.

Thans alot for the help!