So I haven’t made an actual compute shader. I want to get the physical geometry of an animation. So this is the code that I have written to load the positions and bone information.
//ComputeMesh.h
#pragma once
#include "animvert.h"
#define GLEW_STATIC
#include "GL/glew.h"
#include <vector>
struct compVert {
float pos[4];
float weights[4];
float boneIDs[4];
};
using namespace std;
class ComputeMesh
{
public:
~ComputeMesh();
void draw();
void loadverts(vector<AnimVertex> verts);
void loadinds(vector<GLuint> inds);
void init();
private:
unsigned int totinds;
unsigned int vertcnt;
GLuint vao, vbo, ibo;
vector<GLuint> inds;
vector<compVert> verts;
};
//ComputeMesh.cpp
#include "ComputeMesh.h"
ComputeMesh::~ComputeMesh()
{
glDeleteVertexArrays(1, &vao);
glDeleteBuffers(1, &vbo);
glDeleteBuffers(1, &ibo);
}
void ComputeMesh::draw()
{
glBindVertexArray(vao);
glDrawElements(GL_TRIANGLES, inds.size(), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}
void ComputeMesh::loadverts(vector<AnimVertex> iverts)
{
compVert vert;
for (int i = 0; i < iverts.size(); i++) {
for (int j = 0; j < 4; j++) {
vert.pos[j] = j < 3 ? iverts.at(i).position[j] : 1;
vert.weights[j] = iverts.at(i).m_Weights[j];
vert.boneIDs[j] = iverts.at(i).m_BoneIDs[j];
verts.push_back(vert);
}
}
vertcnt += iverts.size();
}
void ComputeMesh::loadinds(vector<GLuint> iinds)
{
for (int i = 0; i < iinds.size(); i++) inds.push_back(iinds.at(i) + vertcnt);
totinds = inds.size();
}
void ComputeMesh::init()
{
glGenVertexArrays(1, &vao);
glGenBuffers(1, &vbo);
glGenBuffers(1, &ibo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(compVert) * verts.size(), &verts[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, inds.size() * sizeof(GLuint), &inds[0], GL_STATIC_DRAW);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 48, (GLvoid*)0);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 48, (GLvoid*)(4 * sizeof(GLfloat)));
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, 48, (GLvoid*)(4 * sizeof(GLfloat)));
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, vbo);
}