• Advertisement
Sign in to follow this  

OpenGL My OGL Render Class

This topic is 4468 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
I also plan on adding operator overloads to the Vector3 class so I can work with them a bit more naturally :)

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
Main goals are as follows: Switch to tri's, make it so AddBasicTriToRenderer takes a tri object, instead of 80 parameters, and fix ptemp de-allocation

Share this post


Link to post
Share on other sites
Looks good. As you gain more experience, you'll find different and better ways of doing things, but that's pretty good for a start up. [smile]

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
Sign in to follow this  

  • Advertisement
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By LifeArtist
      Good Evening,
      I want to make a 2D game which involves displaying some debug information. Especially for collision, enemy sights and so on ...
      First of I was thinking about all those shapes which I need will need for debugging purposes: circles, rectangles, lines, polygons.
      I am really stucked right now because of the fundamental question:
      Where do I store my vertices positions for each line (object)? Currently I am not using a model matrix because I am using orthographic projection and set the final position within the VBO. That means that if I add a new line I would have to expand the "points" array and re-upload (recall glBufferData) it every time. The other method would be to use a model matrix and a fixed vbo for a line but it would be also messy to exactly create a line from (0,0) to (100,20) calculating the rotation and scale to make it fit.
      If I proceed with option 1 "updating the array each frame" I was thinking of having 4 draw calls every frame for the lines vao, polygons vao and so on. 
      In addition to that I am planning to use some sort of ECS based architecture. So the other question would be:
      Should I treat those debug objects as entities/components?
      For me it would make sense to treat them as entities but that's creates a new issue with the previous array approach because it would have for example a transform and render component. A special render component for debug objects (no texture etc) ... For me the transform component is also just a matrix but how would I then define a line?
      Treating them as components would'nt be a good idea in my eyes because then I would always need an entity. Well entity is just an id !? So maybe its a component?
      Regards,
      LifeArtist
    • By QQemka
      Hello. I am coding a small thingy in my spare time. All i want to achieve is to load a heightmap (as the lowest possible walking terrain), some static meshes (elements of the environment) and a dynamic character (meaning i can move, collide with heightmap/static meshes and hold a varying item in a hand ). Got a bunch of questions, or rather problems i can't find solution to myself. Nearly all are deal with graphics/gpu, not the coding part. My c++ is on high enough level.
      Let's go:
      Heightmap - i obviously want it to be textured, size is hardcoded to 256x256 squares. I can't have one huge texture stretched over entire terrain cause every pixel would be enormous. Thats why i decided to use 2 specified textures. First will be a tileset consisting of 16 square tiles (u v range from 0 to 0.25 for first tile and so on) and second a 256x256 buffer with 0-15 value representing index of the tile from tileset for every heigtmap square. Problem is, how do i blend the edges nicely and make some computationally cheap changes so its not obvious there are only 16 tiles? Is it possible to generate such terrain with some existing program?
      Collisions - i want to use bounding sphere and aabb. But should i store them for a model or entity instance? Meaning i have 20 same trees spawned using the same tree model, but every entity got its own transformation (position, scale etc). Storing collision component per instance grats faster access + is precalculated and transformed (takes additional memory, but who cares?), so i stick with this, right? What should i do if object is dynamically rotated? The aabb is no longer aligned and calculating per vertex min/max everytime object rotates/scales is pretty expensive, right?
      Drawing aabb - problem similar to above (storing aabb data per instance or model). This time in my opinion per model is enough since every instance also does not have own vertex buffer but uses the shared one (so 20 trees share reference to one tree model). So rendering aabb is about taking the model's aabb, transforming with instance matrix and voila. What about aabb vertex buffer (this is more of a cosmetic question, just curious, bumped onto it in time of writing this). Is it better to make it as 8 points and index buffer (12 lines), or only 2 vertices with min/max x/y/z and having the shaders dynamically generate 6 other vertices and draw the box? Or maybe there should be just ONE 1x1x1 cube box template moved/scaled per entity?
      What if one model got a diffuse texture and a normal map, and other has only diffuse? Should i pass some bool flag to shader with that info, or just assume that my game supports only diffuse maps without fancy stuff?
      There were several more but i forgot/solved them at time of writing
      Thanks in advance
    • By RenanRR
      Hi All,
      I'm reading the tutorials from learnOpengl site (nice site) and I'm having a question on the camera (https://learnopengl.com/Getting-started/Camera).
      I always saw the camera being manipulated with the lookat, but in tutorial I saw the camera being changed through the MVP arrays, which do not seem to be camera, but rather the scene that changes:
      Vertex Shader:
      #version 330 core layout (location = 0) in vec3 aPos; layout (location = 1) in vec2 aTexCoord; out vec2 TexCoord; uniform mat4 model; uniform mat4 view; uniform mat4 projection; void main() { gl_Position = projection * view * model * vec4(aPos, 1.0f); TexCoord = vec2(aTexCoord.x, aTexCoord.y); } then, the matrix manipulated:
      ..... glm::mat4 projection = glm::perspective(glm::radians(fov), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f); ourShader.setMat4("projection", projection); .... glm::mat4 view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp); ourShader.setMat4("view", view); .... model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f)); ourShader.setMat4("model", model);  
      So, some doubts:
      - Why use it like that?
      - Is it okay to manipulate the camera that way?
      -in this way, are not the vertex's positions that changes instead of the camera?
      - I need to pass MVP to all shaders of object in my scenes ?
       
      What it seems, is that the camera stands still and the scenery that changes...
      it's right?
       
       
      Thank you
       
    • By dpadam450
      Sampling a floating point texture where the alpha channel holds 4-bytes of packed data into the float. I don't know how to cast the raw memory to treat it as an integer so I can perform bit-shifting operations.

      int rgbValue = int(textureSample.w);//4 bytes of data packed as color
      // algorithm might not be correct and endianness might need switching.
      vec3 extractedData = vec3(  rgbValue & 0xFF000000,  (rgbValue << 8) & 0xFF000000, (rgbValue << 16) & 0xFF000000);
      extractedData /= 255.0f;
    • By Devashish Khandelwal
      While writing a simple renderer using OpenGL, I faced an issue with the glGetUniformLocation function. For some reason, the location is coming to be -1.
      Anyone has any idea .. what should I do?
  • Advertisement