Compare commits
3 Commits
3171ebe504
...
Sterowanie
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fa824192b7 | ||
|
|
79c6a1e144 | ||
|
|
01d7766b6a |
24
FPSCounter.cpp
Normal file
24
FPSCounter.cpp
Normal 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;
|
||||||
|
};
|
||||||
@@ -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" />
|
||||||
|
|||||||
@@ -33,6 +33,9 @@
|
|||||||
<ClCompile Include="plane.cpp">
|
<ClCompile Include="plane.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="FPSCounter.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="loadOBJ.h">
|
<ClInclude Include="loadOBJ.h">
|
||||||
|
|||||||
284
main.cpp
284
main.cpp
@@ -31,6 +31,8 @@
|
|||||||
#include "GL/glfw3.h"
|
#include "GL/glfw3.h"
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
#include "timeh.hpp"
|
#include "timeh.hpp"
|
||||||
|
#include "FPSCounter.cpp"
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
using namespace glm;
|
using namespace glm;
|
||||||
|
|
||||||
@@ -71,51 +73,29 @@ void SetDCPixelFormat(HDC hDC);
|
|||||||
|
|
||||||
int polygonmode = 0;
|
int polygonmode = 0;
|
||||||
std::time_t lastTime = std::time(nullptr);
|
std::time_t lastTime = std::time(nullptr);
|
||||||
int monitormode = 0;
|
int monitormode = 1;
|
||||||
int monitormodecounter = 0;
|
int monitormodecounter = 0;
|
||||||
std::time_t monitormodehelper;
|
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)
|
|
||||||
|
|
||||||
// Zmienne do sterowania kamerą
|
|
||||||
float CameraHeight = 50.0f; // Wysokość kamery
|
|
||||||
|
|
||||||
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 -= 1.1f * moveX;
|
|
||||||
Foward -= 1.1f * moveZ;
|
|
||||||
}
|
|
||||||
// Ruch w tył
|
|
||||||
else {
|
|
||||||
Sides += 1.1f * moveX;
|
|
||||||
Foward += 1.1f * moveZ;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Funkcja do obrotu kamery i łazika (A/D)
|
|
||||||
void RotateRoverAndCamera(float angle) {
|
|
||||||
Rotation += angle;
|
|
||||||
if (Rotation >= 360.0f) Rotation -= 360.0f;
|
|
||||||
if (Rotation < 0.0f) Rotation += 360.0f;
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
//Zmienne do ruchu ##############################################^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
//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 keyWPressed = false;
|
||||||
bool keySPressed = false;
|
bool keySPressed = false;
|
||||||
bool keyAPressed = false;
|
bool keyAPressed = false;
|
||||||
@@ -130,10 +110,13 @@ float MoveSpeed = 1.0f; // Prędkość poruszania się
|
|||||||
|
|
||||||
|
|
||||||
float velocity = 0.0f; // Aktualna prędkość łazika
|
float velocity = 0.0f; // Aktualna prędkość łazika
|
||||||
const float friction = 0.1f; // Współczynnik tarcia (μ)
|
const float friction = 0.05f; // Współczynnik tarcia (μ)
|
||||||
const float maxSpeed = 3.0f; // Maksymalna prędkość łazika
|
const float maxSpeed = 3.0f; // Maksymalna prędkość łazika
|
||||||
const float acceleration = 0.1f;
|
const float acceleration = 0.2f;
|
||||||
|
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 = 3.0f; // Maksymalna prędkość obrotu
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -167,17 +150,18 @@ void RotateRoverAndCamera(float angle) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void UpdateRover() {
|
void UpdateRover() {
|
||||||
// Przyspieszanie
|
// Przyspieszanie w przód
|
||||||
if (keyWPressed) {
|
if (keyWPressed) {
|
||||||
velocity += acceleration;
|
velocity += acceleration;
|
||||||
if (velocity > maxSpeed) velocity = maxSpeed;
|
if (velocity > maxSpeed) velocity = maxSpeed;
|
||||||
}
|
}
|
||||||
|
// Przyspieszanie w tył
|
||||||
else if (keySPressed) {
|
else if (keySPressed) {
|
||||||
velocity -= acceleration;
|
velocity -= acceleration;
|
||||||
if (velocity < -maxSpeed) velocity = -maxSpeed;
|
if (velocity < -maxSpeed) velocity = -maxSpeed;
|
||||||
}
|
}
|
||||||
|
// Hamowanie (wytracanie prędkości z powodu tarcia)
|
||||||
else {
|
else {
|
||||||
// Hamowanie (wytracanie prędkości z powodu tarcia)
|
|
||||||
if (velocity > 0) {
|
if (velocity > 0) {
|
||||||
velocity -= friction;
|
velocity -= friction;
|
||||||
if (velocity < 0) velocity = 0;
|
if (velocity < 0) velocity = 0;
|
||||||
@@ -190,16 +174,34 @@ void UpdateRover() {
|
|||||||
|
|
||||||
// Obracanie
|
// Obracanie
|
||||||
if (keyAPressed) {
|
if (keyAPressed) {
|
||||||
RotateRoverAndCamera(3.0f);
|
rotationVelocity += rotationAcceleration;
|
||||||
|
if (rotationVelocity > maxRotationSpeed) rotationVelocity = maxRotationSpeed;
|
||||||
}
|
}
|
||||||
else if (keyDPressed) {
|
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
|
// Aktualizacja pozycji na podstawie prędkości
|
||||||
float radRotation = Rotation * GL_PI / 180.0f;
|
float radRotation = Rotation * GL_PI / 180.0f;
|
||||||
Sides-= velocity * cos(radRotation);
|
Sides -= velocity * cos(radRotation);
|
||||||
Foward-= velocity * sin(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
|
||||||
@@ -405,7 +407,7 @@ void SetupRC() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void RenderScene(void) {
|
void RenderScene(void) {
|
||||||
|
|
||||||
//float normal[3]; // Storage for calculated surface normal
|
//float normal[3]; // Storage for calculated surface normal
|
||||||
|
|
||||||
// Save the matrix state and do the rotations
|
// Save the matrix state and do the rotations
|
||||||
@@ -430,19 +432,7 @@ void RenderScene(void) {
|
|||||||
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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;
|
|
||||||
monitormodecounter = 0;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
monitormodecounter++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Clear the screen
|
// Clear the screen
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
@@ -483,7 +473,7 @@ void RenderScene(void) {
|
|||||||
user.draw();
|
user.draw();
|
||||||
UpdateRover();
|
UpdateRover();
|
||||||
glPopMatrix();
|
glPopMatrix();
|
||||||
|
fpsCounter.update();
|
||||||
|
|
||||||
// Zamiana buforów (double buffering)
|
// Zamiana buforów (double buffering)
|
||||||
|
|
||||||
@@ -573,69 +563,88 @@ HPALETTE GetOpenGLPalette(HDC hDC) {
|
|||||||
// Return the handle to the new palette
|
// Return the handle to the new palette
|
||||||
return hRetPal;
|
return hRetPal;
|
||||||
}
|
}
|
||||||
|
void CreateConsole()
|
||||||
int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
|
{
|
||||||
|
// Tworzenie nowej konsoli
|
||||||
MSG msg; // Windows message structure
|
if (AllocConsole())
|
||||||
WNDCLASS wc; // Windows class structure
|
{
|
||||||
HWND hWnd; // Storeage for window handle
|
// Przekierowanie standardowych strumieni do konsoli
|
||||||
|
FILE* conin;
|
||||||
|
FILE* conout;
|
||||||
hInstance = hInst;
|
FILE* conerr;
|
||||||
|
freopen_s(&conin, "conin$", "r", stdin);
|
||||||
// Register Window style
|
freopen_s(&conout, "conout$", "w", stdout);
|
||||||
wc.style = CS_HREDRAW | CS_VREDRAW;
|
freopen_s(&conerr, "conout$", "w", stderr);
|
||||||
wc.lpfnWndProc = (WNDPROC)WndProc;
|
}
|
||||||
wc.cbClsExtra = 0;
|
else
|
||||||
wc.cbWndExtra = 0;
|
{
|
||||||
wc.hInstance = hInstance;
|
MessageBox(NULL, "Nie udało się utworzyć konsoli.", "Błąd", MB_OK | MB_ICONERROR);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
// Window procedure, handles all messages for this program
|
||||||
@@ -733,13 +742,19 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
|
|||||||
SwapBuffers(hDC);
|
SwapBuffers(hDC);
|
||||||
|
|
||||||
// Validate the newly painted client area
|
// Validate the newly painted client area
|
||||||
if (!monitormode) ValidateRect(hWnd, NULL);
|
if (!monitormode) {
|
||||||
else InvalidateRect(hWnd, NULL, FALSE);
|
ValidateRect(hWnd, NULL);
|
||||||
break;
|
}
|
||||||
|
else {
|
||||||
|
InvalidateRect(hWnd, NULL, FALSE);
|
||||||
|
}
|
||||||
|
|
||||||
// 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) {
|
||||||
@@ -849,15 +864,8 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
|
|||||||
case 'D':
|
case 'D':
|
||||||
keyDPressed = true;
|
keyDPressed = true;
|
||||||
break;
|
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:
|
default:
|
||||||
timestampedCout("Nacisnieto nierozpoznany klawisz: " << (int)wParam);
|
timestampedCout("Nacisnieto nierozpoznany klawisz: " << (int)wParam);
|
||||||
@@ -950,4 +958,4 @@ BOOL APIENTRY AboutDlgProc(HWND hDlg, UINT message, UINT wParam, LONG lParam) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user