Proper obj loading kinda

This commit is contained in:
Ben
2019-03-01 13:55:42 +00:00
parent d44c0f0303
commit 320fc6cf9b
10 changed files with 162771 additions and 16 deletions

316
resources/sponza.mtl Executable file
View File

@@ -0,0 +1,316 @@
# 3ds Max Wavefront OBJ Exporter v0.97b - (c)2007 guruware
# File Created: 26.07.2011 17:00:30
newmtl sp_00_luk_mali
Ns 50.000000
Ni 1.500000
d 1.000000
Tr 0.000000
Tf 1.000000 1.000000 1.000000
illum 2
Ka 0.000000 0.000000 0.000000
Kd 0.745098 0.709804 0.674510
Ks 0.000000 0.000000 0.000000
Ke 0.000000 0.000000 0.000000
map_Ka SP_LUK.JPG
map_Kd SP_LUK.JPG
map_bump sp_luk-bump.JPG
bump sp_luk-bump.JPG
newmtl sp_svod_kapitel
Ns 50.000000
Ni 1.500000
d 1.000000
Tr 0.000000
Tf 1.000000 1.000000 1.000000
illum 2
Ka 0.000000 0.000000 0.000000
Kd 0.713726 0.705882 0.658824
Ks 0.000000 0.000000 0.000000
Ke 0.000000 0.000000 0.000000
map_Ka 00_SKAP.JPG
map_Kd 00_SKAP.JPG
map_bump 00_SKAP.JPG
bump 00_SKAP.JPG
newmtl sp_01_stub_baza_
Ns 19.999998
Ni 1.500000
d 1.000000
Tr 0.000000
Tf 1.000000 1.000000 1.000000
illum 2
Ka 0.000000 0.000000 0.000000
Kd 0.784314 0.784314 0.784314
Ks 0.000000 0.000000 0.000000
Ke 0.000000 0.000000 0.000000
newmtl sp_01_stub_kut
Ns 50.000000
Ni 1.500000
d 1.000000
Tr 0.000000
Tf 1.000000 1.000000 1.000000
illum 2
Ka 0.000000 0.000000 0.000000
Kd 0.737255 0.709804 0.670588
Ks 0.000000 0.000000 0.000000
Ke 0.000000 0.000000 0.000000
map_Ka 01_STUB.JPG
map_Kd 01_STUB.JPG
map_bump 01_STUB-bump.jpg
bump 01_STUB-bump.jpg
newmtl sp_00_stup
Ns 50.000000
Ni 1.500000
d 1.000000
Tr 0.000000
Tf 1.000000 1.000000 1.000000
illum 2
Ka 0.000000 0.000000 0.000000
Kd 0.737255 0.709804 0.670588
Ks 0.000000 0.000000 0.000000
Ke 0.000000 0.000000 0.000000
map_Ka 01_STUB.JPG
map_Kd 01_STUB.JPG
map_bump 01_STUB-bump.jpg
bump 01_STUB-bump.jpg
newmtl sp_01_stub_baza
Ns 50.000000
Ni 1.500000
d 1.000000
Tr 0.000000
Tf 1.000000 1.000000 1.000000
illum 2
Ka 0.000000 0.000000 0.000000
Kd 0.800000 0.784314 0.749020
Ks 0.000000 0.000000 0.000000
Ke 0.000000 0.000000 0.000000
map_Ka 01_S_BA.JPG
map_Kd 01_S_BA.JPG
map_bump 01_S_BA.JPG
bump 01_S_BA.JPG
newmtl sp_00_luk_mal1
Ns 50.000000
Ni 1.500000
d 1.000000
Tr 0.000000
Tf 1.000000 1.000000 1.000000
illum 2
Ka 0.000000 0.000000 0.000000
Kd 0.745098 0.709804 0.674510
Ks 0.000000 0.000000 0.000000
Ke 0.000000 0.000000 0.000000
map_Ka 01_ST_KP.JPG
map_Kd 01_ST_KP.JPG
map_bump 01_St_kp-bump.jpg
bump 01_St_kp-bump.jpg
newmtl sp_01_stub
Ns 50.000000
Ni 1.500000
d 1.000000
Tr 0.000000
Tf 1.000000 1.000000 1.000000
illum 2
Ka 0.000000 0.000000 0.000000
Kd 0.737255 0.709804 0.670588
Ks 0.000000 0.000000 0.000000
Ke 0.000000 0.000000 0.000000
map_Ka 01_STUB.JPG
map_Kd 01_STUB.JPG
map_bump 01_STUB-bump.jpg
bump 01_STUB-bump.jpg
newmtl sp_01_stup
Ns 50.000000
Ni 1.500000
d 1.000000
Tr 0.000000
Tf 1.000000 1.000000 1.000000
illum 2
Ka 0.000000 0.000000 0.000000
Kd 0.827451 0.800000 0.768628
Ks 0.000000 0.000000 0.000000
Ke 0.000000 0.000000 0.000000
map_Ka X01_ST.JPG
map_Kd X01_ST.JPG
newmtl sp_vijenac
Ns 50.000000
Ni 1.500000
d 1.000000
Tr 0.000000
Tf 1.000000 1.000000 1.000000
illum 2
Ka 0.000000 0.000000 0.000000
Kd 0.713726 0.705882 0.658824
Ks 0.000000 0.000000 0.000000
Ke 0.000000 0.000000 0.000000
map_Ka 00_SKAP.JPG
map_Kd 00_SKAP.JPG
map_bump 00_SKAP.JPG
bump 00_SKAP.JPG
newmtl sp_00_svod
Ns 1.000000
Ni 1.500000
d 1.000000
Tr 0.000000
Tf 1.000000 1.000000 1.000000
illum 2
Ka 0.145098 0.145098 0.145098
Kd 0.941177 0.866667 0.737255
Ks 0.034039 0.032314 0.029333
Ke 0.000000 0.000000 0.000000
map_Kd KAMEN-stup.JPG
map_Ka KAMEN-stup.JPG
map_bump KAMEN-stup.jpg
bump KAMEN-stup.jpg
newmtl sp_02_reljef
Ns 50.000000
Ni 1.500000
d 1.000000
Tr 0.000000
Tf 1.000000 1.000000 1.000000
illum 2
Ka 0.000000 0.000000 0.000000
Kd 0.529412 0.498039 0.490196
Ks 0.000000 0.000000 0.000000
Ke 0.000000 0.000000 0.000000
map_Ka RELJEF.JPG
map_Kd RELJEF.JPG
map_bump reljef-bump.jpg
bump reljef-bump.jpg
newmtl sp_01_luk_a
Ns 50.000000
Ni 1.500000
d 1.000000
Tr 0.000000
Tf 1.000000 1.000000 1.000000
illum 2
Ka 0.000000 0.000000 0.000000
Kd 0.745098 0.709804 0.674510
Ks 0.000000 0.000000 0.000000
Ke 0.000000 0.000000 0.000000
map_Ka SP_LUK.JPG
map_Kd SP_LUK.JPG
map_bump sp_luk-bump.JPG
bump sp_luk-bump.JPG
newmtl sp_zid_vani
Ns 50.000000
Ni 1.500000
d 1.000000
Tr 0.000000
Tf 1.000000 1.000000 1.000000
illum 2
Ka 0.000000 0.000000 0.000000
Kd 0.627451 0.572549 0.560784
Ks 0.000000 0.000000 0.000000
Ke 0.000000 0.000000 0.000000
map_Ka KAMEN.JPG
map_Kd KAMEN.JPG
map_bump KAMEN-bump.jpg
bump KAMEN-bump.jpg
newmtl sp_01_stup_baza
Ns 50.000000
Ni 1.500000
d 1.000000
Tr 0.000000
Tf 1.000000 1.000000 1.000000
illum 2
Ka 0.000000 0.000000 0.000000
Kd 0.800000 0.784314 0.749020
Ks 0.000000 0.000000 0.000000
Ke 0.000000 0.000000 0.000000
map_Ka 01_S_BA.JPG
map_Kd 01_S_BA.JPG
map_bump 01_S_BA.JPG
bump 01_S_BA.JPG
newmtl sp_00_zid
Ns 50.000000
Ni 1.500000
d 1.000000
Tr 0.000000
Tf 1.000000 1.000000 1.000000
illum 2
Ka 0.000000 0.000000 0.000000
Kd 0.627451 0.572549 0.560784
Ks 0.000000 0.000000 0.000000
Ke 0.000000 0.000000 0.000000
map_Ka KAMEN.JPG
map_Kd KAMEN.JPG
map_bump KAMEN-bump.jpg
bump KAMEN-bump.jpg
newmtl sp_00_prozor
Ns 50.000000
Ni 1.500000
d 1.000000
Tr 0.000000
Tf 1.000000 1.000000 1.000000
illum 2
Ka 0.000000 0.000000 0.000000
Kd 1.000000 1.000000 1.000000
Ks 0.000000 0.000000 0.000000
Ke 0.000000 0.000000 0.000000
map_Ka PROZOR1.JPG
map_Kd PROZOR1.JPG
map_bump PROZOR1.JPG
bump PROZOR1.JPG
newmtl sp_00_vrata_krug
Ns 19.999998
Ni 1.500000
d 1.000000
Tr 0.000000
Tf 1.000000 1.000000 1.000000
illum 2
Ka 0.000000 0.000000 0.000000
Kd 0.784314 0.784314 0.784314
Ks 0.000000 0.000000 0.000000
Ke 0.000000 0.000000 0.000000
map_Ka VRATA_KR.JPG
map_Kd VRATA_KR.JPG
map_bump VRATA_KR.JPG
bump VRATA_KR.JPG
newmtl sp_00_pod
Ns 50.000000
Ni 1.500000
d 1.000000
Tr 0.000000
Tf 1.000000 1.000000 1.000000
illum 2
Ka 0.000000 0.000000 0.000000
Kd 0.627451 0.572549 0.560784
Ks 0.000000 0.000000 0.000000
Ke 0.000000 0.000000 0.000000
map_Ka KAMEN.JPG
map_Kd KAMEN.JPG
map_bump KAMEN-bump.jpg
bump KAMEN-bump.jpg
newmtl sp_00_vrata_kock
Ns 19.999998
Ni 1.500000
d 1.000000
Tr 0.000000
Tf 1.000000 1.000000 1.000000
illum 2
Ka 0.000000 0.000000 0.000000
Kd 0.784314 0.784314 0.784314
Ks 0.000000 0.000000 0.000000
Ke 0.000000 0.000000 0.000000
map_Ka VRATA_KO.JPG
map_Kd VRATA_KO.JPG
map_bump VRATA_KO.JPG
bump VRATA_KO.JPG

162358
resources/sponza.obj Executable file

File diff suppressed because it is too large Load Diff

BIN
resources/sponza.zip Normal file

Binary file not shown.

28
resources/test.obj Normal file
View File

@@ -0,0 +1,28 @@
o Cube
s 1
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
vn 0.000000 -1.000000 0.000000
vn 0.000000 1.000000 0.000000
vn 1.000000 0.000000 0.000000
vn 0.000000 0.000000 1.000000
vn -1.000000 0.000000 0.000000
vn 0.000000 0.000000 -1.000000
f 1/2/1 3//1 2//1
f 5//2 8//2 6//2
f 1//3 5//3 2//3
f 2//4 6//4 3//4
f 3//5 8//5 7//5
f 5//6 8//6 1//6
f 4//1 3//1 1//1
f 8//2 7//2 6//2
f 5//3 6//3 2//3
f 6//4 7//4 3//4
f 4//5 8//5 3//5
f 1//6 4//6 8//6

39
resources/test1.obj Normal file
View File

@@ -0,0 +1,39 @@
# Blender v2.79 (sub 0) OBJ File: ''
# www.blender.org
mtllib test.mtl
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 -0.999999
v 0.985777 1.192230 2.337569
v -1.377941 1.709185 1.023966
v -1.000000 1.000000 -1.000000
v 0.517399 2.721372 1.811071
v -1.866757 2.621557 0.910967
v 0.548957 1.837827 -1.224019
v -1.690411 2.280694 -1.395202
v -0.783580 1.471610 2.999746
v -1.362162 2.293163 2.078320
v -0.980705 2.326940 1.789054
vn 0.0000 -1.0000 0.0000
vn 0.1613 0.9751 -0.1521
vn 1.0000 0.0026 0.0026
vn -0.3069 -0.2775 0.9104
vn -0.9937 -0.0793 -0.0793
vn 0.0000 0.0000 -1.0000
vn -0.4940 -0.8691 -0.0268
usemtl Material
s off
f 1//1 2//1 3//1 4//1
f 5//2 8//2 7//2 6//2
f 1//3 5//3 6//3 2//3
f 2//4 6//4 7//4 3//4
f 3//5 7//5 8//5 4//5
f 5//6 1//6 4//6 8//6
f 6//7 13//7 14//7 15//7 7//7
l 6 9
l 7 10
l 5 11
l 8 12

View File

@@ -83,6 +83,7 @@ Display::Display(std::string name, Logger& logger, int w, int h,
// Load OpenGL
gladLoadGLLoader(SDL_GL_GetProcAddress);
glEnable(GL_MULTISAMPLE);
glEnable(GL_DEPTH_TEST);
logger << LOGGER_INFO << "Loaded OpenGL" << LOGGER_ENDL;
logger << LOGGER_ENDL;
isClosed = false;
@@ -175,6 +176,7 @@ Display::Display(std::string name, Logger& logger, int w, int h,
// Load OpenGL
gladLoadGLLoader(SDL_GL_GetProcAddress);
glEnable(GL_MULTISAMPLE);
glEnable(GL_DEPTH_TEST);
logger << LOGGER_INFO << "Loaded OpenGL" << LOGGER_ENDL;
logger << LOGGER_ENDL;
isClosed = false;

View File

@@ -29,14 +29,25 @@ int main (int argc, char** argv) {
Shader shader;
shader.load("./resources/shaders/phong").attatch().link().use();
Mesh mesh{ "./resources/dragon.obj" };
Mesh mesh{ "./resources/test1.obj" };
mesh.setup();
SDL_Event e;
while (!display.isClosed) {
while (SDL_PollEvent(&e))
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT || e.key.keysym.sym == SDLK_ESCAPE)
display.isClosed = true;
}
const Uint8* state = SDL_GetKeyboardState(NULL);
if (state[SDL_SCANCODE_Q]) {
mesh.rotation += -1.5f;
}
if (state[SDL_SCANCODE_E]) {
mesh.rotation += 1.5f;
}
mesh.bind();
mesh.render(shader);

View File

@@ -92,7 +92,7 @@ void Mesh::render(Shader& shader) {
// Model matrice
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, { -17.0f, -17.0f, -17.0f });
model = glm::rotate(model, glm::radians(-160.0f), glm::vec3(0.0f, 1.0f, 0.0f));
model = glm::rotate(model, glm::radians(-160.0f + this->rotation), glm::vec3(0.0f, 1.0f, 0.0f));
// Gets uniform for model matrice, to be used later
GLint uniTrans = glGetUniformLocation(shader.getProgram(), "model");
glUniformMatrix4fv(uniTrans, 1, GL_FALSE, glm::value_ptr(model));

View File

@@ -56,6 +56,8 @@ public:
//one buffer :)
std::vector<glm::vec3> texCoords;
std::vector<unsigned int> indices;
float rotation = 0.0f;
private:
GLuint vertexBuffer;
GLuint indexBuffer;

View File

@@ -17,23 +17,18 @@ void OBJLtoGLM(ObjLMesh& mesh,
for (const auto &shape : mesh.shapes) {
for (const auto& index : shape.mesh.indices) {
glm::vec3 vertex {
mesh.attrib.vertices[3 * index.vertex_index + 0],
mesh.attrib.vertices[3 * index.vertex_index + 1],
mesh.attrib.vertices[3 * index.vertex_index + 2]
};
outVert.push_back(vertex);
if (uniqueVertices.count(vertex) == 0) {
uniqueVertices[vertex] = static_cast<uint32_t>(outVert.size());
outVert.push_back(vertex);
}
// outNorm.push_back({
// mesh.attrib.normals[3 * index.normal_index + 0],
// mesh.attrib.normals[3 * index.normal_index + 1],
// mesh.attrib.normals[3 * index.normal_index + 2]
// });
outNorm.push_back({
mesh.attrib.normals[3 * index.normal_index + 0],
mesh.attrib.normals[3 * index.normal_index + 1],
mesh.attrib.normals[3 * index.normal_index + 2]
});
// outTexCoord.push_back({
// mesh.attrib.texcoords[2 * index.texcoord_index + 0],
@@ -41,11 +36,15 @@ void OBJLtoGLM(ObjLMesh& mesh,
// 0.0f
// });
outIndices.push_back(index.vertex_index);
// if (uniqueVertices.count(vertex) == 0) {
// uniqueVertices[vertex] = static_cast<uint32_t>(outVert.size());
// }
outIndices.push_back(outIndices.size());
}
}
ComputeNormals(outNorm, outVert, outIndices);
// ComputeNormals(outNorm, outVert, outIndices);
}
void ComputeNormals(std::vector<glm::vec3>& normals,