Jump to content

  • Log In with Google      Sign In   
  • Create Account


Trouble understanding passing vertex data between GLSL/OpenGL


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
1 reply to this topic

#1 mv348   Members   -  Reputation: 249

Like
0Likes
Like

Posted 09 September 2012 - 09:20 AM

I think I understand pretty well how to pass vertex attributes from OpenGL to GLSL. If my vertex attribute is 'position' in my vertex shader:

// vertex shader
attribute vec4 position;
...


Then first I use:

//C++
GLint position_id = glGetAttribLocation(shader_program,"position");
glBindBuffer(GL_ARRAY_BUFFER, positionVBO);
glEnableVertexAttribArray(position_id);
glVertexAttribPointer(position_id, VERTEX_STRIDE, GL_FLOAT, GL_FALSE, 0, 0); 

Easy.

However, passing data back into a buffer from within a shader is not clear to me.
In this tutorial for rendering a scene to a texture, they create a color variable:
//fragment shader
layout(location = 0) out vec3 color;

That is rendered to a frame buffer with an attached texture. The following openGL function:

glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, renderedTexture, 0);

attaches the texture 'renderedTexture' to an FBO, and presumably GL_COLOR_ATTACHMENT0 corresponds to the 'layout=0' in the shader.
I guess that makes sense. But later in this shadow mapping tutorial, they render only the depth attribute to the FBO using GL_DEPTH_ATTACHMENT.

//C++
glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthTexture, 0);

// fragment shader
layout(location = 0) out float fragmentdepth;

Now I don't really see how the ATTACHMENT parameter and the layout location value correspond. How does OpenGL know to render the 'fragmentdepth' value into the depthTexture? What if you had multiple frame buffer objects, and wanted to output color to one, and depth to another? How would you set the layout location value then?

Sponsor:

#2 tanzanite7   Members   -  Reputation: 1202

Like
0Likes
Like

Posted 09 September 2012 - 03:21 PM

I would advise to stick with one glsl version - less confusing. Start with 3.2 or something, where all shader stages get their input from "in" and send out with "out" instead of "attribute", "varying".

Also, one can use layout location for vertex shader input too, so this is not needed:
GLint position_id = glGetAttribLocation(shader_program,"position");
as you can do that in glsl source:
layout(location = 0) in vec3 position

Can not say for sure about the buffer numbering, too sleepy, so i wont.

Edited by tanzanite7, 09 September 2012 - 03:22 PM.





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