PDA

View Full Version : OpenGL GLM C++ vertex and matrix transformations confusion

Predator105
01-11-2013, 08:08 PM
I'm pretty confused now that I can no longer do a simply gl_Position = pos * MVP; in my vertex shader, and instead want to transform the vertices before I send them via glBufferSubData.

This is my poor attempt at trying to get the vertex coordinates to be pixel based. Lets say I want a rect to be 100 pixels from 0,0. I was hoping this would work, and I've been fiddling it with for a while.

What am I doing wrong here, or what is it that I actually want to do?

glm::mat4 view = glm::translate(glm::mat4(), glm::vec3(0.0f, 0.0f, 0.0f));
glm::mat4 ortho = glm::ortho(0.0f, float(SCREEN_W), float(SCREEN_H), 0.0f, -1.0f, 1.0f);

glm::vec4 rect = glm::vec4(0.0f, 0.0f, 1.0f, 1.0f);

glm::vec4 transformed = rect * view * ortho;

std::cout << "x: " << transformed.x << " y: " << transformed.y << " w: " << transformed.w << " t: " << transformed.t << " z : " << transformed.z << "\n";

float x = transformed.x;
float width = transformed.w;

float y = transformed.y;
float height = transformed.z;

vertices[0][0] = x; // top left X
vertices[0][1] = y; //top left Y

vertices[1][0] = x; // bottom left X
vertices[1][1] = height; // bottom left Y

vertices[2][0] = width; // bottom right X
vertices[2][1] = height; //bottom right Y

vertices[3][0] = width; // top right X
vertices[3][1] = y; // top right Y

Alfonse Reinheart
01-11-2013, 10:50 PM
gl_Position = pos * MVP

That pretty much explains everything; your transforms are backwards. The matrix goes on the left; the position goes on the right. And if that actually worked in your shader, it's probably because you transposed the matrix when you uploaded it with glUniformMatrix.

That's why your CPU equivalent is failing; there's no transposition correction being applied, so your math is backwards.