poprawiona wydajność, zużycie RAMu

This commit is contained in:
2024-11-28 02:19:26 +01:00
parent bab9ab751c
commit 2f8f14d279
4 changed files with 79 additions and 122 deletions

151
main.cpp
View File

@@ -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) {