From 2d0c8446bb879fdea98c9b43b11f60316ac3a3f3 Mon Sep 17 00:00:00 2001 From: Maurice Mohlek Date: Fri, 21 Aug 2020 19:55:22 +0200 Subject: [PATCH] stash --- engine | 2 +- src/main.cpp | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++- src/shader.h | 31 ++++++++++++++ 3 files changed, 144 insertions(+), 2 deletions(-) create mode 100644 src/shader.h diff --git a/engine b/engine index 1d49b83..d8f2f5a 160000 --- a/engine +++ b/engine @@ -1 +1 @@ -Subproject commit 1d49b83488399bea4a7bc7a707d91e74715be68d +Subproject commit d8f2f5a2afccef6c7b0b599197974e8bd4b60668 diff --git a/src/main.cpp b/src/main.cpp index efc8bf3..32b7efc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,34 @@ #include +#include +#include +#include + +#include +#include +#include #include +#include +#include +#include +#include + +#include "shader.h" + +#include + +struct Clock +{ + uint32_t last_tick_time = 0; + uint32_t delta = 0; + + void tick() + { + uint32_t tick_time = SDL_GetTicks(); + delta = tick_time - last_tick_time; + last_tick_time = tick_time; + } +}; int main(int argc, char** argv) { Engine::Window* win = new Engine::Window("Medieval something", 800, 600); @@ -9,8 +37,91 @@ int main(int argc, char** argv) { return 1; } - do { + std::ifstream in("Medieval_House.obj", std::ios::in); + if (!in) { + fprintf(stderr, "Could not open file"); + exit(1); + } + + std::vector verticies; + std::vector texture; + std::vector faceIndex; + std::vector textureIndex; + std::string line; + while (std::getline(in, line)) { + std::string prefix = line.substr(0, 2); + if (prefix == "v ") { // verticies + std::istringstream l(line.substr(2)); + double x,y,z; + l >> x; l >> y; l >> z; + verticies.push_back(glm::vec3(x, y, z)); + } else if (prefix == "vt") { // texture coordinates + std::istringstream l(line.substr(3)); + int u, v ; + l >> u; l >> v; + texture.push_back(glm::vec2(u, v)); + } else if (prefix == "f ") { // faces + const char* c = line.substr(2).c_str(); + int vA, vB, vC, tA, tB, tC; + sscanf(c, "%i/%i %i/%i %i/%i", &vA, &tA, &vB, &tB, &vC, &tC); + faceIndex.push_back(vA -1); + faceIndex.push_back(vB -1); + faceIndex.push_back(vC -1); + textureIndex.push_back(tA -1); + textureIndex.push_back(tB -1); + textureIndex.push_back(tC -1); + } + } + + Engine::Buffer vbo(verticies.size()); + for (int i = 0; i < verticies.size(); ++i) { + vbo[i] = verticies[i]; + } + vbo.flush(); + + Engine::Buffer elements(faceIndex.size(), GL_ELEMENT_ARRAY_BUFFER); + for (int i = 0; i < faceIndex.size(); ++i) { + elements[i] = faceIndex[i]; + } + elements.flush(); + + Engine::VertexArrayObject vao; + vao.setBuffer(0, vbo); + vao.setBuffer(0, elements); + vao.bind(); + + Engine::ShaderProgram program; + Engine::Shader vertexShader(GL_VERTEX_SHADER, Shaders::mainVert); + Engine::Shader fragmentShader(GL_FRAGMENT_SHADER, Shaders::mainFrag); + program.shaders.push_back(vertexShader); + program.shaders.push_back(fragmentShader); + program.link(); + + program.use(); + + glm::mat4 proj = glm::perspective(glm::radians(45.0f), (float)win->getWidth() / (float)win->getHeight(), 0.1f, 100.0f); + glm::mat4 model = glm::mat4(1.0f); + model = glm::scale(model, glm::vec3(0.003f, 0.003f, 0.003f)); + glm::mat4 view = glm::mat4(1.0f); + view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f)); + + GLint uniformModel = program.getUniformLocation("model"); + GLint uniformView = program.getUniformLocation("view"); + GLint uniformProj = program.getUniformLocation("projection"); + + glUniformMatrix4fv(uniformView, 1, GL_FALSE, glm::value_ptr(view)); + glUniformMatrix4fv(uniformProj, 1, GL_FALSE, glm::value_ptr(proj)); + + Clock clock; + + //elements.bind(); + do { + clock.tick(); + float angle = clock.delta / 1000.0f; + model = glm::rotate(model, angle, glm::vec3(1.0f)); + glUniformMatrix4fv(uniformModel, 1, GL_FALSE, glm::value_ptr(model)); + glDrawElements(GL_TRIANGLES, elements._size, GL_UNSIGNED_INT, 0); } while (win->loop()); return 0; diff --git a/src/shader.h b/src/shader.h new file mode 100644 index 0000000..4cffcbb --- /dev/null +++ b/src/shader.h @@ -0,0 +1,31 @@ +#ifndef H_SHADERS +#define H_SHADERS + +#include + +namespace Shaders { + std::string mainVert = R"( + #version 410 core + + layout(location = 0) in vec3 position; + + uniform mat4 model; + uniform mat4 view; + uniform mat4 projection; + + void main() { + gl_Position = projection * view * model * vec4(position, 1.0); + //gl_Position = vec4(position, 1.0); + } + )"; + + std::string mainFrag = R"( + #version 410 core + + void main() { + gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); + } + )"; +} + +#endif \ No newline at end of file