So I’ve been racking my brain over this for a couple hours and I just can’t seem to figure it out.
Essentially, I just want an object (an image) to move diagonally while it rotates around it’s own axis (in the center of the image). How would the matrix transformation for this look like in modern openGL? (without using glPushMatrix, glTranslatef, etc) What would the vertex shader have to look like and what kind of values would I have to supply to it?
Since I’m probably not explaining myself too well, let me demonstrate with images (i hope imgur is okay, embedded doesn’t allow for .gif):
https://imgur.com/a/4YFx6Na
my vector shader has this math in it:
gl_Position = transform * vec4(aPos.x+offsetX, aPos.y+offsetY, aPos.z, 1.0);
my main function has this relevant code in it:
float xOffset = 0.0f; //starting X position of the image
float yOffset = 0.3f; //same but for Y
float velX = (1.0f / 800.0f) * 2.5f; // velocity vector normalized to 0,1 from 0, window_width = 800
float velY = (1.0f / 600.0f) * 2.5f; // same here but with window_height = 600
float rots = 0.1f; // rotation speed abbreviated as rots
glm::mat4 rot(0.f);
while(running) {
xOffset += velX;
yOffset += velY;
shader.setFloat("offsetX", xOffset);
shader.setFloat("offsetY", yOffset);
// i made a generic shader class that loads my shaders from a file
// it has a set<type>() function where you supply the name of a
// uniform and a value and it changes that uniform to the value
// think of this as glUniform1f(programID, value)
rot = glm::rotate(rot, glm::radians(rots), glm::vec3(0.0, 0.0, 1.0));
// this rotates the matrix (rot) by rotation speed
unsigned int transformLoc = glGetUniformLocation(shader.ID, "transform");
glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(rot));
// and then modifies the "transform" uniform of the vector shader
// after that is done, render the image
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
shader.use();
// my generic shader class has a .use method which just uses the loaded and linked shader program
// which is created in the Shader() constructor
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
glfwSwapBuffers(window);
glfwPollEvents();
// some math for "bouncing" the image off the walls
if (xOffset + velX > 0.8 || xOffset + velX < -0.8) {
velX *= -1;
state *= -1;
}
if (yOffset + velY > 0.8 || yOffset + velY < -0.8) {
velY *= -1;
state *= -1;
}
}
I tried using glm::translate() for the translation part of the math, but then I changed it to use the offsetX,Y values as uniforms in the vector shader which i just modify before each rendering. Somehow, I thought this would change something but it didn’t.