Thursday 19 September 2013

OpenGL multiple matrix transormations

OpenGL multiple matrix transormations

I have simple vertex shader for models
#version 330
layout(location = 0) in vec3 VertexPosition;
layout(location = 1) in vec3 VertexNormal;
layout(location = 2) in vec2 VertexUV;
out VS_GS_VERTEX
{
vec3 vs_worldpos;
vec3 vs_normal;
vec2 VertexUV;
} vertex_out;
uniform mat4 modelMatrix;
uniform mat4 projectionMatrix;
uniform mat4 lookAtMatrix;
void main(void)
{
mat4 MVP = projectionMatrix * lookAtMatrix * modelMatrix;
gl_Position = MVP * vec4(VertexPosition, 1.0);
gl_Normal = mat3(modelMatrix) * VertexNormal;
vertex_out.vs_worldpos = gl_Position.xyz;
vertex_out.vs_normal = gl_Normal;
vertex_out.VertexUV = VertexUV;
}
and I pass there modelMatrix
modelMat := MatrixMultiply(transMat, baseMat);
modelMat := MatrixMultiply(modelMat, scaleMat);
modelMat := MatrixMultiply(modelMat, rotMat);
glUniformMatrix4fv(modelMatrix_loc, 1, false, @modelMat);
where transMat for positioning, scaleMat to enlarge whole picture and
rotMat to rotate whole terrain. Everything fine while baseMat is
coordinates of model on terrain
baseMat := CreateTranslationMatrix(AffineVectorMake(pos.x, pos.y, pos.z));
but when I trying also to enlarge model by own model's scale (model have
zero based coordinates, for example roots of tree at (0, 0, 0))
scMat := CreateScaleMatrix(AffineVectorMake(scale, scale, scale));
trMat := CreateTranslationMatrix(AffineVectorMake(pos.x, pos.y, pos.z));
baseMat := MatrixMultiply(scMat, trMat);

models not just enlarged, they also changing coordinates and not placed on
terrain anymore. Is it possible to make one model matrix with model's
scaling, rotation, translation, yet another translation, scaling and
rotation? Or I should do something else? Matrix math from GLScene and
seems works fine.

No comments:

Post a Comment