This commit is contained in:
2023-11-21 07:58:09 -05:00
parent a125a20c8e
commit 06f731e91e
12 changed files with 1595 additions and 126 deletions

View File

@@ -16,7 +16,8 @@ add_executable(SDL3D src/main.cpp
src/types/vertex.h
src/types/skybox.h
src/types/geometry.h
src/types/moby.h)
src/types/moby.h
)
find_package(OpenGL)
target_link_libraries(SDL3D SDL2 freeimage ${OPENGL_LIBRARIES})

24
cube.obj Normal file
View File

@@ -0,0 +1,24 @@
# Blender 3.6.4
# www.blender.org
o Cube
v 1.000000 1.000000 -1.000000
v 1.000000 -1.000000 -1.000000
v 1.000000 1.000000 1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 1.000000 -1.000000
v -1.000000 -1.000000 -1.000000
v -1.000000 1.000000 1.000000
v -1.000000 -1.000000 1.000000
s 0
f 5 3 1
f 3 8 4
f 7 6 8
f 2 8 6
f 1 4 2
f 5 2 6
f 5 7 3
f 3 7 8
f 7 5 6
f 2 4 8
f 1 3 4
f 5 1 2

4
run.sh
View File

@@ -1,2 +1,2 @@
sleep 0.5
vblank_mode=0 mangohud --dlsym /home/william/Documents/GitHub/SDL3D/cmake-build-debug/SDL3D
sleep 0.2
vblank_mode=0 mangohud --dlsym ./cmake-build-debug/SDL3D

1446
smallsphere.obj Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -36,15 +36,15 @@ public:
float nearPlane = 0.01f;
float farPlane = 100.0f;
float fov = 72.5f;
Uint8* keyState = nullptr;
SDL_Event event;
Uint8* keyState;
[[nodiscard]] float framerate() const {
return 1.f / this->frameDelta;
}
//Prevent taking a ton of screenshots really fast.
void takeScreenshot() const {
//Prevent taking a ton of screenshots really fast.
std::string fName = std::to_string(time(nullptr)) + ".bmp";
std::ifstream f (fName, std::ifstream::in);
if(f.good()) {

View File

@@ -60,6 +60,7 @@ void render() {
//*Always* render the camera first.
getCamera()->render();
getSkybox()->render();
glPushMatrix();
glBegin(GL_QUADS);
glColor3f(1,0.5,1);

View File

@@ -45,21 +45,21 @@ public:
}
if (engine->keyState[SDL_SCANCODE_LEFT] == 1) {
this->angles.y +=75.0f*engine->frameDelta;
this->angle.y +=75.0f*engine->frameDelta;
}
if (engine->keyState[SDL_SCANCODE_RIGHT] == 1) {
this->angles.y -=75.0f*engine->frameDelta;
this->angle.y -=75.0f*engine->frameDelta;
}
if (engine->keyState[SDL_SCANCODE_UP] == 1) {
this->angles.x +=75.0f*engine->frameDelta;
this->angle.x +=75.0f*engine->frameDelta;
}
if (engine->keyState[SDL_SCANCODE_DOWN] == 1) {
this->angles.x -=75.0f*engine->frameDelta;
this->angle.x -=75.0f*engine->frameDelta;
}
}
this->angles.clamp();
glRotatef(-angles.x,1.0f, 0.0f, 0.0f);
glRotatef(-angles.y,0.0f, 1.0f, 0.0f);
this->angle.clampToViewAngle();
glRotatef(-angle.x,1.0f, 0.0f, 0.0f);
glRotatef(-angle.y,0.0f, 1.0f, 0.0f);
glTranslatef(0.0f, 0.0f, 0.0f);
gluLookAt(position.x, position.y, position.z, // camera position
@@ -69,7 +69,7 @@ public:
if (engine->debug && engine->tickCount %64 == 0) {
std::cout << "Camera:" << std::endl;
std::cout << "X: " << position.x << " Y: " << position.y << " Z: " << position.z << std::endl;
std::cout << "Pitch: " << angles.x << " Yaw: " << angles.y << " Roll: " << angles.z << std::endl;
std::cout << "Pitch: " << angle.x << " Yaw: " << angle.y << " Roll: " << angle.z << std::endl;
}
}
@@ -84,5 +84,7 @@ public:
inline Camera* getCamera() {
for (auto& e : entityList)
if (auto* c = dynamic_cast<Camera*>(e))
return dynamic_cast<Camera *>(e);
return c;
std::cerr << "Attempt to get Camera pointer while not in scene." << std::endl;
engine->quit();
}

View File

@@ -5,47 +5,47 @@
class Moby : public Entity {
public:
float velocity = 0;
angle angles = {0,0,0}; //Pitch Yaw Roll, The orientation of the entity in the world,
angle velAngles = {0,0,0}; //The angle of an entities velocity.
Angle angle = {0,0,0}; //Pitch Yaw Roll, The orientation of the entity in the world,
//Angle velAngles = {0,0,0}; //The angle of an entities velocity.
vector3 upVector = {0.0f,1.0f,0.0f};
void move(angle a, float speed) {
void move(Angle a, float speed) {
this->position.z += (speed*engine->frameDelta) * a.x;
this->position.y += (speed*engine->frameDelta) * a.y;
this->position.x += (speed*engine->frameDelta) * a.z;
}
//forward angle
angle fAngle() {
angle a;
a.x = -(cos(glm::radians(this->angles.y)) * cos(glm::radians(this->angles.x)));
a.y = sin(glm::radians(this->angles.x));
a.z = -(sin(glm::radians(this->angles.y)) * cos(glm::radians(this->angles.x)));
Angle fAngle() {
Angle a;
a.x = -(cos(glm::radians(this->angle.y)) * cos(glm::radians(this->angle.x)));
a.y = sin(glm::radians(this->angle.x));
a.z = -(sin(glm::radians(this->angle.y)) * cos(glm::radians(this->angle.x)));
return a;
}
//back angle
angle bAngle() {
angle a;
a.x = cos(glm::radians(this->angles.y)) * cos(glm::radians(this->angles.x));
a.y = -(sin(glm::radians(this->angles.x)));
a.z = sin(glm::radians(this->angles.y)) * cos(glm::radians(this->angles.x));
Angle bAngle() {
Angle a;
a.x = cos(glm::radians(this->angle.y)) * cos(glm::radians(this->angle.x));
a.y = -(sin(glm::radians(this->angle.x)));
a.z = sin(glm::radians(this->angle.y)) * cos(glm::radians(this->angle.x));
return a;
}
//left angle
angle lAngle() {
angle f = fAngle();
angle a;
Angle lAngle() {
Angle f = fAngle();
Angle a;
a.x = f.y * upVector.z - f.z * upVector.y;
a.y = f.z * upVector.x - f.x * upVector.z;
a.z = f.x * upVector.y - f.y * upVector.x;
return a;
}
angle rAngle() {
angle f = fAngle();
angle a;
Angle rAngle() {
Angle f = fAngle();
Angle a;
a.x = -(f.y * upVector.z - f.z * upVector.y);
a.y = (f.z * upVector.x - f.x * upVector.z);
a.z = -(f.x * upVector.y - f.y * upVector.x);

View File

@@ -1,6 +1,10 @@
#pragma once
#include <iostream>
#include "moby.h"
#include "entityList.h"
#include "../engine/engine.h"
class Player : public Moby {
public:
bool alive;
@@ -23,5 +27,7 @@ public:
inline Player* getPlayer() {
for (auto& e : entityList)
if (auto* p = dynamic_cast<Player*>(e) )
return dynamic_cast<Player *>(e);
return p;
std::cerr << "Attempt to get Player pointer while not in scene." << std::endl;
engine->quit();
}

View File

@@ -5,88 +5,22 @@
#include "entityList.h"
#include "moby.h"
#include "camera.h"
#include "vertex.h"
class Skybox : public Entity {
public:
const Geometry geometry = {
{0.000f, 1.000f, 0.000f,
0.894f, 0.2235f, 0.000f,
0.276f, 0.2235f, -0.4255f,
-0.724f, 0.2235f, -0.263f,
-0.724f, 0.2235f, 0.263f,
0.276f, 0.2235f, 0.4255f,
0.724f, -0.2235f, -0.263f,
-0.276f, -0.2235f, -0.4255f,
-0.894f, -0.2235f, 0.000f,
-0.276f, -0.2235f, 0.4255f,
0.724f, -0.2235f, 0.263f,
0.000f, -1.000f, 0.000f},
{0, 1, 2,
0, 2, 3,
0, 3, 4,
0, 4, 5,
0, 5, 1,
1, 6, 2,
2, 7, 3,
3, 8, 4,
4, 9, 5,
5, 10, 1,
11, 6, 1,
11, 7, 2,
11, 8, 3,
11, 9, 4,
11, 10, 5,
6, 7, 2,
7, 8, 3,
8, 9, 4,
9, 10, 5,
10, 6, 1}
};
//VertexArray geometry = {{{1.0,1.0,-1.0},{1.0,-1.0,-1.0},{1.0,1.0,1.0},{1.0,-1.0,1.0},{-1.0,1.0,-1.0},{-1.0,-1.0,-1.0},{-1.0,1.0,1.0},{-1.0,-1.0,1.0}},
//{1,5,7,3,4,3,7,8,8,7,5,6,6,2,4,8,2,1,3,4,6,5,1,2}};
VertexArray geometry;
void render() {
//TODO: Teleport the sphere such that the center of the sphere is the cameras position every frame.
if (engine->frameCount == 1)
geometry.load("../cube.obj");
glPushMatrix();
glColor3f(1,0,0);
this->position.set(getCamera()->position);
glTranslatef(position.x,position.y+2,position.z);
glBegin(GL_QUADS);
glColor3f(1,0,0);
// 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();
glColor3f(0.75,0.75,0.75);
//this->position.set(getCamera()->position);
//glTranslatef(position.x,position.y+2,position.z);
geometry.draw();
glPopMatrix();
}
@@ -95,5 +29,7 @@ public:
inline Skybox* getSkybox() {
for (auto& e : entityList)
if (auto* s = dynamic_cast<Skybox*>(e) )
return dynamic_cast<Skybox *>(e);
return s;
std::cerr << "Attempt to get Skybox pointer while not in scene." << std::endl;
engine->quit();
}

View File

@@ -10,9 +10,9 @@ public:
float z = 0; //roll
};
class angle : public vector3 {
class Angle : public vector3 {
public:
void clamp() {
void clampToViewAngle() {
if (this->x > 89.0f)
this->x = 89.0f;
if (this->x <= -89.0f)
@@ -29,6 +29,23 @@ public:
if(this->z <= -360.0f)
this->z = 0.0;
}
void clampToAngle() {
if (this->x <= -180.0f)
this->x = 180.0f;
if (this->x >= 180.01f)
this->x = -179.9f;
//TODO: Make this entirely seamless by getting the amount they rotated passed -180 and +180 by.
if (this->y <= -180.0f)
this->y = 180.0f;
if (this->y >= 180.01f)
this->y = -179.9f;
if (this->z <= -180.0f)
this->z = 180.0f;
if (this->z >= 180.01f)
this->z = -179.9f;
}
};
class Position : public vector3 {

View File

@@ -7,16 +7,18 @@
#include <SDL2/SDL_opengl.h>
#include <GL/glu.h>
#include <GL/gl.h>
#include "vector3.h"
struct vertex {
struct Vertex {
GLfloat x, y, z;
};
class vertexarray {
class VertexArray {
public:
std::vector<vertex> vertices;
std::vector<Vertex> vertices;
std::vector<GLint> indices;
//Works
void load(const char* file) {
//This will take a .obj
std::ifstream f(file);
@@ -27,19 +29,53 @@ public:
iss >> prefix;
if (prefix == "v") {
vertex v;
Vertex v;
iss >> v.x >> v.y >> v.z;
this->vertices.push_back(v);
vertices.push_back(v);
std::cout << v.x << " " << v.y << " " << v.z << std::endl;
}
else if (prefix == "f") {
if (prefix == "f") {
unsigned int v1, v2, v3;
char slash;
iss >> v1 >> slash >> slash >> v2 >> slash >> slash >> v3;
this->indices.push_back(v1 - 1);
this->indices.push_back(v2 - 1);
this->indices.push_back(v3 - 1);
iss >> v1 >> v2 >> v3;
indices.push_back(v1);
indices.push_back(v2);
indices.push_back(v3);
}
}
f.close();
}
//Wrong
void draw() {
glPushMatrix();
glBegin(GL_TRIANGLES);
for (size_t i = 0; i < indices.size(); i +=3) {
GLint index1 = this->indices[i];
if (i == 0)
index1 = 1;
GLint index2 = this->indices[i + 1];
GLint index3 = this->indices[i + 2];
glVertex3f(
this->vertices[index1].x,
this->vertices[index1].y,
this->vertices[index1].z);
glVertex3f(
this->vertices[index2].x,
this->vertices[index2].y,
this->vertices[index2].z);
glVertex3f(
this->vertices[index3].x,
this->vertices[index3].y,
this->vertices[index3].z);
}
glEnd();
glPopMatrix();
}
};