Thought I should restart OpenGL again and tried converting NeHe lessons into some sort of reusable classes form or functions. I have read several OOP implementation conversations of NeHe code, but this ain't what this post is about. But might be something that I have missed somewhere [looksaround].
I have implemented the rendering functions for Triangles and Quads in a little class form and using them in my application. When I use these, I get very strange errors that are hard to describe. Shape of Triangle is very odd, Quad is having different vertices's or runtime errors when closing application [disturbed].
However, I tried NeHe implementation of Lesson 02 and implemented the rendering calls in the Render function everything worked fine as it should be. But when I started using my designed classes, I got strange errors again. If I use only one Triangle or Quad, it works fine. Using multi versions, or using both gives very strange errors as explained above.
Here is the code of my designed header file for Shapes:
// Line Structure
struct sLine
{
float x; // Position on X - Axis
float y; // Position on Y - Axis
float z; // Position on Z - Axis
};
// Class Triangle
class cTriangle
{
public: // Members that can be accessed by anyone
sLine Line [2]; // Array of Lines
void Render (void); // Render Triangle
};
// Class Quad
class cQuad
{
public: // Members that can be accessed by anyone
sLine Line [3]; // Array of Lines
void Render (void); // Render Quad
};
The code used to represent the Render function of both Triangle and Quad class:
void cTriangle::Render (void)
{
glBegin (GL_TRIANGLES); // Begin Drawing
glVertex3f (Line[0].x, Line[0].y, Line[0].z); // Draw Line 01
glVertex3f (Line[1].x, Line[1].y, Line[1].z); // Draw Line 02
glVertex3f (Line[2].x, Line[2].y, Line[2].z); // Draw Line 03
glEnd (); // End Drawing
}
void cQuad::Render (void)
{
glBegin (GL_QUADS); // Begin Drawing
glVertex3f (Line[0].x, Line[0].y, Line[0].z); // Draw Line 01
glVertex3f (Line[1].x, Line[1].y, Line[1].z); // Draw Line 02
glVertex3f (Line[2].x, Line[2].y, Line[2].z); // Draw Line 03
glVertex3f (Line[3].x, Line[3].y, Line[3].z); // Draw Line 04
glEnd (); // End Drawing
}
This is how I setup the vertices's for Triangle and Quad classes:
Triangle.Line[0].x = 0.0f; Triangle.Line[0].y = 1.0f; Triangle.Line[0].z = 0.0f;
Triangle.Line[1].x =-1.0f; Triangle.Line[1].y =-1.0f; Triangle.Line[1].z = 0.0f;
Triangle.Line[2].x = 1.0f; Triangle.Line[2].y =-1.0f; Triangle.Line[2].z = 0.0f;
Quad.Line[0].x =-1.0f; Quad.Line[0].y = 1.0f; Quad.Line[0].z = 0.0f;
Quad.Line[1].x = 1.0f; Quad.Line[1].y = 1.0f; Quad.Line[1].z = 0.0f;
Quad.Line[2].x = 1.0f; Quad.Line[2].y =-1.0f; Quad.Line[2].z = 0.0f;
Quad.Line[3].x =-1.0f; Quad.Line[3].y =-1.0f; Quad.Line[3].z = 0.0f;
And this is how I use them in Render functions:
int cGame::RenderScene (void)
{
// Clear Color and Depth Buffer
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity (); // Reset Current Model View Matrix
glTranslatef (-1.5f, 0.0f,-6.0f); // Move Pointer
// Drawing Triangle
Triangle.Render ();
glTranslatef ( 3.0f, 0.0f, 0.0f); // Move Pointer
// Drawing Quad
Quad.Render ();
return true; // Scene Rendered Successfully
}
My observations:
- If I do not use Quad and only use Triangle it works fine.
- If I use another Triangle class, strange errors appear.
- Even if I comment out the render function of quad in the above example and only use the quad class for setting up vertices, the error remains ... [crying]
Can't figure out what is wrong. But of course, if instead of using this class, I just use them plainly in the render function like this, it works fine:
int cGame::RenderScene (void)
{
// Clear Color and Depth Buffer
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity (); // Reset Current Model View Matrix
glTranslatef (-1.5f, 0.0f,-6.0f); // Move Pointer
// Drawing Triangle
glBegin (GL_TRIANGLES); // Begin Drawing
glVertex3f ( 0.0f, 1.0f, 0.0f); // Draw Line 01
glVertex3f (-1.0f,-1.0f, 0.0f); // Draw Line 02
glVertex3f ( 1.0f,-1.0f, 0.0f); // Draw Line 03
glEnd (); // End Drawing
glTranslatef ( 3.0f, 0.0f, 0.0f); // Move Pointer
// Drawing Quad
glBegin (GL_QUADS); // Begin Drawing
glVertex3f (-1.0f, 1.0f, 0.0f); // Draw Line 01
glVertex3f ( 1.0f, 1.0f, 0.0f); // Draw Line 02
glVertex3f ( 1.0f,-1.0f, 0.0f); // Draw Line 03
glVertex3f (-1.0f,-1.0f, 0.0f); // Draw Line 04
glEnd (); // End Drawing
return true; // Scene Rendered Successfully
}
Any help or information on this would be really appreciated. I believe this has something to do with OpenGL not with the classes. But I can be wrong ...
Regards,
Syed