2021-11-30 21:29:18 -06:00
|
|
|
#include "mesh.h"
|
|
|
|
|
2022-02-06 10:14:57 -06:00
|
|
|
#include "render.h"
|
2021-11-30 21:29:18 -06:00
|
|
|
#include "shader.h"
|
|
|
|
#include "texture.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2022-11-19 17:13:57 -06:00
|
|
|
void DrawMesh(struct mesh *mesh, struct shader *shader)
|
2021-11-30 21:29:18 -06:00
|
|
|
{
|
|
|
|
// bind appropriate textures
|
|
|
|
uint32_t diffuseNr = 1;
|
|
|
|
uint32_t specularNr = 1;
|
|
|
|
uint32_t normalNr = 1;
|
|
|
|
uint32_t heightNr = 1;
|
|
|
|
|
2022-06-28 22:13:39 -05:00
|
|
|
for (int i = 0; i < (mesh->te - mesh->textures); i++) {
|
2021-11-30 21:29:18 -06:00
|
|
|
glActiveTexture(GL_TEXTURE0 + i); // active proper texture unit before binding
|
|
|
|
// retrieve texture number (the N in diffuse_textureN)
|
2022-02-06 10:14:57 -06:00
|
|
|
char number = 0;
|
2021-11-30 21:29:18 -06:00
|
|
|
// TODO: malloc every single frame ... nope! Change to stack
|
2022-02-06 10:14:57 -06:00
|
|
|
/*char *name =
|
2021-11-30 21:29:18 -06:00
|
|
|
(char *) malloc(sizeof(char) *
|
2022-02-06 10:14:57 -06:00
|
|
|
(strlen(mesh->textures[i].type) + 2));*/
|
|
|
|
if (mesh->textures[i].type == TEX_DIFF)
|
2021-11-30 21:29:18 -06:00
|
|
|
number = diffuseNr++;
|
2022-02-06 10:14:57 -06:00
|
|
|
else if (mesh->textures[i].type == TEX_SPEC)
|
2021-11-30 21:29:18 -06:00
|
|
|
number = specularNr++;
|
2022-02-06 10:14:57 -06:00
|
|
|
else if (mesh->textures[i].type == TEX_NORM)
|
2021-11-30 21:29:18 -06:00
|
|
|
number = normalNr++;
|
2022-02-06 10:14:57 -06:00
|
|
|
else if (mesh->textures[i].type == TEX_HEIGHT)
|
2021-11-30 21:29:18 -06:00
|
|
|
number = heightNr++;
|
|
|
|
|
2022-02-06 10:14:57 -06:00
|
|
|
|
|
|
|
/*
|
2021-11-30 21:29:18 -06:00
|
|
|
glUniform1i(glGetUniformLocation(shader->id, name), i);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, mesh->textures[i].id);
|
|
|
|
free(name);
|
2022-02-06 10:14:57 -06:00
|
|
|
*/
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// draw mesh
|
|
|
|
glBindVertexArray(mesh->VAO);
|
|
|
|
DrawMeshAgain(mesh);
|
|
|
|
|
|
|
|
// DEBUG
|
|
|
|
// triCount += indices.size() / 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-19 17:13:57 -06:00
|
|
|
void DrawMeshAgain(struct mesh *mesh)
|
2021-11-30 21:29:18 -06:00
|
|
|
{
|
|
|
|
glDrawElements(GL_TRIANGLES, (mesh->ie - mesh->indices),
|
|
|
|
GL_UNSIGNED_INT, 0);
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:13:57 -06:00
|
|
|
struct mesh *MakeMesh(struct Vertex *vertices, struct Vertex *ve,
|
2021-11-30 21:29:18 -06:00
|
|
|
uint32_t * indices, uint32_t * ie,
|
|
|
|
struct Texture *textures, struct Texture *te)
|
|
|
|
{
|
2022-11-19 17:13:57 -06:00
|
|
|
struct mesh *newmesh = (struct mesh *) malloc(sizeof(struct mesh));
|
2021-11-30 21:29:18 -06:00
|
|
|
newmesh->vertices = vertices;
|
|
|
|
newmesh->ve = ve;
|
|
|
|
newmesh->indices = indices;
|
|
|
|
newmesh->ie = ie;
|
|
|
|
newmesh->textures = textures;
|
|
|
|
newmesh->te = te;
|
|
|
|
|
|
|
|
setupmesh(newmesh);
|
|
|
|
return newmesh;
|
|
|
|
}
|
|
|
|
|
2022-11-19 17:13:57 -06:00
|
|
|
void setupmesh(struct mesh *mesh)
|
2021-11-30 21:29:18 -06:00
|
|
|
{
|
|
|
|
// create buffers/arrays
|
|
|
|
glGenVertexArrays(1, &mesh->VAO);
|
|
|
|
glGenBuffers(1, &mesh->VBO);
|
|
|
|
glGenBuffers(1, &mesh->EBO);
|
|
|
|
|
|
|
|
glBindVertexArray(mesh->VAO);
|
|
|
|
// load data into vertex buffers
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, mesh->VBO);
|
|
|
|
|
|
|
|
|
|
|
|
// The effect is that we can simply pass a pointer to the struct and it translates perfectly to vevc array which
|
|
|
|
// again translates to 3/2 floats which translates to a byte array.
|
|
|
|
glBufferData(GL_ARRAY_BUFFER,
|
|
|
|
(mesh->ve - mesh->vertices) * sizeof(struct Vertex),
|
|
|
|
&mesh->vertices[0], GL_STATIC_DRAW);
|
|
|
|
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh->EBO);
|
|
|
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER,
|
|
|
|
(mesh->ie - mesh->indices) * sizeof(uint32_t),
|
|
|
|
&mesh->indices[0], GL_STATIC_DRAW);
|
|
|
|
|
|
|
|
// set the vertex attribute pointers
|
|
|
|
// vertex Positions
|
|
|
|
glEnableVertexAttribArray(0);
|
|
|
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(struct Vertex),
|
|
|
|
(void *) 0);
|
|
|
|
// vertex normals
|
|
|
|
glEnableVertexAttribArray(1);
|
|
|
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(struct Vertex),
|
|
|
|
(void *) offsetof(struct Vertex, Normal[3]));
|
|
|
|
// vertex texture coords
|
|
|
|
glEnableVertexAttribArray(2);
|
|
|
|
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(struct Vertex),
|
|
|
|
(void *) offsetof(struct Vertex, TexCoords[2]));
|
|
|
|
// vertex tangent
|
|
|
|
glEnableVertexAttribArray(3);
|
|
|
|
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(struct Vertex),
|
|
|
|
(void *) offsetof(struct Vertex, Tangent[3]));
|
|
|
|
// vertex bitangent
|
|
|
|
glEnableVertexAttribArray(4);
|
|
|
|
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, sizeof(struct Vertex),
|
|
|
|
(void *) offsetof(struct Vertex, Bitangent[3]));
|
|
|
|
|
|
|
|
// Bone ids
|
|
|
|
glEnableVertexAttribArray(5);
|
|
|
|
glVertexAttribPointer(5, 4, GL_INT, GL_FALSE, sizeof(struct Vertex),
|
|
|
|
(void *) offsetof(struct Vertex,
|
|
|
|
m_BoneIDs
|
|
|
|
[MAX_BONE_INFLUENCE]));
|
|
|
|
|
|
|
|
// Weights
|
|
|
|
glEnableVertexAttribArray(6);
|
|
|
|
glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, sizeof(struct Vertex),
|
|
|
|
(void *) offsetof(struct Vertex, m_Weights));
|
|
|
|
|
|
|
|
glBindVertexArray(0);
|
|
|
|
}
|