Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Passing attributes to shaders


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
19 replies to this topic

#1 nkarasch   Members   -  Reputation: 171

Like
0Likes
Like

Posted 03 January 2013 - 04:01 PM

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");

 



Sponsor:

#2 TheChubu   Crossbones+   -  Reputation: 4564

Like
1Likes
Like

Posted 03 January 2013 - 04:10 PM

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.


Edited by TheChubu, 03 January 2013 - 04:16 PM.

"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


#3 nkarasch   Members   -  Reputation: 171

Like
0Likes
Like

Posted 03 January 2013 - 04:19 PM

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.



#4 TheChubu   Crossbones+   -  Reputation: 4564

Like
1Likes
Like

Posted 03 January 2013 - 04:32 PM

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


#5 nkarasch   Members   -  Reputation: 171

Like
0Likes
Like

Posted 03 January 2013 - 05:07 PM

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.



#6 HappyCoder   Members   -  Reputation: 2825

Like
0Likes
Like

Posted 03 January 2013 - 05:25 PM

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.



#7 nkarasch   Members   -  Reputation: 171

Like
0Likes
Like

Posted 03 January 2013 - 05:28 PM

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



#8 beans222   Members   -  Reputation: 1133

Like
1Likes
Like

Posted 03 January 2013 - 05:40 PM

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

 


#9 nkarasch   Members   -  Reputation: 171

Like
0Likes
Like

Posted 03 January 2013 - 06:10 PM

#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



#10 beans222   Members   -  Reputation: 1133

Like
0Likes
Like

Posted 03 January 2013 - 11:21 PM

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

 


#11 nkarasch   Members   -  Reputation: 171

Like
0Likes
Like

Posted 03 January 2013 - 11:53 PM

System.out.println(glGetShaderInfoLog(vertexShader, glGetProgrami(shaderProgram, GL_INFO_LOG_LENGTH)));

Isn't giving me anything, hmmm.

 

And yeah, I am going to move away from the built in matrix functions.

 

Also, I'm using Java so I don't think GLM is an option. LWJGL has appropriate tools built in though.

 

Thanks for the response



#12 Nusakan   Members   -  Reputation: 444

Like
0Likes
Like

Posted 03 January 2013 - 11:53 PM

I am not entirely sure about this since i haven't used Opengl for a while but if i remember correctly, generating and binding then vao, vbo goes like this.

 

gluint vaoID;
gluint vboID;
glGenVertexArrays(1, &vaoID); // Create Vertex Array Object  
glBindVertexArray(vaoID); // Bind Vertex Array Object  
  
glGenBuffers(1, &vboID); // Generate Vertex Buffer Object  
glBindBuffer(GL_ARRAY_BUFFER, vboID[0]); // Bind Vertex Buffer Object  


#13 nkarasch   Members   -  Reputation: 171

Like
1Likes
Like

Posted 03 January 2013 - 11:57 PM

I am not entirely sure about this since i haven't used Opengl for a while but if i remember correctly, generating and binding then vao, vbo goes like this.

 

gluint vaoID;
gluint vboID;
glGenVertexArrays(1, &vaoID); // Create Vertex Array Object  
glBindVertexArray(vaoID); // Bind Vertex Array Object  
  
glGenBuffers(1, &vboID); // Generate Vertex Buffer Object  
glBindBuffer(GL_ARRAY_BUFFER, vboID[0]); // Bind Vertex Buffer Object  

Mine looks different because I'm doing it in LWJGL. The gl functions are the same, you just don't have access to pointers in the same way so it ends up looking a bit different. You also have to pass chunks of data as FloatBuffers.



#14 beans222   Members   -  Reputation: 1133

Like
0Likes
Like

Posted 04 January 2013 - 12:10 AM

You need to call glGetShader with the shader object (not the program object with glGetProgram) to get the length.

Sorry about recommending the wrong language library; I was paying attention to the gl, not the language.


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

 


#15 nkarasch   Members   -  Reputation: 171

Like
0Likes
Like

Posted 04 January 2013 - 12:38 AM

Is

gl_ModelViewProjectionMatrix

unknown by GSLS 3.3? Even

gl_Position = gl_ModelViewProjectionMatrix*gl_Vertex;

isn't working, it is definitely my problem line



#16 powly k   Members   -  Reputation: 653

Like
1Likes
Like

Posted 04 January 2013 - 04:55 AM

Yes, it is. In 3.0 and newer, the built-in matrix stuff was dropped - glTranslate, glRotate and all their friends. So you'll have to have your own uniform matrix - it's not too hard to build yourself, there is tons of projection / lookat matrix code out there, and those are the two you most likely want to have.

Edited by powly k, 04 January 2013 - 04:56 AM.


#17 nkarasch   Members   -  Reputation: 171

Like
0Likes
Like

Posted 04 January 2013 - 10:45 AM

Thanks. I got around to implementing my own matrices today, hasn't worked out so well so far but I know it's just errors in my logic

 

For your amusement:



#18 TheChubu   Crossbones+   -  Reputation: 4564

Like
1Likes
Like

Posted 04 January 2013 - 10:43 PM

You could read this http://www.arcsynthesis.org/gltut/

 

Rotation, perspective and translation matrices are explained there. You also have different space transformations too (model to world, word to view, etc). And quaternion rotation for when you get bored of matrices :D

 

It may not be easy to understand, depending on the familiarity you have with linear algebra (even then, the books starts to apply it to graphics so it may get complicated anyway).


"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


#19 nkarasch   Members   -  Reputation: 171

Like
0Likes
Like

Posted 05 January 2013 - 08:48 AM

Disclaimer: This code is an ugly mess that has been getting abused for a few days. It is not pretty, I'm aware. I'm just desperate to get it working, then the beautification process will occur.

 

Also, the delta time is no longer working as intended. I know how to fix it, but there is no point when I can't get rotations working.

 

I'm having a tough time here. My translations work perfectly. However, as soon as I start adding rotations in, things go nuts. X axis rotations alone are fine, Y axis alone are fine but when I have both activated I end up spinning and rolling out of control. I'm also pretty sure that the rotations are all more like revolutions around the original starting point.

 

Any insight would be great. I was stole this code http://www.lloydgoodall.com/tutorials/first-person-camera-control-with-lwjgl/, using the built in matrix stack and it worked great. A lot of the code below is ugly remnants of it

 

package karasch.lwjgl.utility;

import java.io.FileNotFoundException;
import java.io.IOException;
import karasch.lwjgl.entitysetup.IEntity;

import org.lwjgl.input.Mouse;
import org.lwjgl.util.vector.Matrix4f;
import org.lwjgl.util.vector.Vector3f;

public class View implements IEntity {

	private Vector3f position;
	private float x, y, z, yaw, pitch;
	private float moveSpeed, mouseSensitivity, deltaMoveSpeed;
	int dx, dy;
	static Matrix4f viewMatrix;

	public View(float x, float y, float z) throws FileNotFoundException,
			IOException {

		moveSpeed = 0.00001f;

		dx = Mouse.getDX();
		dy = Mouse.getDY();
		
		yaw = 0;
		pitch = 0;

		position = new Vector3f(x, y, z);
		mouseSensitivity = 0.0000025f;

		viewMatrix = new Matrix4f();
		viewMatrix.setIdentity();
	}

	// increment the camera's current yaw rotation
	public void yaw(float amount) {
		// increment the yaw by the amount param
		yaw += amount;
	}

	// increment the camera's current yaw rotation
	public void pitch(float amount) {
		// increment the pitch by the amount param
		pitch += amount;
	}
	
	public void applyTransformations() {
		viewMatrix.rotate(pitch, new Vector3f(1, 0, 0));
		viewMatrix.rotate(yaw, new Vector3f(0, 1, 0));
		viewMatrix.translate(position);
		System.out.println(viewMatrix);
	}

	public void update(int delta) {
		deltaMoveSpeed = moveSpeed * delta;

		dx = Mouse.getDX();
		dy = Mouse.getDY();

		this.yaw(dx * mouseSensitivity);
		this.pitch(dy * -mouseSensitivity);
		if (Input.dPressed) {
			strafeRight(deltaMoveSpeed);
		}

		if (Input.aPressed) {
			strafeLeft(deltaMoveSpeed);
		}

		if (Input.wPressed) {
			walkForward(deltaMoveSpeed);
		}

		if (Input.sPressed) {
			walkBackward(deltaMoveSpeed);
		}

		if (Input.controlPressed) {
			moveDown(deltaMoveSpeed);
		}

		if (Input.spacePressed) {
			moveUp(deltaMoveSpeed);
		}
	}

	public static Matrix4f getViewMatrix() {
		return viewMatrix;
	}

	// moves the camera forward relative to its current rotation (yaw)
	// moves the camera forward relative to its current rotation (yaw)
	public void walkForward(float distance) {
		position.x -= distance * (float) Math.sin(Math.toRadians(yaw));
		position.z += distance * (float) Math.cos(Math.toRadians(yaw));
	}

	// moves the camera backward relative to its current rotation (yaw)
	public void walkBackward(float distance) {
		position.x += distance * (float) Math.sin(Math.toRadians(yaw));
		position.z -= distance * (float) Math.cos(Math.toRadians(yaw));
	}

	public void moveUp(float distance) {
		position.y -= distance;

	}

	public void moveDown(float distance) {
		position.y += distance;

	}

	// strafes the camera left relitive to its current rotation (yaw)
	public void strafeLeft(float distance) {
		position.x -= distance * (float) Math.sin(Math.toRadians(yaw - 90));
		position.z += distance * (float) Math.cos(Math.toRadians(yaw - 90));
	}

	// strafes the camera right relitive to its current rotation (yaw)
	public void strafeRight(float distance) {
		position.x -= distance * (float) Math.sin(Math.toRadians(yaw + 90));
		position.z += distance * (float) Math.cos(Math.toRadians(yaw + 90));
	}

	public Vector3f getPosition() {
		return position;
	}

	public float getX() {
		return x;
	}

	public void setX(float x) {
		this.x = x;
	}

	public float getY() {
		return y;
	}

	public void setY(float y) {
		this.y = y;
	}

	public float getZ() {
		return z;
	}

	public void setZ(float z) {
		this.z = z;
	}

	@Override
	public void destroy() {
	}

	@Override
	public void render() {
		this.applyTransformations();
	}
}


#20 __SKYe   Members   -  Reputation: 1071

Like
0Likes
Like

Posted 05 January 2013 - 09:04 PM

Does just one rotation work as expected?

 

Try changing the order of the rotations, doing the yaw first and the pitch last.

 

Also, do you apply the transformations on an identity matrix each time you render the scene? I think it is necessary to do so, else you're doing translations over rotations over translations, etc.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS