mirror of
https://github.com/devZoGok/vb01.git
synced 2026-08-26 19:43:30 +00:00
only loading one's model data to the GPU once
This commit is contained in:
@@ -3,7 +3,6 @@
|
|||||||
namespace vb01{
|
namespace vb01{
|
||||||
Box::Box(Vector3 size){
|
Box::Box(Vector3 size){
|
||||||
setSize(size);
|
setSize(size);
|
||||||
construct();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Box::setSize(Vector3 size){
|
void Box::setSize(Vector3 size){
|
||||||
|
|||||||
@@ -30,51 +30,7 @@ namespace vb01{
|
|||||||
for(Mesh *m : meshes)
|
for(Mesh *m : meshes)
|
||||||
delete m;
|
delete m;
|
||||||
|
|
||||||
if(material)
|
if(material) delete material;
|
||||||
delete material;
|
|
||||||
|
|
||||||
glDeleteVertexArrays(1, &VAO);
|
|
||||||
glDeleteBuffers(1, &EBO);
|
|
||||||
glDeleteBuffers(1, &VBO);
|
|
||||||
}
|
|
||||||
|
|
||||||
//TODO allow to init meshes to be drawn statically or dynamically
|
|
||||||
void Mesh::construct(){
|
|
||||||
glGenVertexArrays(1, &VAO);
|
|
||||||
glGenBuffers(1, &VBO);
|
|
||||||
glGenBuffers(1, &EBO);
|
|
||||||
|
|
||||||
u32 size = sizeof(MeshData::GpuVertex);
|
|
||||||
|
|
||||||
glBindVertexArray(VAO);
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
|
||||||
|
|
||||||
MeshData::GpuVertex *glVertData = meshBase.toGpuVerts();
|
|
||||||
glBufferData(GL_ARRAY_BUFFER, 3 * meshBase.numTris * size, glVertData, GL_DYNAMIC_DRAW);
|
|
||||||
delete glVertData;
|
|
||||||
|
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
|
||||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 3 * meshBase.numTris * sizeof(u32), meshBase.indices, GL_STATIC_DRAW);
|
|
||||||
|
|
||||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, size, (void*)0);
|
|
||||||
glEnableVertexAttribArray(0);
|
|
||||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::GpuVertex, norm)));
|
|
||||||
glEnableVertexAttribArray(1);
|
|
||||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::GpuVertex, uv)));
|
|
||||||
glEnableVertexAttribArray(2);
|
|
||||||
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::GpuVertex, tan)));
|
|
||||||
glEnableVertexAttribArray(3);
|
|
||||||
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::GpuVertex, biTan)));
|
|
||||||
glEnableVertexAttribArray(4);
|
|
||||||
glVertexAttribPointer(5, 4, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::GpuVertex, weights)));
|
|
||||||
glEnableVertexAttribArray(5);
|
|
||||||
glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::GpuVertex, boneIndices)));
|
|
||||||
glEnableVertexAttribArray(6);
|
|
||||||
|
|
||||||
for(int i = 0; i < meshBase.numShapeKeys; i++){
|
|
||||||
glVertexAttribPointer(7 + i, 3, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::GpuVertex, shapeKeyOffsets) + 3 * i * sizeof(float)));
|
|
||||||
glEnableVertexAttribArray(7 + i);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Mesh::initFramebuffer(u32 &framebuffer, u32 &renderbuffer, int width){
|
void Mesh::initFramebuffer(u32 &framebuffer, u32 &renderbuffer, int width){
|
||||||
@@ -94,7 +50,7 @@ namespace vb01{
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Mesh::updateVerts(MeshData meshData){
|
void Mesh::updateVerts(MeshData meshData){
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
glBindBuffer(GL_ARRAY_BUFFER, meshData.VBO);
|
||||||
|
|
||||||
MeshData::GpuVertex *glVertData = meshBase.toGpuVerts();
|
MeshData::GpuVertex *glVertData = meshBase.toGpuVerts();
|
||||||
glBufferSubData(GL_ARRAY_BUFFER, 0, 3 * sizeof(MeshData::GpuVertex) * meshBase.numTris, glVertData);
|
glBufferSubData(GL_ARRAY_BUFFER, 0, 3 * sizeof(MeshData::GpuVertex) * meshBase.numTris, glVertData);
|
||||||
@@ -130,8 +86,15 @@ namespace vb01{
|
|||||||
float fov = cam->getFov(), width = root->getWidth(), height = root->getHeight(), nearPlane = cam->getNearPlane(), farPlane = cam->getFarPlane();
|
float fov = cam->getFov(), width = root->getWidth(), height = root->getHeight(), nearPlane = cam->getNearPlane(), farPlane = cam->getFarPlane();
|
||||||
mat4 proj = perspective(radians(fov), width / height, nearPlane, farPlane);
|
mat4 proj = perspective(radians(fov), width / height, nearPlane, farPlane);
|
||||||
|
|
||||||
material->update();
|
|
||||||
Shader *shader = material->getShader();
|
Shader *shader = material->getShader();
|
||||||
|
material->update();
|
||||||
|
|
||||||
|
shader->setBool(castShadow, "castShadow");
|
||||||
|
shader->setBool(skeleton, "animated");
|
||||||
|
shader->setMat4(view, "view");
|
||||||
|
shader->setMat4(proj, "proj");
|
||||||
|
shader->setVec3(camPos, "camPos");
|
||||||
|
shader->setMat4(model, "model");
|
||||||
|
|
||||||
if(skeleton)
|
if(skeleton)
|
||||||
updateSkeleton(shader);
|
updateSkeleton(shader);
|
||||||
@@ -142,13 +105,6 @@ namespace vb01{
|
|||||||
if(reflect)
|
if(reflect)
|
||||||
updateReflection(pos);
|
updateReflection(pos);
|
||||||
|
|
||||||
shader->setBool(castShadow, "castShadow");
|
|
||||||
shader->setBool(skeleton, "animated");
|
|
||||||
shader->setMat4(view, "view");
|
|
||||||
shader->setMat4(proj, "proj");
|
|
||||||
shader->setVec3(camPos, "camPos");
|
|
||||||
shader->setMat4(model, "model");
|
|
||||||
|
|
||||||
if(shader->getName() == "gui"){
|
if(shader->getName() == "gui"){
|
||||||
shader->setVec2(Vector2((float)width, (float)height), "screen");
|
shader->setVec2(Vector2((float)width, (float)height), "screen");
|
||||||
|
|
||||||
@@ -401,7 +357,7 @@ namespace vb01{
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Mesh::render(){
|
void Mesh::render(){
|
||||||
glBindVertexArray(VAO);
|
glBindVertexArray(meshBase.VAO);
|
||||||
glPolygonMode(GL_FRONT_AND_BACK, wireframe ? GL_LINE : GL_FILL);
|
glPolygonMode(GL_FRONT_AND_BACK, wireframe ? GL_LINE : GL_FILL);
|
||||||
glDrawElements(GL_TRIANGLES, 3 * meshBase.numTris, GL_UNSIGNED_INT, 0);
|
glDrawElements(GL_TRIANGLES, 3 * meshBase.numTris, GL_UNSIGNED_INT, 0);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,7 +20,6 @@ namespace vb01{
|
|||||||
public:
|
public:
|
||||||
Mesh(MeshData);
|
Mesh(MeshData);
|
||||||
~Mesh();
|
~Mesh();
|
||||||
void construct();
|
|
||||||
void updateVerts(MeshData);
|
void updateVerts(MeshData);
|
||||||
virtual void update();
|
virtual void update();
|
||||||
void render();
|
void render();
|
||||||
@@ -67,7 +66,6 @@ namespace vb01{
|
|||||||
int numShapeKeys = 0;
|
int numShapeKeys = 0;
|
||||||
MeshData::ShapeKey *shapeKeys = nullptr;
|
MeshData::ShapeKey *shapeKeys = nullptr;
|
||||||
bool castShadow = false, reflect = false, reflective = false, wireframe = false;
|
bool castShadow = false, reflect = false, reflective = false, wireframe = false;
|
||||||
u32 VAO, VBO, EBO;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,83 @@
|
|||||||
#include "meshData.h"
|
#include "meshData.h"
|
||||||
|
|
||||||
|
#include "glad.h"
|
||||||
|
#include <glfw3.h>
|
||||||
|
#include <glm.hpp>
|
||||||
|
#include <ext.hpp>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
namespace vb01{
|
namespace vb01{
|
||||||
|
//TODO allow to init meshes to be drawn statically or dynamically
|
||||||
|
MeshData::MeshData(
|
||||||
|
Vector3 *pos,
|
||||||
|
float **w,
|
||||||
|
int **bi,
|
||||||
|
Vector3 **sko,
|
||||||
|
int np,
|
||||||
|
Vector3 *norm,
|
||||||
|
Vertex *vert,
|
||||||
|
u32 *ind,
|
||||||
|
int nt,
|
||||||
|
std::string an,
|
||||||
|
std::string *vg,
|
||||||
|
int nvg,
|
||||||
|
std::string fsn,
|
||||||
|
ShapeKey *sk,
|
||||||
|
int nsk
|
||||||
|
) :
|
||||||
|
positions(pos),
|
||||||
|
weights(w),
|
||||||
|
boneIndices(bi),
|
||||||
|
shapeKeyOffsets(sko),
|
||||||
|
numPos(np),
|
||||||
|
normals(norm),
|
||||||
|
vertices(vert),
|
||||||
|
indices(ind),
|
||||||
|
numTris(nt),
|
||||||
|
vertexGroups(vg),
|
||||||
|
numVertexGroups(nvg),
|
||||||
|
fullSkeletonName(fsn),
|
||||||
|
shapeKeys(sk),
|
||||||
|
numShapeKeys(nsk)
|
||||||
|
{
|
||||||
|
glGenVertexArrays(1, &VAO);
|
||||||
|
glGenBuffers(1, &VBO);
|
||||||
|
glGenBuffers(1, &EBO);
|
||||||
|
|
||||||
|
u32 size = sizeof(MeshData::GpuVertex);
|
||||||
|
|
||||||
|
glBindVertexArray(VAO);
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||||
|
|
||||||
|
MeshData::GpuVertex *glVertData = toGpuVerts();
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, 3 * numTris * size, glVertData, GL_DYNAMIC_DRAW);
|
||||||
|
delete glVertData;
|
||||||
|
|
||||||
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
||||||
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 3 * numTris * sizeof(u32), indices, GL_STATIC_DRAW);
|
||||||
|
|
||||||
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, size, (void*)0);
|
||||||
|
glEnableVertexAttribArray(0);
|
||||||
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::GpuVertex, norm)));
|
||||||
|
glEnableVertexAttribArray(1);
|
||||||
|
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::GpuVertex, uv)));
|
||||||
|
glEnableVertexAttribArray(2);
|
||||||
|
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::GpuVertex, tan)));
|
||||||
|
glEnableVertexAttribArray(3);
|
||||||
|
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::GpuVertex, biTan)));
|
||||||
|
glEnableVertexAttribArray(4);
|
||||||
|
glVertexAttribPointer(5, 4, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::GpuVertex, weights)));
|
||||||
|
glEnableVertexAttribArray(5);
|
||||||
|
glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::GpuVertex, boneIndices)));
|
||||||
|
glEnableVertexAttribArray(6);
|
||||||
|
|
||||||
|
for(int i = 0; i < numShapeKeys; i++){
|
||||||
|
glVertexAttribPointer(7 + i, 3, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::GpuVertex, shapeKeyOffsets) + 3 * i * sizeof(float)));
|
||||||
|
glEnableVertexAttribArray(7 + i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void MeshData::ShapeKey::animate(float value, KeyframeChannel keyframeChannel){
|
void MeshData::ShapeKey::animate(float value, KeyframeChannel keyframeChannel){
|
||||||
switch(keyframeChannel.type){
|
switch(keyframeChannel.type){
|
||||||
case KeyframeChannel::SHAPE_KEY_MIN:
|
case KeyframeChannel::SHAPE_KEY_MIN:
|
||||||
|
|||||||
+1
-16
@@ -48,22 +48,7 @@ namespace vb01{
|
|||||||
std::string attachableName = "", fullSkeletonName = "";
|
std::string attachableName = "", fullSkeletonName = "";
|
||||||
|
|
||||||
MeshData(){}
|
MeshData(){}
|
||||||
MeshData(Vector3 *pos, float **w, int **bi, Vector3 **sko, int np, Vector3 *norm, Vertex *vert, u32 *ind, int nt, std::string an = "", std::string *vg = nullptr, int nvg = 0, std::string fsn = "", ShapeKey *sk = nullptr, int nsk = 0) :
|
MeshData(Vector3*, float**, int**, Vector3**, int, Vector3*, Vertex*, u32*, int, std::string = "", std::string *vg = nullptr, int = 0, std::string = "", ShapeKey *sk = nullptr, int = 0);
|
||||||
positions(pos),
|
|
||||||
weights(w),
|
|
||||||
boneIndices(bi),
|
|
||||||
shapeKeyOffsets(sko),
|
|
||||||
numPos(np),
|
|
||||||
normals(norm),
|
|
||||||
vertices(vert),
|
|
||||||
indices(ind),
|
|
||||||
numTris(nt),
|
|
||||||
vertexGroups(vg),
|
|
||||||
numVertexGroups(nvg),
|
|
||||||
fullSkeletonName(fsn),
|
|
||||||
shapeKeys(sk),
|
|
||||||
numShapeKeys(nsk)
|
|
||||||
{}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -19,10 +19,6 @@ namespace vb01{
|
|||||||
vector<Node*> descendants = vector<Node*>{clonedRoot};
|
vector<Node*> descendants = vector<Node*>{clonedRoot};
|
||||||
clonedRoot->getDescendants(descendants);
|
clonedRoot->getDescendants(descendants);
|
||||||
|
|
||||||
for(Node *desc : descendants)
|
|
||||||
for(Mesh *mesh : desc->getMeshes())
|
|
||||||
mesh->construct();
|
|
||||||
|
|
||||||
for(Node *child : clonedRoot->getChildren())
|
for(Node *child : clonedRoot->getChildren())
|
||||||
attachChild(child);
|
attachChild(child);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ namespace vb01{
|
|||||||
this->numHorDiv = numHorDiv;
|
this->numHorDiv = numHorDiv;
|
||||||
|
|
||||||
setSize(size);
|
setSize(size);
|
||||||
Mesh::construct();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Quad::setSize(Vector3 size){
|
void Quad::setSize(Vector3 size){
|
||||||
|
|||||||
+18
-1
@@ -190,7 +190,24 @@ namespace vb01{
|
|||||||
|
|
||||||
const char *fullSkeletonName = meshEl->Attribute("skeleton");
|
const char *fullSkeletonName = meshEl->Attribute("skeleton");
|
||||||
string name = string(meshEl->Parent()->ToElement()->Attribute("name")) + string(meshEl->Attribute("name"));
|
string name = string(meshEl->Parent()->ToElement()->Attribute("name")) + string(meshEl->Attribute("name"));
|
||||||
Mesh *mesh = new Mesh(MeshData(vertPos, weights, boneIndices, shapeKeyOffsets, numVertPos, normals, vertices, indices, numVertices / 3, name, vertexGroups, numVertexGroups, (fullSkeletonName ? fullSkeletonName : ""), shapeKeys, numShapeKeys));
|
MeshData meshData = MeshData(
|
||||||
|
vertPos,
|
||||||
|
weights,
|
||||||
|
boneIndices,
|
||||||
|
shapeKeyOffsets,
|
||||||
|
numVertPos,
|
||||||
|
normals,
|
||||||
|
vertices,
|
||||||
|
indices,
|
||||||
|
numVertices / 3,
|
||||||
|
name,
|
||||||
|
vertexGroups,
|
||||||
|
numVertexGroups,
|
||||||
|
(fullSkeletonName ? fullSkeletonName : ""),
|
||||||
|
shapeKeys,
|
||||||
|
numShapeKeys
|
||||||
|
);
|
||||||
|
Mesh *mesh = new Mesh(meshData);
|
||||||
|
|
||||||
return mesh;
|
return mesh;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user