OpenGL ES 2.0 specifiying position attribute vec3 or vec4

In the OpenGL ES 2.0 introduction, found here: http://www.webreference.com/programming/opengl_es/2.html a vertex shader is defined:

GLbyte vShaderStr[] =
   "attribute vec4 vPosition;   \n"
   "void main()                 \n"
   "{                           \n"
   "   gl_Position = vPosition; \n"
   "};                          \n";

The vPosition attribute is a four component vector.

Later in the text the application will compile the vertex shader, together with the fragment shader. With the glBindAttribLocation a handle to pass the applications vertex data to the shader is established:

// Bind vPosition to attribute 0
glBindAttribLocation(programObject, 0, "vPosition");

The 2 shaders are now linked and the program is ready to use.

The use case is this:

GLfloat vVertices[] = {0.0f, 0.5f, 0.0f,
                        -0.5f, -0.5f, 0.0f,
                        0.5f, -0.5f, 0.0f};
...
// Load the vertex data
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vVertices);
glEnableVertexAttribArray(0);
glDrawArrays(GL_TRIANGLES, 0, 3);

Which means:

  • glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vVertices) : pass the vertex data to the program, use location 0, each vertex has 3 components, type is float, no normalisation shall be used.

  • glEnableVertexAttribArray(0) : The shader will use the passed data.

  • glDrawArrays(GL_TRIANGLES, 0, 3); : The shader will execute the code to draw a single triangle, using the first (and only) three vertices found in the specified array.

My Questions are:

  • The vertex shader is expecting a four component vertex position (x,y,z,w), the program passes only three components (x,y,z) data Why is this working?

  • The shader needs a vec4, the w-component must be 1 to work as expected. Which step is responsible for adding the w-component correctly?

Answers


See this answer: Can you use glVertexAttribPointer to assign a smaller vector to a larger one?

As mentioned in that post it appears in the gles specifications: http://www.khronos.org/registry/gles/specs/2.0/es_full_spec_2.0.24.pdf In section 2.8 of this document it says:

When an array element i is transferred to the GL by the DrawArrays or DrawElements commands, each generic attribute is expanded to four components. If size is one then the x component of the attribute is specified by the array; the y, z, and w components are implicitly set to zero, zero, and one, respectively. If size is two then the x and y components of the attribute are specified by the array; the z, and w components are implicitly set to zero, and one, respectively. If size is three then x, y, and z are specified, and w is implicitly set to one. If size is four then all components are specified.


Need Your Help

Unhealthy web role instance in azure cloud service deployment

azure certificate instance azure-configuration

My web role instance can't run after the deployment to azure, it tries to start it and then it reports the instance as "Unhealthy" and gives the following message: "The role could not be started (R...

How can I tell premake to generate a Makefile that uses clang?

build makefile make premake

I would like to build my application using clang when generating a Makefile (gmake toolchain). Is there a way to do this?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.