1919#include < SDL2/SDL.h>
2020#endif
2121
22- #include " defines.h"
23- #include " vertex_buffer.h"
24- #include " index_buffer.h"
25- #include " shader.h"
26- #include " floating_camera.h"
27-
28- void openGLDebugCallback (GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void * userParam) {
29- std::cout << " [OpenGL Error] " << message << std::endl;
30- }
31-
3222#ifdef _DEBUG
3323
3424void _GLGetError (const char * file, int line, const char * call) {
@@ -45,6 +35,17 @@ void _GLGetError(const char* file, int line, const char* call) {
4535
4636#endif
4737
38+ #include " defines.h"
39+ #include " vertex_buffer.h"
40+ #include " index_buffer.h"
41+ #include " shader.h"
42+ #include " floating_camera.h"
43+ #include " mesh.h"
44+
45+ void openGLDebugCallback (GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void * userParam) {
46+ std::cout << " [OpenGL Error] " << message << std::endl;
47+ }
48+
4849int main (int argc, char ** argv) {
4950 SDL_Window* window;
5051 SDL_Init (SDL_INIT_EVERYTHING);
@@ -79,43 +80,13 @@ int main(int argc, char** argv) {
7980 glDebugMessageCallback (openGLDebugCallback, 0 );
8081 #endif
8182
82- std::vector<Vertex> vertices;
83- uint64 numVertices = 0 ;
84-
85- std::vector<uint32> indices;
86- uint64 numIndices = 0 ;
87-
88- std::ifstream input = std::ifstream (" models/monkey.bmf" , std::ios::in | std::ios::binary);
89- if (!input.is_open ()) {
90- std::cout << " Error reading model file" << std::endl;
91- return 1 ;
92- }
93- input.read ((char *)&numVertices, sizeof (uint64));
94- input.read ((char *)&numIndices, sizeof (uint64));
95-
96- for (uint64 i = 0 ; i < numVertices; i++) {
97- Vertex vertex;
98- input.read ((char *)&vertex.position .x , sizeof (float ));
99- input.read ((char *)&vertex.position .y , sizeof (float ));
100- input.read ((char *)&vertex.position .z , sizeof (float ));
101- input.read ((char *)&vertex.normal .x , sizeof (float ));
102- input.read ((char *)&vertex.normal .y , sizeof (float ));
103- input.read ((char *)&vertex.normal .z , sizeof (float ));
104- vertices.push_back (vertex);
105- }
106- for (uint64 i = 0 ; i < numIndices; i++) {
107- uint32 index;
108- input.read ((char *)&index, sizeof (uint32));
109- indices.push_back (index);
110- }
111-
112- IndexBuffer indexBuffer (indices.data (), numIndices, sizeof (indices[0 ]));
113-
114- VertexBuffer vertexBuffer (vertices.data (), numVertices);
115- vertexBuffer.unbind ();
116-
11783 Shader shader (" shaders/basic.vs" , " shaders/basic.fs" );
11884 shader.bind ();
85+ Material material = {};
86+ material.diffuse = {0 .4f , 0 .2f , 0 .1f };
87+ // material.specular = material.diffuse;
88+ // material.shininess = 4.0f;
89+ Mesh mesh (" models/monkey.bmf" , material, &shader);
11990
12091 uint64 perfCounterFrequency = SDL_GetPerformanceFrequency ();
12192 uint64 lastCounter = SDL_GetPerformanceCounter ();
@@ -236,14 +207,10 @@ int main(int argc, char** argv) {
236207 glm::mat4 modelView = camera.getView () * model;
237208 glm::mat4 invModelView = glm::transpose (glm::inverse (modelView));
238209
239- vertexBuffer.bind ();
240- indexBuffer.bind ();
241210 GLCALL (glUniformMatrix4fv (modelViewProjMatrixLocation, 1 , GL_FALSE, &modelViewProj[0 ][0 ]));
242211 GLCALL (glUniformMatrix4fv (modelViewLocation, 1 , GL_FALSE, &modelView[0 ][0 ]));
243212 GLCALL (glUniformMatrix4fv (invModelViewLocation, 1 , GL_FALSE, &invModelView[0 ][0 ]));
244- GLCALL (glDrawElements (GL_TRIANGLES, numIndices, GL_UNSIGNED_INT, 0 ));
245- indexBuffer.unbind ();
246- vertexBuffer.unbind ();
213+ mesh.render ();
247214
248215 SDL_GL_SwapWindow (window);
249216
0 commit comments