Skip to content

Instantly share code, notes, and snippets.

@jqly
Last active June 25, 2019 07:33
Show Gist options
  • Save jqly/277b404106f669556423120924bb3727 to your computer and use it in GitHub Desktop.
Save jqly/277b404106f669556423120924bb3727 to your computer and use it in GitHub Desktop.
class DrawCall {
public:
DrawCall(Model* model, Mesh* mesh, GLenum mode)
: model_{model}, mesh_{mesh}, mode_{mode}
void draw()
{
if (!model->indices.empty()) {
glBindVertexArray(mesh->vao);
if (mesh->acode == vertex_attrib::PositionNormalTexcoord) {
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
} else if (mesh->acode == vertex_attrib::PositionTangent) {
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
} else {
std::cerr << "Mesh not supported.\n";
exit(1);
}
// Setting up the materials.
for (auto& part : model->parts) {
glDrawElements(mode_, part.icount, GL_UNSIGNED_INT, part.istart);
}
if (mesh->acode == vertex_attrib::PositionNormalTexcoord) {
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);
} else if (mesh->acode == vertex_attrib::PositionTangent) {
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
} else {
std::cerr << "Mesh not supported.\n";
exit(1);
}
}
glBindVertexArray(mesh->vao);
if (mesh->acode == vertex_attrib::PositionNormalTexcoord) {
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_bufs[index]);
if (mesh->primitive_restart_number != 0) {
glEnable(GL_PRIMITIVE_RESTART);
glPrimitiveRestartIndex(mesh->primitive_restart_number);
glDrawElements(GL_LINE_STRIP, model->, GL_UNSIGNED_INT, (GLvoid*)0);
}
} else if (mesh->acode == vertex_attrib::PositionTangent) {
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
} else {
std::cerr << "Mesh not supported.\n";
exit(1);
}
glEnable(GL_PRIMITIVE_RESTART);
glPrimitiveRestartIndex(0xFFFFFFFF);
glDrawElements(GL_LINE_STRIP, num_indices[index], GL_UNSIGNED_INT, (GLvoid*)0);
for (auto attrib : attribs)
glDisableVertexAttribArray(attrib);
glBindVertexArray(0);
}
private:
Model* model_;
Mesh* mesh_;
GLenum mode_;
};
void bind_vao_and_enable_attribs()
{
glBindVertexArray(mesh_->vao);
if (acode == vertex_attrib::PositionNormalTexcoord) {
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
} else if (acode == vertex_attrib::PositionTangent) {
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
} else {
std::cerr << "Mesh not supported.\n";
exit(1);
}
if (primitive_restart_number) {
glEnable(GL_PRIMITIVE_RESTART);
glPrimitiveRestartIndex(primitive_restart_number);
}
}
void unbind_vao_and_disable_attribs()
{
glBindVertexArray(mesh_->vao);
if (acode == vertex_attrib::PositionNormalTexcoord) {
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);
} else if (acode == vertex_attrib::PositionTangent) {
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
} else {
std::cerr << "Mesh not supported.\n";
exit(1);
}
if (primitive_restart_number) {
glDisable(GL_PRIMITIVE_RESTART);
glPrimitiveRestartIndex(primitive_restart_number);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment