Make the model loader actually work

This commit is contained in:
2023-11-21 14:11:18 -05:00
parent 06f731e91e
commit 2a035b9f21
7 changed files with 465 additions and 387 deletions

View File

@@ -1,24 +0,0 @@
# 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

99
models/cone.obj Normal file
View File

@@ -0,0 +1,99 @@
# Blender 3.6.4
# www.blender.org
o Cone
v 0.000000 -1.000000 1.000000
v -0.195090 -1.000000 0.980785
v -0.382683 -1.000000 0.923880
v -0.555570 -1.000000 0.831470
v -0.707107 -1.000000 0.707107
v -0.831470 -1.000000 0.555570
v -0.923880 -1.000000 0.382683
v -0.980785 -1.000000 0.195090
v -1.000000 -1.000000 0.000000
v -0.980785 -1.000000 -0.195090
v -0.923880 -1.000000 -0.382683
v -0.831470 -1.000000 -0.555570
v -0.707107 -1.000000 -0.707107
v -0.555570 -1.000000 -0.831470
v -0.382683 -1.000000 -0.923880
v -0.195090 -1.000000 -0.980785
v 0.000000 -1.000000 -1.000000
v 0.195090 -1.000000 -0.980785
v 0.382683 -1.000000 -0.923880
v 0.555570 -1.000000 -0.831470
v 0.707107 -1.000000 -0.707107
v 0.831470 -1.000000 -0.555570
v 0.923880 -1.000000 -0.382683
v 0.980785 -1.000000 -0.195090
v 1.000000 -1.000000 0.000000
v 0.980785 -1.000000 0.195090
v 0.923880 -1.000000 0.382683
v 0.831470 -1.000000 0.555570
v 0.707107 -1.000000 0.707107
v 0.555570 -1.000000 0.831470
v 0.382683 -1.000000 0.923880
v 0.195090 -1.000000 0.980785
v 0.000000 1.000000 0.000000
s 0
f 1 33 2
f 2 33 3
f 3 33 4
f 4 33 5
f 5 33 6
f 6 33 7
f 7 33 8
f 8 33 9
f 9 33 10
f 10 33 11
f 11 33 12
f 12 33 13
f 13 33 14
f 14 33 15
f 15 33 16
f 16 33 17
f 17 33 18
f 18 33 19
f 19 33 20
f 20 33 21
f 21 33 22
f 22 33 23
f 23 33 24
f 24 33 25
f 25 33 26
f 26 33 27
f 27 33 28
f 28 33 29
f 29 33 30
f 30 33 31
f 16 24 32
f 31 33 32
f 32 33 1
f 32 1 2
f 2 3 4
f 4 5 6
f 6 7 8
f 8 9 10
f 10 11 12
f 12 13 14
f 14 15 16
f 16 17 18
f 18 19 20
f 20 21 22
f 22 23 24
f 24 25 26
f 26 27 28
f 28 29 30
f 30 31 32
f 32 2 4
f 4 6 8
f 8 10 12
f 12 14 16
f 16 18 20
f 20 22 24
f 24 26 28
f 28 30 32
f 32 4 8
f 8 12 16
f 16 20 24
f 24 28 32
f 32 8 16

24
models/cube.obj Normal file
View File

@@ -0,0 +1,24 @@
# Blender v3.6.4 OBJ File: ''
# www.blender.org
o Cube
v 0.900000 0.900000 -0.900000
v 0.900000 -0.900000 -0.900000
v 0.900000 0.900000 0.900000
v 0.900000 -0.900000 0.900000
v -0.900000 0.900000 -0.900000
v -0.900000 -0.900000 -0.900000
v -0.900000 0.900000 0.900000
v -0.900000 -0.900000 0.900000
s off
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

File diff suppressed because it is too large Load Diff

View File

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

View File

@@ -9,17 +9,17 @@
class Skybox : public Entity {
public:
//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() {
if (engine->frameCount == 1)
geometry.load("../cube.obj");
if (engine->frameCount == 1) {
geometry.load("../models/sphere.obj");
std::this_thread::sleep_for(std::chrono::microseconds(1000));
}
glPushMatrix();
glColor3f(0.75,0.75,0.75);
//this->position.set(getCamera()->position);
//glTranslatef(position.x,position.y+2,position.z);
glTranslatef(position.x,position.y+2,position.z);
geometry.draw();
glPopMatrix();
}

View File

@@ -16,66 +16,46 @@ struct Vertex {
class VertexArray {
public:
std::vector<Vertex> vertices;
std::vector<GLint> indices;
std::vector<unsigned int> indices;
//Works
void load(const char* file) {
//This will take a .obj
std::ifstream f(file);
void load(const std::string& filename) {
std::ifstream file(filename);
if (!file.is_open()) {
std::cout << "File not found: " << filename << std::endl;
engine->quit();
return;
}
std::string line;
while (std::getline(f, line)) {
std::istringstream iss(line);
while (std::getline(file, line)) {
std::istringstream stream(line);
std::string prefix;
iss >> prefix;
stream >> prefix;
if (prefix == "v") {
Vertex v;
iss >> v.x >> v.y >> v.z;
vertices.push_back(v);
std::cout << v.x << " " << v.y << " " << v.z << std::endl;
}
if (prefix == "f") {
float x, y, z;
stream >> x >> y >> z;
vertices.push_back({x, y, z});
} else if (prefix == "f") {
unsigned int v1, v2, v3;
iss >> v1 >> v2 >> v3;
indices.push_back(v1);
indices.push_back(v2);
indices.push_back(v3);
stream >> v1 >> v2 >> v3;
indices.push_back(v1 - 1);
indices.push_back(v2 - 1);
indices.push_back(v3 - 1);
}
}
f.close();
file.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);
for (size_t i = 0; i < indices.size(); i += 3) {
glVertex3f(vertices[indices[i]].x, vertices[indices[i]].y, vertices[indices[i]].z);
glVertex3f(vertices[indices[i + 1]].x, vertices[indices[i + 1]].y, vertices[indices[i + 1]].z);
glVertex3f(vertices[indices[i + 2]].x, vertices[indices[i + 2]].y, vertices[indices[i + 2]].z);
}
glEnd();
glPopMatrix();
}
};