Tazdij 9 лет назад
Родитель
Сommit
2859cb4665
3 измененных файлов с 111 добавлено и 142 удалено
  1. 50 124
      src/game/main.c
  2. 54 11
      src/odingine/mesh.c
  3. 7 7
      src/odingine/mesh.h

+ 50 - 124
src/game/main.c

@@ -24,6 +24,8 @@
 #include "shaders.h"
 #include "textures.h"
 
+#include "mesh.h"
+
 #include "viking_math.h"
 
 const int SCREEN_WIDTH = 640;
@@ -40,47 +42,38 @@ void close();
 static ODIN_Window* window = 0;
 
 
+static const GLuint indices[36] = {
+    0, 1, 2,
+    3, 0, 4,
+    5, 0, 6,
+    3, 6, 0,
+    0, 2, 4,
+    5, 1, 0,
+    2, 1, 5,
+    7, 6, 3,
+    6, 7, 5,
+    7, 3, 4,
+    7, 4, 2,
+    7, 2, 5
+};
+
 // Test OpenGL rendering
-static const GLfloat vertices[108] = {
-        -1.0f,-1.0f,-1.0f, // triangle 1 : begin
-        -1.0f,-1.0f, 1.0f,
-        -1.0f, 1.0f, 1.0f, // triangle 1 : end
-        1.0f, 1.0f,-1.0f, // triangle 2 : begin
-        -1.0f,-1.0f,-1.0f,
-        -1.0f, 1.0f,-1.0f, // triangle 2 : end
-        1.0f,-1.0f, 1.0f,
-        -1.0f,-1.0f,-1.0f,
-        1.0f,-1.0f,-1.0f,
-        1.0f, 1.0f,-1.0f,
-        1.0f,-1.0f,-1.0f,
-        -1.0f,-1.0f,-1.0f,
-        -1.0f,-1.0f,-1.0f,
-        -1.0f, 1.0f, 1.0f,
-        -1.0f, 1.0f,-1.0f,
-        1.0f,-1.0f, 1.0f,
-        -1.0f,-1.0f, 1.0f,
-        -1.0f,-1.0f,-1.0f,
-        -1.0f, 1.0f, 1.0f,
-        -1.0f,-1.0f, 1.0f,
-        1.0f,-1.0f, 1.0f,
-        1.0f, 1.0f, 1.0f,
-        1.0f,-1.0f,-1.0f,
-        1.0f, 1.0f,-1.0f,
-        1.0f,-1.0f,-1.0f,
-        1.0f, 1.0f, 1.0f,
-        1.0f,-1.0f, 1.0f,
-        1.0f, 1.0f, 1.0f,
-        1.0f, 1.0f,-1.0f,
-        -1.0f, 1.0f,-1.0f,
-        1.0f, 1.0f, 1.0f,
-        -1.0f, 1.0f,-1.0f,
-        -1.0f, 1.0f, 1.0f,
-        1.0f, 1.0f, 1.0f,
-        -1.0f, 1.0f, 1.0f,
-        1.0f,-1.0f, 1.0f
+static const GLfloat vertices[24] = {
+    
+    
+        -1.0f,-1.0f,-1.0f, // 0
+        -1.0f,-1.0f, 1.0f, // 1
+        -1.0f, 1.0f, 1.0f, // 2
+    
+         1.0f, 1.0f,-1.0f, // 3
+        -1.0f, 1.0f,-1.0f, // 4
+    
+         1.0f,-1.0f, 1.0f, // 5
+         1.0f,-1.0f,-1.0f, // 6
+         1.0f, 1.0f, 1.0f  // 7
 };
 
-static const GLfloat colors[108] = {
+static const GLfloat colors[24] = {
     0.583f,  0.771f,  0.014f,
     0.609f,  0.115f,  0.436f,
     0.327f,  0.483f,  0.844f,
@@ -88,38 +81,10 @@ static const GLfloat colors[108] = {
     0.435f,  0.602f,  0.223f,
     0.310f,  0.747f,  0.185f,
     0.597f,  0.770f,  0.761f,
-    0.559f,  0.436f,  0.730f,
-    0.359f,  0.583f,  0.152f,
-    0.483f,  0.596f,  0.789f,
-    0.559f,  0.861f,  0.639f,
-    0.195f,  0.548f,  0.859f,
-    0.014f,  0.184f,  0.576f,
-    0.771f,  0.328f,  0.970f,
-    0.406f,  0.615f,  0.116f,
-    0.676f,  0.977f,  0.133f,
-    0.971f,  0.572f,  0.833f,
-    0.140f,  0.616f,  0.489f,
-    0.997f,  0.513f,  0.064f,
-    0.945f,  0.719f,  0.592f,
-    0.543f,  0.021f,  0.978f,
-    0.279f,  0.317f,  0.505f,
-    0.167f,  0.620f,  0.077f,
-    0.347f,  0.857f,  0.137f,
-    0.055f,  0.953f,  0.042f,
-    0.714f,  0.505f,  0.345f,
-    0.783f,  0.290f,  0.734f,
-    0.722f,  0.645f,  0.174f,
-    0.302f,  0.455f,  0.848f,
-    0.225f,  0.587f,  0.040f,
-    0.517f,  0.713f,  0.338f,
-    0.053f,  0.959f,  0.120f,
-    0.393f,  0.621f,  0.362f,
-    0.673f,  0.211f,  0.457f,
-    0.820f,  0.883f,  0.371f,
-    0.982f,  0.099f,  0.879f
+    0.559f,  0.436f,  0.730f
 };
 
-static const GLfloat uvs[72] = {
+static const GLfloat uvs[16] = {
     0.0f, 0.0f,
     0.0f, -1.0f,
     1.0f, 0.0f,
@@ -130,47 +95,8 @@ static const GLfloat uvs[72] = {
     
 	0.667979f, 1.0f-0.335851f,
     0.336024f, 1.0f-0.671877f,
-    0.667969f, 1.0f-0.671889f,
-    
-	1.000023f, 1.0f-0.000013f,
-    0.668104f, 1.0f-0.000013f,
-    0.667979f, 1.0f-0.335851f,
-    
-	0.000059f, 1.0f-0.000004f,
-    0.335973f, 1.0f-0.335903f,
-    0.336098f, 1.0f-0.000071f,
-    
-	0.667979f, 1.0f-0.335851f,
-    0.335973f, 1.0f-0.335903f,
-    0.336024f, 1.0f-0.671877f,
-    
-	1.000004f, 1.0f-0.671847f,
-    0.999958f, 1.0f-0.336064f,
-    0.667979f, 1.0f-0.335851f,
-    
-	0.668104f, 1.0f-0.000013f,
-    0.335973f, 1.0f-0.335903f,
-    0.667979f, 1.0f-0.335851f,
-    
-	0.335973f, 1.0f-0.335903f,
-    0.668104f, 1.0f-0.000013f,
-    0.336098f, 1.0f-0.000071f,
-    
-	0.000103f, 1.0f-0.336048f,
-    0.000004f, 1.0f-0.671870f,
-    0.336024f, 1.0f-0.671877f,
-    
-	0.000103f, 1.0f-0.336048f,
-    0.336024f, 1.0f-0.671877f,
-    0.335973f, 1.0f-0.335903f,
-    
-	0.667969f, 1.0f-0.671889f,
-    1.000004f, 1.0f-0.671847f,
-    0.667979f, 1.0f-0.335851f
 };
 
-GLuint VaoID = 0;
-GLuint VboID = 0;
 // End OpenGL rendering Test
 
 // Function Declorations
@@ -471,23 +397,14 @@ int main(int argc, char* argv[]) {
 	// TEMP: Render test setup
 	printf("Starting gl triangle setup.\n");
 	
-	glGenVertexArrays(1, &VaoID);
-	glBindVertexArray(VaoID);
-
-	glGenBuffers(1, &VboID);
-	glBindBuffer(GL_ARRAY_BUFFER, VboID);
-	glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 108, vertices, GL_STATIC_DRAW);
-    
-    GLuint colorbuffer;
-    glGenBuffers(1, &colorbuffer);
-    glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
-    glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 108, colors, GL_STATIC_DRAW);
-    
-    GLuint uvbuffer;
-    glGenBuffers(1, &uvbuffer);
-    glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
-    glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 72, uvs, GL_STATIC_DRAW);
+	
+    ODIN_Mesh* mesh =  Mesh_newMesh();
+    Mesh_loadBufferData(mesh, 0, GL_ARRAY_BUFFER, GL_FLOAT, sizeof(GLfloat), 3, &vertices[0], 24);
+    Mesh_loadBufferData(mesh, 1, GL_ARRAY_BUFFER, GL_FLOAT, sizeof(GLfloat), 3, &colors[0], 24);
+    Mesh_loadBufferData(mesh, 2, GL_ARRAY_BUFFER, GL_FLOAT, sizeof(GLfloat), 2, &uvs[0], 16);
     
+    // Load the index buffer now
+    Mesh_loadIBOData(mesh, &indices[0], 36);
     
     
 
@@ -582,9 +499,11 @@ int main(int argc, char* argv[]) {
 		// OpenGL Rendering loop
 		Window_clearColor(100.0/255.0, 149.0/255.0, 237.0/255.0, 1.0);
         
+        
         glUseProgram(shaderTest->program_id);
         glUniformMatrix4fv(MvpID, 1, GL_FALSE, mvp);
 
+        /*
 		// Render studd here?
 		glEnableVertexAttribArray(0);
 		glBindBuffer(GL_ARRAY_BUFFER, VboID);
@@ -616,7 +535,14 @@ int main(int argc, char* argv[]) {
         
 		glDrawArrays(GL_TRIANGLES, 0, 12*3);
 		glDisableVertexAttribArray(0);
-
+        */
+        
+        // Bind mesh vao
+        Mesh_bindVao(mesh);
+        
+        glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, 0);
+        
+        Mesh_unbindVao();
 
 		Window_swapBuffer(window);
 

+ 54 - 11
src/odingine/mesh.c

@@ -12,39 +12,80 @@ void Mesh_unbindVbo(GLenum buffer_type) {
     glBindBuffer(buffer_type, 0);
 }
 
+int Mesh_isIndexed(ODIN_Mesh* mesh) {
+    if (mesh->ibo_id != 0) {
+        return 1;
+    }
+    
+    return 0;
+}
+
 ODIN_Mesh* Mesh_newMesh() {
-	ODIN_Mesh* mesh = (ODIN_Mesh*)malloc(sizeof(ODIN_Mesh));
+	// Create the mesh struct
+    ODIN_Mesh* mesh = (ODIN_Mesh*)malloc(sizeof(ODIN_Mesh));
 
+    // Create the VAO
 	glGenVertexArrays(1, &(mesh->vao_id));
     
-    // initialize the buffers array
+    // Initialize the buffers array for all 16
     mesh->buffers = (ODIN_MeshBuffer*)malloc(sizeof(ODIN_MeshBuffer) * 16);
 	
-
-	
-	//mesh->vao_id = 0;
+    mesh->ibo_count = 0;
+    mesh->ibo_id = 0;
 
 	return mesh;
 }
 
 void Mesh_freeMesh(ODIN_Mesh* mesh) {
+    // TODO: Free all of the buffers in Video RAM
+    // TODO: Free IBO in Video RAM
+    // TODO: Free Buffer array
+    // TODO: Free IBO
+    // TODO: Free VAO in Video Ram
+    
+    // Free Mesh struct
 	free(mesh);
 }
 
-void Mesh_loadBuffer(ODIN_Mesh* mesh, GLuint location, GLenum buffer_type,
-                       GLenum element_type, GLubyte element_size, GLuint element_count,
-                       void* data) {
+// load the index buffer for this mesh
+void Mesh_loadIBOData(ODIN_Mesh* mesh, GLuint* data, GLsizei count) {
+    // Bind the VAO
+    Mesh_bindVao(mesh);
+    
+    mesh->ibo_count = count;
+    glGenBuffers(1, &(mesh->ibo_id));
+    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh->ibo_id);
+    glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * sizeof(GLuint), data, GL_STATIC_DRAW);
+    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
+    
+    Mesh_unbindVao();
+}
+
+void Mesh_loadBufferData(ODIN_Mesh* mesh, GLuint location, GLenum buffer_type,
+                       GLenum element_type, GLsizei element_size, GLuint component_count,
+                       void* data, GLsizei data_count) {
     
     Mesh_bindVao(mesh);
     
-    ODIN_MeshBuffer* buffer = mesh->buffers;
+    // Get the pointer to the element in the buffer array
+    ODIN_MeshBuffer* buffer = mesh->buffers + location;
+    
+    buffer->element_type = element_type;
+    buffer->element_size = element_size;
+    buffer->buffer_type = buffer_type;
+    buffer->component_count = component_count;
+    buffer->buffer_location = location;
     
     glGenBuffers(1, &buffer->vbo_id);
     glBindBuffer(buffer_type, buffer->vbo_id);
-    glBufferData(buffer_type, buffer->element_count * element_size, data, GL_STATIC_DRAW);
+    glBufferData(buffer_type, data_count * element_size, data, GL_STATIC_DRAW);
+    
+    
+    //glEnableVertexAttribArray(index);
+    //glVertexAttribPointer(index, buffer->GetComponentCount(), GL_FLOAT, GL_FALSE, 0, 0);
     
     glEnableVertexAttribArray(location);
-    glVertexAttribPointer(location, buffer->element_count, element_type, GL_FALSE, 0, 0);
+    glVertexAttribPointer(location, buffer->component_count, element_type, GL_FALSE, 0, 0);
     
     Mesh_unbindVbo(buffer_type);
     Mesh_unbindVao();
@@ -52,8 +93,10 @@ void Mesh_loadBuffer(ODIN_Mesh* mesh, GLuint location, GLenum buffer_type,
 
 void Mesh_bindVao(ODIN_Mesh* mesh) {
 	glBindVertexArray(mesh->vao_id);
+    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh->ibo_id);
 }
 
 void Mesh_unbindVao() {
+    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
 	glBindVertexArray(0);
 }

+ 7 - 7
src/odingine/mesh.h

@@ -24,7 +24,7 @@ typedef struct ODIN_MeshBuffer {
 	GLenum buffer_type; // The type of buffer GL_ARRAY_BUFFER or GL_ELEMENT_ARRAY_BUFFER
 	GLubyte element_size; // The size of each element in bytes
 	GLenum element_type; // The actual type of the data in the buffer
-	GLuint element_count; // The count of elements in the buffer
+	GLuint component_count; // The number of elements per component (x, y, z) = 3
 } ODIN_MeshBuffer;
 
 typedef struct ODIN_Mesh {
@@ -32,8 +32,8 @@ typedef struct ODIN_Mesh {
 	GLuint vao_id;
 	
 	// The index buffer will be managed by the Mesh directly
-	GLuint vertex_count; // Should this be the unique vertex count or the total number of indices?
-	GLuint ibx_id;
+	GLsizei ibo_count; // Should this be the unique vertex count or the total number of indices?
+	GLuint ibo_id;
 
     // buffers is always malloc'd for 16 elements.
 	ODIN_MeshBuffer* buffers;
@@ -43,11 +43,11 @@ typedef struct ODIN_Mesh {
 ODIN_Mesh* Mesh_newMesh();
 void Mesh_freeMesh(ODIN_Mesh* mesh);
 
-void Mesh_loadVertexData(ODIN_Mesh* mesh, GLuint vertex_count, GLfloat vetices, GLuint index_count, GLfloat indeces);
+void Mesh_loadIBOData(ODIN_Mesh* mesh, GLuint* data, GLsizei count);
 
-void Mesh_loadBuffer(ODIN_Mesh* mesh, GLuint location, GLenum buffer_type,
-						GLenum element_type, GLubyte element_size, GLuint element_count,
-                        void* data);
+void Mesh_loadBufferData(ODIN_Mesh* mesh, GLuint location, GLenum buffer_type,
+                         GLenum element_type, GLsizei element_size, GLuint component_count,
+                         void* data, GLsizei data_count);
 
 void Mesh_bindVao(ODIN_Mesh* mesh);
 void Mesh_unbindVao();