Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a value from vec3 in vertex shader? OpenGL 3.3

I have the following vertex shader:

#version 330                                                                      
layout (location = 0) in vec3 Position;                                           
uniform mat4 gWVP;                                                                
out vec4 Color;
void main()                                                                       
{                                                                                 
    gl_Position = gWVP * vec4(Position, 1.0);                     
};

How can I get, for example, the third value of vec3? The first my thought was: "Maybe I can get it by multiplying this vector(Position) on something?" But I am not sure that something like "vertical vector type" exists. So, what is the best way? I need this value to set the color of the pixel.

like image 271
Denis Avatar asked Jan 17 '15 09:01

Denis


People also ask

What is the output of a vertex shader?

A vertex shader receives a single vertex from the vertex stream and generates a single vertex to the output vertex stream. There must be a 1:1 mapping from input vertices to output vertices. Vertex shaders typically perform transformations to post-projection space, for consumption by the Vertex Post-Processing stage.

Why is gl_Position a vec4?

In the last assignment statement, gl_Position is the special built-in variable that is used in the vertex shader to give the coordinates of the vertex. gl_Position is of type vec4, requiring four numbers, because the coordinates are specified as homogeneous coordinates (Subsection 3.5. 3).

What is vec4 in OpenGL?

vec4 is a floating point vector with four components. It can be initialized by: Providing a scalar value for each component. Providing one scalar value. This value is used for all components.

How does a vertex shader pass data to a fragment shader?

A vertex shader receives its input data from a vertex attribute. But how does a fragment shader gets its data? Data is passed from shader to shader by using the in and out keywords. You create an output shader variable by using the out keyword.


1 Answers

There are at least 4 options:

  1. You can access vector components with component names x, y, z, w. This is mostly used for vectors that represent points/vectors. In your example, that would be Position.z.
  2. You can use component names r, g, b, a. This is mostly used for vectors that represent colors. In your example, you could use Position.b, even though that would not be very readable. On the other hand, Color.b would be a good option for the other variable.
  3. You can use component names s, t, p, q. This is mostly used for vectors that represent texture coordinates. In our example, Position.p would also give you the 3rd component.
  4. You can use the subscript notation with 0-based indices. In your example, Position[2] also gives he 3rd element.
like image 116
Reto Koradi Avatar answered Oct 21 '22 04:10

Reto Koradi