Thread: Developing a Renderer Class for OpenGL

  1. #1
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968

    Developing a Renderer Class for OpenGL

    Well, step one in developing your own 3d engine I'd say, is to make the render class...

    I have a mild understanding of what it has to do, I have to declare various primitives, triangles, quads, etc... I have to take these definitions (will be in vectors,) and then do what with them?

    I'm going to send the renderer class a milkshape model, I need to use those primitives to actually draw out the milkshape, how do I go about tieing these 2 together?

    EDIT: I probably am really stupid right now, and have no idea what I'm talking about, please tell me if I'm speaking nonsense in possibly a non flaming manner
    Last edited by Shamino; 11-21-2005 at 10:27 PM.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Before you dive headfirst into a major disaster I'd recommend you learn a bit about the underlying data structures that make or break a game. There are far more important things than the renderer. Figure out the resource management scheme and how it's all going to work together in memory first. I could explain the rendering scheme I use but you would probably be confused since you did not understand my posts about your frame rate issues and how to fix them.

    I'd say you should learn to walk before you want to run.

  3. #3
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    I guess memory management is a key issue I have looked over in the past, I've just recently learned how to work things out and organize data in an object oriented manner, as shown in the file I'll attach. I have a tree chart here that I've been working and re-working and working some more to display the class hierarchy of my new project, the renderer seemed like a good place to start.

    I've recently come under the understanding of vectors and how they work, as well as constructors and other memory management tools, alot of people have been very helpful to me recently.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Well you seem to have vectors down, but your use of virtuals disturbs me.

    Although your inheritance example is correct I would recommend keeping your inheritance to one level. One derivation. Any more than that will cause your cool C++ structure to be nearly unusable because it will be confusing for you.

    Two questions:

    1. Has a -> don't use inheritance
    2. Is a -> use inheritance

    So a car would look like this:

    Code:
    class Wheel {}
    
    class Auto
    {
      Wheel *MyWheels;
    };
    
    
    class Car:public Auto
    {
    }

    Correct:
    A car is an auto
    A car has a wheel or set of wheels

    Incorrect:
    A car has a auto.
    A car is a wheel.
    Last edited by VirtualAce; 11-23-2005 at 12:20 PM.

  5. #5
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    Technically in the instance of what I'm defining (animal types and hierarchy, they all inherit each other, but just for that case I am very well aware

    cool, btw, heres what I got so far for my renderer.

    Animal[0]->Poodle

    Correct, because poodle IS an animal

    Renderer.h
    Code:
    #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();
    };
    My Vector3.h so I can use that nifty vector3 obj
    Code:
    class Vector3
    {
    public:
     float x, y, z;
    
      Vector3()
      {
      }
    
      Vector3(float x, float y, float z);
    
    // And so on
    };
    And finally, renderer.cpp
    Code:
    #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;
    }
    Last edited by Shamino; 11-24-2005 at 05:23 AM.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  6. #6
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    Rewrote Renderer.h

    Don't know what to do with the RenderBasicTris() function now though...

    Code:
    #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;
    
    class GLRenderer
    {
    public:
    
    	void	AddBasicTriToRenderer(BasicTri* t)
    	{
    
    		// then add to the vector
    		mBasicTri.push_back(t);
    	}
    
    };
    I think that is a bit more efficient, and avoids passing variables to the addtri function in huge amounts, like 14 floats or whatever... Now All I gotta do is change the triangle, which i would have been doing anyways... Easier to work with now
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  2. Specializing class
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2008, 04:30 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Replies: 7
    Last Post: 05-26-2005, 10:48 AM