From 72e8f3d0f4cdab2e560e37c9f76ec002329b0f7e Mon Sep 17 00:00:00 2001 From: xkamikoo <58092037+xkamikoo@users.noreply.github.com> Date: Sun, 24 Jan 2021 02:57:03 +0100 Subject: [PATCH] changed shipmodel to corvette, assimp --- src/main.cpp | 36 ++++++++++++++++++++++++++++-------- src/mesh.h | 17 +++++++++-------- src/model.h | 10 +++++----- 3 files changed, 42 insertions(+), 21 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 2134f92..f502c59 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,6 +16,7 @@ #include #include #include +#include "model.h" #define STB_IMAGE_IMPLEMENTATION @@ -40,10 +41,15 @@ GLuint shipTexture; obj::Model sphereModel; obj::Model cubeModel; obj::Model shipModel; + Core::RenderContext sphereContext; Core::RenderContext cubeContext; Core::RenderContext shipContext; +//assimp +std::shared_ptr corvette; +std::vector corvetteMeshes; + float cameraAngle = 0; glm::vec3 cameraPos = glm::vec3(-6, 0, 0); glm::vec3 cameraDir; @@ -114,6 +120,22 @@ void drawObject(GLuint program, Core::RenderContext context, glm::mat4 modelMatr glUseProgram(0); } +void drawFromAssimpModel(GLuint program, std::shared_ptr model, glm::mat4 modelMatrix, glm::vec3 color) +{ + glUseProgram(program); + + glUniform3f(glGetUniformLocation(program, "objectColor"), color.x, color.y, color.z); + + glm::mat4 transformation = perspectiveMatrix * cameraMatrix * modelMatrix; + + glUniformMatrix4fv(glGetUniformLocation(program, "modelMatrix"), 1, GL_FALSE, (float*)&modelMatrix); + glUniformMatrix4fv(glGetUniformLocation(program, "transformation"), 1, GL_FALSE, (float*)&transformation); + + model->Draw(program); + + glUseProgram(0); +} + //Skybox unsigned int loadCubemap(std::vector faces) { @@ -224,13 +246,14 @@ void renderScene() drawObjectTexture(programSun, sphereContext, sunModelMatrix2, glm::vec3(0.9f, 0.9f, 2.0f), sunTexture); - glm::mat4 shipModelMatrix = glm::translate(cameraPos + cameraDir * 0.6f + glm::vec3(0, -0.25f, 0)) * glm::rotate(-cameraAngle + glm::radians(90.0f), glm::vec3(0, 1, 0)) * glm::scale(glm::vec3(0.25f)); - drawObjectTexture(programTex, shipContext, shipModelMatrix, glm::vec3(1.f) ,shipTexture); + glUseProgram(programTex); + glm::mat4 shipModelMatrix = glm::translate(cameraPos + cameraDir * 0.6f + glm::vec3(0, -0.25f, 0)) * glm::rotate(-cameraAngle + glm::radians(90.0f), glm::vec3(0, 1, 0)) * glm::scale(glm::vec3(0.0001f)); + //drawObjectTexture(programTex, shipContext, shipModelMatrix, glm::vec3(1.f), shipTexture); lights[0].position = sunPos; lights[1].position = sunPos2; @@ -248,7 +271,7 @@ void renderScene() earth = glm::rotate(earth, time/3.0f, glm::vec3(0.0f, 0.0f, 1.0f)); drawObjectTexture(programTex, sphereContext, earth, glm::vec3(0.8f, 0.8f, 0.8f), earthTexture); drawObjectTexture(programTex, sphereContext, moon, glm::vec3(0.9f, 1.0f, 0.9f), moonTexture); - + drawFromAssimpModel(programTex, corvette, shipModelMatrix, glm::vec3(1)); glUseProgram(0); glutSwapBuffers(); @@ -265,17 +288,14 @@ void init() - + corvette = std::make_shared("models/Corvette-F3.obj"); //shipModel = obj::loadModelFromFile("models/spaceship.obj"); - shipModel = obj::loadModelFromFile("models/spaceship.obj"); sphereModel = obj::loadModelFromFile("models/sphere.obj"); cubeModel = obj::loadModelFromFile("models/cube.obj"); sphereContext.initFromOBJ(sphereModel); cubeContext.initFromOBJ(cubeModel); - shipContext.initFromOBJ(shipModel); - - + //shipContext.initFromOBJ(shipModel); shipTexture = Core::LoadTexture("textures/spaceship.png"); sunTexture = Core::LoadTexture("textures/sun.png"); earthTexture = Core::LoadTexture("textures/earth2.png"); diff --git a/src/mesh.h b/src/mesh.h index 33e81f9..bfd0250 100644 --- a/src/mesh.h +++ b/src/mesh.h @@ -17,10 +17,10 @@ using namespace std; struct Vertex { // position glm::vec3 Position; + // texCoords + glm::vec2 TexCoords; // normal glm::vec3 Normal; - // texCoords - glm::vec2 TexCoords; // tangent glm::vec3 Tangent; // bitangent @@ -83,7 +83,7 @@ public: glBindTexture(GL_TEXTURE_2D, textures[i].id); } - glUniformMatrix4fv(glGetUniformLocation(program, "model"), 1, GL_FALSE, (float*)&matrix); + //glUniformMatrix4fv(glGetUniformLocation(program, "model"), 1, GL_FALSE, (float*)&matrix); // draw mesh glBindVertexArray(VAO); glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0); @@ -120,12 +120,13 @@ private: // vertex Positions glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0); - // vertex normals - glEnableVertexAttribArray(1); - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Normal)); // vertex texture coords - glEnableVertexAttribArray(2); - glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, TexCoords)); + glEnableVertexAttribArray(1); + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, TexCoords)); + + // vertex normals + glEnableVertexAttribArray(2); + glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Normal)); // vertex tangent glEnableVertexAttribArray(3); glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Tangent)); diff --git a/src/model.h b/src/model.h index ac8bf72..2e22ef1 100644 --- a/src/model.h +++ b/src/model.h @@ -66,25 +66,25 @@ private: directory = path.substr(0, path.find_last_of('/')); // process ASSIMP's root node recursively - processNode(scene->mRootNode, scene,glm::mat4()); + processNode(scene->mRootNode, scene, glm::mat4()); } // processes a node in a recursive fashion. Processes each individual mesh located at the node and repeats this process on its children nodes (if any). - void processNode(aiNode* node, const aiScene* scene,glm::mat4 matrix) + void processNode(aiNode* node, const aiScene* scene, glm::mat4 matrix) { - glm::mat4 outMatrix = matrix * mat4_cast(node->mTransformation); + //glm::mat4 outMatrix = matrix * glm::mat4_cast(node->mTransformation); // process each mesh located at the current node for (unsigned int i = 0; i < node->mNumMeshes; i++) { // the node object only contains indices to index the actual objects in the scene. // the scene contains all the data, node is just to keep stuff organized (like relations between nodes). aiMesh* mesh = scene->mMeshes[node->mMeshes[i]]; - meshes.push_back(processMesh(mesh, scene, outMatrix)); + meshes.push_back(processMesh(mesh, scene, glm::mat4())); //tu byl out } // after we've processed all of the meshes (if any) we then recursively process each of the children nodes for (unsigned int i = 0; i < node->mNumChildren; i++) { - processNode(node->mChildren[i], scene, outMatrix); + processNode(node->mChildren[i], scene, glm::mat4()); // tu byl out } }