Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 19 Jun 2012
Offline Last Active Jun 06 2014 01:49 AM

Topics I've Started

Removing items with glBufferSubData

29 April 2014 - 06:02 AM

Hey guys,

I'm currently working on making a text drawing module in my project. It's working for the most part but i'm having some trouble when trying to 'backspace' characters (removing a quad from the back of the buffer).


So at the moment this is how I'm doing things 

1. Create a buffer object with a size enough to hold 120 or so characters (a character is a quad made up of 6 vertices)

2. When the user presses a key, add 6 new vertices to the end of the vbo (with correct tex coords for the character) using glBufferSubData


This works fine and I can add those 120 characters by pressing letters on the keyboard (It re-builds the buffer by another 120 characters when you reach the limit)

Now the problem is I want to erase a character when the backspace (39) key is pressed It doesnt visually appear to have an effect until you add another character.


Example use case:

- Press 'a' 

- Press 'b'

- Press 'c'

- Press 'd'


My buffer contains 4 quads (each quad is 6 vertices) and on screen it prints "abcd" fine. 

Now If i press backspace once it should read - "abc"

but it still reads - "abcd"

However if I add a new character say 'e' (after backspace) the text becomes - "abce". Which leads me to believe it did in fact remove the last quad from the buffer BUT was not updated till more data was written to that area of memory. 

So it really did erase that character but it doesnt get updated till you add another quad

This is how I'm updating my buffer after a backspace:

void Text2D::PopLetter()
glBindBuffer(GL_ARRAY_BUFFER, m_vbo->vData->m_vboId);

// remove 6 vertices from the end of the vector

int currSize = m_vbo->vData->vertices.size() * sizeof(Vertex);
int amountToOverwrite = sizeof(Vertex) * 6;

glBufferSubData(GL_ARRAY_BUFFER, currSize, amountToOverwrite, nullptr);

So after this function call, the last quad is still visible, yet when I add another character the old one is just replaced with the new one if that makes sense.


Is there a better way to do this?


Thanks guys

Having problems updating vbo

13 February 2013 - 08:20 AM



The past couple of days I've been running into some troubles attempting to update my VBO. I've taken the approach of allocating extra size at the start, so that when I need to add some more sprites in there is room. 


Basically I'm doing things like this:


Create all the sprites I need at the start and put them in a VBO, but make sure the VBO has space for extra sprites


// all my sprite data currently resides here
std::vector<Vertex> m_vertices;

// create an opengl VBO

glGenVertexArrays(1, &m_vaoId);

// times 6 because there are 6 verts for a quad of two triangles
int size = (m_vertices.size() + (numExtraSprites * 6)) * sizeof(Vertex);

glGenBuffers(1, &m_vboId);
glBindBuffer(GL_ARRAY_BUFFER, m_vboId);
glBufferSubData(GL_ARRAY_BUFFER, 0, m_vertices.size() * sizeof(Vertex), &m_vertices[0]);

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), BUFFER_OFFSET(0));

glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), BUFFER_OFFSET(12));


Now this is fine, everything works perfectly and draws as expected.


I've got it setup so that when i click with the mouse, it will add another sprite of the same type at the mouse location. 


This just involves me creating a temporary std::vector<Vertex> with 6 new vertices in it positioned at the mouse location. 

Then I attempt to update my VBO.


void SpriteBatch::Update()
    int offset = m_vertices.size() * sizeof(Vertex);
    int size = sizeof(Vertex) * 6; // 6 because there is one more sprite to be updated

    glBindBuffer(GL_ARRAY_BUFFER, m_vboId);
                    &m_tempSpriteVec[0]); // m_tempSpriteVec holds the new 6 verts

    m_vertices.insert(m_vertices.end(), m_tempSpriteVec.begin(), m_tempSpriteVec.end());



This does not work, even though the vbo should have sufficient size? I've noticed if I just update one of the existing sprites (say the first 6 vertices of the vbo) then It works, but shouldn't it be big enough for the new data?


Thanks guys

Problems with creating view matrix

01 January 2013 - 03:41 AM

Hey guys,

I'm attempting to roll my own matrix and vector mathematics to do my transformations and creating MV and MVP matrices to send to my shaders. I've managed to get perspective working but I'm having a few problems with my view matrix.

What is working:

I can look around the world with the camera and everything looks and moves fine.

What's the problem:

As soon as I begin to translate my view matrix I get really weird behaviour which I find hard to explain. When I start moving around the world The camera seems to change the direction its looking at.

This is my create view code:


Matrix4x4 createView( Vector3* pos, Vector3* dir, Vector3* up )
	Matrix4x4 mat = identity();
	Matrix4x4 invView = identity();

	Vector3 rowZ = sub(dir, pos);
	Vector3 row4 = { pos->x, pos->y, pos->z };
	Vector3 rowX = crossProd(up, &rowZ);
	Vector3 rowY = {0};	

	setEqual(&rowY, up);

	setRowv(&mat, 0, &rowX, 0.0f);
	setRowv(&mat, 1, &rowY, 0.0f);
	setRowv(&mat, 2, &rowZ, 0.0f);
	setRowv(&mat, 3, &row4, 1.0f);

	invView = inverseMat4(&mat);	// has to be inverse for camera transformations

	return invView;


My matrix inverse code is from Mesa3D glu implementation (http://stackoverflow.com/questions/1148309/inverting-a-4x4-matrix)


And here is where the camera gets updated


void updateCam( Camera* cam )
	Vector3	sum = addVec(&cam->dir, &cam->pos);

	cam->view = createView(&cam->pos, &sum, &cam->up);


For translation I'm just adding directly to the cam.pos vector.


Rotation seems to work fine but here it is anyway:


void rotateView( Camera* cam, float yawAmt, float pitchAmt)
	float cosPitch;
	float cosYaw;
	float sinPitch;
	float sinYaw;

	cam->yaw += yawAmt;
	cam->pitch += pitchAmt;

	sinYaw = sin(degToRad(cam->yaw));
	sinPitch = sin(degToRad(cam->pitch));
	cosYaw = cos(degToRad(cam->yaw));
	cosPitch = cos(degToRad(cam->pitch));
	cam->dir.x = cosPitch * sinYaw;
	cam->dir.z = cosPitch * cosYaw;
	cam->dir.y = sinPitch;


Thanks for the help, I'm sure the problem lies within the create view function.



Second Shader not working

27 November 2012 - 09:37 AM

Hey everyone,

I've got a strange problem most likely tied to my inexperience with using shaders. Basically only my first shader program seems to work. I'm doing error checking with glGetShaderInfoLog etc and the shaders seem to compile fine. I've also been trying to print out the active uniform variables and it will only ever work for the first shader that I create.

Some pseudo code for my program flow:


int shader1 = CreateShader("passthrough.vert", "passthrough.frag");
int shader2 = CreateShader("diffuse.vert", "diffuse.frag");

void SetUniforms()
// set uniforms here for shader1 or 2, depending on which is bound

void PrintActiveUniforms()
// print active uniforms here. If shader1 is bound, the uniforms are printed fine. If shader2 is bound the uniforms do not print out.

void Render()
void RenderAllGeometry()
	for (int i = 0; i < meshes.size(); i++)
		GLuint vao = meshes.at(i)->getVao();
		glDrawElements(GL_TRIANGLES, mesh->GetIndexBuffer().Size(), GL_UNSIGNED_INT, BUFFER_OFFSET(0));

This all works perfectly if it is shader1 that is bound, but shader2 being bound results in nothing being drawn. I've tested both shaders and they work, but it's only ever the first program created that will result in the geometry rendering with that shader.

I can't see why this isn't working and I can post more code if needed. But basically my shader loading code is taken from a tutorial website and I can actually get something drawing with the first bound shader. The only thing I can think of is that vertex array objects might have an effect on the shaders used?

Thanks for the assistance in advance.

Assimp debug exe - application error

29 September 2012 - 07:52 AM


I've just downloaded assimp and have run into a couple of troubles. I've linked the libraries assimp.lib (release and debug) and my application compiles fine (i'm using VS2010). However in debug mode I get the error

"The application was unable to start correctly (0xc0150002). Click OK to close the application."

It runs fine in Release mode however. I'm using the debug dll for the debug exe. I've tried to find out how to fix this on google but i've had no luck, just hoping someone here knows what exactly is wrong.

Thanks guys,