changed shipmodel to corvette, assimp
This commit is contained in:
parent
1bf4112f99
commit
72e8f3d0f4
36
src/main.cpp
36
src/main.cpp
@ -16,6 +16,7 @@
|
|||||||
#include <assimp/Importer.hpp>
|
#include <assimp/Importer.hpp>
|
||||||
#include <assimp/scene.h>
|
#include <assimp/scene.h>
|
||||||
#include <assimp/postprocess.h>
|
#include <assimp/postprocess.h>
|
||||||
|
#include "model.h"
|
||||||
|
|
||||||
|
|
||||||
#define STB_IMAGE_IMPLEMENTATION
|
#define STB_IMAGE_IMPLEMENTATION
|
||||||
@ -40,10 +41,15 @@ GLuint shipTexture;
|
|||||||
obj::Model sphereModel;
|
obj::Model sphereModel;
|
||||||
obj::Model cubeModel;
|
obj::Model cubeModel;
|
||||||
obj::Model shipModel;
|
obj::Model shipModel;
|
||||||
|
|
||||||
Core::RenderContext sphereContext;
|
Core::RenderContext sphereContext;
|
||||||
Core::RenderContext cubeContext;
|
Core::RenderContext cubeContext;
|
||||||
Core::RenderContext shipContext;
|
Core::RenderContext shipContext;
|
||||||
|
|
||||||
|
//assimp
|
||||||
|
std::shared_ptr<Model> corvette;
|
||||||
|
std::vector<Core::RenderContext> corvetteMeshes;
|
||||||
|
|
||||||
float cameraAngle = 0;
|
float cameraAngle = 0;
|
||||||
glm::vec3 cameraPos = glm::vec3(-6, 0, 0);
|
glm::vec3 cameraPos = glm::vec3(-6, 0, 0);
|
||||||
glm::vec3 cameraDir;
|
glm::vec3 cameraDir;
|
||||||
@ -114,6 +120,22 @@ void drawObject(GLuint program, Core::RenderContext context, glm::mat4 modelMatr
|
|||||||
glUseProgram(0);
|
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
|
//Skybox
|
||||||
unsigned int loadCubemap(std::vector<std::string> faces)
|
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);
|
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);
|
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[0].position = sunPos;
|
||||||
lights[1].position = sunPos2;
|
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));
|
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, earth, glm::vec3(0.8f, 0.8f, 0.8f), earthTexture);
|
||||||
drawObjectTexture(programTex, sphereContext, moon, glm::vec3(0.9f, 1.0f, 0.9f), moonTexture);
|
drawObjectTexture(programTex, sphereContext, moon, glm::vec3(0.9f, 1.0f, 0.9f), moonTexture);
|
||||||
|
drawFromAssimpModel(programTex, corvette, shipModelMatrix, glm::vec3(1));
|
||||||
|
|
||||||
glUseProgram(0);
|
glUseProgram(0);
|
||||||
glutSwapBuffers();
|
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");
|
||||||
shipModel = obj::loadModelFromFile("models/spaceship.obj");
|
|
||||||
sphereModel = obj::loadModelFromFile("models/sphere.obj");
|
sphereModel = obj::loadModelFromFile("models/sphere.obj");
|
||||||
cubeModel = obj::loadModelFromFile("models/cube.obj");
|
cubeModel = obj::loadModelFromFile("models/cube.obj");
|
||||||
|
|
||||||
sphereContext.initFromOBJ(sphereModel);
|
sphereContext.initFromOBJ(sphereModel);
|
||||||
cubeContext.initFromOBJ(cubeModel);
|
cubeContext.initFromOBJ(cubeModel);
|
||||||
shipContext.initFromOBJ(shipModel);
|
//shipContext.initFromOBJ(shipModel);
|
||||||
|
|
||||||
|
|
||||||
shipTexture = Core::LoadTexture("textures/spaceship.png");
|
shipTexture = Core::LoadTexture("textures/spaceship.png");
|
||||||
sunTexture = Core::LoadTexture("textures/sun.png");
|
sunTexture = Core::LoadTexture("textures/sun.png");
|
||||||
earthTexture = Core::LoadTexture("textures/earth2.png");
|
earthTexture = Core::LoadTexture("textures/earth2.png");
|
||||||
|
17
src/mesh.h
17
src/mesh.h
@ -17,10 +17,10 @@ using namespace std;
|
|||||||
struct Vertex {
|
struct Vertex {
|
||||||
// position
|
// position
|
||||||
glm::vec3 Position;
|
glm::vec3 Position;
|
||||||
|
// texCoords
|
||||||
|
glm::vec2 TexCoords;
|
||||||
// normal
|
// normal
|
||||||
glm::vec3 Normal;
|
glm::vec3 Normal;
|
||||||
// texCoords
|
|
||||||
glm::vec2 TexCoords;
|
|
||||||
// tangent
|
// tangent
|
||||||
glm::vec3 Tangent;
|
glm::vec3 Tangent;
|
||||||
// bitangent
|
// bitangent
|
||||||
@ -83,7 +83,7 @@ public:
|
|||||||
glBindTexture(GL_TEXTURE_2D, textures[i].id);
|
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
|
// draw mesh
|
||||||
glBindVertexArray(VAO);
|
glBindVertexArray(VAO);
|
||||||
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
|
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
|
||||||
@ -120,12 +120,13 @@ private:
|
|||||||
// vertex Positions
|
// vertex Positions
|
||||||
glEnableVertexAttribArray(0);
|
glEnableVertexAttribArray(0);
|
||||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)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
|
// vertex texture coords
|
||||||
glEnableVertexAttribArray(2);
|
glEnableVertexAttribArray(1);
|
||||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, TexCoords));
|
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
|
// vertex tangent
|
||||||
glEnableVertexAttribArray(3);
|
glEnableVertexAttribArray(3);
|
||||||
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Tangent));
|
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Tangent));
|
||||||
|
10
src/model.h
10
src/model.h
@ -66,25 +66,25 @@ private:
|
|||||||
directory = path.substr(0, path.find_last_of('/'));
|
directory = path.substr(0, path.find_last_of('/'));
|
||||||
|
|
||||||
// process ASSIMP's root node recursively
|
// 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).
|
// 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
|
// process each mesh located at the current node
|
||||||
for (unsigned int i = 0; i < node->mNumMeshes; i++)
|
for (unsigned int i = 0; i < node->mNumMeshes; i++)
|
||||||
{
|
{
|
||||||
// the node object only contains indices to index the actual objects in the scene.
|
// 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).
|
// the scene contains all the data, node is just to keep stuff organized (like relations between nodes).
|
||||||
aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
|
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
|
// 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++)
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user