improve camera movement some, experiment with rendering multiple objects (look up), some small refactoring.
This commit is contained in:
2023-11-19 08:22:18 -05:00
parent 25bc4ee2b7
commit 4ab73a137f
7 changed files with 109 additions and 14 deletions

View File

@@ -4,10 +4,12 @@
#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <glm/glm.hpp>
#include <FreeImage.h>
enum class gamestate: uint8_t {
enum class GAMESTATE: uint8_t {
NORMAL = 0, //Gameplay.
IN_MAIN_MENU = 1,
IN_PAUSE_MENU = 2,
@@ -18,7 +20,7 @@ enum class gamestate: uint8_t {
class Engine {
public:
gamestate gameState = gamestate::NULL_;
GAMESTATE gameState = GAMESTATE::NULL_;
bool debug = true;
bool takingScreenshot = false; //Because
SDL_Window *window = nullptr;

View File

@@ -3,6 +3,7 @@
#include <thread>
#include "engine.h"
#include "../types/camera.h"
#include "../types/skybox.h"
#include "../types/entityList.h"
@@ -44,16 +45,23 @@ void pre_render() {
auto camera = new(Camera);
entityList.storeEntity(camera);
entityList.getCamera()->position.set(0.0f,0.0f,5.0f);
auto skybox = new(Skybox);
entityList.storeEntity(skybox);
}
process_sdl_events();
engine->frameCount++;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
entityList.getCamera()->render();
entityList.getSkybox()->position.x = entityList.getCamera()->position.x;
entityList.getSkybox()->position.z = entityList.getCamera()->position.z;
entityList.getSkybox()->position.y = entityList.getCamera()->position.y;
}
void render() {
entityList.getSkybox()->render();
entityList.getCamera()->render();
glTranslatef(0.0f, 0.0f, 0.0f);
glPushMatrix();
glBegin(GL_QUADS);
glColor3f(1,0.5,1);
// Front face
@@ -92,7 +100,6 @@ void render() {
glVertex3f(0.5f, -0.5f, 0.5f);
glVertex3f(-0.5f, -0.5f, 0.5f);
glEnd();
glPopMatrix();
glPushMatrix();

View File

@@ -11,15 +11,15 @@
engine->tickCount++;
switch (engine->gameState) {
case gamestate::NORMAL:
case GAMESTATE::NORMAL:
break;
case gamestate::IN_MAIN_MENU:
case GAMESTATE::IN_MAIN_MENU:
break;
case gamestate::IN_PAUSE_MENU:
case GAMESTATE::IN_PAUSE_MENU:
break;
case gamestate::IN_LEVEL_ANIMATION:
case GAMESTATE::IN_LEVEL_ANIMATION:
break;
case gamestate::IN_QUIT:
case GAMESTATE::IN_QUIT:
engine->quit();
break;
}

View File

@@ -2,7 +2,7 @@
#include "moby.h"
#include "../engine/engine.h"
enum class cameramode: uint8_t {
enum class CAMERAMODE: uint8_t {
FREECAM = 0,
FOLLOW_PLAYER = 1
};
@@ -15,6 +15,32 @@ public:
position.z += offset.z;
}
void moveForward(float speed) {
vector3 forwardDirection(
cos(glm::radians(this->angles.y)) * cos(glm::radians(this->angles.x)),
sin(glm::radians(this->angles.x)),
sin(glm::radians(this->angles.y)) * cos(glm::radians(this->angles.x))
);
//forwardDirection.normalize();
//My axis are flipped????
this->position.z += speed * -forwardDirection.x;
this->position.x += speed * -forwardDirection.z;
this->position.y += speed * forwardDirection.y;
}
void moveBack(float speed) {
vector3 forwardDirection(
cos(glm::radians(this->angles.y)) * cos(glm::radians(this->angles.x)),
sin(glm::radians(this->angles.x)),
sin(glm::radians(this->angles.y)) * cos(glm::radians(this->angles.x))
);
//forwardDirection.normalize();
//My axis are flipped????
this->position.z += speed * forwardDirection.x;
this->position.x += speed * forwardDirection.z;
this->position.y += speed * forwardDirection.y;
}
void move(float x, float y, float z) {
position.x += x;
position.y += y;
@@ -27,10 +53,11 @@ public:
//TODO: Make the direction that is considered "forward" not a set axis direction
//But instead determined by the yaw.
if (keyState[SDL_SCANCODE_S] == 1) {
this->position.z = this->position.z+0.1f;
moveBack(0.125);
}
if (keyState[SDL_SCANCODE_W] == 1) {
this->position.z = this->position.z-0.1f;
//this->position.z = this->position.z-0.1f;
moveForward(0.125);
}
if (keyState[SDL_SCANCODE_A] == 1) {
this->position.x = this->position.x-0.025f;;

View File

@@ -3,6 +3,7 @@
#include "entity.h"
#include "player.h"
#include "camera.h"
#include "skybox.h"
class entitylist {
public:
@@ -20,6 +21,12 @@ public:
return dynamic_cast<Camera *>(e);
}
inline Skybox* getSkybox() {
for (auto& e : this->list)
if (auto* p = dynamic_cast<Skybox*>(e) )
return dynamic_cast<Skybox *>(e);
}
inline void storeEntity(Entity* e) {
this->list.push_back(e);
}

View File

@@ -3,7 +3,8 @@
#include <SDL2/SDL_opengl.h>
#include "moby.h"
#include "geometry.h"
class skybox : public Moby {
#include "entityList.h"
class Skybox : public Moby {
public:
const Geometry geometry = {
{0.000f, 1.000f, 0.000f,
@@ -43,6 +44,48 @@ public:
void render() {
//TODO: Teleport the sphere such that the center of the sphere is the cameras position every frame.
glPushMatrix();
glColor3f(1,0,0);
glTranslatef(position.x,position.y+10,position.z);
glBegin(GL_QUADS);
glColor3f(1,0.5,1);
// Front face
glVertex3f(-0.5f, -0.5f, 0.5f);
glVertex3f(0.5f, -0.5f, 0.5f);
glVertex3f(0.5f, 0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, 0.5f);
// Back face
glVertex3f(-0.5f, -0.5f, -0.5f);
glVertex3f(0.5f, -0.5f, -0.5f);
glVertex3f(0.5f, 0.5f, -0.5f);
glVertex3f(-0.5f, 0.5f, -0.5f);
// Left face
glVertex3f(-0.5f, -0.5f, -0.5f);
glVertex3f(-0.5f, -0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, -0.5f);
// Right face
glVertex3f(0.5f, -0.5f, -0.5f);
glVertex3f(0.5f, -0.5f, 0.5f);
glVertex3f(0.5f, 0.5f, 0.5f);
glVertex3f(0.5f, 0.5f, -0.5f);
// Top face
glVertex3f(-0.5f, 0.5f, -0.5f);
glVertex3f(0.5f, 0.5f, -0.5f);
glVertex3f(0.5f, 0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, 0.5f);
// Bottom face
glVertex3f(-0.5f, -0.5f, -0.5f);
glVertex3f(0.5f, -0.5f, -0.5f);
glVertex3f(0.5f, -0.5f, 0.5f);
glVertex3f(-0.5f, -0.5f, 0.5f);
glEnd();
glPopMatrix();
}
};

View File

@@ -8,6 +8,15 @@ public:
float x = 0; //pitch
float y = 0; //yaw
float z = 0; //roll
void normalize() {
float length = sqrt(x * x + y * y + z * z);
if (length != 0.0f) {
x /= length;
y /= length;
z /= length;
}
}
};
class angle : public vector3 {