diff --git a/Makefile b/Makefile index e6f5247..b645180 100644 --- a/Makefile +++ b/Makefile @@ -4,9 +4,10 @@ CFLAGS = -I. DEPS = -lglew32 -lopengl32 -lglu32 -lgdi32 LINK = -L. -DGLEW_STATIC OUTPUT = output.exe +CPPSTD = c++17 default: - $(CC) -g *.cpp $(CFLAGS) $(DEPS) $(LINK) -static -static-libgcc -fno-keep-inline-dllexport -o $(OUTPUT) + $(CC) -g *.cpp $(CFLAGS) $(DEPS) $(LINK) -std=$(CPPSTD) -static -static-libgcc -fno-keep-inline-dllexport -o $(OUTPUT) run: default $(OUTPUT) diff --git a/loadOBJ.cpp b/loadOBJ.cpp index fb90bf1..40798cb 100644 --- a/loadOBJ.cpp +++ b/loadOBJ.cpp @@ -15,11 +15,15 @@ bool loadOBJ(const char* path, std::vector & out_vertices, std::vecto return false; } - int line = 0; + unsigned int line = 0; + unsigned int vs = 0; + unsigned int vts = 0; + unsigned int vns = 0; + unsigned int fs = 0; + char lineHeader[128]; while (1) { - char lineHeader[128]; // read the first word of the line int res = fscanf(file, "%s", lineHeader); if (res == EOF) break; // EOF = End Of File. Quit the loop. @@ -30,23 +34,27 @@ bool loadOBJ(const char* path, std::vector & out_vertices, std::vecto glm::vec3 vertex; fscanf(file, "%f %f %f\n", &vertex.x, &vertex.y, &vertex.z); temp_vertices.push_back(vertex); + vs++; } else if (strcmp(lineHeader, "vt") == 0) { glm::vec2 uv; fscanf(file, "%f %f\n", &uv.x, &uv.y); temp_uvs.push_back(uv); + vts++; } else if (strcmp(lineHeader, "vn") == 0) { glm::vec3 normal; fscanf(file, "%f %f %f\n", &normal.x, &normal.y, &normal.z); temp_normals.push_back(normal); + vns++; } else if (strcmp(lineHeader, "f") == 0) { unsigned int vertexIndex[3], uvIndex[3], normalIndex[3]; int matches = fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%d\n", &vertexIndex[0], &uvIndex[0], &normalIndex[0], &vertexIndex[1], &uvIndex[1], &normalIndex[1], &vertexIndex[2], &uvIndex[2], &normalIndex[2]); + fs++; if (matches != 9){ printf("File can't be read by our simple parser. Try exporting with other options (%d matches on line %d)\n", matches, line); @@ -64,24 +72,27 @@ bool loadOBJ(const char* path, std::vector & out_vertices, std::vecto normalIndices.push_back(normalIndex[2]); } - // For each vertex of each triangle - for (unsigned int i = 0; i < vertexIndices.size(); i++) { - unsigned int vertexIndex = vertexIndices[i]; - unsigned int uvIndex = uvIndices[i]; - unsigned int normalIndex = normalIndices[i]; - - glm::vec3 vertex1 = temp_vertices[vertexIndex - 1]; - glm::vec2 vertex2 = temp_uvs[uvIndex - 1]; - glm::vec3 vertex3 = temp_normals[normalIndex - 1]; - - out_vertices .push_back(vertex1); - out_uvs .push_back(vertex2); - out_normals .push_back(vertex3); - } - line++; - } + // For each vertex of each triangle + for (unsigned int i = 0; i < vertexIndices.size(); i++) { + unsigned int vertexIndex = vertexIndices[i]; + unsigned int uvIndex = uvIndices[i]; + unsigned int normalIndex = normalIndices[i]; + + glm::vec3 vertex1 = temp_vertices[vertexIndex - 1]; + glm::vec2 vertex2 = temp_uvs[uvIndex - 1]; + glm::vec3 vertex3 = temp_normals[normalIndex - 1]; + + out_vertices.push_back(vertex1); + out_uvs.push_back(vertex2); + out_normals.push_back(vertex3); + } + + fclose(file); + + printf("(loadOBJ.cpp) Loaded file with %d v, %d vt, %d vn, %d f. %d lines total.\n", vs, vts, vns, fs, line); + return true; } \ No newline at end of file diff --git a/main.cpp b/main.cpp index 4e9e00f..84d530d 100644 --- a/main.cpp +++ b/main.cpp @@ -67,7 +67,6 @@ BOOL APIENTRY AboutDlgProc(HWND hDlg, UINT message, UINT wParam, LONG lParam); void SetDCPixelFormat(HDC hDC); int polygonmode = 0; -char loadCount = 0; // Change viewing volume and viewport. Called when window is resized void ChangeSize(GLsizei w, GLsizei h) { @@ -100,47 +99,6 @@ void ChangeSize(GLsizei w, GLsizei h) { glLoadIdentity(); } -void SetupRC() { - // Light values and coordinates - //GLfloat ambientLight[] = { 0.3f, 0.3f, 0.3f, 1.0f }; - //GLfloat diffuseLight[] = { 0.7f, 0.7f, 0.7f, 1.0f }; - //GLfloat specular[] = { 1.0f, 1.0f, 1.0f, 1.0f }; - //GLfloat lightPos[] = { 0.0f, 150.0f, 150.0f, 1.0f }; - //GLfloat specref[] = { 1.0f, 1.0f, 1.0f, 1.0f }; - - - glEnable(GL_DEPTH_TEST); // Hidden surface removal - glFrontFace(GL_CCW); // Counter clock-wise polygons face out - //glEnable(GL_CULL_FACE); // Do not calculate inside of jet - - // Enable lighting - //glEnable(GL_LIGHTING); - - // Setup and enable light 0 - //glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight); - //glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight); - //glLightfv(GL_LIGHT0, GL_SPECULAR, specular); - //glLightfv(GL_LIGHT0, GL_POSITION, lightPos); - //glEnable(GL_LIGHT0); - - // Enable color tracking - //glEnable(GL_COLOR_MATERIAL); - - // Set Material properties to follow glColor values - //glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); - - // All materials hereafter have full specular reflectivity - // with a high shine - //glMaterialfv(GL_FRONT, GL_SPECULAR, specref); - //glMateriali(GL_FRONT, GL_SHININESS, 128); - - - // White background - glClearColor(1.0f, 1.0f, 1.0f, 1.0f); - // Black brush - glColor3f(0.0, 0.0, 0.0); -} - // LoadBitmapFile // opis: ładuje mapę bitową z pliku i zwraca jej adres. // Wypełnia strukturę nagłówka. @@ -239,14 +197,45 @@ GLuint vertexbuffer; GLuint uvbuffer; std::time_t lastTime = std::time(nullptr); -void setup(){ - // załaduj model +void SetupRC() { + // Light values and coordinates + //GLfloat ambientLight[] = { 0.3f, 0.3f, 0.3f, 1.0f }; + //GLfloat diffuseLight[] = { 0.7f, 0.7f, 0.7f, 1.0f }; + //GLfloat specular[] = { 1.0f, 1.0f, 1.0f, 1.0f }; + //GLfloat lightPos[] = { 0.0f, 150.0f, 150.0f, 1.0f }; + //GLfloat specref[] = { 1.0f, 1.0f, 1.0f, 1.0f }; - // if(!glfwInit()) { - // fprintf(stderr, "Failed to initialize GLFW\n"); - // getchar(); - // return; - // } + + glEnable(GL_DEPTH_TEST); // Hidden surface removal + glFrontFace(GL_CCW); // Counter clock-wise polygons face out + glEnable(GL_CULL_FACE); // Do not calculate inside of jet // !!! znacząco poprawia wydajność + + // Enable lighting + //glEnable(GL_LIGHTING); + + // Setup and enable light 0 + //glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight); + //glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight); + //glLightfv(GL_LIGHT0, GL_SPECULAR, specular); + //glLightfv(GL_LIGHT0, GL_POSITION, lightPos); + //glEnable(GL_LIGHT0); + + // Enable color tracking + //glEnable(GL_COLOR_MATERIAL); + + // Set Material properties to follow glColor values + //glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); + + // All materials hereafter have full specular reflectivity + // with a high shine + //glMaterialfv(GL_FRONT, GL_SPECULAR, specref); + //glMateriali(GL_FRONT, GL_SHININESS, 128); + + + // White background + glClearColor(1.0f, 1.0f, 1.0f, 1.0f); + // Black brush + glColor3f(0.0, 0.0, 0.0); // Initialize GLEW timestampedCout("Inicjalizowanie GLEW..."); @@ -260,44 +249,34 @@ void setup(){ timestampedCout("Zainicjalizowano GLEW."); - /* */ - // Read our .obj file - + // Załaduj model z pliku .obj // TODO: zmierzyć czas ładowania łazika w cyklach procesora/mikrosekundach timestampedCout("Ladowanie lazika..."); bool res = loadOBJ("res/models/lazik3.obj", vertices, uvs, normals); // bool res = loadOBJ("res/models/suzanne.obj", vertices, uvs, normals); - if (res) { - timestampedCout("Pomyslnie zaladowano model lazika."); - } + if (res) timestampedCout("Pomyslnie zaladowano model lazika.") else timestampedCout("Nie udalo sie zaladowac modelu lazika."); - /* */ - glGenBuffers(1, &vertexbuffer); glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW); + // timestampedCout("rozmiar vertexbuffer: " << vertices.size() * sizeof(glm::vec3)); glGenBuffers(1, &uvbuffer); glBindBuffer(GL_ARRAY_BUFFER, uvbuffer); glBufferData(GL_ARRAY_BUFFER, uvs.size() * sizeof(glm::vec2), &uvs[0], GL_STATIC_DRAW); - - loadCount++; + // timestampedCout("rozmiar uvbuffer: " << uvs.size() * sizeof(glm::vec2)); //glClearColor(0.0f, 0.0f, 0.4f, 0.0f); } void RenderScene(void) { - if (loadCount == 0) setup(); //float normal[3]; // Storage for calculated surface normal - // Clear the window with current clearing color - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - // Save the matrix state and do the rotations glPushMatrix(); glRotatef(xRot, 1.0f, 0.0f, 0.0f); @@ -310,25 +289,10 @@ void RenderScene(void) { // 0, 1, 0 // probably glm::vec3(0,1,0), but (0,-1,0) would make you looking upside-down, which can be great too // ); - ///////////////////////////////////////////////////////////////// - // MIEJSCE NA KOD OPENGL DO TWORZENIA WLASNYCH SCEN: // - ///////////////////////////////////////////////////////////////// - //szescian(); - - //Sposób na odróżnienie "przedniej" i "tylniej" ściany wielokąta: - //glPolygonMode(GL_BACK,GL_LINE); - //walec(40, 40); - //szescian(); - switch (polygonmode) { - case 0: - glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); - break; - case 1: glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); break; - default: glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); } @@ -346,7 +310,7 @@ void RenderScene(void) { // // Set our "myTextureSampler" sampler to use Texture Unit 0 // glUniform1i(TextureID, 0); - // 1rst attribute buffer : vertices + // 1st attribute buffer : vertices glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); glVertexAttribPointer( @@ -370,8 +334,8 @@ void RenderScene(void) { (void*)0 // array buffer offset ); - // Draw the triangle! - glDrawArrays(GL_TRIANGLES, 0, vertices.size() ); + // Draw vertices + glDrawArrays(GL_TRIANGLES, 0, vertices.size()); glDisableVertexAttribArray(0); glDisableVertexAttribArray(1); @@ -387,29 +351,14 @@ void RenderScene(void) { nowy.create(25, 10, 0, 10); */ - glDrawArrays(GL_TRIANGLES, 0, vertices.size()); - - /* - szescian nowy2; - nowy2.create(0, 10, 0, 10); - szescian nowy3; - nowy3.create(0, 20, -10, 10); - */ - - //Uzyskanie siatki: - //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); - //Wyrysowanie prostokata: //glRectd(-10.0,-10.0,20.0,20.0); - ///////////////////////////////////////////////////////////////// - ///////////////////////////////////////////////////////////////// - ///////////////////////////////////////////////////////////////// - glPopMatrix(); - glMatrixMode(GL_MODELVIEW); + glPopMatrix(); // wymagane + glMatrixMode(GL_MODELVIEW); // zmniejsza zużycie GPU // Flush drawing commands - glFlush(); + // glFlush(); } // If necessary, creates a 3-3-2 palette for the device context listed. @@ -626,7 +575,6 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) ChangeSize(LOWORD(lParam), HIWORD(lParam)); break; - // The painting function. This message sent by Windows // whenever the screen needs updating. case WM_PAINT: @@ -757,9 +705,6 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) return (0L); } - - - // Dialog procedure. BOOL APIENTRY AboutDlgProc(HWND hDlg, UINT message, UINT wParam, LONG lParam) { diff --git a/output.exe b/output.exe index 496b567..3c83945 100644 Binary files a/output.exe and b/output.exe differ