Reputation: 585
I've been struggling with an issue with my vertex shader. I have a sprite with the following position attributes:
sprite->vertices[0][0] = -1.0f;
sprite->vertices[0][1] = +1.0f;
sprite->vertices[1][0] = -1.0f;
sprite->vertices[1][1] = -1.0f;
sprite->vertices[2][0] = +1.0f;
sprite->vertices[2][1] = +1.0f;
sprite->vertices[3][0] = +1.0f;
sprite->vertices[3][1] = -1.0f;
And I use the attribute this way:
glUseProgram(sprite->program);
glEnableVertexAttribArray(sprite->position_attrib);
glVertexAttribPointer(sprite->position_attrib, 2, GL_FLOAT, GL_FALSE, 0,
sprite->vertices);
Here is my vertex shader:
static const char character_vshader_g[] =
"#ifdef GL_ES\n"
"precision mediump float;\n"
"#endif\n"
"uniform mat4 mvp_u;\n"
"attribute vec4 position_a;\n"
"attribute vec2 texture_a;\n"
"varying vec2 texture_v;\n"
"mat4 tmp;\n"
"void main() {\n"
" tmp = mat4(\n"
" vec4(0.5, 0 , 0 , 0 ),\n"
" vec4(0 , 0.5, 0 , 0 ),\n"
" vec4(0 , 0 , 0.5, 0 ),\n"
" vec4(0 , 0 , 0 , 0.5) \n"
" );\n"
" gl_Position = tmp * position_a;\n"
" texture_v = texture_a;\n"
"}\n";
Basically I'm trying to manage my own mvp uniform matrix and in order to debug it, I hard coded this basic scaling matrix tmp.
My issue is that this multiplication:
gl_Position = tmp * position_a;
Does not change anything at all unless I put all 0 in tmp. Then my quad disappears. Otherwise I see my quad occupying the full viewport (I do not have any kind of projection as you can see so the default viewport is -1,-1 to 1,1).
So it's almost like tmp is not doing anything.
Can anyone help?
Upvotes: 1
Views: 660
Reputation: 162164
Oh, your matrix gets applied just fine. The problem is, that the ww
element is 0.5
, which when multiplied with a vec2
will yield some vec4(0.5x, 0.5y, 0, 0.5)
. However one of the last, hardcoded steps in the transform pipeline is the homogenous perspective divide v' = v/v.w
. But a vec4(0.5x, 0.5y, 0, 0.5) / 0.5 == vec4(x, y, 0, 1)
which looks just like as if nothing happened at all.
Solution to your problem: Make the w-row of your matrix 0 0 0 1
, i.e. in your code
" tmp = mat4(\n"
" vec4(0.5, 0 , 0 , 0 ),\n"
" vec4(0 , 0.5, 0 , 0 ),\n"
" vec4(0 , 0 , 0.5, 0 ),\n"
" vec4(0 , 0 , 0 , 1 ) \n"
Upvotes: 4