próba modularyzacji kodu, dodane klasy lazik oraz plane

- Makefile uwzględnia glfw
- projekt w visual studio także powinien linkować glfw
- dodano plik .gitignore
- klasa lazik stworzona z myślą o łaziku
- bliźniaczo podobna klasa plane stworzona z myślą o mapie
- dodano surowy projekt mapy (res/models/mapka.obj)
- usunięto zbędną klasę sześcian oraz plik wykonywalny .exe
- funkcja timestampedCout() przeniesiona do plików timeh.cpp/.hpp, co pozwala na używanie jej wszędzie
- w main.cpp:
	- zakomentowano masę (niepotrzebnego)/przeniesionego kodu
	- tryb monitorowania wydajności wyłącza ValidateRect(), co wymusza ciągłe renderowanie nowych klatek. pozwala to oszacować wpływ zmian na wydajność programu.
This commit is contained in:
2024-12-07 03:47:17 +01:00
parent ef1a8b225b
commit 2b51e31307
16 changed files with 387 additions and 154 deletions

102
main.cpp
View File

@@ -23,20 +23,22 @@
//#include <stdio.h>
#include <iostream>
#include "RESOURCE.H" // About box resource identifiers.
#include "szescian.h"
#include "loadOBJ.h"
#include "lazik.hpp"
#include "plane.hpp"
//#include <vector>
#include "GL/glm/glm.hpp"
//#include "GL/glfw3.h"
#include "GL/glfw3.h"
#include <ctime>
#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 <glm::vec3> vertices;
std::vector <glm::vec2> uvs;
std::vector <glm::vec3> 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