prosperon/source/engine/3d/mesh.c

126 lines
3.7 KiB
C
Raw Normal View History

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++) {
2023-05-04 17:07:00 -05:00
// glActiveTexture(GL_TEXTURE0 + i); // active proper texture unit before binding
2021-11-30 21:29:18 -06:00
// 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
2023-01-02 07:55:26 -06:00
/*char *name = malloc(sizeof(char) *(strlen(mesh->textures[i].type) + 2));*/
/*
2022-02-06 10:14:57 -06:00
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++;
2023-01-02 07:55:26 -06:00
*/
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
{
}
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
{
2023-05-04 17:07:00 -05:00
/*
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);
2023-01-02 07:55:26 -06:00
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(struct Vertex), NULL);
2021-11-30 21:29:18 -06:00
// vertex normals
glEnableVertexAttribArray(1);
2023-04-19 15:16:35 -05:00
// glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(struct Vertex), offsetof(struct Vertex, Normal[3]));
2021-11-30 21:29:18 -06:00
// vertex texture coords
glEnableVertexAttribArray(2);
2023-04-19 15:16:35 -05:00
// glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(struct Vertex), offsetof(struct Vertex, TexCoords[2]));
2021-11-30 21:29:18 -06:00
// vertex tangent
glEnableVertexAttribArray(3);
2023-04-19 15:16:35 -05:00
// glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(struct Vertex), offsetof(struct Vertex, Tangent[3]));
2021-11-30 21:29:18 -06:00
// vertex bitangent
glEnableVertexAttribArray(4);
2023-04-19 15:16:35 -05:00
// glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, sizeof(struct Vertex), offsetof(struct Vertex, Bitangent[3]));
2021-11-30 21:29:18 -06:00
// Bone ids
glEnableVertexAttribArray(5);
2023-05-04 17:07:00 -05:00
glVertexAttribPointer(5, 4, GL_INT, GL_FALSE, sizeof(struct Vertex), offsetof(struct Vertex,
2021-11-30 21:29:18 -06:00
m_BoneIDs
[MAX_BONE_INFLUENCE]));
2023-05-04 17:07:00 -05:00
2021-11-30 21:29:18 -06:00
// Weights
glEnableVertexAttribArray(6);
2023-04-19 15:16:35 -05:00
// glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, sizeof(struct Vertex), offsetof(struct Vertex, m_Weights));
2021-11-30 21:29:18 -06:00
glBindVertexArray(0);
2023-05-04 17:07:00 -05:00
*/
2021-11-30 21:29:18 -06:00
}