• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Nick Karasch

Passing attributes to shaders

19 posts in this topic

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

 

0

Share this post


Link to post
Share on other sites

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
1

Share this post


Link to post
Share on other sites
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.

0

Share this post


Link to post
Share on other sites

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.

1

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites
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.

0

Share this post


Link to post
Share on other sites
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

0

Share this post


Link to post
Share on other sites
#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

0

Share this post


Link to post
Share on other sites

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').

0

Share this post


Link to post
Share on other sites
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

0

Share this post


Link to post
Share on other sites

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  
0

Share this post


Link to post
Share on other sites
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.

1

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

Is

gl_ModelViewProjectionMatrix

unknown by GSLS 3.3? Even

gl_Position = gl_ModelViewProjectionMatrix*gl_Vertex;

isn't working, it is definitely my problem line

0

Share this post


Link to post
Share on other sites
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
1

Share this post


Link to post
Share on other sites

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).

1

Share this post


Link to post
Share on other sites

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();
	}
}
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0