refactor(rendering): use VBOs for torus instead of deprecated immediate mode
This commit is contained in:
parent
1e5e60bbcf
commit
527f159ff3
2 changed files with 59 additions and 13 deletions
|
@ -577,6 +577,7 @@ void renderRotationArcs() {
|
|||
handleShader->set("viewPos", camera.cameraPos);
|
||||
|
||||
PartAssembly assembly = PartAssembly::FromSelection(gDataModel->GetService<Selection>());
|
||||
if (assembly.size() == Vector3::ZERO) return; // No parts are selected
|
||||
|
||||
for (HandleFace face : HandleFace::Faces) {
|
||||
if (glm::any(glm::lessThan(face.normal, glm::vec3(0)))) continue;
|
||||
|
|
|
@ -7,7 +7,42 @@
|
|||
|
||||
struct vec { float x; float y; float z; };
|
||||
|
||||
void genTorusPoint(float outerRadius, float innerRadius, float tubeSides, float ringSides, int tube, int ring) {
|
||||
unsigned int torusVAO, torusVBO;
|
||||
int lastSize = 0;
|
||||
void initTorus(int tubeSides, int ringSides) {
|
||||
// Free existing buffer
|
||||
if (torusVAO != 0)
|
||||
glDeleteVertexArrays(1, &torusVAO);
|
||||
if (torusVBO != 0)
|
||||
glDeleteBuffers(1, &torusVBO);
|
||||
|
||||
lastSize = tubeSides * ringSides;
|
||||
|
||||
// Set up buffer
|
||||
glGenVertexArrays(1, &torusVAO);
|
||||
glBindVertexArray(torusVAO);
|
||||
|
||||
glGenBuffers(1, &torusVBO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, torusVBO);
|
||||
|
||||
// Dynamic, because we update the vertices often V~~~~~~~~~~~~~~
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 8 * 6 * tubeSides * ringSides, NULL, GL_DYNAMIC_DRAW);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(0 * sizeof(float)));
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
|
||||
glEnableVertexAttribArray(2);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
void genTorusPoint(float* vertex, float outerRadius, float innerRadius, float tubeSides, float ringSides, int tube, int ring) {
|
||||
float angle = float(tube) / tubeSides * 2 * PI;
|
||||
float xL = innerRadius * cos(angle);
|
||||
float yL = innerRadius * sin(angle);
|
||||
|
@ -17,31 +52,41 @@ void genTorusPoint(float outerRadius, float innerRadius, float tubeSides, float
|
|||
float y = (outerRadius + xL) * sin(outerAngle);
|
||||
float z = yL;
|
||||
|
||||
// return vec { x, y, z };
|
||||
glVertex3f(x, y, z);
|
||||
vertex[0] = x; vertex[1] = y; vertex[2] = z;
|
||||
// TODO: Add normals and tex coords
|
||||
}
|
||||
|
||||
// made by yours truly
|
||||
void genTorus(float outerRadius, float innerRadius, int tubeSides, int ringSides) {
|
||||
glBegin(GL_TRIANGLES);
|
||||
// Automatically generate VBO ad-hoc
|
||||
if (lastSize != tubeSides * ringSides)
|
||||
initTorus(tubeSides, ringSides);
|
||||
|
||||
float* vertices = new float[(tubeSides * ringSides * 6) * 8];
|
||||
|
||||
int vi = 0;
|
||||
for (int i = 0; i < tubeSides; i++) {
|
||||
float tube = float(i) / tubeSides;
|
||||
for (int j = 0; j < ringSides; j++) {
|
||||
float ring = float(j) / ringSides;
|
||||
|
||||
int in = (i+1) % tubeSides;
|
||||
int jn = (j+1) % tubeSides;
|
||||
|
||||
genTorusPoint(outerRadius, innerRadius, tubeSides, ringSides, i, j);
|
||||
genTorusPoint(outerRadius, innerRadius, tubeSides, ringSides, in, j);
|
||||
genTorusPoint(outerRadius, innerRadius, tubeSides, ringSides, in, jn);
|
||||
genTorusPoint(&vertices[8*vi++], outerRadius, innerRadius, tubeSides, ringSides, i, j);
|
||||
genTorusPoint(&vertices[8*vi++], outerRadius, innerRadius, tubeSides, ringSides, in, j);
|
||||
genTorusPoint(&vertices[8*vi++], outerRadius, innerRadius, tubeSides, ringSides, in, jn);
|
||||
|
||||
genTorusPoint(outerRadius, innerRadius, tubeSides, ringSides, in, jn);
|
||||
genTorusPoint(outerRadius, innerRadius, tubeSides, ringSides, i, jn);
|
||||
genTorusPoint(outerRadius, innerRadius, tubeSides, ringSides, i, j);
|
||||
genTorusPoint(&vertices[8*vi++], outerRadius, innerRadius, tubeSides, ringSides, in, jn);
|
||||
genTorusPoint(&vertices[8*vi++], outerRadius, innerRadius, tubeSides, ringSides, i, jn);
|
||||
genTorusPoint(&vertices[8*vi++], outerRadius, innerRadius, tubeSides, ringSides, i, j);
|
||||
}
|
||||
}
|
||||
|
||||
glEnd();
|
||||
// Bind new data
|
||||
glBindBuffer(GL_ARRAY_BUFFER, torusVBO);
|
||||
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float) * 8 * 6 * tubeSides * ringSides, vertices);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
|
||||
// Draw
|
||||
glBindVertexArray(torusVAO);
|
||||
glDrawArrays(GL_TRIANGLES, 0, tubeSides * ringSides * 6);
|
||||
}
|
Loading…
Add table
Reference in a new issue