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->Width, Panel1->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