Files
vb01/mesh.cpp
T
devZoGok aebc069053 3 types of lights
supported number of n lights
2021-10-18 19:03:19 +03:00

77 lines
2.4 KiB
C++

#include"root.h"
#include"mesh.h"
#include<glad.h>
#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(){}
void Mesh::construct(){
glGenVertexArrays(1,&VAO);
glGenBuffers(1,&VBO);
glGenBuffers(1,&EBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER,VBO);
glBufferData(GL_ARRAY_BUFFER, 3*numTris*sizeof(Vertex), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,3*numTris*sizeof(unsigned int),indices,GL_STATIC_DRAW);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,sizeof(Vertex),(void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,sizeof(Vertex),(void*)(offsetof(Vertex,norm)));
glEnableVertexAttribArray(1);
glVertexAttribPointer(2,2,GL_FLOAT,GL_FALSE,sizeof(Vertex),(void*)(offsetof(Vertex,texCoords)));
glEnableVertexAttribArray(2);
}
void Mesh::update(){
Root *root=Root::getSingleton();
Camera *cam=root->getCamera();
float fov=cam->getFov(),width=root->getWidth(),height=root->getHeight();
float nearPlane=cam->getNearPlane(),farPlane=cam->getFarPlane();
Vector3 pos=node->getPosition();
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));
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);
material->update();
material->getShader()->setVec3(vec3(camPos.x,camPos.y,camPos.z),"camPos");
material->getShader()->setMat4(model,"model");
material->getShader()->setMat4(view,"view");
material->getShader()->setMat4(proj,"proj");
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES,3*numTris,GL_UNSIGNED_INT,0);
}
}