Passing attributes to shaders

Started by
18 comments, last by _paf 11 years, 3 months ago

I'm pretty confused and sick of failing, I searched like crazy and resisted the urge to make a new thread for a long time but here it is.

I don't feel like my attributes are getting passed into my shaders properly. Here is my code for when I bind objects to VBOs

(this is LWJGL)


		int vaoId = glGenVertexArrays();
		glBindVertexArray(vaoId);

		int vboVertexHandle = glGenBuffers();
		int vboTextureHandle = glGenBuffers();
		int vboNormalHandle = glGenBuffers();

		glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
		glBufferData(GL_ARRAY_BUFFER, vertices, GL_STATIC_DRAW);
		glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);

		glBindBuffer(GL_ARRAY_BUFFER, vboTextureHandle);
		glBufferData(GL_ARRAY_BUFFER, textureCoordinates, GL_STATIC_DRAW);
		glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0);

		glBindBuffer(GL_ARRAY_BUFFER, vboNormalHandle);
		glBufferData(GL_ARRAY_BUFFER, normals, GL_STATIC_DRAW);
		glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0);
		
		glEnableVertexAttribArray(0);
		glEnableVertexAttribArray(1);
		glEnableVertexAttribArray(2);

		glBindBuffer(GL_ARRAY_BUFFER, 0);
		glBindVertexArray(0);
		return vaoId;

when I call glVertexAttribute(), the first number can be whatever I want...right? It is the position that I want to find that information at in the VAO in the future?

and then I did this


	glBindAttribLocation(shaderProgram, 0, "VertexPosition");
	glBindAttribLocation(shaderProgram, 1, "TextureCoordinate");
	glBindAttribLocation(shaderProgram, 2, "Normals");

before using the shader, and tried initialize access of the information in the vertex shader like this


in vec3 VertexPosition;

Is this right? What is missing?

Also, if I'm using GLSL 4.0 is using the location like this


layout (location = 0) in vec3 VertexPosition;

a direct replacement for this?


glBindAttribLocation(shaderProgram, 0, "VertexPosition");

Advertisement

That last part is true. You no longer need glBindAttribLocation if you're using the layouts in the shader (works since OpenGL 3.2 I think).

About your vbos, I'm not sure. I don't know if its wrong though it looks okay. 3 different pointers for 3 different vbos that hold different attributes. Though I'd store everything in a single vbo.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

That last part is true. You no longer need glBindAttribLocation if you're using the layouts in the shader (works since OpenGL 3.2 I think).

About your vbos, I'm not sure. I don't know if its wrong though it looks okay. 3 different pointers for 3 different vbos that hold different attributes. Though I'd store everything in a single vbo.

Thanks for confirming that for me.

Mixing the three vbos together are called interleaved, right? I wanted to get it straightened out with them separated before introducing any complications that could occur from having them mixed.

Not necessarily. You could have them interleaved (which is a bit tricky at first) or just a continuous single VBO (first all the positions, then all the texture coords, then all the normals).

This is my interleaved vbo: It stores things as X,Y,Z,W,R,G,B,A - X,Y,Z,W,R,G,B,A - and so on.


public void genInterleavedVAO ( FloatBuffer vertexFloatBuffer, IntBuffer indexIntBuffer )
{
//Generate vertex array object.
this.vertArrayObjId = GL30.glGenVertexArrays();
GL30.glBindVertexArray( this.vertArrayObjId );
//Generate vertex buffer object.
this.vertBuffObjId = GL15.glGenBuffers();
GL15.glBindBuffer( GL15.GL_ARRAY_BUFFER, this.vertBuffObjId );
GL15.glBufferData( GL15.GL_ARRAY_BUFFER, vertexFloatBuffer, GL15.GL_STATIC_DRAW );
/*
* Set up attribute pointer, set up stride as 32 bytes.
* Thus, the space between the beginning of each vertex 
* is 32 bytes. 16 bytes for the four floats of the vertex
* and another 16 byte jump for the RGBA floats. 
*/
GL20.glVertexAttribPointer( 0, 4, GL11.GL_FLOAT, false, 32, 0 );
/* 
* Set up attribute pointer. This one also has a 32 byte stride.
* But, the first attribute starts at a 16 byte offset (jumping
* over the 0 attribute).
*/
GL20.glVertexAttribPointer( 1, 4, GL11.GL_FLOAT, false, 32, 16 );
//Unbind the colored vbo.
GL15.glBindBuffer( GL15.GL_ARRAY_BUFFER, 0 );
//Unbind the vertex array object.
GL30.glBindVertexArray(0);
//Generate vbo for the element indexes.
this.indexBuffObjId = GL15.glGenBuffers();
GL15.glBindBuffer( GL15.GL_ELEMENT_ARRAY_BUFFER, this.indexBuffObjId );
GL15.glBufferData( GL15.GL_ELEMENT_ARRAY_BUFFER, indexIntBuffer, GL15.GL_STATIC_DRAW );
//Unbind index vbo.
GL15.glBindBuffer( GL15.GL_ELEMENT_ARRAY_BUFFER, 0 );
 
}

If you want a continuous vbo, you'd only need to set up the offset and leave the stride alone.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

How can I pass (is that even the right word?) a uniform?

The example I'm looking at has


uniform sampler2D myTextureSampler;

in the shader

My render code has this


	        glActiveTexture(GL_TEXTURE0);
		glBindTexture(GL_TEXTURE_2D, textureId);
                int texLoc = glGetUniformLocation(shaderProgram, "myTextureSampler");
		glUniform1i(texLoc, 0);

When I print texLoc out it is always -1

What is wrong here?

Thanks for the continuous vbo example.

When I print texLoc out it is always -1

What is wrong here?

Thanks for the continuous vbo example.

If myTextureSampler is not used then the compiler optimizes it out of the program as if it weren't declared.

My current game project Platform RPG
When I print texLoc out it is always -1

What is wrong here?

Thanks for the continuous vbo example.

If myTextureSampler is not used then the compiler optimizes it out of the program as if it weren't declared.


#version 330 core

in vec2 TC;

out vec3 color;

uniform sampler2D myTextureSampler;

void main(void) {
	
	color = texture( myTextureSampler, TC ).rbg;
}

It is used :\

That fixed function stuff was waaay easier, the learning curve on this seems really steep

Check that your program object is linking properly.

New C/C++ Build Tool 'Stir' (doesn't just generate Makefiles, it does the build): https://github.com/space222/stir


#version 330 core

layout (location = 0) in vec3 VertexPosition;
layout (location = 1) in Vec2 TextureCoordinate;

out vec2 TC;

void main(void) {

	TC = TextureCoordinate;
	gl_Position = gl_ModelViewProjectionMatrix * vec4(VertexPosition, 1);
}

Yeah, my vertex shader isn't compiling correctly. I'm still using the built in matrix functions at the moment. Hmmmm

The obvious error (obvious mainly because of the post's code coloring) is that the first vec2 type has the wrong case V in it.

You should switch away from using the built-in matrix math functions.

http://glm.g-truc.net/ is header only library that's very easy to use.

Btw, the shader compiler is capable of giving you an error message (the 'info log').

New C/C++ Build Tool 'Stir' (doesn't just generate Makefiles, it does the build): https://github.com/space222/stir

This topic is closed to new replies.

Advertisement