This commit is contained in:
devZoGok
2021-10-18 19:04:02 +03:00
parent 2187f32e7e
commit 0c5f39a96f
13 changed files with 196 additions and 59 deletions
+4 -2
View File
@@ -1,3 +1,4 @@
#include"model.h"
#include"light.h"
#include"node.h"
#include"root.h"
@@ -23,8 +24,9 @@ namespace vb01{
int numLights=0,thisId=-1;
for(Node *n : descendants){
std::vector<Light*> lights=n->getLights();
if(n->getMesh()){
Material *mat=n->getMesh()->getMaterial();
vector<Mesh*> meshes=n->getMeshes();
for(Mesh *mesh : meshes){
Material *mat=mesh->getMaterial();
if(mat&&mat->isLightingEnabled())
materials.push_back(mat);
}
+17 -8
View File
@@ -3,6 +3,7 @@
#include"node.h"
#include"quad.h"
#include"material.h"
#include"model.h"
#include"light.h"
#include<glm.hpp>
@@ -16,17 +17,25 @@ int main(){
Vector3 v1[]={Vector3(2,2,.5),Vector3(.5,.5,.5)};
Vector3 v2[]={Vector3(-.0,0,2),Vector3(.5,0,-2)};
for(int i=0;i<1;i++){
Quad *q=new Quad(v1[i]);
Node *node=new Node(v2[i]);
node->setObject(q);
Model *node=new Model("/home/dominykas/c++/FSim/jet00.obj");
//node->setObject(q);
Material *mat=new Material();
mat->setDiffuseMap("/home/dominykas/c++/FSim/woodChips.jpg");
mat->setLightingEnabled(true);
q->setMaterial(mat);
root->getRootNode()->attachChild(node);
mat->addDiffuseMap("/home/dominykas/c++/FSim/woodChips.jpg");
mat->setLightingEnabled(false);
node->setMaterial(mat);
//root->getRootNode()->attachChild(node);
}
Quad *q=new Quad(v1[0]);
Node *n=new Node(v2[0]);
n->attachMesh(q);
Material *mat=new Material();
mat->addDiffuseMap("/home/dominykas/c++/FSim/woodChips.jpg");
mat->setLightingEnabled(false);
q->setMaterial(mat);
root->getRootNode()->attachChild(n);
Camera *cam=Root::getSingleton()->getCamera();
cam->setPosition(Vector3(0,0,-4));
cam->setPosition(Vector3(0,0,-5));
Light *l1=new Light();
l1->setPosition(Vector3(-.5,.1,0));
l1->setColor(Vector3(0,1,0));
+6 -3
View File
@@ -14,8 +14,11 @@ namespace vb01{
void Material::update(){
shader->use();
shader->setBool(lightingEnabled,"lightingEnabled");
if(diffuseMap)diffuseMap->select();
if(normalMap)normalMap->select();
if(specularMap)specularMap->select();
for(Texture *t : diffuseMapTextures)
t->select();
for(Texture *t : normalMapTextures)
t->select();
for(Texture *t : specularMapTextures)
t->select();
}
}
+8 -5
View File
@@ -1,24 +1,27 @@
#ifndef MATERIAL_H
#define MATERIAL_H
#include"texture.h"
#include"shader.h"
#include"texture.h"
#include<vector>
namespace vb01{
class Texture;
class Material{
public:
Material();
~Material();
void update();
void setDiffuseMap(std::string diffuseMap){this->diffuseMap=new Texture(diffuseMap);}
void setNormalMap(std::string normalMap){this->normalMap=new Texture(normalMap);}
void setSpecularMap(std::string specularMap){this->specularMap=new Texture(specularMap);}
void addDiffuseMap(std::string diffuseMap){diffuseMapTextures.push_back(new Texture(diffuseMap));}
void addNormalMap(std::string normalMap){normalMapTextures.push_back(new Texture(normalMap));}
void addSpecularMap(std::string specularMap){specularMapTextures.push_back(new Texture(specularMap));}
inline void setLightingEnabled(bool lighting){this->lightingEnabled=lighting;}
inline Shader* getShader(){return shader;}
inline bool isLightingEnabled(){return lightingEnabled;}
private:
bool lightingEnabled=false;
Texture *diffuseMap=nullptr,*normalMap=nullptr,*specularMap=nullptr;
std::vector<Texture*> diffuseMapTextures,normalMapTextures,specularMapTextures;
Shader *shader;
};
}
+8 -14
View File
@@ -4,30 +4,24 @@
#include<glfw3.h>
#include<glm.hpp>
#include<ext.hpp>
#include<Importer.hpp>
#include<postprocess.h>
#include<iostream>
#include<cstdlib>
using namespace std;
using namespace glm;
using namespace Assimp;
namespace vb01{
Mesh::Mesh(string path){
Importer importer;
const aiScene *scene=importer.ReadFile(path,aiProcess_Triangulate | aiProcess_FlipUVs | aiProcess_GenNormals);
if(!scene||scene->mFlags&AI_SCENE_FLAGS_INCOMPLETE||!scene->mRootNode){
cout<<"Failed to load model\n";
exit(-1);
}
construct();
}
Mesh::Mesh(){
}
Mesh::Mesh(Vertex *vertices, unsigned int *indices,int numTris){
this->vertices=vertices;
this->indices=indices;
this->numTris=numTris;
construct();
}
Mesh::~Mesh(){}
void Mesh::construct(){
@@ -59,7 +53,7 @@ namespace vb01{
Vector3 dir=cam->getDirection(),up=cam->getUp(),camPos=cam->getPosition();
mat4 model=translate(mat4(1.),vec3(pos.x,pos.y,pos.z));
model=rotate(model,38.f,vec3(1,0,0));
//model=rotate(model,38.f,vec3(1,0,0));
mat4 view=lookAt(vec3(camPos.x,camPos.y,camPos.z),vec3(camPos.x+dir.x,camPos.y+dir.y,camPos.z+dir.z),vec3(up.x,up.y,up.z));
mat4 proj=perspective(radians(fov),width/height,nearPlane,farPlane);
//proj=ortho(0.f,800.f,0.f,-600.f,nearPlane,farPlane);
+11 -11
View File
@@ -5,36 +5,36 @@
#include<vector>
#include<string>
#include"material.h"
#include<scene.h>
namespace vb01{
class Node;
class Mesh{
protected:
public:
struct Vertex{
Vector3 pos,norm;
Vector2 texCoords;
};
void construct();
Vertex *vertices;
unsigned int *indices,VAO,VBO,EBO;
int numTris;
Node *node=nullptr;
public:
Mesh(std::string);
Mesh();
Mesh(Vertex*,unsigned int*,int);
~Mesh();
virtual void update();
void setMaterial(Material *mat){this->material=mat;}
inline void setNode(Node *node){this->node=node;}
inline Node* getNode(){return node;}
inline Material* getMaterial(){return material;}
inline std::vector<Mesh*>& getMeshes(){return meshes;}
private:
Material *material=nullptr;
void processNode(aiNode*,const aiScene*);
void processMesh(aiMesh*, const aiScene*);
Node *node=nullptr;
std::vector<Mesh*> meshes;
std::string name="";
protected:
void construct();
Vertex *vertices;
unsigned int *indices,VAO,VBO,EBO;
int numTris;
};
}
+82
View File
@@ -0,0 +1,82 @@
#include"model.h"
#include"mesh.h"
#include"material.h"
#include"vector.h"
#include<vector>
#include<iostream>
#include<Importer.hpp>
#include<scene.h>
#include<postprocess.h>
using namespace std;
using namespace Assimp;
namespace vb01{
void Model::processNode(aiNode *node, const aiScene *scene, Node *currentNode){
for(int i=0;i<node->mNumMeshes;i++){
aiMesh *mesh=scene->mMeshes[node->mMeshes[i]];
processMesh(mesh,scene,currentNode);
}
for(int i=0;i<node->mNumChildren;i++){
Node *childNode=new Node();
currentNode->attachChild(childNode);
processNode(node->mChildren[i],scene,childNode);
}
}
void Model::processMesh(aiMesh *mesh, const aiScene *scene, Node *currentNode){
const int numTris=mesh->mNumFaces;
Mesh::Vertex vertices[3*numTris];
unsigned int indices[3*numTris];
for(int i=0;i<3*numTris;i++){
Mesh::Vertex v;
v.pos.x=mesh->mVertices[i].x;
v.pos.y=mesh->mVertices[i].y;
v.pos.z=mesh->mVertices[i].z;
v.norm.x=mesh->mNormals[i].x;
v.norm.y=mesh->mNormals[i].y;
v.norm.z=mesh->mNormals[i].z;
if(mesh->mTextureCoords[0]){
v.texCoords.x=mesh->mTextureCoords[0][i].x;
v.texCoords.y=mesh->mTextureCoords[0][i].y;
}
else
v.texCoords=Vector2::VEC_ZERO;
vertices[i]=v;
}
for(int i=0;i<numTris;i++){
for(int j=0;j<mesh->mFaces[i].mNumIndices;j++){
indices[i+j]=mesh->mFaces[i].mIndices[j];
}
}
//if(mesh->mMaterialIndex>=0){
// aiMaterial *material=scene->mMaterial[mesh->mMaterialIndex];
//}
currentNode->attachMesh(new Mesh(vertices,indices,numTris));
}
Model::Model(string path) : Node(){
Importer importer;
const aiScene *scene=importer.ReadFile(path,aiProcess_Triangulate | aiProcess_FlipUVs | aiProcess_GenNormals);
if(!scene||scene->mFlags&AI_SCENE_FLAGS_INCOMPLETE||!scene->mRootNode){
cout<<"Failed to load model:"<<importer.GetErrorString()<<endl;
exit(-1);
}
processNode(scene->mRootNode,scene,this);
}
Model::~Model(){}
void Model::update(){
Node::update();
}
void Model::setMaterial(Material *mat){
for(Node *node : children)
for(Mesh *mesh : node->getMeshes())
mesh->setMaterial(mat);
this->material=mat;
}
}
+34
View File
@@ -0,0 +1,34 @@
#ifndef MODEL_H
#define MODEL_H
#include<vector>
#include<string>
#include"node.h"
class aiScene;
class aiNode;
class aiMesh;
class aiMaterial;
namespace vb01{
class Mesh;
class Material;
class Texture;
class Model : public Node{
public:
Model(std::string);
~Model();
void update();
void setMaterial(Material*);
private:
void processNode(aiNode*, const aiScene*, Node*);
void processMesh(aiMesh*, const aiScene*, Node*);
// std::vector<Texture*> processTexture(aiMaterial*, Assimp::aiTextureType);
void processMaterial();
Material* material;
};
}
#endif
+12 -8
View File
@@ -2,6 +2,7 @@
#include"node.h"
#include"mesh.h"
#include"light.h"
#include"material.h"
using namespace std;
@@ -13,8 +14,8 @@ namespace vb01{
Node::~Node(){}
void Node::update(){
if(mesh)
mesh->update();
for(Mesh *m : meshes)
m->update();
for(Light *l : lights)
l->update();
for(Node *c : children)
@@ -31,8 +32,8 @@ namespace vb01{
descendants.push_back(child);
}
void Node::setObject(Mesh *mesh){
this->mesh=mesh;
void Node::attachMesh(Mesh *mesh){
meshes.push_back(mesh);
mesh->setNode(this);
}
@@ -46,9 +47,12 @@ namespace vb01{
int numLights=0;
for(Node *n : descendants)
numLights+=n->getNumLights();
for(Node *n : descendants)
if(n->getMesh())
if(n->getMesh()->getMaterial())
n->getMesh()->getMaterial()->getShader()->setNumLights(numLights>0?numLights:1);
for(Node *n : descendants){
vector<Mesh*> meshes=n->getMeshes();
for(Mesh *m : meshes){
Material *mat=m->getMaterial();
if(mat) mat->getShader()->setNumLights(numLights>0?numLights:1);
}
}
}
}
+8 -6
View File
@@ -7,26 +7,28 @@
namespace vb01{
class Mesh;
class Model;
class Light;
class Node{
public:
Node(Vector3);
Node(Vector3=Vector3::VEC_ZERO);
~Node();
void setObject(Mesh*);
void update();
void attachMesh(Mesh*);
virtual void update();
void attachChild(Node*);
void addLight(Light*);
inline Mesh* getMesh(){return mesh;}
inline std::vector<Mesh*>& getMeshes(){return meshes;}
inline Node* getParent(){return parent;}
inline Vector3 getPosition(){return pos;}
inline std::vector<Node*>& getDescendants(){return descendants;}
inline std::vector<Node*>& getChildren(){return children;}
inline std::vector<Light*>& getLights(){return lights;}
inline Light* getLight(int i){return lights[i];}
inline int getNumLights(){return lights.size();}
private:
protected:
Vector3 pos;
Mesh *mesh=nullptr;
std::vector<Mesh*> meshes;
std::vector<Node*> children,descendants;
std::vector<Light*> lights;
Node *parent=nullptr;
+2 -2
View File
@@ -40,8 +40,8 @@ namespace vb01{
}
glEnable(GL_DEPTH_TEST);
glEnable(GL_STENCIL_TEST);
//glEnable(GL_CULL_FACE);
//glCullFace(GL_FRONT_AND_BACK);
glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT_AND_BACK);
}
void Root::update(){
+3
View File
@@ -9,6 +9,8 @@ class GLFWwindow;
namespace vb01{
void foo();
class Mesh;
class Root{
public:
Root();
@@ -25,6 +27,7 @@ namespace vb01{
GLFWwindow *window;
Node *rootNode;
Camera *camera;
std::vector<Mesh*> meshes;
void framebuffer_size_callback(GLFWwindow*,int,int);
};
}
+1
View File
@@ -11,6 +11,7 @@ namespace vb01{
private:
unsigned int texture;
int width,height,numChannels;
float weight;
unsigned char *data;
};
}