OpenGL My OGL Render Class

This topic is 4869 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Here we go! Its the birth of a render class, very incredibly simple, only sends a quad to the renderer and then, renders it. Renderer.h
#include <vector>
#include "Vector3.h"

using namespace std;

{
Vector3 P1, P2, P3, P4;
int	Texture;//Index into gpTextureManager
};

class GLRenderer
{
public:
GLRenderer()
{

}

float P2X,float	P2Y,float	P2Z,
float P3X,float	P3Y,float	P3Z,
float P4X,float	P4Y,float	P4Z,
int	Texture)
{
pData->P1	=	Vector3(P1X,P1Y,P1Z);
pData->P2	=	Vector3(P2X,P2Y,P2Z);
pData->P3	=	Vector3(P3X,P3Y,P3Z);
pData->P4	=	Vector3(P4X,P4Y,P4Z);
pData->Texture	=	Texture;
}

};

Renderer.cpp
#include <windows.h>
#include <gl\gl.h>			// Header File For The OpenGL32 Library
#include <gl\glu.h>			// Header File For The GLu32 Library
#include <gl\glaux.h>		// Header File For The Glaux Library
#include "Renderer.h"

{
return;

glDisable(GL_LIGHTING);
glDisable(GL_BLEND);

{
pTemp	=	*ptr;

glBegin(GL_QUADS);//Cannot be brought out of loop due to bindtexture call

glVertex3f(pTemp->P1.x,pTemp->P1.y,pTemp->P1.z);

glVertex3f(pTemp->P2.x,pTemp->P2.y,pTemp->P2.z);

glVertex3f(pTemp->P3.x,pTemp->P3.y,pTemp->P3.z);

glVertex3f(pTemp->P4.x,pTemp->P4.y,pTemp->P4.z);

glEnd();
}
delete pTemp;
}
and my lil vector3 class, proves very useful :d Vector3.h
class Vector3
{
public:
float x, y, z;

Vector3()
{
}

Vector3(float x, float y, float z);

// And so on
};

Share on other sites

• Use triangles because you can draw anything with triangles, and you'll have to add it eventually. So, you might as well just have it draw only triangles.

• If you're changing it to triangles, you'll need to add texture coords to your class.

• Initialize 'pTemp' to NULL inside "RenderBasicQuads". It's not strictly necessary for this code since you assign it at the beginning of every loop iteration, but get into the habit, else you'll forget and wonder why there's an invalid memory access at 0x92739838 instead of 0x00000000, which is much easier to debug.

• You haven't included a call to glTexCoords (or whatever its called) to specify the texture coords for each vertex.

• Have the function "AddBasicQuadToRenderer" actually take a BasicQuad object, or 3 Vector3 objects and the texID, or whatever object type you'll be using, else you'll accidently swap a single parameter and be looking for it for hours.

• With the "delete pTemp;" statment inside "RenderBasicQuads", it will effectively delete the memory allocated for BasicQuad object for the last element in the list, but won't delete the list element, therefore causing memory access problems the next time you try to access the list element, because the pointer in the list element is still pointing to what is now an invalid memory address to access. I'm guessing that it's not what you're trying to do.

• Your list of things to be rendered should be kept inside the GLRenderer list. If a class has functionality, but no data members, make the functions static so you don't have to bother actually creating an object which has no data, just to run the functions.

• If you're not turning off/on any features like lighting, blending, just put the glDisable function calls in the same function where you initialize OpenGL. It's wasteful and just might make your rendering slower (don't quote me on this), calling these two every time you render.

It's late here. Please let me know if I got any of this wrong.

[Edited by - Endar on November 24, 2005 6:49:37 AM]

Share on other sites
I also plan on adding operator overloads to the Vector3 class so I can work with them a bit more naturally :)

Share on other sites
Did you also know that class and struct are almost exactly the same thing?

So when you have a struct, you can add constructors, functions and overloaded operators to it. I have a couple of things like this, where, even though all the data members are all public, I have functions and operators to make things simpler and the code more readable.

That's really all the suggestions I have for the moment. I hope I helped.

Share on other sites
Quote:

I have no idea what you mean.

struct	BasicQuad{	Vector3 P1, P2, P3, P4;	int	Texture;	BasicQuad(const BasicQuad& b)	{		P1 = b.P1;		P2 = b.P2;		P3 = b.P3;		P4 = b.P4;		Texture = b.Texture;	}	BasicQuad& operator=(const BasicQuad& b)	{		if( &b != this ){			P1 = b.P1;			P2 = b.P2;			P3 = b.P3;			P4 = b.P4;			Texture = b.Texture;		}		return *this;	}};// blahvoid	AddBasicQuadToRenderer(const BasicQuad& b){	// this	BasicQuad	*pData = new BasicQuad;	(*pData) = b;	// OR this	BasicQuad *pData = new BasicQuad(b);	// then add to the vector	mBasicQuads.push_back(pData);}

See? Also, don't overload operators if it doesn't make sense. I mean, if you can look at an operator and the types on either side and instantly know what it does, even if you haven't seen the code before, then it's right. If you have to think for a second, chances are that it doens't belong and should have a proper function.

Share on other sites
Maybe i said overload and shouldn't have, thats exactly what I ment....

Throw the object in the parameters instead of having 30 million floats.... Looks much more effient than my code... But I don't have much use for my Vector3 class now do i ?

Share on other sites
In the code that you have there? Not really. But keep it in there and keep building it up, because for anything more complex you will use it.

You'll need it to do the dot product, cross product, have all the common overloaded operators (+,-,/,+=,-=,/= etc). It'll probably end up hidden inside a vertex class, which would be something like this:

class Vertex{public:	Vector3 pos;	// position	Vector3 normal;	// normal	float u,v;	// texture coords	// functions};

So don't throw it away, or even consider stopping using it. As you get onto more complex things, pretty much anything more complex than what you have right now, you'll need it. It's one of the major building blocks of anything 3d graphics related.

Share on other sites
I saw a whole bunch of premade engines using it, but i didn't want to use someones engine so i just invented my own vector3.

Share on other sites
I did the same. In fact, it was the first thing that I wrote that I still use.

It's small enough to be mostly finished in a couple of days/week. If you don't know about vector math you learn a lot by reading the resources that you use to write it.

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 14
• 12
• 29
• 11
• 44
• Forum Statistics

• Total Topics
634856
• Total Posts
3019661
×