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)