Jump to content
  • Advertisement
Sign in to follow this  
Shamino

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.

If you intended to correct an error in the post then please contact us.

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;

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

std::vector<BasicQuad*>	mBasicQuads;



class GLRenderer
{
public:
	GLRenderer()
	{

	}

	
	void	AddBasicQuadToRenderer(float P1X,float  P1Y,float	P1Z,
								   float P2X,float	P2Y,float	P2Z,
								   float P3X,float	P3Y,float	P3Z,
								   float P4X,float	P4Y,float	P4Z,
								   int	Texture)
	{
		BasicQuad	*pData = new	BasicQuad;
		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;
		mBasicQuads.push_back(pData);
	}


	void RenderBasicQuads();
};
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"

void GLRenderer::RenderBasicQuads()
{
	if(!mBasicQuads.size())
	return;

	glDisable(GL_LIGHTING);
	glDisable(GL_BLEND);

	BasicQuad	*	pTemp;

	std::vector<BasicQuad*>::iterator	ptr;
	
	for(ptr = mBasicQuads.begin(); ptr != mBasicQuads.end(); ptr++)
	{
		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
};
Any comments? suggestions? etc?

Share this post


Link to post
Share on other sites
Advertisement

  • 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 actually added the call to glBindTexture inside "RenderBasicQuads".

  • 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.

  • In "AddBasicQuadToRenderer", instead of assigning every data member yourself (points 1,2,3,4 and texID), just make it an overloaded assignment operator for the BasicQuad class. Because if you end up changing the BasicQuad class you'll have to re-write the "AddBasicQuadToRenderer" code every time you make a small change. With the overloaded operator in the BasicQuad class, you just have to change the BasicQuad code, which you'll already be doing. Otherwise you'll find yourself having to re-write 50 lines of code all over the place when you make add an integer to a struct or class.


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 this post


Link to post
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 this post


Link to post
Share on other sites
About overloading the basic quad structure, I can overload an addition operator to just add it to the function instead of writing in the parameters myself? That is nifty...

Share this post


Link to post
Share on other sites
Quote:
Original post by Shamino
About overloading the basic quad structure, I can overload an addition operator to just add it to the function instead of writing in the parameters myself? That is nifty...


I have no idea what you mean.

When I was talking about overloading for the BasicQuad struct I was talking about something like this:


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;
}
};


// blah

void 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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!