Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 20 Aug 2012
Offline Last Active Yesterday, 11:52 PM

Topics I've Started

Uniform Buffer Confusion

16 October 2014 - 11:32 PM

Hey everyone, I'm trying to implement my shader system and I was previously hardcoding uniforms but I want to go for a more dynamic approach. I found out about Uniform Buffers. So as I understand, what you want is to potentially have a uniform buffer for the Camera and its properties, a uniform buffer for a potential material, etc. Here is the issue I'm running into.


I have a shader program class and in the shader program class, I want it to store a list of all uniform buffer attributes inside the actual program. So basically, each uniform buffer attribute would tell you the number of indices, the buffer name, the buffer byte size, the offsets, the names of the indices, etc. The problem here is that the buffer index in the program isn't predetermined (if it can be, it still would cause issues since you would have to make sure index 1 is the same for all shader programs). So I wanted to get an output array after linking that would state all the uniform buffer names in the program. From there, I could query all the details I need for each uniform buffer and put that into a Uniform Buffer Attribute class.


When rendering, the Renderable object would have its list of uniform buffers and would check to see if the properties of each uniform buffer in the renderable match that which is in the shader program that it is using to render itself. If true, it would bind it over and if not, I would get an warning. 


Unfortunatly, I couldn't find a way online to query the names of all uniform buffers in a shader program. Instead, everyone puts in constant strings since they know before hand what uniform buffers are in the shaders. This doesn't work well since I want my c++ program to easily switch between shader programs/ uniform buffers for each renderable. 


An issue that I see with my approach as well is that the same uniform buffer can have different binding indexes for different programs which could remove the benefit of not having to rebind the uniform buffer after switching shader programs.


I'm not sure if there is some general way around this but I couldn't find a answer online. I was looking a lot at this tutorial (http://www.gamedev.net/page/resources/_/technical/opengl/opengl-40-using-uniform-blocks-and-uniform-buffer-objects-r2860) and other links on different sites but they all use glGetUniformBlockIndex with a constant uniform buffer name. Here's how my code looks for my uniform buffer attribute:

class GLUniformBufferAttributes
		GLuint m_BufferSize;
		std::string m_BufferName;

		std::vector <GLuint> m_UniformOffsetArray;
		std::vector <std::string> m_UniformNameArray;

		GLUniformBufferAttributes ();
		~GLUniformBufferAttributes ();

		bool SetAttributes (GLuint bufferSize, std::string & bufferName, const std::vector<GLuint> & offsetArray, 
							const std::vector<std::string> & nameArray);

My uniform buffer class is sparse and probably has errors since I haven't tried populating it yet but instead getting the querying of attributes to work properly first. As of now, I'm following this link (http://gamedev.stackexchange.com/questions/48926/opengl-fetching-the-names-of-all-uniform-blocks-in-your-program) which does something similar with the index's. The query buffer name should probably be done before the loop but I couldn't find a function to do it. Here's how I'm trying to query the data in my shader program class after linknig:

class GLShaderProgram
		GLuint m_ShaderProgId;
		std::vector <GLUniformBufferAttributes> m_UniformAttributeArray;
bool GLShaderProgram::InitializeUniformBlocks ()
		// Get all uniform buffer specifications
		GLint numUniformBlocks; // Get number of uniform buffers
		glGetProgramiv(m_ShaderProgId, GL_ACTIVE_UNIFORM_BLOCKS, &numUniformBlocks);

		m_UniformAttributeArray = std::vector <GLUniformBufferAttributes> (numUniformBlocks, GLUniformBufferAttributes());
		for ( unsigned int index = 0; index < numUniformBlocks; index++ ) // unsafe unsigned vs signed int mistmatch
			// Query buffer size
			GLint blockSize;
			glGetActiveUniformBlockiv(m_ShaderProgId, index, GL_UNIFORM_BLOCK_DATA_SIZE, &blockSize);

			// Query buffer name
			GLchar bufferName;

			// Query number of uniforms
			GLint numUniforms;
			glGetActiveUniformBlockiv(m_ShaderProgId, index, GL_UNIFORM_BLOCK_NAME_LENGTH, &numUniforms);

			// Query the names of uniforms
			std::vector <GLchar> nameArray;
			glGetActiveUniformBlockName(m_ShaderProgId, index, numUniforms, nullptr, &nameArray[0]);

			// Query the offsets of uniforms
			std::vector <GLint> offsetArray;
			glGetActiveUniformBlockiv(m_ShaderProgId, index, GL_UNIFORM_OFFSET, &offsetArray[0]);

			//m_UniformAttributeArray[index].SetAttributes(bufferSize, bufferName, offsetArray, nameArray);

		return true;
#version 330

layout (location = 0) in vec3 Position;
layout (location = 1) in vec3 Normal;
layout (location = 2) in vec2 TexCoords;

uniform TestBlock
	vec4 v1;
	float r1;

uniform mat4 gWorld;

out vec2 TexCoord;

void main()
    gl_Position = gWorld * vec4(Position, 1.0);
	TexCoord = TexCoords;

Another note, I have a uniform defined in my vertex shader which itself executes properly, however I'm assuming the index loop is messed up since when it gets the uniform buffer at index 0, it pulls out a completely different uniform buffer which I'm assuming is the default one in shader programs since it has weird variable names and what not. It has 10 variables in it and they're names are single letters + numbers. Thanks in advance.

Rendered Object disappears under Perspective Projection

27 September 2014 - 12:49 PM



I'm trying to a render a simple model using a wrapper of OpenGL that I'm creating. I'm following multiple tutorials while attempting to make my rendering pipeline, naimly http://ogldev.atspace.co.uk/ and http://www.opengl-tutorial.org/. When I render my triangle given the coordinates (-1.0f, -1.0f, 0.0f), (1.0f, -1.0f, 0.0f),( 0.0f, 1.0f, 0.0f) under orthogonal projection the model appears as expected. I can also translate it using the Orientation class I made. But once I use glm::perspective and multiply it by the orientation matrix, the model never displays. I tried the same parameters as the tutorials have but the object just disappears and I can't figure out why.


My parameters for glm perspective are FOV = 45.0f, Aspect Ratio = 640.0f / 480.0f, Near = 1.0f, Far = 100.0f. I set the object to be scaled by 1 (keeps original size) and I tried putting the object in positive and negative 3 in the z direction, both of which didn't display. For doing orientation, I use glm's translate, rotate and scale functions. The WVP matrix I was sending (minus the view) became:


0.0   2.414213   0.0   0.0

0.0   0.0   -7.080808   -1

0.0   0.0   -2.020202   0.0

1.810660   0.0   0.0   0.0


The vertex shader simply takes this value and multiplies it by the position and the fragment shader colors the triangle red.


Game Actor and System Architecture

01 July 2014 - 10:10 PM

Hey guys, I've been looking through some books and online on the topic of game engine architectures and how actors factor in. A big one was from this thread right here (http://www.gamedev.net/topic/617256-component-entity-model-without-rtti/page-2). The way I understood it is like this:


Actor Component: Defines some relatively independent data that represents some isolated attribute of a larger being. For example, a actor for a gun might have a component for the gun's model, a component for the amount of ammo, and a component for the damage properties of the gun.


Actor: A list of actor components.


System: Runs the game logic, has a list of actors on which the system operates. An example, the physics system has a list of actors that have a physics object which it uses to check for collisions and notify's the actors and their components when a collision happens.


This is where things get kind of shady. A system is supposed to carry out game logic but it doesn't make sense for all the game logic to be done in a system. Using the physics system example, it makes sense for the system to find collisions but when a collision happens, it doesn't always mean calculate the reflection of both objects. Sometimes, I might be colliding with ammo so I should be picking it up instead. Stuff like that doesn't make sense to be done in the system but rather in the actor/their components. 


This works nice but then it makes defining the components a bit more iffy. If the ammo actor is supposed to have some way of reacting to a collision, how does the physics system know which component it should be looking for? There might only be one type of component that is a physics collision model which could describe the collision model for the ammo, but that same component could be used for a rigid body on another actor which should react by physics laws to a collision. 


So the way I understand it, here is how it roughly looks right now:

class Actor
    std::vector <IActorComponent> m_ActorComponents;

class IActorComponent
    // will be overridden and will have some new properties
    virtual bool VInit ();
    virtual bool VDestroy ();

class ISystem
    virtual void VInit ();
    virtual void VUpdate (unsigned int deltaMs);
    virtual void VDestroy ();

And here is a implementation:

class CollisionModelComponent : public IActorComponent
    std::vector <Vertices> m_VertexArray;

class PhysicsSystem : public ISystem
    std::list <Actor> m_Actors;
    void VUpdate ()
        for every actor
            if actor collided
                // What do we look for here? How do we know to run ammo collision response or rigid body response?

You could make a collision response actor component which tells the physics system how to respond to a collision but then you have a issue where the ammo collision response has to have access to the ammo component.


In my code, the actors are created from xml files and each actor is created the same through a factory class. In it, I loop through all the nodes of a xml file and apply the properties to the given component at hand. All components override the virtual VInit function which takes no parameters. If I wanted to create a dependancy between ammo component and collision response component, I would need to somehow pass the ammo instance to the collision response through the init but not all components need a dependancy so it doesn't make sense to have it by default pass a pointer to some actor component through VInit. There could also be cases where we have multiple dependancies which complicates the process.


Is there another way to do this or some way to restructure or apply constraints in order to make this architecture work? It's a really clean architecture if one where to be able to make everything separable. Any help?

Abstract Class Deriving Upwards

19 June 2014 - 10:00 PM

Hey guys,


I have a question regarding casting abstract classes to their derived counterparts. I'm trying to upcast from the base class to the derived class but both classes are encapsulated in smart pointers. Here's an example:

#include <memory>
#include <string>

	class Shape
		//std::string * type;
		virtual int Area ();

	class Box : public Shape
		//char * m_pBuffer;
		virtual int Area ();

	int Box::Area ()
		return 2;

	int test ()
		std::shared_ptr <Shape> pShape (new Box ());
		std::shared_ptr <Box> pBox = static_cast <std::shared_ptr <Box>> (pShape); // no conversion
		std::shared_ptr <Box> pSecondBox = pShape; // no conversion

		Shape * pStrongShape = new Box ();
		Box * pStrongBox = static_cast <Box*> (pStrongShape); // works

In the test function, at the beginning, i'm trying to upcast from Shape to Box by using the static_cast but I get an error telling me that there is no suitable conversion between the two. I can't use dynamic_cast either since it tells me to dynamic cast, the type must be of type pointer. What would be the proper way of achieving this using smart pointers? Shouldn't static casts do the conversion properly for me? Or am I forced to convert between normal pointers to smart pointers in order for this to work? 

File Loading Adds Weird Characters to the end

19 May 2014 - 04:13 PM

Hello everyone, I got some code that loads a text file into a char* buffer and for the most part, it works! The issue is that it adds some weird extra characters to the end. Here is an example:




In this example above, the text file has the words "WORKS!" and the char buffer allocated is taken from the size of the file which in this case, is new char [6].


Here is the code:

bool DefaultResourceLoader::VLoadResource (std::shared_ptr <ResourceHandle> pResourceHandle)
		std::ifstream loadedFile;
		loadedFile.read(pResourceHandle->GetWritableResourceBuffer(), pResourceHandle->GetFileSize());
		if ( loadedFile.fail() )
		return true;

GetResourceName returns the file name as a string. GetWriteableResourceBuffer returns the char* buffer which is of size of the file, in this case, new char[6]. GetFileSize also returns the file size so 6 for this example.


Is there any reason why those characters get printed out? I'm using unicode in my VS2010 settings, and I tried type casting it to char* after, or creating a string for it and printing it. I also tried printf and cout but both result in the same thing.