it will look like this
shader.vs
#version 330 core
layout(std140, binding = 0) uniform MeshState {
int RenderFlag;
float Alpha;
float BlendMeshTexCoordU;
float BlendMeshTexCoordV;
float BlendMeshLight;
};
uniform mat4 uModelView;
uniform mat4 uProjection;
layout(location = 0) in vec3 aPosition;
layout(location = 1) in vec2 aTexCoord;
layout(location = 2) in vec3 aNormal;
out vec2 TexCoord;
out vec3 LightEffect;
void main() {
gl_Position = uProjection * uModelView * vec4(aPosition, 1.0);
TexCoord = aTexCoord;
if (RenderFlag == 1) { // RENDER_TEXTURE
TexCoord.x += BlendMeshTexCoordU;
TexCoord.y += BlendMeshTexCoordV;
}
LightEffect = vec3(BlendMeshLight);
}
struct MeshRenderState {
int RenderFlag;
float Alpha;
float BlendMeshTexCoordU;
float BlendMeshTexCoordV;
float BlendMeshLight;
};
// make UBO with VAO
GLuint uboMeshState;
glGenBuffers(1, &uboMeshState);
glBindBuffer(GL_UNIFORM_BUFFER, uboMeshState);
glBufferData(GL_UNIFORM_BUFFER, sizeof(MeshRenderState), NULL, GL_DYNAMIC_DRAW);
glBindBufferBase(GL_UNIFORM_BUFFER, 0, uboMeshState);
void UpdateMeshUBO(int RenderFlag, float Alpha, float BlendMeshTexCoordU, float BlendMeshTexCoordV, float BlendMeshLight) {
MeshRenderState state = { RenderFlag, Alpha, BlendMeshTexCoordU, BlendMeshTexCoordV, BlendMeshLight };
glBindBuffer(GL_UNIFORM_BUFFER, uboMeshState);
glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(MeshRenderState), &state);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
}
void BMD::RenderMesh(int i, int RenderFlag, float Alpha, float BlendMeshTexCoordU, float BlendMeshTexCoordV, float BlendMeshLight, int MeshTexture)
{
if (i >= 0 && i < NumMeshs)
{
Mesh_t* m = &Meshs;
if (m->NumTriangles == 0)
return;
int Texture = IndexTexture[m->Texture];
if (MeshTexture != -1)
Texture = MeshTexture;
if (Texture == -1)
return;
// Update UBO before drawing
UpdateMeshUBO(RenderFlag, Alpha, BlendMeshTexCoordU, BlendMeshTexCoordV, BlendMeshLight);
glUseProgram(shaderProgram);
GLfloat modelView[16];
glGetFloatv(GL_MODELVIEW_MATRIX, modelView);
glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "uModelView"), 1, GL_FALSE, modelView);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, Texture);
glUniform1i(glGetUniformLocation(shaderProgram, "uTexture"), 0);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, m->NumTriangles * 3, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
glUseProgram(0);
}
}