Merge remote-tracking branch 'origin/Sterowanie_na_3' into Sterowanie_na_3

This commit is contained in:
2025-01-08 05:10:12 +01:00
4 changed files with 432 additions and 234 deletions

24
FPSCounter.cpp Normal file
View File

@@ -0,0 +1,24 @@
#include <iostream>
#include <chrono>
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<double> 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<std::chrono::high_resolution_clock> lastTime;
};

View File

@@ -121,6 +121,7 @@
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="FPSCounter.cpp" />
<ClCompile Include="glew.c" /> <ClCompile Include="glew.c" />
<ClCompile Include="lazik.cpp" /> <ClCompile Include="lazik.cpp" />
<ClCompile Include="loadOBJ.cpp" /> <ClCompile Include="loadOBJ.cpp" />

View File

@@ -33,10 +33,7 @@
<ClCompile Include="plane.cpp"> <ClCompile Include="plane.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="shader.cpp"> <ClCompile Include="FPSCounter.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="texture.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
</ItemGroup> </ItemGroup>

234
main.cpp
View File

@@ -33,7 +33,8 @@
#include "timeh.hpp" #include "timeh.hpp"
#include "texture.hpp" #include "texture.hpp"
#include "shader.hpp" #include "shader.hpp"
#include "FPSCounter.cpp"
#include <thread>
using namespace glm; using namespace glm;
@@ -79,7 +80,7 @@ int monitormodecounter = 0;
std::time_t monitormodehelper; std::time_t monitormodehelper;
//Zmienne do ruchu ############################################## //Zmienne do ruchu ##############################################
/*
float Foward = 0.0f; // Pozycja łazika w przód/tył float Foward = 0.0f; // Pozycja łazika w przód/tył
float Sides = 0.0f; // Pozycja łazika w lewo/prawo float Sides = 0.0f; // Pozycja łazika w lewo/prawo
float Rotation = 0.0f; // Rotacja łazika (w stopniach) float Rotation = 0.0f; // Rotacja łazika (w stopniach)
@@ -113,6 +114,131 @@ void RotateRoverAndCamera(float angle) {
if (Rotation >= 360.0f) Rotation -= 360.0f; if (Rotation >= 360.0f) Rotation -= 360.0f;
if (Rotation < 0.0f) Rotation += 360.0f; if (Rotation < 0.0f) Rotation += 360.0f;
} }
*/
//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<double> 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<double>(frameTime - elapsed.count()));
}
lastTime = std::chrono::high_resolution_clock::now();
}
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;
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
// 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 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 {
if (velocity > 0) {
velocity -= friction;
if (velocity < 0) velocity = 0;
}
else if (velocity < 0) {
velocity += friction;
if (velocity > 0) velocity = 0;
}
}
// Obracanie
if (keyAPressed) {
rotationVelocity += rotationAcceleration;
if (rotationVelocity > maxRotationSpeed) rotationVelocity = maxRotationSpeed;
}
else if (keyDPressed) {
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);
// 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 // Change viewing volume and viewport. Called when window is resized
void ChangeSize(GLsizei w, GLsizei h) { void ChangeSize(GLsizei w, GLsizei h) {
@@ -672,6 +798,7 @@ void SetupRC() {
glfwSwapInterval(1); glfwSwapInterval(1);
//glClearColor(0.0f, 0.0f, 0.4f, 0.0f); //glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
} }
void RenderScene(void) { void RenderScene(void) {
@@ -730,9 +857,9 @@ void RenderScene(void) {
//Sterowanie //Sterowanie
//############################################################################################ //############################################################################################
gluLookAt( 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 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) Foward, 0.0f, Sides, // Punkt, na który patrzy kamera (łazik)
0.0f, 1.0f, 0.0f // Wektor "góry" 0.0f, 1.0f, 0.0f // Wektor "góry"
); );
@@ -746,10 +873,12 @@ void RenderScene(void) {
// Rysowanie łazika (porusza się i obraca) // Rysowanie łazika (porusza się i obraca)
glPushMatrix(); glPushMatrix();
glTranslatef(Foward, 0.0f, Sides); // Translacja łazika na jego pozycję 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 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 glColor3f(1.0, 0.0, 0.0); // Czerwony kolor dla łazika
user.draw(); user.draw();
UpdateRover();
glPopMatrix(); glPopMatrix();
//skrzynka(50); //skrzynka(50);
@@ -767,6 +896,10 @@ void RenderScene(void) {
//glColor3f(0.0, 1.0, 0.0); //glColor3f(0.0, 1.0, 0.0);
//mapa.draw(); //mapa.draw();
//glColor3f(0.0, 0.0, 0.0); //glColor3f(0.0, 0.0, 0.0);
fpsCounter.update();
// Zamiana buforów (double buffering)
// Swap buffers // Swap buffers
//glfwSwapBuffers(window); //glfwSwapBuffers(window);
@@ -853,13 +986,28 @@ HPALETTE GetOpenGLPalette(HDC hDC) {
// Return the handle to the new palette // Return the handle to the new palette
return hRetPal; return hRetPal;
} }
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);
}
}
int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
CreateConsole();
MSG msg; // Windows message structure MSG msg; // Windows message structure
WNDCLASS wc; // Windows class structure WNDCLASS wc; // Windows class structure
HWND hWnd; // Storeage for window handle HWND hWnd; // Storeage for window handle
hInstance = hInst; hInstance = hInst;
// Register Window style // Register Window style
@@ -891,7 +1039,7 @@ int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
// Window position and size // Window position and size
50, 50, 50, 50,
400, 400, 800, 800,
NULL, NULL,
NULL, NULL,
hInstance, hInstance,
@@ -901,7 +1049,11 @@ int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
if (hWnd == NULL) return FALSE; if (hWnd == NULL) return FALSE;
const WORD ID_TIMER = 1;
SetTimer(hWnd, ID_TIMER, 100, NULL);
// Display the window // Display the window
ShowWindow(hWnd, SW_SHOW); ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd); UpdateWindow(hWnd);
@@ -933,6 +1085,11 @@ float radRotation = Rotation * GL_PI / 180.0f;
// Create palette if needed // Create palette if needed
hPalette = GetOpenGLPalette(hDC); hPalette = GetOpenGLPalette(hDC);
// Create the rendering context and make it current
hRC = wglCreateContext(hDC);
wglMakeCurrent(hDC, hRC);
SetupRC();
// Create the rendering context and make it current // Create the rendering context and make it current
hRC = wglCreateContext(hDC); hRC = wglCreateContext(hDC);
wglMakeCurrent(hDC, hRC); wglMakeCurrent(hDC, hRC);
@@ -1053,9 +1210,12 @@ float radRotation = Rotation * GL_PI / 180.0f;
else InvalidateRect(hWnd, NULL, FALSE); else InvalidateRect(hWnd, NULL, FALSE);
break; break;
// Windows is telling the application that it may modify // Limit FPS
// the system palette. This message in essance asks the LimitFPS(targetFPS);
// application for a new palette.
// Update FPS counter
fpsCounter.update();
break;
case WM_QUERYNEWPALETTE: case WM_QUERYNEWPALETTE:
// If the palette was created. // If the palette was created.
if (hPalette) { if (hPalette) {
@@ -1093,11 +1253,31 @@ float radRotation = Rotation * GL_PI / 180.0f;
return 0; return 0;
} }
break; 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;
// Key press, check for arrow keys to do cube rotation.
case WM_KEYDOWN: case WM_KEYDOWN:
switch (wParam) { switch (wParam) {
case VK_UP: case VK_UP:
xRot -= 5.0f; xRot -= 5.0f;
break; break;
@@ -1133,27 +1313,17 @@ float radRotation = Rotation * GL_PI / 180.0f;
if (polygonmode) timestampedCout("Uwaga! Tryb wireframe jest niewydajny i powinien sluzyc tylko do debugowania!"); if (polygonmode) timestampedCout("Uwaga! Tryb wireframe jest niewydajny i powinien sluzyc tylko do debugowania!");
break; break;
case 'W': // Ruch w przód case 'W':
MoveRover(true); keyWPressed = true;
break; break;
case 'S': // Ruch w tył case 'S':
MoveRover(false); keySPressed = true;
break; break;
case 'A':
case 'A': // Obrót w lewo keyAPressed = true;
RotateRoverAndCamera(5.0f); // Obrót łazika i kamery w lewo
break; break;
case 'D': // Obrót w prawo case 'D':
RotateRoverAndCamera(-5.0f); // Obrót łazika i kamery w prawo keyDPressed = true;
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; break;
case 114: // F3 case 114: // F3
@@ -1193,6 +1363,12 @@ float radRotation = Rotation * GL_PI / 180.0f;
} }
break; break;
case WM_TIMER:
RenderScene();
SwapBuffers(hDC);
ValidateRect(hWnd, NULL);
break;
default: // Passes it on if unproccessed default: // Passes it on if unproccessed
return (DefWindowProc(hWnd, message, wParam, lParam)); return (DefWindowProc(hWnd, message, wParam, lParam));