1

Could I bind a vec2 array to a vec4 variable in shader language?

For example, in the code blow, the variable "position" is a vec4 type, and I tried to bind variable "squareVertices" to it, but squareVertices is a vec2 type array, only with (x,y) coodinates, maybe default (x,y,z,w) = (x,y,0,1)?

My Vertex shader and attribute bind code:

attribute vec4 position;
attribute vec2 textureCoordinate;
varying vec2 coordinate;
void main()
{
    gl_Position = position;
    coordinate = textureCoordinate;
}

glBindAttribLocation(gProgram, ATTRIB_VERTEX, "position");
glBindAttribLocation(gProgram, ATTRIB_TEXTURE, "textureCoordinate");

// bind attribute values
glVertexAttribPointer(ATTRIB_VERTEX, 2, GL_FLOAT, 0, 0, squareVertices);
glEnableVertexAttribArray(ATTRIB_VERTEX);

glVertexAttribPointer(ATTRIB_TEXTURE, 2, GL_FLOAT, 0, 0, coordVertices);
glEnableVertexAttribArray(ATTRIB_TEXTURE);

// squareVertices definition
static const GLfloat squareVertices[] = {
    -1.0f, -1.0f,
    0.0f, -1.0f,
    -1.0f,  0.0f,
    0.0f,  0.0f,
};

1 Answer 1

1

Yes, this is legal. And, you're correct that the default values of z and w are 0 and 1, respectively. This is covered in section 2.7 of the OpenGL ES 2.0 specification.

Sign up to request clarification or add additional context in comments.

1 Comment

Six years later, this just solved my doubt: how the first examples in OpenGL ES 2 for Android: A Quick-Start Guide could have only x and y coordinates, while in the vertex shader a_Position was a vec4. Tested it in new code and works... One OpenGL mystery less, thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.