diff --git a/.gitignore b/.gitignore index 8e80bee..e679b6c 100644 --- a/.gitignore +++ b/.gitignore @@ -7,8 +7,9 @@ grafikaKBT x64 output.exe +output2.exe # obiekty testowe res/models/lazik.obj res/models/lazik2.obj -res/models/lazik4,5.obj \ No newline at end of file +res/models/lazik4,5.obj diff --git a/Makefile b/Makefile index b645180..7b40cbb 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ # mingw-w64-x86_64-gcc-14.1.0-3 to ostatnia wspierana wersja gcc, w której można stosować wildcard'y "*.cpp" CC = "C:\\msys64\\mingw64\\bin\\g++.exe" CFLAGS = -I. -DEPS = -lglew32 -lopengl32 -lglu32 -lgdi32 +DEPS = -lglew32 -lglfw3 -lopengl32 -lglu32 -lgdi32 LINK = -L. -DGLEW_STATIC OUTPUT = output.exe CPPSTD = c++17 diff --git a/glfw3.dll b/glfw3.dll new file mode 100644 index 0000000..85697ef Binary files /dev/null and b/glfw3.dll differ diff --git a/grafikaKBT.vcxproj b/grafikaKBT.vcxproj index 8b5e17d..912f673 100644 --- a/grafikaKBT.vcxproj +++ b/grafikaKBT.vcxproj @@ -116,18 +116,26 @@ Windows true true + glfw3.lib;%(AdditionalDependencies) + + + + + + + diff --git a/grafikaKBT.vcxproj.filters b/grafikaKBT.vcxproj.filters index 55b62ff..20eb58f 100644 --- a/grafikaKBT.vcxproj.filters +++ b/grafikaKBT.vcxproj.filters @@ -24,6 +24,15 @@ Source Files + + Source Files + + + Source Files + + + Source Files + @@ -35,5 +44,16 @@ Header Files + + Source Files + + + Source Files + + + + + Source Files + \ No newline at end of file diff --git a/lazik.cpp b/lazik.cpp new file mode 100644 index 0000000..baceb03 --- /dev/null +++ b/lazik.cpp @@ -0,0 +1,77 @@ +#include "lazik.hpp" + +lazik::lazik(float x, float y, float z, const char* modelpath){ + + this->c_x = x; + this->c_y = y; + this->c_z = z; + this->modelpath = modelpath; + timestampedCout("lazik.cpp: Zaladowano dane w konstruktorze.") + +} + +void lazik::loadModel() { + + timestampedCout("lazik.cpp:"); + std::cout << " Ladowanie modelu ze sciezki " << this->modelpath << "...\n"; + bool res = loadOBJ(this->modelpath, this->vertices, this->uvs, this->normals); + if (res) timestampedCout("Pomyslnie zaladowano model lazika.") + else timestampedCout("Nie udalo sie zaladowac modelu lazika."); + + glGenBuffers(1, &vertexbuffer); + glBindBuffer(GL_ARRAY_BUFFER, this->vertexbuffer); + glBufferData(GL_ARRAY_BUFFER, this->vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW); + + glGenBuffers(1, &uvbuffer); + glBindBuffer(GL_ARRAY_BUFFER, this->uvbuffer); + glBufferData(GL_ARRAY_BUFFER, this->uvs.size() * sizeof(glm::vec2), &uvs[0], GL_STATIC_DRAW); + +} + +void lazik::draw() { + + glEnable(GL_CULL_FACE); + + // 1st attribute buffer: vertices + glEnableVertexAttribArray(0); + glBindBuffer(GL_ARRAY_BUFFER, this->vertexbuffer); + glVertexAttribPointer( + 0, // attribute + 3, // size + GL_FLOAT, // type + GL_FALSE, // normalized? + 0, // stride + (void*)0 // array buffer offset + ); + + // 2nd attribute buffer: UVs + glEnableVertexAttribArray(1); + glBindBuffer(GL_ARRAY_BUFFER, this->uvbuffer); + glVertexAttribPointer( + 1, // attribute + 2, // size + GL_FLOAT, // type + GL_FALSE, // normalized? + 0, // stride + (void*)0 // array buffer offset + ); + + // Draw vertices + glDrawArrays(GL_TRIANGLES, 0, this->vertices.size()); + + glDisableVertexAttribArray(0); + glDisableVertexAttribArray(1); + +} + +void lazik::moveX(float x){ + timestampedCout("dummy moveX"); +} + +void lazik::moveY(float y){ + timestampedCout("dummy moveY"); +} + +void lazik::moveZ(float z){ + timestampedCout("dummy moveZ"); +} \ No newline at end of file diff --git a/lazik.hpp b/lazik.hpp new file mode 100644 index 0000000..7e4a81a --- /dev/null +++ b/lazik.hpp @@ -0,0 +1,33 @@ +#pragma once +#include +#include "GL/glew.h" +#include +#include +#include +#include +#include "timeh.hpp" +#include "GL/glm/glm.hpp" +#include "loadOBJ.h" + +class lazik { + private: + float c_x{0}; + float c_y{0}; + float c_z{0}; + float rot_x{0}; + float rot_y{0}; + float rot_z{0}; + std::vector vertices; + std::vector uvs; + std::vector normals; // Won't be used at the moment. + GLuint vertexbuffer; + GLuint uvbuffer; + const char* modelpath; + public: + lazik(float x, float y, float z, const char* modelpath); + void loadModel(); + void draw(); + void moveX(float x); + void moveY(float y); + void moveZ(float z); +}; \ No newline at end of file diff --git a/main.cpp b/main.cpp index 1d3efa2..9eaafd1 100644 --- a/main.cpp +++ b/main.cpp @@ -23,20 +23,22 @@ //#include #include #include "RESOURCE.H" // About box resource identifiers. -#include "szescian.h" #include "loadOBJ.h" +#include "lazik.hpp" +#include "plane.hpp" //#include #include "GL/glm/glm.hpp" -//#include "GL/glfw3.h" +#include "GL/glfw3.h" #include +#include "timeh.hpp" using namespace glm; #define glRGB(x, y, z) glColor3ub((GLubyte)x, (GLubyte)y, (GLubyte)z) #define BITMAP_ID 0x4D42 // identyfikator formatu BMP #define GL_PI 3.1415 -#define getTime lastTime = std::time(nullptr); -#define timestampedCout(msg) {getTime; std::cout << "( " << lastTime << ") " << msg << "\n";} +//#define getTime lastTime = std::time(nullptr); +//#define timestampedCout(msg) {getTime; std::cout << "( " << lastTime << ") " << msg << "\n";} //using namespace std; @@ -45,6 +47,7 @@ HPALETTE hPalette = NULL; // Application name and instance storeage static LPCTSTR lpszAppName = "grafikaKBT"; static HINSTANCE hInstance; +GLFWwindow* window; // Rotation amounts static GLfloat xRot = 0.0f; @@ -194,11 +197,8 @@ void SetDCPixelFormat(HDC hDC) { SetPixelFormat(hDC, nPixelFormat, &pfd); } -std::vector vertices; -std::vector uvs; -std::vector normals; // Won't be used at the moment. -GLuint vertexbuffer; -GLuint uvbuffer; +lazik user(0.0f, 0.0f, 0.0f, "res/models/lazik4.obj"); +plane mapa(0.0f, 0.0f, 0.0f, "res/models/mapka.obj"); void SetupRC() { // Light values and coordinates @@ -244,33 +244,29 @@ void SetupRC() { timestampedCout("Inicjalizowanie GLEW..."); glewExperimental = true; // Needed for core profile if (glewInit() != GLEW_OK) { - fprintf(stderr, "Failed to initialize GLEW\n"); - getchar(); - // glfwTerminate(); + timestampedCout("Failed to initialize GLEW"); return; } timestampedCout("Zainicjalizowano GLEW."); + // glfw3 jest w teorii niepotrzebny, ale może się przydać + // do przepisania kodu na podobny do tego stąd: + // https://github.com/opengl-tutorials/ogl/blob/master/tutorial07_model_loading/tutorial07.cpp + timestampedCout("Inicjalizowanie GLFW3..."); + if(!glfwInit()) { + timestampedCout("Failed to initialize GLFW"); + } + timestampedCout("Zainicjalizowano GLFW3."); + // Załaduj model z pliku .obj - // TODO: zmierzyć czas ładowania łazika w cyklach procesora/mikrosekundach - timestampedCout("Ladowanie lazika..."); - bool res = loadOBJ("res/models/lazik4.obj", vertices, uvs, normals); - // bool res = loadOBJ("res/models/suzanne.obj", vertices, uvs, normals); + timestampedCout("Ladowanie modelu lazika..."); + user.loadModel(); + timestampedCout("Ladowanie modelu mapki..."); + mapa.loadModel(); - 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); - // timestampedCout("rozmiar uvbuffer: " << uvs.size() * sizeof(glm::vec2)); + glfwSwapInterval(1); //glClearColor(0.0f, 0.0f, 0.4f, 0.0f); @@ -287,9 +283,9 @@ void RenderScene(void) { glRotatef(zRot, 0.0f, 0.0f, 1.0f); // gluLookAt( - // 0, 0, 0, // the position of your camera, in world space + // 0, 0, 0, // the position of your camera, in world space // 0, 0, 0, // where you want to look at, in world space - // 0, 1, 0 // probably glm::vec3(0,1,0), but (0,-1,0) would make you looking upside-down, which can be great too + // 0, 1, 0 // probably glm::vec3(0,1,0), but (0,-1,0) would make you looking upside-down, which can be great too // ); switch (polygonmode) { @@ -303,6 +299,7 @@ void RenderScene(void) { // prymitywny licznik FPS if (monitormode) { std::time_t now_t = std::time(nullptr); + // yRot -= 1; // showcase demo if (now_t > monitormodehelper) { std::cout << (int)(monitormodecounter / (now_t - monitormodehelper)) << " fps\n"; monitormodehelper = now_t; @@ -325,47 +322,13 @@ void RenderScene(void) { // // Set our "myTextureSampler" sampler to use Texture Unit 0 // glUniform1i(TextureID, 0); - // 1st attribute buffer : vertices - glEnableVertexAttribArray(0); - glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); - glVertexAttribPointer( - 0, // attribute - 3, // size - GL_FLOAT, // type - GL_FALSE, // normalized? - 0, // stride - (void*)0 // array buffer offset - ); - - // 2nd attribute buffer: UVs - glEnableVertexAttribArray(1); - glBindBuffer(GL_ARRAY_BUFFER, uvbuffer); - glVertexAttribPointer( - 1, // attribute - 2, // size - GL_FLOAT, // type - GL_FALSE, // normalized? - 0, // stride - (void*)0 // array buffer offset - ); - - // Draw vertices - glDrawArrays(GL_TRIANGLES, 0, vertices.size()); - - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); + user.draw(); + mapa.draw(); // Swap buffers //glfwSwapBuffers(window); //glfwPollEvents(); - /* - szescian nowy; - nowy.create(0, 0, 0, 10); - nowy.create(-10, 20, 0, 10); - nowy.create(25, 10, 0, 10); - */ - //Wyrysowanie prostokata: //glRectd(-10.0,-10.0,20.0,20.0); @@ -530,6 +493,8 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) hRC = wglCreateContext(hDC); wglMakeCurrent(hDC, hRC); SetupRC(); + + /* glGenTextures(2, &texture[0]); // tworzy obiekt tekstury // ładuje pierwszy obraz tekstury: @@ -567,6 +532,8 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) // ustalenie sposobu mieszania tekstury z tłem glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); + + */ break; // Window is being destroyed, cleanup @@ -599,7 +566,8 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) SwapBuffers(hDC); // Validate the newly painted client area - ValidateRect(hWnd, NULL); + if (!monitormode) ValidateRect(hWnd, NULL); + else InvalidateRect(hWnd, NULL, FALSE); break; // Windows is telling the application that it may modify diff --git a/output.exe b/output.exe deleted file mode 100644 index 3f5578b..0000000 Binary files a/output.exe and /dev/null differ diff --git a/plane.cpp b/plane.cpp new file mode 100644 index 0000000..0c38e78 --- /dev/null +++ b/plane.cpp @@ -0,0 +1,77 @@ +#include "plane.hpp" + +plane::plane(float x, float y, float z, const char* modelpath){ + + this->c_x = x; + this->c_y = y; + this->c_z = z; + this->modelpath = modelpath; + timestampedCout("plane.cpp: Zaladowano dane w konstruktorze.") + +} + +void plane::loadModel() { + + timestampedCout("plane.cpp:"); + std::cout << " Ladowanie modelu ze sciezki " << this->modelpath << "...\n"; + bool res = loadOBJ(this->modelpath, this->vertices, this->uvs, this->normals); + if (res) timestampedCout("Pomyslnie zaladowano model mapki.") + else timestampedCout("Nie udalo sie zaladowac modelu mapki."); + + glGenBuffers(1, &vertexbuffer); + glBindBuffer(GL_ARRAY_BUFFER, this->vertexbuffer); + glBufferData(GL_ARRAY_BUFFER, this->vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW); + + glGenBuffers(1, &uvbuffer); + glBindBuffer(GL_ARRAY_BUFFER, this->uvbuffer); + glBufferData(GL_ARRAY_BUFFER, this->uvs.size() * sizeof(glm::vec2), &uvs[0], GL_STATIC_DRAW); + +} + +void plane::draw() { + + glDisable(GL_CULL_FACE); + + // 1st attribute buffer: vertices + glEnableVertexAttribArray(0); + glBindBuffer(GL_ARRAY_BUFFER, this->vertexbuffer); + glVertexAttribPointer( + 0, // attribute + 3, // size + GL_FLOAT, // type + GL_FALSE, // normalized? + 0, // stride + (void*)0 // array buffer offset + ); + + // 2nd attribute buffer: UVs + glEnableVertexAttribArray(1); + glBindBuffer(GL_ARRAY_BUFFER, this->uvbuffer); + glVertexAttribPointer( + 1, // attribute + 2, // size + GL_FLOAT, // type + GL_FALSE, // normalized? + 0, // stride + (void*)0 // array buffer offset + ); + + // Draw vertices + glDrawArrays(GL_TRIANGLES, 0, this->vertices.size()); + + glDisableVertexAttribArray(0); + glDisableVertexAttribArray(1); + +} + +void plane::moveX(float x){ + timestampedCout("dummy moveX"); +} + +void plane::moveY(float y){ + timestampedCout("dummy moveY"); +} + +void plane::moveZ(float z){ + timestampedCout("dummy moveZ"); +} \ No newline at end of file diff --git a/plane.hpp b/plane.hpp new file mode 100644 index 0000000..8262d91 --- /dev/null +++ b/plane.hpp @@ -0,0 +1,33 @@ +#pragma once +#include +#include "GL/glew.h" +#include +#include +#include +#include +#include "timeh.hpp" +#include "GL/glm/glm.hpp" +#include "loadOBJ.h" + +class plane { + private: + float c_x{0}; + float c_y{0}; + float c_z{0}; + float rot_x{0}; + float rot_y{0}; + float rot_z{0}; + std::vector vertices; + std::vector uvs; + std::vector normals; // Won't be used at the moment. + GLuint vertexbuffer; + GLuint uvbuffer; + const char* modelpath; + public: + plane(float x, float y, float z, const char* modelpath); + void loadModel(); + void draw(); + void moveX(float x); + void moveY(float y); + void moveZ(float z); +}; \ No newline at end of file diff --git a/res/models/mapka.obj b/res/models/mapka.obj new file mode 100644 index 0000000..46620cd --- /dev/null +++ b/res/models/mapka.obj @@ -0,0 +1,91 @@ +# Blender 4.2.1 LTS +# www.blender.org +o Plane +v -25.000000 -0.200000 25.000000 +v 275.000000 -0.200000 25.000000 +v -25.000000 -0.200000 -275.000000 +v 275.000000 -0.200000 -275.000000 +vn -0.0000 1.0000 -0.0000 +vt 1.000000 0.000000 +vt 0.000000 1.000000 +vt 0.000000 0.000000 +vt 1.000000 1.000000 +s 0 +f 2/1/1 3/2/1 1/3/1 +f 2/1/1 4/4/1 3/2/1 +o Cube +v -15.000000 0.000000 15.000000 +v -15.000000 40.000000 15.000000 +v -15.000000 0.000000 -15.000000 +v -15.000000 40.000000 -15.000000 +v 15.000000 0.000000 15.000000 +v 15.000000 40.000000 15.000000 +v 15.000000 0.000000 -15.000000 +v 15.000000 40.000000 -15.000000 +v 15.000000 40.000000 15.000000 +v 0.000000 52.654823 15.000000 +v 0.000000 52.654823 -15.000000 +v -15.000000 20.000000 -15.000000 +v 0.000000 40.000000 -15.000000 +v 15.000000 20.000000 -15.000000 +v 0.000000 20.000000 -15.000000 +v -15.000000 10.000000 -15.000000 +v -7.500000 20.000000 -15.000000 +v 15.000000 10.000000 -15.000000 +v 7.500000 20.000000 -15.000000 +vn -1.0000 -0.0000 -0.0000 +vn -0.0000 -0.0000 -1.0000 +vn 1.0000 -0.0000 -0.0000 +vn -0.0000 -0.0000 1.0000 +vn -0.6448 0.7643 -0.0000 +vn 0.6448 0.7643 -0.0000 +vt 0.625000 0.000000 +vt 0.500000 0.250000 +vt 0.437500 0.250000 +vt 0.625000 0.375000 +vt 0.500000 0.500000 +vt 0.500000 0.437500 +vt 0.625000 0.750000 +vt 0.375000 0.750000 +vt 0.375000 1.000000 +vt 0.750000 0.750000 +vt 0.000000 0.000000 +vt 0.437500 0.500000 +vt 0.625000 0.250000 +vt 0.500000 0.375000 +vt 0.500000 0.312500 +vt 0.375000 0.250000 +vt 0.750000 0.500000 +vt 0.625000 0.500000 +vt 0.375000 0.000000 +vt 0.375000 0.500000 +vt 0.625000 1.000000 +s 0 +f 6/5/2 16/6/2 20/7/2 +f 17/8/3 18/9/3 23/10/3 +f 18/9/4 10/11/4 9/12/4 +f 10/11/5 5/13/5 9/12/5 +f 14/14/5 6/15/5 13/11/5 +f 14/14/6 8/15/6 6/15/6 +f 22/16/3 23/10/3 18/9/3 +f 8/17/3 19/18/3 21/19/3 +f 20/7/3 21/19/3 7/20/3 +f 17/8/3 15/21/3 12/22/3 +f 14/14/7 12/22/7 15/21/7 +f 7/20/2 5/23/2 6/5/2 +f 6/5/2 8/17/2 16/6/2 +f 20/7/2 7/20/2 6/5/2 +f 23/10/3 19/18/3 17/8/3 +f 17/8/3 12/22/3 18/9/3 +f 9/12/4 11/24/4 22/16/4 +f 18/9/4 12/22/4 10/11/4 +f 9/12/4 22/16/4 18/9/4 +f 10/11/5 6/25/5 5/13/5 +f 14/14/6 15/21/6 8/15/6 +f 22/16/3 11/24/3 23/10/3 +f 21/19/3 16/6/3 8/17/3 +f 8/17/3 17/8/3 19/18/3 +f 20/7/3 16/6/3 21/19/3 +f 17/8/3 8/15/3 15/21/3 +f 14/14/7 13/11/7 12/22/7 +l 10 13 diff --git a/szescian.cpp b/szescian.cpp deleted file mode 100644 index fc9cda5..0000000 --- a/szescian.cpp +++ /dev/null @@ -1,71 +0,0 @@ -#include "szescian.h" - -void szescian::create(float x, float y, float z, float KRAWEDZ) { - - this->c_x = x; - this->c_y = y; - this->c_z = z; - - // Parametry wierzcholkow - - GLfloat sa[3] = { x + 0.0f, y + 0.0f, z + 0.0f }; - GLfloat sb[3] = { x + KRAWEDZ, y + 0.0f, z + 0.0f }; - GLfloat sc[3] = { x + KRAWEDZ, y + KRAWEDZ, z + 0.0f }; - GLfloat sd[3] = { x + 0.0f, y + KRAWEDZ, z + 0.0f }; - GLfloat se[3] = { x + 0.0f, y + 0.0f, z - KRAWEDZ }; - GLfloat sf[3] = { x + KRAWEDZ, y + 0.0f, z - KRAWEDZ }; - GLfloat sg[3] = { x + KRAWEDZ, y + KRAWEDZ, z - KRAWEDZ }; - GLfloat sh[3] = { x + 0.0f, y + KRAWEDZ, z - KRAWEDZ }; - - // Sciany skladowe - - glColor3f(1.0f, 0.0f, 0.0f); - glBegin(GL_POLYGON); - glVertex3fv(sa); - glVertex3fv(sb); - glVertex3fv(sc); - glVertex3fv(sd); - glEnd(); - - - glColor3f(0.0f, 1.0f, 0.0f); - glBegin(GL_POLYGON); - glVertex3fv(sb); - glVertex3fv(sf); - glVertex3fv(sg); - glVertex3fv(sc); - glEnd(); - - glColor3f(0.0f, 0.0f, 1.0f); - glBegin(GL_POLYGON); - glVertex3fv(sf); - glVertex3fv(se); - glVertex3fv(sh); - glVertex3fv(sg); - glEnd(); - - glColor3f(1.0f, 1.0f, 0.0f); - glBegin(GL_POLYGON); - glVertex3fv(se); - glVertex3fv(sa); - glVertex3fv(sd); - glVertex3fv(sh); - glEnd(); - - glColor3f(0.0f, 1.0f, 1.0f); - glBegin(GL_POLYGON); - glVertex3fv(sd); - glVertex3fv(sc); - glVertex3fv(sg); - glVertex3fv(sh); - glEnd(); - - glColor3f(1.0f, 0.0f, 1.0f); - glBegin(GL_POLYGON); - glVertex3fv(sa); - glVertex3fv(sb); - glVertex3fv(sf); - glVertex3fv(se); - glEnd(); - -} \ No newline at end of file diff --git a/szescian.h b/szescian.h deleted file mode 100644 index 3b7990c..0000000 --- a/szescian.h +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once -#include -#include -#include - -class szescian -{ - private: - float c_x{0}; - float c_y{0}; - float c_z{0}; - public: - void create(float x, float y, float z, float KRAWEDZ); -}; \ No newline at end of file diff --git a/timeh.cpp b/timeh.cpp new file mode 100644 index 0000000..21fef83 --- /dev/null +++ b/timeh.cpp @@ -0,0 +1,5 @@ +#include "timeh.hpp" + +std::time_t getTime() { + return std::time(nullptr); +} \ No newline at end of file diff --git a/timeh.hpp b/timeh.hpp new file mode 100644 index 0000000..0d939c0 --- /dev/null +++ b/timeh.hpp @@ -0,0 +1,5 @@ +#pragma once +#include +#include +std::time_t getTime(); +#define timestampedCout(msg) {std::time_t currentTime = getTime(); std::cout << "( " << currentTime << ") " << msg << "\n";} \ No newline at end of file