1 Commits

Author SHA1 Message Date
c7ceee0772 Merge: kolizja z płotem pod klawiszem 'k', naprawiony limit FPS 2025-01-17 13:52:25 +01:00
2 changed files with 246 additions and 256 deletions

View File

@@ -13,7 +13,7 @@ public:
// Aktualizujemy FPS co 1 sekundê // Aktualizujemy FPS co 1 sekundê
if (elapsed.count() >= 1.0) { if (elapsed.count() >= 1.0) {
double fps = frameCount / elapsed.count(); double fps = frameCount / elapsed.count();
std::cout << "FPS: " << fps << "\n"; std::cout << "FPS: " << fps << std::endl;
frameCount = 0; frameCount = 0;
lastTime = currentTime; lastTime = currentTime;
} }

334
main.cpp
View File

@@ -83,12 +83,12 @@ std::time_t monitormodehelper;
//Zmienne do ruchu ##############################################^^^^^^^^^^^^^^^^^^^^^^^^^^^^ //Zmienne do ruchu ##############################################^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FPSCounter fpsCounter; FPSCounter fpsCounter;
static const int targetFPS = 144; // Celowany FPS static const int targetFPS = 144; // Celowany FPS
//Fps counter //Fps counter
static void LimitFPS(int targetFPS) {
static auto lastTime = std::chrono::high_resolution_clock::now(); void LimitFPS(int targetFPS) {
auto currentTime = std::chrono::high_resolution_clock::now(); static auto lastTime = std::chrono::steady_clock::now();
auto currentTime = std::chrono::steady_clock::now();
std::chrono::duration<double> elapsed = currentTime - lastTime; std::chrono::duration<double> elapsed = currentTime - lastTime;
// Obliczamy czas na jedną klatkę // Obliczamy czas na jedną klatkę
@@ -118,17 +118,15 @@ float MoveSpeed = 1.0f; // Prędkość poruszania się
float velocity = 0.0f; // Aktualna prędkość łazika float velocity = 0.0f; // Aktualna prędkość łazika
float rotationVelocity = 0.0f; // Prędkość obrotu łazika const float friction = 0.05f; // Współczynnik tarcia (μ)
const float maxSpeed = 3.0f; // Maksymalna prędkość łazika
const float friction = 0.1f; // Współczynnik tarcia (μ)
const float maxSpeed = 5.0f; // Maksymalna prędkość łazika
const float acceleration = 0.2f; const float acceleration = 0.2f;
float rotationVelocity = 0.0f; // Prędkość obrotu łazika
const float rotationAcceleration = 0.075f; // Przyspieszenie obrotu const float rotationAcceleration = 0.1f; // Przyspieszenie obrotu
const float rotationFriction = 0.1f; // Współczynnik tarcia obrotu const float rotationFriction = 0.05f; // Współczynnik tarcia obrotu
const float maxRotationSpeed = 0.7f; // Maksymalna prędkość obrotu const float maxRotationSpeed = 3.0f; // Maksymalna prędkość obrotu
// Struktura do reprezentacji płotu // Struktura do reprezentacji płotu
struct Plot { struct Plot {
@@ -142,7 +140,7 @@ struct Plot {
// Funkcja sprawdzająca kolizję z płotem // Funkcja sprawdzająca kolizję z płotem
// Funkcja sprawdzająca kolizję z płotem, uwzględniając wymiary łazika // Funkcja sprawdzająca kolizję z płotem, uwzględniając wymiary łazika
static bool CheckFenceCollision(float roverXMin, float roverXMax, float roverZMin, float roverZMax, const Plot& plot) { bool CheckFenceCollision(float roverXMin, float roverXMax, float roverZMin, float roverZMax, const Plot& plot) {
if (plot.mod_x == 0) { // Płot pionowy (równoległy do osi Z) if (plot.mod_x == 0) { // Płot pionowy (równoległy do osi Z)
float xMin = plot.xc - plot.gruboscY / 2.0f; float xMin = plot.xc - plot.gruboscY / 2.0f;
float xMax = plot.xc + plot.gruboscY / 2.0f; float xMax = plot.xc + plot.gruboscY / 2.0f;
@@ -171,7 +169,7 @@ static bool CheckFenceCollision(float roverXMin, float roverXMax, float roverZMi
} }
// Funkcja ogólna do sprawdzania kolizji ze wszystkimi płotami // Funkcja ogólna do sprawdzania kolizji ze wszystkimi płotami
static bool CheckAllFencesCollision(float roverXMin, float roverXMax, float roverZMin, float roverZMax, const std::vector<Plot>& fences) { bool CheckAllFencesCollision(float roverXMin, float roverXMax, float roverZMin, float roverZMax, const std::vector<Plot>& fences) {
for (const auto& fence : fences) { for (const auto& fence : fences) {
if (CheckFenceCollision(roverXMin, roverXMax, roverZMin, roverZMax, fence)) { if (CheckFenceCollision(roverXMin, roverXMax, roverZMin, roverZMax, fence)) {
return true; // Kolizja wykryta z którymś płotem return true; // Kolizja wykryta z którymś płotem
@@ -180,7 +178,7 @@ static bool CheckAllFencesCollision(float roverXMin, float roverXMax, float rove
return false; // Brak kolizji return false; // Brak kolizji
} }
static void UpdateRover(const std::vector<Plot>& fences) { void UpdateRover(const std::vector<Plot>& fences) {
// Przyspieszanie w przód // Przyspieszanie w przód
if (keyWPressed) { if (keyWPressed) {
velocity += acceleration; velocity += acceleration;
@@ -203,7 +201,7 @@ static void UpdateRover(const std::vector<Plot>& fences) {
} }
} }
// Obracanie (rotacja z driftowaniem) // Obracanie
if (keyAPressed) { if (keyAPressed) {
rotationVelocity += rotationAcceleration; rotationVelocity += rotationAcceleration;
if (rotationVelocity > maxRotationSpeed) rotationVelocity = maxRotationSpeed; if (rotationVelocity > maxRotationSpeed) rotationVelocity = maxRotationSpeed;
@@ -213,14 +211,13 @@ static void UpdateRover(const std::vector<Plot>& fences) {
if (rotationVelocity < -maxRotationSpeed) rotationVelocity = -maxRotationSpeed; if (rotationVelocity < -maxRotationSpeed) rotationVelocity = -maxRotationSpeed;
} }
else { else {
// Jeśli żadna z klawiszy A/D nie jest wciśnięta, to stopniowo spowalniamy rotację (drift) // Hamowanie obrotu (wytracanie prędkości z powodu tarcia)
float driftFactor = 0.1f; // Mniejsza wartość = dłuższy drift
if (rotationVelocity > 0) { if (rotationVelocity > 0) {
rotationVelocity -= rotationFriction * driftFactor; rotationVelocity -= rotationFriction;
if (rotationVelocity < 0) rotationVelocity = 0; if (rotationVelocity < 0) rotationVelocity = 0;
} }
else if (rotationVelocity < 0) { else if (rotationVelocity < 0) {
rotationVelocity += rotationFriction * driftFactor; rotationVelocity += rotationFriction;
if (rotationVelocity > 0) rotationVelocity = 0; if (rotationVelocity > 0) rotationVelocity = 0;
} }
} }
@@ -230,22 +227,18 @@ static void UpdateRover(const std::vector<Plot>& fences) {
float newSides = Sides - velocity * cos(radRotation); // Nowa pozycja w osi X float newSides = Sides - velocity * cos(radRotation); // Nowa pozycja w osi X
float newFoward = Foward - velocity * sin(radRotation); // Nowa pozycja w osi Z float newFoward = Foward - velocity * sin(radRotation); // Nowa pozycja w osi Z
// Wymiary łazika (połówki w osi X i Z)
const float roverHalfWidthX = 19.0f; // 38/2
const float roverHalfLengthZ = 12.0f; // 24/2
// Wyliczenie obszaru zajmowanego przez łazik // Wyliczenie obszaru zajmowanego przez łazik
float roverXMin = newSides - roverHalfWidthX; float roverXMin = newSides - 15.0f;
float roverXMax = newSides + roverHalfWidthX; float roverXMax = newSides + 15.0f;
float roverZMin = newFoward - roverHalfLengthZ; float roverZMin = newFoward - 15.0f;
float roverZMax = newFoward + roverHalfLengthZ; float roverZMax = newFoward + 15.0f;
// Sprawdzanie kolizji przed aktualizacją pozycji // Sprawdzanie kolizji przed aktualizacją pozycji
if (!Kolizja == true) { if (Kolizja == true) {
if (CheckAllFencesCollision(roverZMin, roverZMax, roverXMin, roverXMax, fences)) { if (CheckAllFencesCollision(roverXMin, roverXMax, roverZMin, roverZMax, fences)) {
// Jeśli jest kolizja, zatrzymujemy łazik // Jeśli jest kolizja, zatrzymujemy łazik
velocity = 0.0f; velocity = 0.0f;
//cout << "Kolizja podczas ruchu\n"; cout << "Kolizja podczas ruchu\n";
} }
else { else {
// Jeśli brak kolizji, aktualizujemy pozycję // Jeśli brak kolizji, aktualizujemy pozycję
@@ -255,44 +248,27 @@ static void UpdateRover(const std::vector<Plot>& fences) {
// Sprawdzanie kolizji podczas obrotu // Sprawdzanie kolizji podczas obrotu
if (rotationVelocity != 0.0f) { if (rotationVelocity != 0.0f) {
// Wyliczamy nową rotację // Wyliczamy narożniki łazika po obrocie
float newRotation = Rotation + rotationVelocity; float newRotation = Rotation + rotationVelocity;
float radNewRotation = newRotation * GL_PI / 180.0f; float radNewRotation = newRotation * GL_PI / 180.0f;
// Obracamy narożniki łazika // Wyliczamy offsety narożników w nowych współrzędnych
std::vector<std::pair<float, float>> corners(4); float offsetX = 15.0f * cos(radNewRotation);
corners[0] = {Sides - roverHalfWidthX, Foward - roverHalfLengthZ}; // Lewy dolny float offsetZ = 15.0f * sin(radNewRotation);
corners[1] = {Sides + roverHalfWidthX, Foward - roverHalfLengthZ}; // Prawy dolny
corners[2] = {Sides - roverHalfWidthX, Foward + roverHalfLengthZ}; // Lewy górny
corners[3] = {Sides + roverHalfWidthX, Foward + roverHalfLengthZ}; // Prawy górny
bool collisionDetected = false;
// Obracamy wszystkie narożniki
for (auto& corner : corners) {
float x = corner.first;
float z = corner.second;
corner.first = Sides + (x - Sides) * cos(radNewRotation) - (z - Foward) * sin(radNewRotation); // Wyznaczamy nowe granice łazika
corner.second = Foward + (x - Sides) * sin(radNewRotation) + (z - Foward) * cos(radNewRotation); float rotatedXMin = Sides - offsetX;
float rotatedXMax = Sides + offsetX;
float rotatedZMin = Foward - offsetZ;
float rotatedZMax = Foward + offsetZ;
// Sprawdzamy kolizję dla granic po obrocie
// Sprawdzamy kolizję na podstawie obróconych narożników if (CheckAllFencesCollision(rotatedXMin, rotatedXMax, rotatedZMin, rotatedZMax, fences)) {
rotationVelocity = 0.0f; // Zatrzymujemy obrót w przypadku kolizji
cout << "Kolizja podczas obrotu\n";
if (CheckAllFencesCollision(corner.first, corner.first, corner.second, corner.second, fences)) {
collisionDetected = true;
break;
} }
if (CheckAllFencesCollision( corner.second, corner.second, corner.first, corner.first, fences)) { else {
collisionDetected = true; // Aktualizujemy rotację tylko, jeśli nie ma kolizji
break;
}
}
if (collisionDetected) {
rotationVelocity = 0.0f; // Zatrzymujemy obrót
//cout << "Kolizja podczas obrotu\n";
} else {
// Aktualizujemy rotację, jeśli nie ma kolizji
Rotation = newRotation; Rotation = newRotation;
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;
@@ -310,19 +286,15 @@ static void UpdateRover(const std::vector<Plot>& fences) {
} }
std::vector<Plot> fences = { std::vector<Plot> fences = {
{-550.0f, 3.0f, 50.0f, 1310.0f, 4.0f, 0}, // 0 - pionowo {-10.0f, 3.0f, 45.0f, 130.0f, 4.0f, 0},
{ 50.0f, 3.0f, -600.0f, 1200.0f, 4.0f, 1}, // 1 - poziomo { 50.0f, 3.0f, -20.0f, 120.0f, 4.0f, 1},
{ 650.0f, 3.0f, 50.0f, 1310.0f, 4.0f, 0}, // 0 - pionowo {110.0f, 3.0f, 45.0f, 130.0f, 4.0f, 0},
{ 50.0f, 3.0f, 695.0f, 1200.0f, 4.0f, 1} // 1 - poziomo { 50.0f, 3.0f, 110.0f, 120.0f, 4.0f, 1}
}; };
// Change viewing volume and viewport. Called when window is resized // Change viewing volume and viewport. Called when window is resized
void static ChangeSize(GLsizei w, GLsizei h) { void ChangeSize(GLsizei w, GLsizei h) {
GLfloat nRange = 100.0f; GLfloat nRange = 100.0f;
//GLfloat fAspect; //GLfloat fAspect;
// Prevent a divide by zero // Prevent a divide by zero
@@ -360,7 +332,7 @@ void static ChangeSize(GLsizei w, GLsizei h) {
// opis: ładuje mapę bitową z pliku i zwraca jej adres. // opis: ładuje mapę bitową z pliku i zwraca jej adres.
// Wypełnia strukturę nagłówka. // Wypełnia strukturę nagłówka.
// Nie obsługuje map 8-bitowych. // Nie obsługuje map 8-bitowych.
static unsigned char* LoadBitmapFile(char* filename, BITMAPINFOHEADER* bitmapInfoHeader) { unsigned char* LoadBitmapFile(char* filename, BITMAPINFOHEADER* bitmapInfoHeader) {
FILE* filePtr; // wskaźnik pozycji pliku FILE* filePtr; // wskaźnik pozycji pliku
BITMAPFILEHEADER bitmapFileHeader; // nagłówek pliku BITMAPFILEHEADER bitmapFileHeader; // nagłówek pliku
unsigned char* bitmapImage; // dane obrazu unsigned char* bitmapImage; // dane obrazu
@@ -445,7 +417,7 @@ void SetDCPixelFormat(HDC hDC) {
SetPixelFormat(hDC, nPixelFormat, &pfd); SetPixelFormat(hDC, nPixelFormat, &pfd);
} }
static void skrzynka(GLfloat k) { void skrzynka(GLfloat k) {
glColor3d(0.8, 0.7, 0.3); glColor3d(0.8, 0.7, 0.3);
@@ -498,29 +470,24 @@ static void skrzynka(GLfloat k) {
glEnd(); glEnd();
} }
static void platforma(GLfloat xc, GLfloat yc, GLfloat zc, GLfloat xlen, GLfloat zlen) { void platforma(GLfloat xc, GLfloat yc, GLfloat zc, GLfloat xlen, GLfloat zlen) {
glColor3d(0.729, 0.91, 0.51); // jasnozielony, dla grass02.bmp
glColor3d(0.729, 0.91, 0.51); // jasnozielony, dla grass02.bmp (https://rgbcolorpicker.com/0-1)
glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture[1]); // Wybieramy teksturę
// Ustawienie powtarzania tekstury
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // Powtarzanie w kierunku S
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); // Powtarzanie w kierunku T
glBindTexture(GL_TEXTURE_2D, texture[1]);
glBegin(GL_QUADS); glBegin(GL_QUADS);
// Powtarzające się współrzędne tekstury (np. *5, aby powtórzyła się 5 razy) glTexCoord2d(1.0, 1.0); glVertex3d(xc - xlen, yc, zc - zlen);
glTexCoord2d(5.0, 5.0); glVertex3d(xc - xlen, yc, zc - zlen); // Lewy dolny glTexCoord2d(1.0, 0.0); glVertex3d(xc + xlen, yc, zc - zlen);
glTexCoord2d(5.0, 0.0); glVertex3d(xc + xlen, yc, zc - zlen); // Prawy dolny glTexCoord2d(0.0, 0.0); glVertex3d(xc + xlen, yc, zc + zlen);
glTexCoord2d(0.0, 0.0); glVertex3d(xc + xlen, yc, zc + zlen); // Prawy górny glTexCoord2d(0.0, 1.0); glVertex3d(xc - xlen, yc, zc + zlen);
glTexCoord2d(0.0, 5.0); glVertex3d(xc - xlen, yc, zc + zlen); // Lewy górny
glEnd(); glEnd();
glDisable(GL_TEXTURE_2D); glDisable(GL_TEXTURE_2D);
} }
void stodola(GLfloat xc, GLfloat yc, GLfloat zc, GLfloat krawedz) {
static void stodola(GLfloat xc, GLfloat yc, GLfloat zc, GLfloat krawedz) {
glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D);
@@ -583,8 +550,7 @@ static void stodola(GLfloat xc, GLfloat yc, GLfloat zc, GLfloat krawedz) {
} }
static void plot(GLfloat xc, GLfloat yc, GLfloat zc, GLfloat length, GLfloat gruboscY, bool mod_x) void plot(GLfloat xc, GLfloat yc, GLfloat zc, GLfloat length, GLfloat gruboscY, bool mod_x) {
{
GLfloat grubosc = 1.0f; GLfloat grubosc = 1.0f;
@@ -806,21 +772,18 @@ static void plot(GLfloat xc, GLfloat yc, GLfloat zc, GLfloat length, GLfloat gru
lazik user(10.0f, 0.0f, 0.0f, "res/models/lazik4.obj"); // obiekty eksportujemy z Forward Axis Z, Up Axis Y. lazik user(10.0f, 0.0f, 0.0f, "res/models/lazik4.obj"); // obiekty eksportujemy z Forward Axis Z, Up Axis Y.
plane mapa( 0.0f, 0.0f, 0.0f, "res/models/mapka3_nofence_noplatform.obj"); plane mapa( 0.0f, 0.0f, 0.0f, "res/models/mapka3_nofence_noplatform.obj");
static void SetupRC() { void SetupRC() {
// Light values and coordinates // Light values and coordinates
GLfloat ambientLight[] = { 0.3f, 0.3f, 0.3f, 1.0f}; GLfloat ambientLight[] = { 0.3f, 0.3f, 0.3f, 1.0f};
GLfloat diffuseLight[] = { 0.7f, 0.7f, 0.7f, 1.0f}; GLfloat diffuseLight[] = { 0.7f, 0.7f, 0.7f, 1.0f};
GLfloat specular[] = { 1.0f, 1.0f, 1.0f, 1.0f}; GLfloat specular[] = { 1.0f, 1.0f, 1.0f, 1.0f};
GLfloat lightPos[] = {50.0f, -100.0f, 50.0f, 1.0f};
GLfloat specref[] = { 1.0f, 1.0f, 1.0f, 1.0f}; GLfloat specref[] = { 1.0f, 1.0f, 1.0f, 1.0f};
// Multiple light positions (for light coming from all sides)
GLfloat lightPos1[] = { 50.0f, -100.0f, 50.0f, 1.0f }; // Light 0 position
GLfloat lightPos2[] = { -50.0f, -100.0f, 50.0f, 1.0f }; // Light 1 position
GLfloat lightPos3[] = { 50.0f, -100.0f, -50.0f, 1.0f }; // Light 2 position
GLfloat lightPos4[] = { -50.0f, -100.0f, -50.0f, 1.0f }; // Light 3 position
glEnable(GL_DEPTH_TEST); // Hidden surface removal glEnable(GL_DEPTH_TEST); // Hidden surface removal
glFrontFace(GL_CCW); // Counter clockwise polygons face out glFrontFace(GL_CCW); // Counter clock-wise polygons face out
//glEnable(GL_CULL_FACE); // Do not calculate inside of jet // !!! znacząco poprawia wydajność
glDepthFunc(GL_LESS); glDepthFunc(GL_LESS);
// Enable lighting // Enable lighting
@@ -830,30 +793,9 @@ static void SetupRC() {
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight); glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight); glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, specular); glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos1); glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
glEnable(GL_LIGHT0); glEnable(GL_LIGHT0);
// Setup and enable light 1
glLightfv(GL_LIGHT1, GL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT1, GL_SPECULAR, specular);
glLightfv(GL_LIGHT1, GL_POSITION, lightPos2);
glEnable(GL_LIGHT1);
// Setup and enable light 2
glLightfv(GL_LIGHT2, GL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT2, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT2, GL_SPECULAR, specular);
glLightfv(GL_LIGHT2, GL_POSITION, lightPos3);
glEnable(GL_LIGHT2);
// Setup and enable light 3
glLightfv(GL_LIGHT3, GL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT3, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT3, GL_SPECULAR, specular);
glLightfv(GL_LIGHT3, GL_POSITION, lightPos4);
glEnable(GL_LIGHT3);
// Enable color tracking // Enable color tracking
glEnable(GL_COLOR_MATERIAL); glEnable(GL_COLOR_MATERIAL);
@@ -862,12 +804,14 @@ static void SetupRC() {
// All materials hereafter have full specular reflectivity // All materials hereafter have full specular reflectivity
// with a high shine // with a high shine
GLfloat specref2[] = {0.2f, 0.2f, 0.2f, 0.2f}; glMaterialfv(GL_FRONT, GL_SPECULAR, specref);
glMaterialfv(GL_FRONT, GL_SPECULAR, specref2);
glMateriali(GL_FRONT, GL_SHININESS, 128); glMateriali(GL_FRONT, GL_SHININESS, 128);
// White background // White background
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
// Black brush
//glColor3f(0.0, 0.0, 0.0);
// Initialize GLEW // Initialize GLEW
timestampedCout("Inicjalizowanie GLEW..."); timestampedCout("Inicjalizowanie GLEW...");
@@ -879,96 +823,143 @@ static void SetupRC() {
timestampedCout("Zainicjalizowano GLEW."); timestampedCout("Zainicjalizowano GLEW.");
// Initialize GLFW3 // 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..."); timestampedCout("Inicjalizowanie GLFW3...");
if (!glfwInit()) { if (!glfwInit()) {
timestampedCout("Failed to initialize GLFW"); timestampedCout("Failed to initialize GLFW");
} }
timestampedCout("Zainicjalizowano GLFW3."); timestampedCout("Zainicjalizowano GLFW3.");
// Load models // glGenVertexArrays(1, &VertexArrayID);
// glBindVertexArray(VertexArrayID);
// Załaduj shadery
// programID = LoadShaders("res/shaders/TransformVertexShader.vertexshader", "res/shaders/TextureFragmentShader.fragmentshader");
// MatrixID = glGetUniformLocation(programID, "MVP");
// user.passProgramID(programID);
// Załaduj model z pliku .obj
timestampedCout("Ladowanie modelu lazika..."); timestampedCout("Ladowanie modelu lazika...");
user.loadModel(); user.loadModel();
timestampedCout("Ladowanie modelu mapki..."); timestampedCout("Ladowanie modelu mapki...");
mapa.loadModel(); mapa.loadModel();
glfwSwapInterval(1); glfwSwapInterval(1);
//glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
} }
void RenderScene(void) {
void static RenderScene(void) {
// Ustawienie liczby próbek dla antyaliasingu
glfwWindowHint(GLFW_SAMPLES, 16); // 4x MSAA (Wielokrotne próbkowanie)
// Włączenie antyaliasingu (MSAA) //float normal[3]; // Storage for calculated surface normal
glEnable(GL_MULTISAMPLE);
// Przywrócenie macierzy modelu i ustawienie obrotów // Save the matrix state and do the rotations
glPushMatrix(); glPushMatrix();
glRotatef(xRot, 1.0f, 0.0f, 0.0f); glRotatef(xRot, 1.0f, 0.0f, 0.0f);
glRotatef(yRot, 0.0f, 1.0f, 0.0f); glRotatef(yRot, 0.0f, 1.0f, 0.0f);
glRotatef(zRot, 0.0f, 0.0f, 1.0f); glRotatef(zRot, 0.0f, 0.0f, 1.0f);
// Ustawienie trybu rysowania wielokątów // gluLookAt(
// 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
// );
switch (polygonmode) { switch (polygonmode) {
case 1: case 1:
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // Rysowanie linii glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
break; break;
default: default:
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); // Wypełnianie poligonów glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
} }
// Czyszczenie ekranu przed rysowaniem
// Clear the screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Przywrócenie macierzy widoku // Use our shader
//timestampedCout("programID wynosi " << programID);
//glUseProgram(programID);
// // Bind our texture in Texture Unit 0
// glActiveTexture(GL_TEXTURE0);
// glBindTexture(GL_TEXTURE_2D, Texture);
// // Set our "myTextureSampler" sampler to use Texture Unit 0
// glUniform1i(TextureID, 0);
//glLoadIdentity(); // Zresetowanie macierzy widoku
//
//############################################################################################
//Sterowanie
//############################################################################################
gluLookAt( gluLookAt(
Foward - 50.0f * sin((Rotation + 180.0f) * GL_PI / 180.0f), // Pozycja kamery Foward - 100.0f * sin((Rotation + 180.0f) * GL_PI / 180.0f), // Pozycja kamery wokół łazika (w poziomie)
CameraHeight/4, // 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 patrzenia (ł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"
); );
// Rysowanie mapy // Rysowanie mapy (nie porusza się)
glPushMatrix(); glPushMatrix();
glColor3f(0.0, 1.0, 0.0); // Zielony kolor glColor3f(0.0, 1.0, 0.0); // Zielony kolor dla mapy
mapa.draw(); platforma(50.0f, 0.0f, 45.0f, 60.0f, 65.0f);
platforma(50.0f, 0.0f, 45.0f, 600.0f, 650.0f); //mapa.draw();
glPopMatrix(); glPopMatrix();
// Rysowanie łazika // Rysowanie łazika (porusza się i obraca)
glPushMatrix(); glPushMatrix();
glTranslatef(Foward, 0.0f, Sides); // Translacja łazika
glRotatef(Rotation, 0.0f, 1.0f, 0.0f); // Obrót łazika
glColor3f(1.0, 0.0, 0.0); // Czerwony kolor dla łazika
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
fpsCounter.update();
user.draw(); user.draw();
UpdateRover(fences); UpdateRover(fences);
fpsCounter.update();
glPopMatrix(); glPopMatrix();
// Rysowanie innych obiektów //skrzynka(50);
plot(-550.0f, 3.0f, 50.0f, 1310.0f, 4.0f, 0); // 0 - pionowo plot(-10.0f, 3.0f, 45.0f, 130.0f, 4.0f, 0); // 0 - pionowo
plot( 50.0f, 3.0f, -600.0f, 1200.0f, 4.0f, 1); // 1 - poziomo plot( 50.0f, 3.0f, -20.0f, 120.0f, 4.0f, 1); // 1 - poziomo
plot( 650.0f, 3.0f, 50.0f, 1310.0f, 4.0f, 0); plot(110.0f, 3.0f, 45.0f, 130.0f, 4.0f, 0);
plot( 50.0f, 3.0f, 695.0f, 1200.0f, 4.0f, 1); plot( 50.0f, 3.0f, 110.0f, 120.0f, 4.0f, 1);
// stodola(2.0f, 0.0f, -7.5f, 20.0f);
stodola(10.0f, 0.0f, 2.0f, 40.0f); stodola(10.0f, 0.0f, 2.0f, 40.0f);
// Zamiana buforów (double buffering) // Zamiana buforów (double buffering)
// glfwSwapBuffers(window); // Przełączenie buforów //glColor3f(1.0, 0.0, 0.0);
// glfwPollEvents(); // Obsługa zdarzeń //user.draw();
//glColor3f(0.0, 1.0, 0.0);
//mapa.draw();
//glColor3f(0.0, 0.0, 0.0);
glPopMatrix(); // Przywrócenie poprzedniej macierzy
glMatrixMode(GL_MODELVIEW); // Ustawienie trybu modelu-widoku
// Wymuszenie wykonania wszystkich rysunków // Zamiana buforów (double buffering)
glFlush();
// Swap buffers
//glfwSwapBuffers(window);
//glfwPollEvents();
//Wyrysowanie prostokata:
//glRectd(-10.0,-10.0,20.0,20.0);
glPopMatrix(); // wymagane
glMatrixMode(GL_MODELVIEW); // zmniejsza zużycie GPU
// Flush drawing commands
//glFlush();
} }
// If necessary, creates a 3-3-2 palette for the device context listed. // If necessary, creates a 3-3-2 palette for the device context listed.
HPALETTE static GetOpenGLPalette(HDC hDC) { HPALETTE GetOpenGLPalette(HDC hDC) {
HPALETTE hRetPal = NULL; // Handle to palette to be created HPALETTE hRetPal = NULL; // Handle to palette to be created
PIXELFORMATDESCRIPTOR pfd; // Pixel Format Descriptor PIXELFORMATDESCRIPTOR pfd; // Pixel Format Descriptor
LOGPALETTE* pPal; // Pointer to memory for logical palette LOGPALETTE* pPal; // Pointer to memory for logical palette
@@ -1038,7 +1029,7 @@ HPALETTE static GetOpenGLPalette(HDC hDC) {
// Return the handle to the new palette // Return the handle to the new palette
return hRetPal; return hRetPal;
} }
void static CreateConsole() { void CreateConsole() {
// Tworzenie nowej konsoli // Tworzenie nowej konsoli
if (AllocConsole()) { if (AllocConsole()) {
// Przekierowanie standardowych strumieni do konsoli // Przekierowanie standardowych strumieni do konsoli
@@ -1056,7 +1047,7 @@ void static CreateConsole() {
int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
CreateConsole(); 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
@@ -1150,8 +1141,8 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
glBindTexture(GL_TEXTURE_2D, texture[0]); // aktywuje obiekt tekstury glBindTexture(GL_TEXTURE_2D, texture[0]); // aktywuje obiekt tekstury
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); // 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_WRAP_T, GL_CLAMP);
@@ -1168,8 +1159,8 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
bitmapData = LoadBitmapFile((char*)"res/img/grass02.bmp", &bitmapInfoHeader); bitmapData = LoadBitmapFile((char*)"res/img/grass02.bmp", &bitmapInfoHeader);
glBindTexture(GL_TEXTURE_2D, texture[1]); // aktywuje obiekt tekstury glBindTexture(GL_TEXTURE_2D, texture[1]); // aktywuje obiekt tekstury
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
@@ -1184,8 +1175,8 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
bitmapData = LoadBitmapFile((char*)"res/img/barnroof.bmp", &bitmapInfoHeader); bitmapData = LoadBitmapFile((char*)"res/img/barnroof.bmp", &bitmapInfoHeader);
glBindTexture(GL_TEXTURE_2D, texture[2]); // aktywuje obiekt tekstury glBindTexture(GL_TEXTURE_2D, texture[2]); // aktywuje obiekt tekstury
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); 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_WRAP_T, GL_CLAMP);
@@ -1200,8 +1191,8 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
bitmapData = LoadBitmapFile((char*)"res/img/brickwall.bmp", &bitmapInfoHeader); bitmapData = LoadBitmapFile((char*)"res/img/brickwall.bmp", &bitmapInfoHeader);
glBindTexture(GL_TEXTURE_2D, texture[3]); // aktywuje obiekt tekstury glBindTexture(GL_TEXTURE_2D, texture[3]); // aktywuje obiekt tekstury
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); 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_WRAP_T, GL_CLAMP);
@@ -1247,7 +1238,6 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
// The painting function. This message sent by Windows // The painting function. This message sent by Windows
// whenever the screen needs updating. // whenever the screen needs updating.
case WM_PAINT: case WM_PAINT:
LimitFPS(targetFPS);
// Call OpenGL drawing code // Call OpenGL drawing code
RenderScene(); RenderScene();
@@ -1260,7 +1250,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
// Limit FPS // Limit FPS
// Uaktualniaj FPS // Uaktualniaj FPS
// Ogranicz FPS LimitFPS(targetFPS); // Ogranicz FPS