Sign in to follow this  
Shamino

OpenGL My OGL Render Class

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

  • 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
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 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
So like this :D


#include <vector>
#include "Vector3.h"

using namespace std;

struct BasicTri
{
Vector3 P1, P2, P3;
int Texture;


BasicTri(const BasicTri& t)
{
P1 = t.P1;
P2 = t.P2;
P3 = t.P3;
Texture = t.Texture;
}

BasicTri& operator=(const BasicTri& t)
{
if( &t != this ){
P1 = t.P1;
P2 = t.P2;
P3 = t.P3;
Texture = t.Texture;
}
return *this;
}
};

std::vector<BasicTri*> mBasicTri;


void AddBasicTriToRenderer(const BasicTri& t)
{
BasicTri *pData = new BasicTri(t);

// then add to the vector
mBasicTri.push_back(pData);
}

Share this post


Link to post
Share on other sites
Founda few unoptimized issues with your code, replaced it with...


#include <vector>
#include "Vector3.h"

using namespace std;

struct BasicTri
{
Vector3 P1, P2, P3;
int Texture;


BasicTri(const BasicTri& t)
{
P1 = t.P1;
P2 = t.P2;
P3 = t.P3;
Texture = t.Texture;
}

BasicTri& operator=(const BasicTri& t)
{
if( &t != this ){
P1 = t.P1;
P2 = t.P2;
P3 = t.P3;
Texture = t.Texture;
}
return *this;
}
};

std::vector<BasicTri*> mBasicTri;


void AddBasicTriToRenderer(BasicTri* t)
{
// then add to the vector
mBasicTri.push_back(t);
}


With your AddBasicTriToRenderer function you created a new triangle when you didnt need to.. i'll show you


void AddBasicTriToRenderer(const BasicTri& t)
{

BasicTri *pData = new BasicTri(t);


// then add to the vector
mBasicTri.push_back(pData);
}


Is now this

void AddBasicTriToRenderer(BasicTri* t)
{

// then add to the vector
mBasicTri.push_back(t);
}




Share this post


Link to post
Share on other sites
Quote:
Original post by Shamino

void AddBasicTriToRenderer(const BasicTri& t)
{

BasicTri *pData = new BasicTri(t);


// then add to the vector
mBasicTri.push_back(pData);
}


Is now this

void AddBasicTriToRenderer(BasicTri* t)
{

// then add to the vector
mBasicTri.push_back(t);
}


Fair enough, if you want to do it that way, but that means that you have to pay attention and every time you add a tri to the list, you need to make sure that you create it on the heap before passing the pointer to the function. If you forget, and you pass in an address from the stack, you'll have problems when that memory is overwritten and used for something else in your program, or when you delete all the list elements, you'll get a very strange error that basically means that you tried to delete some stack memory. But, if you think you can remember to create a new one in memory before passing the point in, I have absolutely no issue with it. [smile]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Announcements

  • Forum Statistics

    • Total Topics
      628402
    • Total Posts
      2982470
  • Similar Content

    • By test opty
      Hi all,
       
      I'm starting OpenGL using a tut on the Web. But at this point I would like to know the primitives needed for creating a window using OpenGL. So on Windows and using MS VS 2017, what is the simplest code required to render a window with the title of "First Rectangle", please?
       
       
    • By DejayHextrix
      Hi, New here. 
      I need some help. My fiance and I like to play this mobile game online that goes by real time. Her and I are always working but when we have free time we like to play this game. We don't always got time throughout the day to Queue Buildings, troops, Upgrades....etc.... 
      I was told to look into DLL Injection and OpenGL/DirectX Hooking. Is this true? Is this what I need to learn? 
      How do I read the Android files, or modify the files, or get the in-game tags/variables for the game I want? 
      Any assistance on this would be most appreciated. I been everywhere and seems no one knows or is to lazy to help me out. It would be nice to have assistance for once. I don't know what I need to learn. 
      So links of topics I need to learn within the comment section would be SOOOOO.....Helpful. Anything to just get me started. 
      Thanks, 
      Dejay Hextrix 
    • By mellinoe
      Hi all,
      First time poster here, although I've been reading posts here for quite a while. This place has been invaluable for learning graphics programming -- thanks for a great resource!
      Right now, I'm working on a graphics abstraction layer for .NET which supports D3D11, Vulkan, and OpenGL at the moment. I have implemented most of my planned features already, and things are working well. Some remaining features that I am planning are Compute Shaders, and some flavor of read-write shader resources. At the moment, my shaders can just get simple read-only access to a uniform (or constant) buffer, a texture, or a sampler. Unfortunately, I'm having a tough time grasping the distinctions between all of the different kinds of read-write resources that are available. In D3D alone, there seem to be 5 or 6 different kinds of resources with similar but different characteristics. On top of that, I get the impression that some of them are more or less "obsoleted" by the newer kinds, and don't have much of a place in modern code. There seem to be a few pivots:
      The data source/destination (buffer or texture) Read-write or read-only Structured or unstructured (?) Ordered vs unordered (?) These are just my observations based on a lot of MSDN and OpenGL doc reading. For my library, I'm not interested in exposing every possibility to the user -- just trying to find a good "middle-ground" that can be represented cleanly across API's which is good enough for common scenarios.
      Can anyone give a sort of "overview" of the different options, and perhaps compare/contrast the concepts between Direct3D, OpenGL, and Vulkan? I'd also be very interested in hearing how other folks have abstracted these concepts in their libraries.
    • By aejt
      I recently started getting into graphics programming (2nd try, first try was many years ago) and I'm working on a 3d rendering engine which I hope to be able to make a 3D game with sooner or later. I have plenty of C++ experience, but not a lot when it comes to graphics, and while it's definitely going much better this time, I'm having trouble figuring out how assets are usually handled by engines.
      I'm not having trouble with handling the GPU resources, but more so with how the resources should be defined and used in the system (materials, models, etc).
      This is my plan now, I've implemented most of it except for the XML parts and factories and those are the ones I'm not sure of at all:
      I have these classes:
      For GPU resources:
      Geometry: holds and manages everything needed to render a geometry: VAO, VBO, EBO. Texture: holds and manages a texture which is loaded into the GPU. Shader: holds and manages a shader which is loaded into the GPU. For assets relying on GPU resources:
      Material: holds a shader resource, multiple texture resources, as well as uniform settings. Mesh: holds a geometry and a material. Model: holds multiple meshes, possibly in a tree structure to more easily support skinning later on? For handling GPU resources:
      ResourceCache<T>: T can be any resource loaded into the GPU. It owns these resources and only hands out handles to them on request (currently string identifiers are used when requesting handles, but all resources are stored in a vector and each handle only contains resource's index in that vector) Resource<T>: The handles given out from ResourceCache. The handles are reference counted and to get the underlying resource you simply deference like with pointers (*handle).  
      And my plan is to define everything into these XML documents to abstract away files:
      Resources.xml for ref-counted GPU resources (geometry, shaders, textures) Resources are assigned names/ids and resource files, and possibly some attributes (what vertex attributes does this geometry have? what vertex attributes does this shader expect? what uniforms does this shader use? and so on) Are reference counted using ResourceCache<T> Assets.xml for assets using the GPU resources (materials, meshes, models) Assets are not reference counted, but they hold handles to ref-counted resources. References the resources defined in Resources.xml by names/ids. The XMLs are loaded into some structure in memory which is then used for loading the resources/assets using factory classes:
      Factory classes for resources:
      For example, a texture factory could contain the texture definitions from the XML containing data about textures in the game, as well as a cache containing all loaded textures. This means it has mappings from each name/id to a file and when asked to load a texture with a name/id, it can look up its path and use a "BinaryLoader" to either load the file and create the resource directly, or asynchronously load the file's data into a queue which then can be read from later to create the resources synchronously in the GL context. These factories only return handles.
      Factory classes for assets:
      Much like for resources, these classes contain the definitions for the assets they can load. For example, with the definition the MaterialFactory will know which shader, textures and possibly uniform a certain material has, and with the help of TextureFactory and ShaderFactory, it can retrieve handles to the resources it needs (Shader + Textures), setup itself from XML data (uniform values), and return a created instance of requested material. These factories return actual instances, not handles (but the instances contain handles).
       
       
      Is this a good or commonly used approach? Is this going to bite me in the ass later on? Are there other more preferable approaches? Is this outside of the scope of a 3d renderer and should be on the engine side? I'd love to receive and kind of advice or suggestions!
      Thanks!
    • By nedondev
      I 'm learning how to create game by using opengl with c/c++ coding, so here is my fist game. In video description also have game contain in Dropbox. May be I will make it better in future.
      Thanks.
  • Popular Now