Devin Zhao
Devin Zhao

Reputation: 159

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

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,
};

Upvotes: 1

Views: 918

Answers (1)

prideout
prideout

Reputation: 3019

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.

Upvotes: 1

Related Questions