From 2afc119bb9b1cf6bcff222c94e58d7b5e81ce42a Mon Sep 17 00:00:00 2001 From: Pc Date: Tue, 7 Jan 2025 20:49:20 +0100 Subject: [PATCH 1/4] Sterowanie na ocene chyba 4.0 --- main.cpp | 548 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 323 insertions(+), 225 deletions(-) diff --git a/main.cpp b/main.cpp index 306b8c1..2b73ba2 100644 --- a/main.cpp +++ b/main.cpp @@ -60,7 +60,7 @@ static GLsizei lastWidth; // Opis tekstury BITMAPINFOHEADER bitmapInfoHeader; // nagłówek obrazu -unsigned char* bitmapData; // dane tekstury +unsigned char* bitmapData; // dane tekstury unsigned int texture[2]; // obiekt tekstury LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); @@ -76,7 +76,7 @@ int monitormodecounter = 0; std::time_t monitormodehelper; //Zmienne do ruchu ############################################## - +/* float Foward = 0.0f; // Pozycja łazika w przód/tył float Sides = 0.0f; // Pozycja łazika w lewo/prawo float Rotation = 0.0f; // Rotacja łazika (w stopniach) @@ -110,6 +110,92 @@ void RotateRoverAndCamera(float angle) { if (Rotation >= 360.0f) Rotation -= 360.0f; if (Rotation < 0.0f) Rotation += 360.0f; } +*/ +//Zmienne do ruchu ##############################################^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +bool keyWPressed = false; +bool keySPressed = false; +bool keyAPressed = false; +bool keyDPressed = false; + +float Foward = 0.0f; // Pozycja łazika w przód/tył +float Sides = 0.0f; // Pozycja łazika w lewo/prawo +float Rotation = 0.0f; // Rotacja łazika (w stopniach) + +float CameraHeight = 50.0f; // Wysokość kamery +float MoveSpeed = 3.0f; // Prędkość poruszania się + +// Funkcja do poruszania łazikiem +void MoveRover(bool forward) { + // Zamieniamy kąt na radiany + float radRotation = Rotation * GL_PI / 180.0f; + + // Wektor ruchu w kierunku przód/tył (kierunek łazika) + float moveX = cos(radRotation); + float moveZ = sin(radRotation); + + // Ruch w przód + if (forward) { + Sides -= MoveSpeed * moveX; + Foward -= MoveSpeed * moveZ; + } + // Ruch w tył + else { + Sides += MoveSpeed * moveX; + Foward += MoveSpeed * moveZ; + } +} + +// Funkcja do obracania łazika wokół osi Y +void RotateRoverAndCamera(float angle) { + Rotation += angle; + if (Rotation >= 360.0f) Rotation -= 360.0f; + if (Rotation < 0.0f) Rotation += 360.0f; +} + +void UpdateRover() { + if (keyWPressed) { + if (keyAPressed) { + // Ruch do przodu i skręt w lewo + RotateRoverAndCamera(3.0f); // Dostosuj wartość kąta według potrzeb + MoveRover(true); + } + else if (keyDPressed) { + // Ruch do przodu i skręt w prawo + RotateRoverAndCamera(-3.0f); // Dostosuj wartość kąta według potrzeb + MoveRover(true); + } + else { + // Ruch do przodu bez skrętu + MoveRover(true); + } + } + else if (keySPressed) { + if (keyAPressed) { + // Ruch do tyłu i skręt w lewo + RotateRoverAndCamera(-3.0f); // Dostosuj wartość kąta według potrzeb + MoveRover(false); + } + else if (keyDPressed) { + // Ruch do tyłu i skręt w prawo + RotateRoverAndCamera(3.0f); // Dostosuj wartość kąta według potrzeb + MoveRover(false); + } + else { + // Ruch do tyłu bez skrętu + MoveRover(false); + } + } + else { + if (keyAPressed) { + // Obrót w miejscu w lewo + RotateRoverAndCamera(3.0f); // Dostosuj wartość kąta według potrzeb + } + else if (keyDPressed) { + // Obrót w miejscu w prawo + RotateRoverAndCamera(-3.0f); // Dostosuj wartość kąta według potrzeb + } + } +} // Change viewing volume and viewport. Called when window is resized void ChangeSize(GLsizei w, GLsizei h) { @@ -153,7 +239,7 @@ void ChangeSize(GLsizei w, GLsizei h) { unsigned char* LoadBitmapFile(char* filename, BITMAPINFOHEADER* bitmapInfoHeader) { FILE* filePtr; // wskaźnik pozycji pliku BITMAPFILEHEADER bitmapFileHeader; // nagłówek pliku - unsigned char* bitmapImage; // dane obrazu + unsigned char* bitmapImage; // dane obrazu int imageIdx = 0; // licznik pikseli unsigned char tempRGB; // zmienna zamiany składowych @@ -238,7 +324,7 @@ void SetDCPixelFormat(HDC hDC) { } lazik user(10.0f, 0.0f, 0.0f, "res/models/lazik4.obj"); -plane mapa(0.0f, 0.0f, 0.0f, "res/models/mapka2.obj"); +plane mapa(0.0f, 0.0f, 0.0f, "res/models/mapka2.obj"); void SetupRC() { // Light values and coordinates @@ -278,7 +364,7 @@ void SetupRC() { // White background glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Black brush - + glColor3f(0.0, 0.0, 0.0); // Initialize GLEW @@ -295,7 +381,7 @@ void SetupRC() { // 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()) { + if (!glfwInit()) { timestampedCout("Failed to initialize GLFW"); } timestampedCout("Zainicjalizowano GLFW3."); @@ -332,11 +418,11 @@ void RenderScene(void) { // ); switch (polygonmode) { - case 1: - glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); - break; - default: - glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); + case 1: + glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + break; + default: + glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); } // prymitywny licznik FPS @@ -385,15 +471,17 @@ void RenderScene(void) { // Rysowanie łazika (porusza się i obraca) glPushMatrix(); + glTranslatef(Foward, 0.0f, Sides); // Translacja łazika na jego pozycję glRotatef(Rotation, 0.0f, 1.0f, 0.0f); // Obrót łazika wokół własnej osi glColor3f(1.0, 0.0, 0.0); // Czerwony kolor dla łazika user.draw(); + UpdateRover(); glPopMatrix(); // Zamiana buforów (double buffering) - + // Swap buffers //glfwSwapBuffers(window); @@ -547,243 +635,253 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) static HGLRC hRC; // Permenant Rendering context static HDC hDC; // Private GDI Device context -float radRotation = Rotation * GL_PI / 180.0f; + float radRotation = Rotation * GL_PI / 180.0f; switch (message) { // Window creation, setup for OpenGL - case WM_CREATE: - // Store the device context - hDC = GetDC(hWnd); + case WM_CREATE: + // Store the device context + hDC = GetDC(hWnd); - // Select the pixel format - SetDCPixelFormat(hDC); + // Select the pixel format + SetDCPixelFormat(hDC); - // Create palette if needed - hPalette = GetOpenGLPalette(hDC); + // Create palette if needed + hPalette = GetOpenGLPalette(hDC); - // Create the rendering context and make it current - hRC = wglCreateContext(hDC); - wglMakeCurrent(hDC, hRC); - SetupRC(); - - /* - glGenTextures(2, &texture[0]); // tworzy obiekt tekstury + // Create the rendering context and make it current + hRC = wglCreateContext(hDC); + wglMakeCurrent(hDC, hRC); + SetupRC(); - // ładuje pierwszy obraz tekstury: - bitmapData = LoadBitmapFile((char*)"Bitmapy\\checker.bmp", &bitmapInfoHeader); + /* + glGenTextures(2, &texture[0]); // tworzy obiekt tekstury - glBindTexture(GL_TEXTURE_2D, texture[0]); // aktywuje obiekt tekstury + // ładuje pierwszy obraz tekstury: + bitmapData = LoadBitmapFile((char*)"Bitmapy\\checker.bmp", &bitmapInfoHeader); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glBindTexture(GL_TEXTURE_2D, texture[0]); // aktywuje obiekt tekstury - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - // tworzy obraz tekstury - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmapInfoHeader.biWidth, - bitmapInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmapData); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); - if (bitmapData) free(bitmapData); + // tworzy obraz tekstury + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmapInfoHeader.biWidth, + bitmapInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmapData); - // ładuje drugi obraz tekstury: - bitmapData = LoadBitmapFile((char*)"Bitmapy\\crate.bmp", &bitmapInfoHeader); - glBindTexture(GL_TEXTURE_2D, texture[1]); // aktywuje obiekt tekstury + if (bitmapData) free(bitmapData); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + // ładuje drugi obraz tekstury: + bitmapData = LoadBitmapFile((char*)"Bitmapy\\crate.bmp", &bitmapInfoHeader); + glBindTexture(GL_TEXTURE_2D, texture[1]); // aktywuje obiekt tekstury - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - // tworzy obraz tekstury - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmapInfoHeader.biWidth, - bitmapInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmapData); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); - if (bitmapData) free(bitmapData); + // tworzy obraz tekstury + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmapInfoHeader.biWidth, + bitmapInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmapData); - // ustalenie sposobu mieszania tekstury z tłem - glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); + if (bitmapData) free(bitmapData); - */ - break; + // ustalenie sposobu mieszania tekstury z tłem + glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); + + */ + break; // Window is being destroyed, cleanup - case WM_DESTROY: - // Deselect the current rendering context and delete it - wglMakeCurrent(hDC, NULL); - wglDeleteContext(hRC); + case WM_DESTROY: + // Deselect the current rendering context and delete it + wglMakeCurrent(hDC, NULL); + wglDeleteContext(hRC); - // Delete the palette if it was created - if (hPalette != NULL) DeleteObject(hPalette); + // Delete the palette if it was created + if (hPalette != NULL) DeleteObject(hPalette); - // Tell the application to terminate after the window - // is gone. - PostQuitMessage(0); - break; + // Tell the application to terminate after the window + // is gone. + PostQuitMessage(0); + break; // Window is resized. - case WM_SIZE: - // Call our function which modifies the clipping - // volume and viewport - ChangeSize(LOWORD(lParam), HIWORD(lParam)); - break; + case WM_SIZE: + // Call our function which modifies the clipping + // volume and viewport + ChangeSize(LOWORD(lParam), HIWORD(lParam)); + break; // The painting function. This message sent by Windows // whenever the screen needs updating. - case WM_PAINT: - // Call OpenGL drawing code - RenderScene(); + case WM_PAINT: + // Call OpenGL drawing code + RenderScene(); - SwapBuffers(hDC); + SwapBuffers(hDC); - // Validate the newly painted client area - if (!monitormode) ValidateRect(hWnd, NULL); - else InvalidateRect(hWnd, NULL, FALSE); - break; + // Validate the newly painted client area + if (!monitormode) ValidateRect(hWnd, NULL); + else InvalidateRect(hWnd, NULL, FALSE); + break; // Windows is telling the application that it may modify // the system palette. This message in essance asks the // application for a new palette. - case WM_QUERYNEWPALETTE: - // If the palette was created. - if (hPalette) { - int nRet; + case WM_QUERYNEWPALETTE: + // If the palette was created. + if (hPalette) { + int nRet; - // Selects the palette into the current device context - SelectPalette(hDC, hPalette, FALSE); + // Selects the palette into the current device context + SelectPalette(hDC, hPalette, FALSE); - // Map entries from the currently selected palette to - // the system palette. The return value is the number - // of palette entries modified. - nRet = RealizePalette(hDC); + // Map entries from the currently selected palette to + // the system palette. The return value is the number + // of palette entries modified. + nRet = RealizePalette(hDC); - // Repaint, forces remap of palette in current window - InvalidateRect(hWnd, NULL, FALSE); + // Repaint, forces remap of palette in current window + InvalidateRect(hWnd, NULL, FALSE); - return nRet; - } - break; + return nRet; + } + break; // This window may set the palette, even though it is not the // currently active window. - case WM_PALETTECHANGED: - // Don't do anything if the palette does not exist, or if - // this is the window that changed the palette. - if ((hPalette != NULL) && ((HWND)wParam != hWnd)) { - // Select the palette into the device context - SelectPalette(hDC, hPalette, FALSE); + case WM_PALETTECHANGED: + // Don't do anything if the palette does not exist, or if + // this is the window that changed the palette. + if ((hPalette != NULL) && ((HWND)wParam != hWnd)) { + // Select the palette into the device context + SelectPalette(hDC, hPalette, FALSE); - // Map entries to system palette - RealizePalette(hDC); + // Map entries to system palette + RealizePalette(hDC); - // Remap the current colors to the newly realized palette - UpdateColors(hDC); - return 0; - } + // Remap the current colors to the newly realized palette + UpdateColors(hDC); + return 0; + } + break; + case WM_KEYUP: + switch (wParam) { + case 'W': + keyWPressed = false; + break; + case 'S': + keySPressed = false; + break; + case 'A': + keyAPressed = false; + break; + case 'D': + keyDPressed = false; + break; + // Obsługa innych klawiszy + } + break; + + + case WM_KEYDOWN: + + + + switch (wParam) { + + case VK_UP: + xRot -= 5.0f; break; - // Key press, check for arrow keys to do cube rotation. - case WM_KEYDOWN: - - switch (wParam) { - case VK_UP: - xRot -= 5.0f; - break; - - case VK_DOWN: - xRot += 5.0f; - break; - - case VK_LEFT: - yRot -= 5.0f; - break; - - case VK_RIGHT: - yRot += 5.0f; - break; - - case 'Q': - zRot += 5.0f; - break; - - case 'E': - zRot -= 5.0f; - break; - - case 'R': - xRot = 0; - yRot = 0; - zRot = 0; - break; - - case ' ': // 32 - polygonmode = !polygonmode; - if (polygonmode) timestampedCout("Uwaga! Tryb wireframe jest niewydajny i powinien sluzyc tylko do debugowania!"); - break; - - case 'W': // Ruch w przód - MoveRover(true); - break; - case 'S': // Ruch w tył - MoveRover(false); - break; - - case 'A': // Obrót w lewo - RotateRoverAndCamera(5.0f); // Obrót łazika i kamery w lewo - break; - case 'D': // Obrót w prawo - RotateRoverAndCamera(-5.0f); // Obrót łazika i kamery w prawo - break; - - case 'J': // Obrót w lewo (wokół własnej osi) - Rotation += 5.0f; - if (Rotation > 360.0f || Rotation < 0.0f) Rotation -= 360.0f; - break; - case 'K': // Obrót w prawo (wokół własnej osi) - Rotation -= 5.0f; - if (Rotation > 360.0f || Rotation < 0.0f) Rotation += 360.0f; - break; - - case 114: // F3 - monitormode = !monitormode; - if (monitormode) { - monitormodehelper = std::time(nullptr) - 1; - timestampedCout("Wlaczono tryb monitorowania wydajnosci."); - } - if (!monitormode) timestampedCout("Wylaczono tryb monitorowania wydajnosci."); - break; - - default: - timestampedCout("Nacisnieto nierozpoznany klawisz: " << (int)wParam); - } - - xRot = (const int)xRot % 360; - yRot = (const int)yRot % 360; - zRot = (const int)zRot % 360; - - InvalidateRect(hWnd, NULL, FALSE); + case VK_DOWN: + xRot += 5.0f; break; + case VK_LEFT: + yRot -= 5.0f; + break; + + case VK_RIGHT: + yRot += 5.0f; + break; + + case 'Q': + zRot += 5.0f; + break; + + case 'E': + zRot -= 5.0f; + break; + + case 'R': + xRot = 0; + yRot = 0; + zRot = 0; + break; + + case ' ': // 32 + polygonmode = !polygonmode; + if (polygonmode) timestampedCout("Uwaga! Tryb wireframe jest niewydajny i powinien sluzyc tylko do debugowania!"); + break; + + case 'W': + keyWPressed = true; + break; + case 'S': + keySPressed = true; + break; + case 'A': + keyAPressed = true; + break; + case 'D': + keyDPressed = true; + break; + + case 114: // F3 + monitormode = !monitormode; + if (monitormode) { + monitormodehelper = std::time(nullptr) - 1; + timestampedCout("Wlaczono tryb monitorowania wydajnosci."); + } + if (!monitormode) timestampedCout("Wylaczono tryb monitorowania wydajnosci."); + break; + + default: + timestampedCout("Nacisnieto nierozpoznany klawisz: " << (int)wParam); + } + + xRot = (const int)xRot % 360; + yRot = (const int)yRot % 360; + zRot = (const int)zRot % 360; + + InvalidateRect(hWnd, NULL, FALSE); + break; + // A menu command - case WM_COMMAND: + case WM_COMMAND: - switch (LOWORD(wParam)) { + switch (LOWORD(wParam)) { - // Exit the program - case ID_FILE_EXIT: - DestroyWindow(hWnd); - break; - - // Display the about box - case ID_HELP_ABOUT: - DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG_ABOUT), hWnd, (DLGPROC)AboutDlgProc); - break; - - } + // Exit the program + case ID_FILE_EXIT: + DestroyWindow(hWnd); break; - default: // Passes it on if unproccessed - return (DefWindowProc(hWnd, message, wParam, lParam)); + // Display the about box + case ID_HELP_ABOUT: + DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG_ABOUT), hWnd, (DLGPROC)AboutDlgProc); + break; + + } + break; + + default: // Passes it on if unproccessed + return (DefWindowProc(hWnd, message, wParam, lParam)); } return (0L); @@ -794,46 +892,46 @@ BOOL APIENTRY AboutDlgProc(HWND hDlg, UINT message, UINT wParam, LONG lParam) { switch (message) { // Initialize the dialog box - case WM_INITDIALOG: - int i; - GLenum glError; + case WM_INITDIALOG: + int i; + GLenum glError; - // glGetString demo - SetDlgItemText(hDlg, IDC_OPENGL_VENDOR, reinterpret_cast(glGetString(GL_VENDOR))); - SetDlgItemText(hDlg, IDC_OPENGL_RENDERER, (LPCSTR)glGetString(GL_RENDERER)); - SetDlgItemText(hDlg, IDC_OPENGL_VERSION, (LPCSTR)glGetString(GL_VERSION)); - SetDlgItemText(hDlg, IDC_OPENGL_EXTENSIONS, (LPCSTR)glGetString(GL_EXTENSIONS)); + // glGetString demo + SetDlgItemText(hDlg, IDC_OPENGL_VENDOR, reinterpret_cast(glGetString(GL_VENDOR))); + SetDlgItemText(hDlg, IDC_OPENGL_RENDERER, (LPCSTR)glGetString(GL_RENDERER)); + SetDlgItemText(hDlg, IDC_OPENGL_VERSION, (LPCSTR)glGetString(GL_VERSION)); + SetDlgItemText(hDlg, IDC_OPENGL_EXTENSIONS, (LPCSTR)glGetString(GL_EXTENSIONS)); - // gluGetString demo - SetDlgItemText(hDlg, IDC_GLU_VERSION, (LPCSTR)gluGetString(GLU_VERSION)); - SetDlgItemText(hDlg, IDC_GLU_EXTENSIONS, (LPCSTR)gluGetString(GLU_EXTENSIONS)); + // gluGetString demo + SetDlgItemText(hDlg, IDC_GLU_VERSION, (LPCSTR)gluGetString(GLU_VERSION)); + SetDlgItemText(hDlg, IDC_GLU_EXTENSIONS, (LPCSTR)gluGetString(GLU_EXTENSIONS)); - // Display any recent error messages - i = 0; - do { - glError = glGetError(); - SetDlgItemText(hDlg, IDC_ERROR1 + i, (LPCSTR)gluErrorString(glError)); - i++; - } while (i < 6 && glError != GL_NO_ERROR); + // Display any recent error messages + i = 0; + do { + glError = glGetError(); + SetDlgItemText(hDlg, IDC_ERROR1 + i, (LPCSTR)gluErrorString(glError)); + i++; + } while (i < 6 && glError != GL_NO_ERROR); - return (TRUE); + return (TRUE); - break; + break; // Process command messages - case WM_COMMAND: + case WM_COMMAND: - // Validate and Make the changes - if (LOWORD(wParam) == IDOK) EndDialog(hDlg, TRUE); - break; + // Validate and Make the changes + if (LOWORD(wParam) == IDOK) EndDialog(hDlg, TRUE); + break; // Closed from sysbox - case WM_CLOSE: + case WM_CLOSE: - EndDialog(hDlg, TRUE); - break; + EndDialog(hDlg, TRUE); + break; } From 039435ed73271f28f788611d05222199a5a58e52 Mon Sep 17 00:00:00 2001 From: Pc Date: Tue, 7 Jan 2025 20:49:20 +0100 Subject: [PATCH 2/4] Sterowanie na ocene chyba 5.0 ? --- main.cpp | 566 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 339 insertions(+), 227 deletions(-) diff --git a/main.cpp b/main.cpp index 306b8c1..1684580 100644 --- a/main.cpp +++ b/main.cpp @@ -60,7 +60,7 @@ static GLsizei lastWidth; // Opis tekstury BITMAPINFOHEADER bitmapInfoHeader; // nagłówek obrazu -unsigned char* bitmapData; // dane tekstury +unsigned char* bitmapData; // dane tekstury unsigned int texture[2]; // obiekt tekstury LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); @@ -76,7 +76,7 @@ int monitormodecounter = 0; std::time_t monitormodehelper; //Zmienne do ruchu ############################################## - +/* float Foward = 0.0f; // Pozycja łazika w przód/tył float Sides = 0.0f; // Pozycja łazika w lewo/prawo float Rotation = 0.0f; // Rotacja łazika (w stopniach) @@ -110,6 +110,97 @@ void RotateRoverAndCamera(float angle) { if (Rotation >= 360.0f) Rotation -= 360.0f; if (Rotation < 0.0f) Rotation += 360.0f; } +*/ +//Zmienne do ruchu ##############################################^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + + + +bool keyWPressed = false; +bool keySPressed = false; +bool keyAPressed = false; +bool keyDPressed = false; + +float Foward = 0.0f; // Pozycja łazika w przód/tył +float Sides = 0.0f; // Pozycja łazika w lewo/prawo +float Rotation = 0.0f; // Rotacja łazika (w stopniach) + +float CameraHeight = 50.0f; // Wysokość kamery +float MoveSpeed = 1.0f; // Prędkość poruszania się + + +float velocity = 0.0f; // Aktualna prędkość łazika +const float friction = 0.1f; // Współczynnik tarcia (μ) +const float maxSpeed = 3.0f; // Maksymalna prędkość łazika +const float acceleration = 0.1f; + + + + + +// Funkcja do poruszania łazikiem +void MoveRover(bool forward) { + // Zamieniamy kąt na radiany + float radRotation = Rotation * GL_PI / 180.0f; + + // Wektor ruchu w kierunku przód/tył (kierunek łazika) + float moveX = cos(radRotation); + float moveZ = sin(radRotation); + + // Ruch w przód + if (forward) { + Sides -= MoveSpeed * moveX; + Foward -= MoveSpeed * moveZ; + } + // Ruch w tył + else { + Sides += MoveSpeed * moveX; + Foward += MoveSpeed * moveZ; + } +} + +// Funkcja do obracania łazika wokół osi Y +void RotateRoverAndCamera(float angle) { + Rotation += angle; + if (Rotation >= 360.0f) Rotation -= 360.0f; + if (Rotation < 0.0f) Rotation += 360.0f; +} + +void UpdateRover() { + // Przyspieszanie + if (keyWPressed) { + velocity += acceleration; + if (velocity > maxSpeed) velocity = maxSpeed; + } + else if (keySPressed) { + velocity -= acceleration; + if (velocity < -maxSpeed) velocity = -maxSpeed; + } + else { + // Hamowanie (wytracanie prędkości z powodu tarcia) + if (velocity > 0) { + velocity -= friction; + if (velocity < 0) velocity = 0; + } + else if (velocity < 0) { + velocity += friction; + if (velocity > 0) velocity = 0; + } + } + + // Obracanie + if (keyAPressed) { + RotateRoverAndCamera(3.0f); + } + else if (keyDPressed) { + RotateRoverAndCamera(-3.0f); + } + + // Aktualizacja pozycji na podstawie prędkości + float radRotation = Rotation * GL_PI / 180.0f; + Sides-= velocity * cos(radRotation); + Foward-= velocity * sin(radRotation); +} // Change viewing volume and viewport. Called when window is resized void ChangeSize(GLsizei w, GLsizei h) { @@ -153,7 +244,7 @@ void ChangeSize(GLsizei w, GLsizei h) { unsigned char* LoadBitmapFile(char* filename, BITMAPINFOHEADER* bitmapInfoHeader) { FILE* filePtr; // wskaźnik pozycji pliku BITMAPFILEHEADER bitmapFileHeader; // nagłówek pliku - unsigned char* bitmapImage; // dane obrazu + unsigned char* bitmapImage; // dane obrazu int imageIdx = 0; // licznik pikseli unsigned char tempRGB; // zmienna zamiany składowych @@ -238,7 +329,7 @@ void SetDCPixelFormat(HDC hDC) { } lazik user(10.0f, 0.0f, 0.0f, "res/models/lazik4.obj"); -plane mapa(0.0f, 0.0f, 0.0f, "res/models/mapka2.obj"); +plane mapa(0.0f, 0.0f, 0.0f, "res/models/mapka2.obj"); void SetupRC() { // Light values and coordinates @@ -278,7 +369,7 @@ void SetupRC() { // White background glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Black brush - + glColor3f(0.0, 0.0, 0.0); // Initialize GLEW @@ -295,7 +386,7 @@ void SetupRC() { // 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()) { + if (!glfwInit()) { timestampedCout("Failed to initialize GLFW"); } timestampedCout("Zainicjalizowano GLFW3."); @@ -332,11 +423,11 @@ void RenderScene(void) { // ); switch (polygonmode) { - case 1: - glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); - break; - default: - glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); + case 1: + glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + break; + default: + glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); } // prymitywny licznik FPS @@ -370,9 +461,9 @@ void RenderScene(void) { //Sterowanie //############################################################################################ gluLookAt( - Foward - 50.0f * sin((Rotation + 180.0f) * GL_PI / 180.0f), // Pozycja kamery wokół łazika (w poziomie) + Foward - 100.0f * sin((Rotation + 180.0f) * GL_PI / 180.0f), // Pozycja kamery wokół łazika (w poziomie) CameraHeight, // Wysokość kamery - Sides - 50.0f * cos((Rotation + 180.0f) * GL_PI / 180.0f), // Kamera wzdłuż osi X i Z + Sides - 100.0f * cos((Rotation + 180.0f) * GL_PI / 180.0f), // Kamera wzdłuż osi X i Z Foward, 0.0f, Sides, // Punkt, na który patrzy kamera (łazik) 0.0f, 1.0f, 0.0f // Wektor "góry" ); @@ -385,15 +476,17 @@ void RenderScene(void) { // Rysowanie łazika (porusza się i obraca) glPushMatrix(); + glTranslatef(Foward, 0.0f, Sides); // Translacja łazika na jego pozycję glRotatef(Rotation, 0.0f, 1.0f, 0.0f); // Obrót łazika wokół własnej osi glColor3f(1.0, 0.0, 0.0); // Czerwony kolor dla łazika user.draw(); + UpdateRover(); glPopMatrix(); // Zamiana buforów (double buffering) - + // Swap buffers //glfwSwapBuffers(window); @@ -487,6 +580,7 @@ int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, WNDCLASS wc; // Windows class structure HWND hWnd; // Storeage for window handle + hInstance = hInst; // Register Window style @@ -528,6 +622,8 @@ int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, if (hWnd == NULL) return FALSE; + const WORD ID_TIMER = 1; + SetTimer(hWnd, ID_TIMER, 100, NULL); // Display the window ShowWindow(hWnd, SW_SHOW); UpdateWindow(hWnd); @@ -547,243 +643,259 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) static HGLRC hRC; // Permenant Rendering context static HDC hDC; // Private GDI Device context -float radRotation = Rotation * GL_PI / 180.0f; + float radRotation = Rotation * GL_PI / 180.0f; switch (message) { // Window creation, setup for OpenGL - case WM_CREATE: - // Store the device context - hDC = GetDC(hWnd); + case WM_CREATE: + // Store the device context + hDC = GetDC(hWnd); - // Select the pixel format - SetDCPixelFormat(hDC); + // Select the pixel format + SetDCPixelFormat(hDC); - // Create palette if needed - hPalette = GetOpenGLPalette(hDC); + // Create palette if needed + hPalette = GetOpenGLPalette(hDC); - // Create the rendering context and make it current - hRC = wglCreateContext(hDC); - wglMakeCurrent(hDC, hRC); - SetupRC(); - - /* - glGenTextures(2, &texture[0]); // tworzy obiekt tekstury + // Create the rendering context and make it current + hRC = wglCreateContext(hDC); + wglMakeCurrent(hDC, hRC); + SetupRC(); - // ładuje pierwszy obraz tekstury: - bitmapData = LoadBitmapFile((char*)"Bitmapy\\checker.bmp", &bitmapInfoHeader); + /* + glGenTextures(2, &texture[0]); // tworzy obiekt tekstury - glBindTexture(GL_TEXTURE_2D, texture[0]); // aktywuje obiekt tekstury + // ładuje pierwszy obraz tekstury: + bitmapData = LoadBitmapFile((char*)"Bitmapy\\checker.bmp", &bitmapInfoHeader); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glBindTexture(GL_TEXTURE_2D, texture[0]); // aktywuje obiekt tekstury - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - // tworzy obraz tekstury - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmapInfoHeader.biWidth, - bitmapInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmapData); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); - if (bitmapData) free(bitmapData); + // tworzy obraz tekstury + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmapInfoHeader.biWidth, + bitmapInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmapData); - // ładuje drugi obraz tekstury: - bitmapData = LoadBitmapFile((char*)"Bitmapy\\crate.bmp", &bitmapInfoHeader); - glBindTexture(GL_TEXTURE_2D, texture[1]); // aktywuje obiekt tekstury + if (bitmapData) free(bitmapData); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + // ładuje drugi obraz tekstury: + bitmapData = LoadBitmapFile((char*)"Bitmapy\\crate.bmp", &bitmapInfoHeader); + glBindTexture(GL_TEXTURE_2D, texture[1]); // aktywuje obiekt tekstury - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - // tworzy obraz tekstury - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmapInfoHeader.biWidth, - bitmapInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmapData); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); - if (bitmapData) free(bitmapData); + // tworzy obraz tekstury + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmapInfoHeader.biWidth, + bitmapInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmapData); - // ustalenie sposobu mieszania tekstury z tłem - glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); + if (bitmapData) free(bitmapData); - */ - break; + // ustalenie sposobu mieszania tekstury z tłem + glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); + + */ + break; // Window is being destroyed, cleanup - case WM_DESTROY: - // Deselect the current rendering context and delete it - wglMakeCurrent(hDC, NULL); - wglDeleteContext(hRC); + case WM_DESTROY: + // Deselect the current rendering context and delete it + wglMakeCurrent(hDC, NULL); + wglDeleteContext(hRC); - // Delete the palette if it was created - if (hPalette != NULL) DeleteObject(hPalette); + // Delete the palette if it was created + if (hPalette != NULL) DeleteObject(hPalette); - // Tell the application to terminate after the window - // is gone. - PostQuitMessage(0); - break; + // Tell the application to terminate after the window + // is gone. + PostQuitMessage(0); + break; // Window is resized. - case WM_SIZE: - // Call our function which modifies the clipping - // volume and viewport - ChangeSize(LOWORD(lParam), HIWORD(lParam)); - break; + case WM_SIZE: + // Call our function which modifies the clipping + // volume and viewport + ChangeSize(LOWORD(lParam), HIWORD(lParam)); + break; // The painting function. This message sent by Windows // whenever the screen needs updating. - case WM_PAINT: - // Call OpenGL drawing code - RenderScene(); + case WM_PAINT: + // Call OpenGL drawing code + RenderScene(); - SwapBuffers(hDC); + SwapBuffers(hDC); - // Validate the newly painted client area - if (!monitormode) ValidateRect(hWnd, NULL); - else InvalidateRect(hWnd, NULL, FALSE); - break; + // Validate the newly painted client area + if (!monitormode) ValidateRect(hWnd, NULL); + else InvalidateRect(hWnd, NULL, FALSE); + break; // Windows is telling the application that it may modify // the system palette. This message in essance asks the // application for a new palette. - case WM_QUERYNEWPALETTE: - // If the palette was created. - if (hPalette) { - int nRet; + case WM_QUERYNEWPALETTE: + // If the palette was created. + if (hPalette) { + int nRet; - // Selects the palette into the current device context - SelectPalette(hDC, hPalette, FALSE); + // Selects the palette into the current device context + SelectPalette(hDC, hPalette, FALSE); - // Map entries from the currently selected palette to - // the system palette. The return value is the number - // of palette entries modified. - nRet = RealizePalette(hDC); + // Map entries from the currently selected palette to + // the system palette. The return value is the number + // of palette entries modified. + nRet = RealizePalette(hDC); - // Repaint, forces remap of palette in current window - InvalidateRect(hWnd, NULL, FALSE); + // Repaint, forces remap of palette in current window + InvalidateRect(hWnd, NULL, FALSE); - return nRet; - } - break; + return nRet; + } + break; // This window may set the palette, even though it is not the // currently active window. - case WM_PALETTECHANGED: - // Don't do anything if the palette does not exist, or if - // this is the window that changed the palette. - if ((hPalette != NULL) && ((HWND)wParam != hWnd)) { - // Select the palette into the device context - SelectPalette(hDC, hPalette, FALSE); + case WM_PALETTECHANGED: + // Don't do anything if the palette does not exist, or if + // this is the window that changed the palette. + if ((hPalette != NULL) && ((HWND)wParam != hWnd)) { + // Select the palette into the device context + SelectPalette(hDC, hPalette, FALSE); - // Map entries to system palette - RealizePalette(hDC); + // Map entries to system palette + RealizePalette(hDC); - // Remap the current colors to the newly realized palette - UpdateColors(hDC); - return 0; - } + // Remap the current colors to the newly realized palette + UpdateColors(hDC); + return 0; + } + break; + case WM_KEYUP: + switch (wParam) { + case 'W': + keyWPressed = false; + break; + case 'S': + keySPressed = false; + break; + case 'A': + keyAPressed = false; + break; + case 'D': + keyDPressed = false; + break; + // Obsługa innych klawiszy + } + break; + + + case WM_KEYDOWN: + + + + switch (wParam) { + + case VK_UP: + xRot -= 5.0f; break; - // Key press, check for arrow keys to do cube rotation. - case WM_KEYDOWN: - - switch (wParam) { - case VK_UP: - xRot -= 5.0f; - break; - - case VK_DOWN: - xRot += 5.0f; - break; - - case VK_LEFT: - yRot -= 5.0f; - break; - - case VK_RIGHT: - yRot += 5.0f; - break; - - case 'Q': - zRot += 5.0f; - break; - - case 'E': - zRot -= 5.0f; - break; - - case 'R': - xRot = 0; - yRot = 0; - zRot = 0; - break; - - case ' ': // 32 - polygonmode = !polygonmode; - if (polygonmode) timestampedCout("Uwaga! Tryb wireframe jest niewydajny i powinien sluzyc tylko do debugowania!"); - break; - - case 'W': // Ruch w przód - MoveRover(true); - break; - case 'S': // Ruch w tył - MoveRover(false); - break; - - case 'A': // Obrót w lewo - RotateRoverAndCamera(5.0f); // Obrót łazika i kamery w lewo - break; - case 'D': // Obrót w prawo - RotateRoverAndCamera(-5.0f); // Obrót łazika i kamery w prawo - break; - - case 'J': // Obrót w lewo (wokół własnej osi) - Rotation += 5.0f; - if (Rotation > 360.0f || Rotation < 0.0f) Rotation -= 360.0f; - break; - case 'K': // Obrót w prawo (wokół własnej osi) - Rotation -= 5.0f; - if (Rotation > 360.0f || Rotation < 0.0f) Rotation += 360.0f; - break; - - case 114: // F3 - monitormode = !monitormode; - if (monitormode) { - monitormodehelper = std::time(nullptr) - 1; - timestampedCout("Wlaczono tryb monitorowania wydajnosci."); - } - if (!monitormode) timestampedCout("Wylaczono tryb monitorowania wydajnosci."); - break; - - default: - timestampedCout("Nacisnieto nierozpoznany klawisz: " << (int)wParam); - } - - xRot = (const int)xRot % 360; - yRot = (const int)yRot % 360; - zRot = (const int)zRot % 360; - - InvalidateRect(hWnd, NULL, FALSE); + case VK_DOWN: + xRot += 5.0f; break; + case VK_LEFT: + yRot -= 5.0f; + break; + + case VK_RIGHT: + yRot += 5.0f; + break; + + case 'Q': + zRot += 5.0f; + break; + + case 'E': + zRot -= 5.0f; + break; + + case 'R': + xRot = 0; + yRot = 0; + zRot = 0; + break; + + case ' ': // 32 + polygonmode = !polygonmode; + if (polygonmode) timestampedCout("Uwaga! Tryb wireframe jest niewydajny i powinien sluzyc tylko do debugowania!"); + break; + + case 'W': + keyWPressed = true; + break; + case 'S': + keySPressed = true; + break; + case 'A': + keyAPressed = true; + break; + case 'D': + keyDPressed = true; + break; + + case 114: // F3 + monitormode = !monitormode; + if (monitormode) { + monitormodehelper = std::time(nullptr) - 1; + timestampedCout("Wlaczono tryb monitorowania wydajnosci."); + } + if (!monitormode) timestampedCout("Wylaczono tryb monitorowania wydajnosci."); + break; + + default: + timestampedCout("Nacisnieto nierozpoznany klawisz: " << (int)wParam); + } + + xRot = (const int)xRot % 360; + yRot = (const int)yRot % 360; + zRot = (const int)zRot % 360; + + InvalidateRect(hWnd, NULL, FALSE); + break; + // A menu command - case WM_COMMAND: + case WM_COMMAND: - switch (LOWORD(wParam)) { + switch (LOWORD(wParam)) { - // Exit the program - case ID_FILE_EXIT: - DestroyWindow(hWnd); - break; - - // Display the about box - case ID_HELP_ABOUT: - DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG_ABOUT), hWnd, (DLGPROC)AboutDlgProc); - break; - - } + // Exit the program + case ID_FILE_EXIT: + DestroyWindow(hWnd); break; - default: // Passes it on if unproccessed - return (DefWindowProc(hWnd, message, wParam, lParam)); + // Display the about box + case ID_HELP_ABOUT: + DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG_ABOUT), hWnd, (DLGPROC)AboutDlgProc); + break; + + } + break; + case WM_TIMER: + { + RenderScene(); + SwapBuffers(hDC); + ValidateRect(hWnd, NULL); + break; + } + default: // Passes it on if unproccessed + return (DefWindowProc(hWnd, message, wParam, lParam)); } return (0L); @@ -794,46 +906,46 @@ BOOL APIENTRY AboutDlgProc(HWND hDlg, UINT message, UINT wParam, LONG lParam) { switch (message) { // Initialize the dialog box - case WM_INITDIALOG: - int i; - GLenum glError; + case WM_INITDIALOG: + int i; + GLenum glError; - // glGetString demo - SetDlgItemText(hDlg, IDC_OPENGL_VENDOR, reinterpret_cast(glGetString(GL_VENDOR))); - SetDlgItemText(hDlg, IDC_OPENGL_RENDERER, (LPCSTR)glGetString(GL_RENDERER)); - SetDlgItemText(hDlg, IDC_OPENGL_VERSION, (LPCSTR)glGetString(GL_VERSION)); - SetDlgItemText(hDlg, IDC_OPENGL_EXTENSIONS, (LPCSTR)glGetString(GL_EXTENSIONS)); + // glGetString demo + SetDlgItemText(hDlg, IDC_OPENGL_VENDOR, reinterpret_cast(glGetString(GL_VENDOR))); + SetDlgItemText(hDlg, IDC_OPENGL_RENDERER, (LPCSTR)glGetString(GL_RENDERER)); + SetDlgItemText(hDlg, IDC_OPENGL_VERSION, (LPCSTR)glGetString(GL_VERSION)); + SetDlgItemText(hDlg, IDC_OPENGL_EXTENSIONS, (LPCSTR)glGetString(GL_EXTENSIONS)); - // gluGetString demo - SetDlgItemText(hDlg, IDC_GLU_VERSION, (LPCSTR)gluGetString(GLU_VERSION)); - SetDlgItemText(hDlg, IDC_GLU_EXTENSIONS, (LPCSTR)gluGetString(GLU_EXTENSIONS)); + // gluGetString demo + SetDlgItemText(hDlg, IDC_GLU_VERSION, (LPCSTR)gluGetString(GLU_VERSION)); + SetDlgItemText(hDlg, IDC_GLU_EXTENSIONS, (LPCSTR)gluGetString(GLU_EXTENSIONS)); - // Display any recent error messages - i = 0; - do { - glError = glGetError(); - SetDlgItemText(hDlg, IDC_ERROR1 + i, (LPCSTR)gluErrorString(glError)); - i++; - } while (i < 6 && glError != GL_NO_ERROR); + // Display any recent error messages + i = 0; + do { + glError = glGetError(); + SetDlgItemText(hDlg, IDC_ERROR1 + i, (LPCSTR)gluErrorString(glError)); + i++; + } while (i < 6 && glError != GL_NO_ERROR); - return (TRUE); + return (TRUE); - break; + break; // Process command messages - case WM_COMMAND: + case WM_COMMAND: - // Validate and Make the changes - if (LOWORD(wParam) == IDOK) EndDialog(hDlg, TRUE); - break; + // Validate and Make the changes + if (LOWORD(wParam) == IDOK) EndDialog(hDlg, TRUE); + break; // Closed from sysbox - case WM_CLOSE: + case WM_CLOSE: - EndDialog(hDlg, TRUE); - break; + EndDialog(hDlg, TRUE); + break; } From 01d7766b6a486f8a1a46bdaff0cea8a89b464c10 Mon Sep 17 00:00:00 2001 From: Pc Date: Tue, 7 Jan 2025 22:20:22 +0100 Subject: [PATCH 3/4] Sterowanie 5.0 + Klasa do fps --- FPSCounter.cpp | 24 +++++ grafikaKBT.vcxproj | 1 + grafikaKBT.vcxproj.filters | 3 + main.cpp | 187 +++++++++++++++++++++++-------------- 4 files changed, 143 insertions(+), 72 deletions(-) create mode 100644 FPSCounter.cpp diff --git a/FPSCounter.cpp b/FPSCounter.cpp new file mode 100644 index 0000000..61144c3 --- /dev/null +++ b/FPSCounter.cpp @@ -0,0 +1,24 @@ +#include +#include + +class FPSCounter { +public: + FPSCounter() : frameCount(0), lastTime(std::chrono::high_resolution_clock::now()) {} + + void update() { + frameCount++; + auto currentTime = std::chrono::high_resolution_clock::now(); + std::chrono::duration elapsed = currentTime - lastTime; + + if (elapsed.count() >= 1.0) { + double fps = frameCount / elapsed.count(); + std::cout << "FPS: " << fps << std::endl; + frameCount = 0; + lastTime = currentTime; + } + } + +private: + int frameCount; + std::chrono::time_point lastTime; +}; diff --git a/grafikaKBT.vcxproj b/grafikaKBT.vcxproj index 403c19e..a6076de 100644 --- a/grafikaKBT.vcxproj +++ b/grafikaKBT.vcxproj @@ -121,6 +121,7 @@ + diff --git a/grafikaKBT.vcxproj.filters b/grafikaKBT.vcxproj.filters index 15876af..5e31f23 100644 --- a/grafikaKBT.vcxproj.filters +++ b/grafikaKBT.vcxproj.filters @@ -33,6 +33,9 @@ Source Files + + Source Files + diff --git a/main.cpp b/main.cpp index 1684580..d798968 100644 --- a/main.cpp +++ b/main.cpp @@ -31,6 +31,7 @@ #include "GL/glfw3.h" #include #include "timeh.hpp" +#include "FPSCounter.cpp" using namespace glm; @@ -113,8 +114,9 @@ void RotateRoverAndCamera(float angle) { */ //Zmienne do ruchu ##############################################^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +FPSCounter fpsCounter; - +//Fps counter bool keyWPressed = false; bool keySPressed = false; @@ -133,7 +135,10 @@ float velocity = 0.0f; // Aktualna prędkość łazika const float friction = 0.1f; // Współczynnik tarcia (μ) const float maxSpeed = 3.0f; // Maksymalna prędkość łazika const float acceleration = 0.1f; - +float rotationVelocity = 0.0f; // Prędkość obrotu łazika +const float rotationAcceleration = 0.1f; // Przyspieszenie obrotu +const float rotationFriction = 0.05f; // Współczynnik tarcia obrotu +const float maxRotationSpeed = 5.0f; // Maksymalna prędkość obrotu @@ -167,17 +172,18 @@ void RotateRoverAndCamera(float angle) { } void UpdateRover() { - // Przyspieszanie + // Przyspieszanie w przód if (keyWPressed) { velocity += acceleration; if (velocity > maxSpeed) velocity = maxSpeed; } + // Przyspieszanie w tył else if (keySPressed) { velocity -= acceleration; if (velocity < -maxSpeed) velocity = -maxSpeed; } + // Hamowanie (wytracanie prędkości z powodu tarcia) else { - // Hamowanie (wytracanie prędkości z powodu tarcia) if (velocity > 0) { velocity -= friction; if (velocity < 0) velocity = 0; @@ -190,16 +196,34 @@ void UpdateRover() { // Obracanie if (keyAPressed) { - RotateRoverAndCamera(3.0f); + rotationVelocity += rotationAcceleration; + if (rotationVelocity > maxRotationSpeed) rotationVelocity = maxRotationSpeed; } else if (keyDPressed) { - RotateRoverAndCamera(-3.0f); + rotationVelocity -= rotationAcceleration; + if (rotationVelocity < -maxRotationSpeed) rotationVelocity = -maxRotationSpeed; + } + else { + // Hamowanie obrotu (wytracanie prędkości z powodu tarcia) + if (rotationVelocity > 0) { + rotationVelocity -= rotationFriction; + if (rotationVelocity < 0) rotationVelocity = 0; + } + else if (rotationVelocity < 0) { + rotationVelocity += rotationFriction; + if (rotationVelocity > 0) rotationVelocity = 0; + } } // Aktualizacja pozycji na podstawie prędkości float radRotation = Rotation * GL_PI / 180.0f; - Sides-= velocity * cos(radRotation); - Foward-= velocity * sin(radRotation); + Sides -= velocity * cos(radRotation); + Foward -= velocity * sin(radRotation); + + // Aktualizacja kąta obrotu + Rotation += rotationVelocity; + if (Rotation >= 360.0f) Rotation -= 360.0f; + if (Rotation < 0.0f) Rotation += 360.0f; } // Change viewing volume and viewport. Called when window is resized @@ -433,7 +457,7 @@ void RenderScene(void) { // prymitywny licznik FPS if (monitormode) { std::time_t now_t = std::time(nullptr); - yRot -= 1; // showcase demo + yRot -= 10; // showcase demo if (now_t > monitormodehelper) { std::cout << (int)(monitormodecounter / (now_t - monitormodehelper)) << " fps\n"; monitormodehelper = now_t; @@ -483,7 +507,7 @@ void RenderScene(void) { user.draw(); UpdateRover(); glPopMatrix(); - + fpsCounter.update(); // Zamiana buforów (double buffering) @@ -573,69 +597,88 @@ HPALETTE GetOpenGLPalette(HDC hDC) { // Return the handle to the new palette return hRetPal; } - -int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { - - MSG msg; // Windows message structure - WNDCLASS wc; // Windows class structure - HWND hWnd; // Storeage for window handle - - - hInstance = hInst; - - // Register Window style - wc.style = CS_HREDRAW | CS_VREDRAW; - wc.lpfnWndProc = (WNDPROC)WndProc; - wc.cbClsExtra = 0; - wc.cbWndExtra = 0; - wc.hInstance = hInstance; - wc.hIcon = NULL; - wc.hCursor = LoadCursor(NULL, IDC_ARROW); - - // No need for background brush for OpenGL window - wc.hbrBackground = NULL; - - wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU); - wc.lpszClassName = lpszAppName; - - // Register the window class - if (RegisterClass(&wc) == 0) return FALSE; - - - // Create the main application window - hWnd = CreateWindow( - lpszAppName, - lpszAppName, - - // OpenGL requires WS_CLIPCHILDREN and WS_CLIPSIBLINGS - WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, - - // Window position and size - 50, 50, - 400, 400, - NULL, - NULL, - hInstance, - NULL); - - // If window was not created, quit - if (hWnd == NULL) return FALSE; - - - const WORD ID_TIMER = 1; - SetTimer(hWnd, ID_TIMER, 100, NULL); - // Display the window - ShowWindow(hWnd, SW_SHOW); - UpdateWindow(hWnd); - - // Process application messages until the application closes - while (GetMessage(&msg, NULL, 0, 0)) { - TranslateMessage(&msg); - DispatchMessage(&msg); +void CreateConsole() +{ + // Tworzenie nowej konsoli + if (AllocConsole()) + { + // Przekierowanie standardowych strumieni do konsoli + FILE* conin; + FILE* conout; + FILE* conerr; + freopen_s(&conin, "conin$", "r", stdin); + freopen_s(&conout, "conout$", "w", stdout); + freopen_s(&conerr, "conout$", "w", stderr); + } + else + { + MessageBox(NULL, "Nie udało się utworzyć konsoli.", "Błąd", MB_OK | MB_ICONERROR); } - - return msg.wParam; } + int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { + CreateConsole(); + MSG msg; // Windows message structure + WNDCLASS wc; // Windows class structure + HWND hWnd; // Storeage for window handle + + + hInstance = hInst; + + // Register Window style + wc.style = CS_HREDRAW | CS_VREDRAW; + wc.lpfnWndProc = (WNDPROC)WndProc; + wc.cbClsExtra = 0; + wc.cbWndExtra = 0; + wc.hInstance = hInstance; + wc.hIcon = NULL; + wc.hCursor = LoadCursor(NULL, IDC_ARROW); + + // No need for background brush for OpenGL window + wc.hbrBackground = NULL; + + wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU); + wc.lpszClassName = lpszAppName; + + // Register the window class + if (RegisterClass(&wc) == 0) return FALSE; + + + // Create the main application window + hWnd = CreateWindow( + lpszAppName, + lpszAppName, + + // OpenGL requires WS_CLIPCHILDREN and WS_CLIPSIBLINGS + WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, + + // Window position and size + 50, 50, + 800, 800, + NULL, + NULL, + hInstance, + NULL); + + // If window was not created, quit + if (hWnd == NULL) return FALSE; + + + const WORD ID_TIMER = 1; + SetTimer(hWnd, ID_TIMER, 100, NULL); + // Display the window + + + ShowWindow(hWnd, SW_SHOW); + UpdateWindow(hWnd); + + // Process application messages until the application closes + while (GetMessage(&msg, NULL, 0, 0)) { + TranslateMessage(&msg); + DispatchMessage(&msg); + } + + return msg.wParam; + } // Window procedure, handles all messages for this program @@ -950,4 +993,4 @@ BOOL APIENTRY AboutDlgProc(HWND hDlg, UINT message, UINT wParam, LONG lParam) { } return FALSE; -} \ No newline at end of file +} From 79c6a1e144e0115bdd05af9faf50507ee269002f Mon Sep 17 00:00:00 2001 From: Pc Date: Tue, 7 Jan 2025 23:01:34 +0100 Subject: [PATCH 4/4] Klasa do fps + Limit Fps --- main.cpp | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/main.cpp b/main.cpp index d798968..a0b6e21 100644 --- a/main.cpp +++ b/main.cpp @@ -32,6 +32,7 @@ #include #include "timeh.hpp" #include "FPSCounter.cpp" +#include using namespace glm; @@ -113,11 +114,22 @@ void RotateRoverAndCamera(float angle) { } */ //Zmienne do ruchu ##############################################^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - FPSCounter fpsCounter; - +static const int targetFPS = 144; // Celowany FPS //Fps counter +void LimitFPS(int targetFPS) { + static auto lastTime = std::chrono::high_resolution_clock::now(); + auto currentTime = std::chrono::high_resolution_clock::now(); + std::chrono::duration elapsed = currentTime - lastTime; + // Jeśli upłynęło za mało czasu, aby osiągnąć target FPS, czekamy + double frameTime = 1.0 / targetFPS; // Czas na jedną klatkę w sekundach + if (elapsed.count() < frameTime) { + std::this_thread::sleep_for(std::chrono::duration(frameTime - elapsed.count())); + } + + lastTime = std::chrono::high_resolution_clock::now(); +} bool keyWPressed = false; bool keySPressed = false; bool keyAPressed = false; @@ -429,7 +441,7 @@ void SetupRC() { } void RenderScene(void) { - + //float normal[3]; // Storage for calculated surface normal // Save the matrix state and do the rotations @@ -457,7 +469,7 @@ void RenderScene(void) { // prymitywny licznik FPS if (monitormode) { std::time_t now_t = std::time(nullptr); - yRot -= 10; // showcase demo + //yRot -= 1; // showcase demo if (now_t > monitormodehelper) { std::cout << (int)(monitormodecounter / (now_t - monitormodehelper)) << " fps\n"; monitormodehelper = now_t; @@ -776,13 +788,19 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) SwapBuffers(hDC); // Validate the newly painted client area - if (!monitormode) ValidateRect(hWnd, NULL); - else InvalidateRect(hWnd, NULL, FALSE); - break; + if (!monitormode) { + ValidateRect(hWnd, NULL); + } + else { + InvalidateRect(hWnd, NULL, FALSE); + } - // Windows is telling the application that it may modify - // the system palette. This message in essance asks the - // application for a new palette. + // Limit FPS + LimitFPS(targetFPS); + + // Update FPS counter + fpsCounter.update(); + break; case WM_QUERYNEWPALETTE: // If the palette was created. if (hPalette) {