I have a small open GL application I’m writing in Java using JOGL.
I’m bashing my head against the wall trying to find the CURRENT way of applying transformations
Essentially what I’m trying to accomplish is to have two objects next to each other. Basically I create objects (methods that calculate all the triangles – i know this is working because both objects show up, they’re just overlapping each other), then in display I put them in VBO and call drawElements. My issue is that I can’t see to find any examples or tutorials online that DON’T use glTranslatef, glRotatef, and glScalef, which I’ve been informed is outdated and used to be part of the fixed function aspect of old open GL.
EDIT: (previous code removed)
Epic fail on my behalf. When he said his shader code, i thought he meant the .java file that loads and compiles the shader. It just hit me that he meant the SHADER CODE, which was in a glsl file. I see now where he has 3 mat4 variables which hold rx, ry, and rz. Weird thing is that even if I SET UP a scale and translate matrix, my shapes no longer appear. The rotation matrices are set up as one would assume, and the last line is “glPosition = rz * ry * rx * vPosition;” I tried declaring translation and scale matrices as well and then doing “= s * t * rz * ry * rx * vPosition”, but no dice.
Here is the current shader code:
attribute vec4 vPosition;
uniform vec3 theta;
uniform vec3 trans;
uniform vec3 scale;
void main()
{
// Compute the sines and cosines of each rotation
// about each axis
vec3 angles = radians (theta);
vec3 c = cos (angles);
vec3 s = sin (angles);
// rotation matricies
mat4 rx = mat4 (1.0, 0.0, 0.0, 0.0,
0.0, c.x, s.x, 0.0,
0.0, -s.x, c.x, 0.0,
0.0, 0.0, 0.0, 1.0);
mat4 ry = mat4 (c.y, 0.0, -s.y, 0.0,
0.0, 1.0, 0.0, 0.0,
s.y, 0.0, c.y, 0.0,
0.0, 0.0, 0.0, 1.0);
mat4 rz = mat4 (c.z, -s.z, 0.0, 0.0,
s.z, c.z, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0);
//mat4 t = mat4 (1.0, 0.0, 0.0, trans.x,
// 0.0, 1.0, 0.0, trans.y,
// 0.0, 0.0, 1.0, trans.z,
// 0.0, 0.0, 0.0, 1.0);
//mat4 s = mat4 (scale.x, 0.0, 0.0, 0.0,
// 0.0, scale.y, 0.0, 0.0,
// 0.0, 0.0, scale.z, 0.0,
// 0.0, 0.0, 0.0, 1.0);
gl_Position = rz * ry * rx * vPosition;
//gl_Position = s * t * rz * ry * rx * vPosition;
}
So essentially, if I uncomment the bottom two matrices, but leave the glPosition statement the same, nothing appears. Also the commented out glPosition statement didn’t work (though I’m not surprised, I’m not too great at matrix multiplication). I also tried doing just s * vPosition just to try and only scale, but again nothing is visible.
Between datenwolf’s link and a few other resources I found, I was able to write a (rather barebones) column-major order matrix class for use in my assignment. I’m posting the source here in case any one else finds it useful.
import java.nio.FloatBuffer;
So in my case I set up individual matrices for translation, scale, and rotations.
so if, for example, I wanted to translate along z, then rotate along y, then scale, I could do:
Where the transformations happen left to right (i.e. first it’s translated, then rotated, then scaled)
Anyway, I hope that can be helpful to anyone else!
Thanks for all the responses.