Model Matrix seems to not be working

#1

I created a Model class for my program I’m making,

class Model
{
protected:
	Shader shader;
	Mesh mesh;
	std::vector<unsigned int> textures;
	glm::vec3 position, rotation, scale;

public:
	Model(Mesh mesh, Shader shader, std::vector<unsigned int> textures, glm::vec3 position, glm::vec3 rotation, glm::vec3 scale)
	{
		this->mesh = mesh;
		this->shader = shader;
		this->textures = textures;
		this->position = position;
		this->rotation = rotation;
		this->scale = scale;

		if (textures[0])
		{
			shader.use();
			for (int i = 0; i < textures.size(); i++)
				shader.setInt("texture" + (i + 1), i);
		}

	}

	//This method requires explicit parameters with the camera and projection matrix, Soon there will be a "global" projection matrix and a Camera.inUse it will use, and render() will be all you need.
	//Will probably make more render methods. (Like HUD)
	void render(Camera camera, glm::mat4 projection)
	{
		glm::mat4 view;
		glm::mat4 proj;
		glm::mat4 model = glm::mat4(1.0f);

		view = camera.GetViewMatrix();
		proj = projection;

		glm::translate(model, position);

		unsigned int modelLoc = glGetUniformLocation(shader.ID, "model");
		unsigned int viewLoc = glGetUniformLocation(shader.ID, "view");
		unsigned int projLoc = glGetUniformLocation(shader.ID, "projection");

		glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
		glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
		glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(proj));

		if (textures[0])
		{
			for (int i = 0; i < textures.size(); i++)
			{
				glActiveTexture(GL_TEXTURE0 + i);
				glBindTexture(GL_TEXTURE_2D, textures[i]);
			}
		}



		shader.use();
		mesh.render();
	}

};

(Note I’m just affecting the position right now until I can get this working)

In the main.cpp, I declare a Model like so

Model model(mesh, helloTriangle, std::vector<unsigned int>{texture1,texture2}, glm::vec3(2.0f,5.0f,-15.0f), glm::vec3(0.0f), glm::vec3(0.25f, 1.0f, 1.0f));

In the main loop

proj = glm::perspective(glm::radians(45.0f), float(Window::WIDTH) / float(Window::HEIGHT), 0.1f, 100.0f);
model.render(camera, proj);

Now it works, for the most part, except for the model matrix. I can walk around the cube I made, so the view matrix works, and I can walk back and it gets smaller, so the projection matrix works. The cube is textured properly, so the shaders and textures work, but the cube stays at the origin. Note if I do this by hand in the main class the model matrix works fine. I tried many things and left astray. Would appreciate any help.

Also here’s my vertex shader:

#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
layout (location = 2) in vec2 aTexCoord;

out vec4 ourColor;
out vec2 TexCoord;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void main()
{
	gl_Position = projection * view * model * vec4(aPos, 1.0);
	ourColor = vec4(aColor, 1.0f);
	TexCoord = aTexCoord;
}

(If any more code is needed I’m happy to oblige)

#2

The glUniform* functions set a uniform variable for the current program, so you need to call glUseProgram before setting the uniforms.

#3

Or you could just use glProgramUniformMatrix and pass the program object directly.

#4

Sorry for the delayed reply. Hadn’t had a chance to work on my code. So I tried what GClements suggested, and still the same problem. Decided to see what my code was actually passing into directly, so after glm::translate, I print the contents of the matrix like so

std::cout << model[0][0] << " " << model[1][0] << " " << model[2][0] << " " << model[3][0] << std::endl;
std::cout << model[0][1] << " " << model[1][1] << " " << model[2][1] << " " << model[3][1] << std::endl;
std::cout << model[0][2] << " " << model[1][2] << " " << model[2][2] << " " << model[3][2] << std::endl;
std::cout << model[0][3] << " " << model[1][3] << " " << model[2][3] << " " << model[3][3] << std::endl << std::endl;

And what I get is the 4x4 identity matrix.

1000
0100
0010
0001

But then I noticed how I was translating the object.
glm::translate(model, position); which is wrong, it should be.
model = glm::translate(model, position);

When I fixed it the cube appeared exactly where it should be, so my mistake wasn’t opengl related, just a silly C++ mistake. Nonetheless I appreciate the suggestions.