Translation Matrix skews Model

Started by Troncoso, September 06, 2013, 19:06:13

Previous topic - Next topic

Troncoso

Hello! I've created a cube that sits in space, and a camera to navigate around it:



That works all well and good. My issue comes from trying to translate it. This is the result, of translating by a vector (0, -4, 0):



This is a view from the bottom of the cube looking up. I can scale it and rotate it just fine using the exact same methods I'm using to translate (except using scalar and rotation matrices), so I don't think it's the way I'm applying the transformation. At the same time, my translation matrix is correct as well. Here's how I build the asset:

Building the asset...
public void buildObjects()
	{
		asset = new GLModelAsset();
		
		asset.shaders = new GLProgram(SH_PATH + "vert_shader.vp", SH_PATH + "fragment_shader.fp");
		asset.texture = GLTextureLoader.get().loadTexture("hazard", IMG_PATH + "wooden-crate.jpg");
		
		// Create the VAO
		asset.vao = glGenVertexArrays();
		glBindVertexArray(asset.vao);

		// Create VBO
		asset.vbo = glGenBuffers();
		glBindBuffer(GL_ARRAY_BUFFER, asset.vbo);

		// Build cube array
		float[] verts = {
			     //  X     Y     Z       U     V
			    // bottom
			    -1.0f,-1.0f,-1.0f,   0.0f, 0.0f,
			     1.0f,-1.0f,-1.0f,   1.0f, 0.0f,
			    -1.0f,-1.0f, 1.0f,   0.0f, 1.0f,
			     1.0f,-1.0f,-1.0f,   1.0f, 0.0f,
			     1.0f,-1.0f, 1.0f,   1.0f, 1.0f,
			    -1.0f,-1.0f, 1.0f,   0.0f, 1.0f,

			    // top
			    -1.0f, 1.0f,-1.0f,   0.0f, 0.0f,
			    -1.0f, 1.0f, 1.0f,   0.0f, 1.0f,
			     1.0f, 1.0f,-1.0f,   1.0f, 0.0f,
			     1.0f, 1.0f,-1.0f,   1.0f, 0.0f,
			    -1.0f, 1.0f, 1.0f,   0.0f, 1.0f,
			     1.0f, 1.0f, 1.0f,   1.0f, 1.0f,

			    // front
			    -1.0f,-1.0f, 1.0f,   1.0f, 0.0f,
			     1.0f,-1.0f, 1.0f,   0.0f, 0.0f,
			    -1.0f, 1.0f, 1.0f,   1.0f, 1.0f,
			     1.0f,-1.0f, 1.0f,   0.0f, 0.0f,
			     1.0f, 1.0f, 1.0f,   0.0f, 1.0f,
			    -1.0f, 1.0f, 1.0f,   1.0f, 1.0f,

			    // back
			    -1.0f,-1.0f,-1.0f,   0.0f, 0.0f,
			    -1.0f, 1.0f,-1.0f,   0.0f, 1.0f,
			     1.0f,-1.0f,-1.0f,   1.0f, 0.0f,
			     1.0f,-1.0f,-1.0f,   1.0f, 0.0f,
			    -1.0f, 1.0f,-1.0f,   0.0f, 1.0f,
			     1.0f, 1.0f,-1.0f,   1.0f, 1.0f,

			    // left
			    -1.0f,-1.0f, 1.0f,   0.0f, 1.0f,
			    -1.0f, 1.0f,-1.0f,   1.0f, 0.0f,
			    -1.0f,-1.0f,-1.0f,   0.0f, 0.0f,
			    -1.0f,-1.0f, 1.0f,   0.0f, 1.0f,
			    -1.0f, 1.0f, 1.0f,   1.0f, 1.0f,
			    -1.0f, 1.0f,-1.0f,   1.0f, 0.0f,

			    // right
			     1.0f,-1.0f, 1.0f,   1.0f, 1.0f,
			     1.0f,-1.0f,-1.0f,   1.0f, 0.0f,
			     1.0f, 1.0f,-1.0f,   0.0f, 0.0f,
			     1.0f,-1.0f, 1.0f,   1.0f, 1.0f,
			     1.0f, 1.0f,-1.0f,   0.0f, 0.0f,
			     1.0f, 1.0f, 1.0f,   0.0f, 1.0f
					    };

		FloatBuffer vertsbuffer = BufferUtils.createFloatBuffer(verts.length);
		vertsbuffer.put(verts).flip();

		// Pass the array to the vbo
		glBufferData(GL_ARRAY_BUFFER, vertsbuffer, GL_STATIC_DRAW);

		int floatsize = 4;
		
		// Enable the 'vert' variable in the vertex shader
		glEnableVertexAttribArray(asset.shaders.getAttrib("vert"));
		glVertexAttribPointer(asset.shaders.getAttrib("vert"), 3, GL_FLOAT, false, 5 * floatsize, 0);
		
		
		glEnableVertexAttribArray(asset.shaders.getAttrib("vertTexCoord"));
		glVertexAttribPointer(asset.shaders.getAttrib("vertTexCoord"), 2, GL_FLOAT, true, 5 * floatsize, 3 * floatsize);

		glBindVertexArray(0);
	}


Creating an instance of it...
GLModelInstance i = new GLModelInstance();
i.asset = asset;
i.transform = Matrix4f.translationMatrix(0, -4f, 0);


Drawing it... (its in a for loop because there will be several)
for (GLModelInstance m : objs) {
			GLModelAsset a = m.asset;
			GLProgram program = a.shaders;
			// Get ready to use shader program and vao
			program.use();

			program.setUniformMatrix4("camera", camera.matrix());
			program.setUniformMatrix4("model", m.transform);

			glActiveTexture(GL_TEXTURE0);
			a.texture.bind();
			program.setUniform("tex", 0);

			glBindVertexArray(a.vao);

			// Draw the triangle
			glDrawArrays(GL_TRIANGLES, 0, 36);

			// Release the program and vao
			glBindVertexArray(0);
			a.texture.unbind();
			program.release();
		}


My vertex shader...
# version 330

uniform mat4 camera;
uniform mat4 model;

in vec3 vert;
in vec2 vertTexCoord;

out vec2 fragTexCoord;

void main()
{
	fragTexCoord = vertTexCoord;
	gl_Position = 	camera * model * vec4(vert, 1);
}


And finally, the method that builds a new translation matrix: (The constructor for Matrix4f initializes the matrix to an identity matrix)
public static Matrix4f translationMatrix(float x, float y, float z)
	{
		Matrix4f dest = new Matrix4f();
		
		dest.m03 = x;
		dest.m13 = y;
		dest.m23 = z;
		
		return dest;
	}


I tried messing around with it. Attempting to translate it in different directions only cause it to skew in different directions. Any help would be great.

Troncoso

Nevermind...I guess there really was more to the translation matrix than that.

broumbroum

I think the Mx be a something more accurate like :
Matrix4f dest = new Matrix4f();
		
		dest.m00 = x;
		dest.m11 = y;
		dest.m22 = z;
                dest.m03 = 1;
                dest.m13 = 1;
                dest.m23 = 1;
		
		return dest;

AFAIK you better try glTranslatef() for yourself.