Struggling to work out translation + rotate + translate with VBO and GLSL

Started by markmandel, February 17, 2013, 23:07:41

Previous topic - Next topic

markmandel

I've been slowly learning some OpenGL with LWJGL over the past few weeks, and I wanted to do something simple, that wasn't an example out of a book.

So I'm trying to draw a 4 sides pyramid that rotates left and right around it's central point when you push the left and right keys.

I (believe) I have the pyramid drawing, but I am struggling to get it to rotate around it's central point, rather than around the origin Y axis.

You can see the full code here:
https://github.com/markmandel/jruby-lwjgl/blob/feature/interactive_pyramid/lib/opengl/interactive_pyramid.rb

(Yep, it's all in JRuby, but it should still be easy enough to understand).

GLSL Shaders:
Vert: https://github.com/markmandel/jruby-lwjgl/blob/feature/interactive_pyramid/lib/glsl/perspective_matrix_vertex_basic.glsl
Frag: https://github.com/markmandel/jruby-lwjgl/blob/feature/interactive_pyramid/lib/glsl/colour_passthrough.glsl

As you can see from the attachments, the triangle renders, but when I hit my left key, it starts to rotate around the origin Y axis, and not the central point of the pyramid.

Pretty sure I'm missing something fundamental here, but I had thought I could combine matrices like this, to translate this pyramid on the z axis back to the origin, rotate it, and then move it back out to where it was:
(This was some proof of concept code, so please ignore all the extra Matrix objects I'm creating).

# calculate the y rotation
	def calc_y_rotation
		#translate in on the z axis
		translation_to_origin = Matrix4f.new
		translation_to_origin.m32 = -(@vertex_data[14])

		#the rotate it on the Y axis.
		cos = Math.cos @y_rotation
		sin = Math.sin @y_rotation
		@y_rotation_matrix.m00 = cos
		@y_rotation_matrix.m02 = sin
		@y_rotation_matrix.m20 = -sin
		@y_rotation_matrix.m22 = cos

		#then translate it back out on the z axis.
		temp = Matrix4f.new
		Matrix4f.mul(translation_to_origin, @y_rotation_matrix, temp)

		translation_to_origin.m32 = (@vertex_data[14])

		result = Matrix4f.new
		Matrix4f.mul(translation_to_origin, temp, result)

		result.store(@y_rotation_buffer)
		@y_rotation_buffer.flip
	end


The @y_rotation_buffer gets passed into the 'modelToCameraMatrix' uniform.
https://github.com/markmandel/jruby-lwjgl/blob/feature/interactive_pyramid/lib/opengl/interactive_pyramid.rb#L135

However, I can clearly see that the result matrix ends up being exactly the same as the original y_rotation_matrix... so I'm missing something somewhere.

Can someone please point me in the right direction? I'm totally at a loss.

Thanks in advance!

markmandel

I worked it out, my ordering was wrong on the matrix multiplication.

Changed it to:
# calculate the y rotation
	def calc_y_rotation
		#translate in on the z axis
		translation_to_origin = Matrix4f.new
		translation_to_origin.m32 = -(@vertex_data[14])

		#the rotate it on the Y axis.
		cos = Math.cos @y_rotation
		sin = Math.sin @y_rotation
		@y_rotation_matrix.m00 = cos
		@y_rotation_matrix.m02 = sin
		@y_rotation_matrix.m20 = -sin
		@y_rotation_matrix.m22 = cos

		#then translate it back out on the z axis.
		temp = Matrix4f.new
		Matrix4f.mul(@y_rotation_matrix, translation_to_origin, temp)  #<< switched order

		translation_to_origin.m32 = (@vertex_data[14])

		result = Matrix4f.new
		@y_rotation_matrix.set_identity
		Matrix4f.mul(translation_to_origin, temp, result) #<< switched order

		result.store(@y_rotation_buffer)
		@y_rotation_buffer.flip
	end