changed shipmodel to corvette, assimp

This commit is contained in:
xkamikoo 2021-01-24 02:57:03 +01:00
parent 1bf4112f99
commit 72e8f3d0f4
3 changed files with 42 additions and 21 deletions

View File

@ -16,6 +16,7 @@
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#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<Model> corvette;
std::vector<Core::RenderContext> 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> 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<std::string> 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<Model>("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");

View File

@ -17,10 +17,10 @@ using namespace std;
struct Vertex {
// position
glm::vec3 Position;
// normal
glm::vec3 Normal;
// texCoords
glm::vec2 TexCoords;
// normal
glm::vec3 Normal;
// 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(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, TexCoords));
// vertex normals
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, TexCoords));
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));

View File

@ -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
}
}