Mesh* MeshBuilder::GenerateRing(const std::string &meshName, Color color, float innerRadius, unsigned numSlices) { std::vector<Vertex> vertex_buffer_data; std::vector<GLuint> index_buffer_data; Vertex v; float anglePerSlice = 360.f / numSlices; for (unsigned i = 0; i < numSlices + 1; ++i) { float theta = i * anglePerSlice; v.pos.Set(CircleX(theta), 0, CircleZ(theta)); v.color = color; vertex_buffer_data.push_back(v); v.pos.Set(innerRadius * CircleX(theta), 0, innerRadius*CircleZ(theta)); v.color = color; vertex_buffer_data.push_back(v); } for (unsigned i = 0; i < numSlices + 1; ++i) { index_buffer_data.push_back(2 * i); index_buffer_data.push_back(2 * i + 1); } Mesh *mesh = new Mesh(meshName); glBindBuffer(GL_ARRAY_BUFFER, mesh->vertexBuffer); glBufferData(GL_ARRAY_BUFFER, vertex_buffer_data.size()*sizeof(Vertex), &vertex_buffer_data[0], GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, mesh->indexBuffer); glBufferData(GL_ARRAY_BUFFER, index_buffer_data.size()*sizeof(GLuint), &index_buffer_data[0], GL_STATIC_DRAW); mesh->indexSize = index_buffer_data.size(); mesh->mode = Mesh::DRAW_TRIANGLE_STRIP; return mesh; }
Mesh* MeshBuilder::GenerateCone(const std::string &meshName, Color color, unsigned numSlice) { std::vector<Vertex> vertex_buffer_data; std::vector<GLuint> index_buffer_data; Vertex v; //Cone shape float anglePerSlice = 360.f / numSlice; for (unsigned slice = 0; slice < numSlice + 1; ++slice) { float theta = slice * anglePerSlice; v.pos.Set(CircleX(theta), 0, CircleZ(theta)); v.color = color; v.normal.Set (CircleX(theta), 1, CircleZ(theta)); vertex_buffer_data.push_back(v); } v.pos.Set(0, 1 , 0); v.color = color; vertex_buffer_data.push_back(v); for (unsigned slice = 0; slice < numSlice + 1; slice++) { index_buffer_data.push_back(slice); index_buffer_data.push_back(numSlice + 1); } //Circle for (unsigned slice = 0; slice < numSlice + 1; ++slice) { float theta = slice * anglePerSlice; v.pos.Set(CircleX(theta), 0, CircleZ(theta)); v.color = color; v.normal = (0, -1, 0); vertex_buffer_data.push_back(v); } for (unsigned slice = 0; slice < numSlice + 1; ++slice) { v.pos.Set(0, 0, 0); v.color = color; vertex_buffer_data.push_back(v); } for (unsigned slice = 0; slice < numSlice + 1; slice++) { index_buffer_data.push_back(slice); index_buffer_data.push_back(numSlice); } Mesh *mesh = new Mesh(meshName); glBindBuffer(GL_ARRAY_BUFFER, mesh->vertexBuffer); glBufferData(GL_ARRAY_BUFFER, vertex_buffer_data.size() * sizeof(Vertex), &vertex_buffer_data[0], GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh->indexBuffer); glBufferData(GL_ELEMENT_ARRAY_BUFFER, index_buffer_data.size() * sizeof(GLuint), &index_buffer_data[0], GL_STATIC_DRAW); mesh->indexSize = index_buffer_data.size(); mesh->mode = Mesh::DRAW_TRIANGLE_STRIP; return mesh; }
Mesh* MeshBuilder::GenerateCone(const std::string &meshName, Color color, unsigned numSlices) { std::vector<Vertex> cone_vertex_buffer_data; std::vector<GLuint> cone_index_buffer_data; Vertex cone; float anglePerSlice = 360.f / numSlices; for (unsigned slice = 0; slice < numSlices + 1; ++slice) { float theta = slice * anglePerSlice; cone.pos.Set(CircleX(theta), 0, CircleZ(theta)); cone.color = color; cone.normal.Set(0, 1, 0); cone_vertex_buffer_data.push_back(cone); } cone.pos.Set(0, 1, 0); cone.color = color; cone.normal.Set(0, 1, 0); cone_vertex_buffer_data.push_back(cone); for (unsigned slice = 0; slice < numSlices + 1; ++slice) { cone_index_buffer_data.push_back(slice); cone_index_buffer_data.push_back(numSlices + 1); } for (unsigned slice = 0; slice < numSlices + 1; ++slice) { float theta = slice * anglePerSlice; cone.pos.Set(CircleX(theta), 0, CircleZ(theta)); cone.color = color; cone.normal.Set(0, -1, 0); cone_vertex_buffer_data.push_back(cone); } for (unsigned slice = numSlices + 1; slice > 0; --slice) { cone_index_buffer_data.push_back(slice); cone_index_buffer_data.push_back(numSlices + 1); } Mesh *mesh = new Mesh(meshName); glBindBuffer(GL_ARRAY_BUFFER, mesh->vertexBuffer); glBufferData(GL_ARRAY_BUFFER, cone_vertex_buffer_data.size() * sizeof(Vertex), &cone_vertex_buffer_data[0], GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, mesh->indexBuffer); glBufferData(GL_ARRAY_BUFFER, cone_index_buffer_data.size() * sizeof(GLuint), &cone_index_buffer_data[0], GL_STATIC_DRAW); mesh->indexSize = cone_index_buffer_data.size(); mesh->mode = Mesh::DRAW_TRIANGLE_STRIP; return mesh; }
Mesh* MeshBuilder::GenerateRing(const std::string &meshName, Color color, float outerRadius, float innerRadius, unsigned numSlices) { std::vector<Vertex> vertex_buffer_data; std::vector<GLuint> index_buffer_data; Vertex v; float scaleRatio = 10; float anglePerSlice = 360.0f / numSlices; //Top for (unsigned i = 0; i < numSlices + 1; ++i) { float theta = i * anglePerSlice; v.pos.Set((innerRadius / scaleRatio)*CircleX(theta), 0, (innerRadius / scaleRatio)*CircleZ(theta)); v.color = color; v.normal.Set(0, 1, 0); vertex_buffer_data.push_back(v); v.pos.Set((outerRadius / scaleRatio)*CircleX(theta), 0, (outerRadius / scaleRatio)*CircleZ(theta)); v.normal.Set(0, 1, 0); v.color = color; vertex_buffer_data.push_back(v); } //Underside for (unsigned i = 0; i < numSlices + 1; ++i) { float theta = i * -anglePerSlice; v.pos.Set((innerRadius / scaleRatio)*CircleX(theta), -0.01, (innerRadius / scaleRatio)*CircleZ(theta)); v.color = color; v.normal.Set(0, -1, 0); vertex_buffer_data.push_back(v); v.pos.Set((outerRadius / scaleRatio)*CircleX(theta), -0.01, (outerRadius / scaleRatio)*CircleZ(theta)); v.normal.Set(0, -1, 0); v.color = color; vertex_buffer_data.push_back(v); } for (unsigned i = 0; i < vertex_buffer_data.size(); ++i) { index_buffer_data.push_back(2 * i); index_buffer_data.push_back(2 * i + 1); } Mesh *mesh = new Mesh(meshName); glBindBuffer(GL_ARRAY_BUFFER, mesh->vertexBuffer); glBufferData(GL_ARRAY_BUFFER, vertex_buffer_data.size() * sizeof(Vertex), &vertex_buffer_data[0], GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh->indexBuffer); glBufferData(GL_ELEMENT_ARRAY_BUFFER, index_buffer_data.size() * sizeof(GLuint), &index_buffer_data[0], GL_STATIC_DRAW); mesh->indexSize = index_buffer_data.size(); mesh->mode = Mesh::DRAW_TRIANGLE_STRIP; return mesh; }
Mesh* MeshBuilder::GenerateCone(const std::string &meshName, Color color, unsigned numStacks) { std::vector<Vertex> vertex_buffer_data; std::vector<GLuint> index_buffer_data; Vertex v; float height = 1; float radius = 1; float increments = 360.0f / numStacks; for (int theta = 0; theta <= 360; theta += increments) //top { v.pos.Set(radius * CircleX(theta), -height, radius * CircleZ(theta)); v.color = color; v.normal.Set(v.pos.x, 1, v.pos.z); v.normal.Normalize(); vertex_buffer_data.push_back(v); v.pos.Set(0, height, 0); v.color = color; v.normal.Set(v.pos.x, 1, v.pos.z); v.normal.Normalize(); vertex_buffer_data.push_back(v); } for (int theta = 0; theta <= 360; theta += increments) //bottom { v.pos.Set(0, -height, 0); v.color = color; v.normal.Set(0, -1, 0); vertex_buffer_data.push_back(v); v.color = color; v.pos.Set(radius * CircleX(theta), -height, radius * CircleZ(theta)); v.normal.Set(0, -1, 0); vertex_buffer_data.push_back(v); } for (unsigned i = 0; i < vertex_buffer_data.size(); ++i) { index_buffer_data.push_back(2 * i); index_buffer_data.push_back(2 * i + 1); } Mesh *mesh = new Mesh(meshName); glBindBuffer(GL_ARRAY_BUFFER, mesh->vertexBuffer); glBufferData(GL_ARRAY_BUFFER, vertex_buffer_data.size() * sizeof(Vertex), &vertex_buffer_data[0], GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh->indexBuffer); glBufferData(GL_ELEMENT_ARRAY_BUFFER, index_buffer_data.size() * sizeof(GLuint), &index_buffer_data[0], GL_STATIC_DRAW); mesh->indexSize = index_buffer_data.size(); mesh->mode = Mesh::DRAW_TRIANGLE_STRIP; return mesh; }
Mesh* MeshBuilder::GenerateRing(const std::string &meshName, Color color, float innerRadius, unsigned numSlices) { std::vector<Vertex> ring_vertex_buffer_data; std::vector<GLuint> ring_index_buffer_data; Vertex ring; float anglePerSlice = 360.f / numSlices; for (unsigned slice = 0; slice < numSlices + 1; ++slice) { float theta = slice * anglePerSlice; ring.pos.Set(CircleX(theta), 0, CircleZ(theta)); ring.color = color; ring.normal.Set(0, 1, 0); ring_vertex_buffer_data.push_back(ring); ring.pos.Set(innerRadius * CircleX(theta), 0, innerRadius * CircleZ(theta)); ring.color = color; ring.normal.Set(0, 1, 0); ring_vertex_buffer_data.push_back(ring); } for (unsigned slice = 0; slice < numSlices + 1; ++slice) { ring_index_buffer_data.push_back(2 * slice); ring_index_buffer_data.push_back(2 * slice + 1); } Mesh *mesh = new Mesh(meshName); glBindBuffer(GL_ARRAY_BUFFER, mesh->vertexBuffer); glBufferData(GL_ARRAY_BUFFER, ring_vertex_buffer_data.size() * sizeof(Vertex), &ring_vertex_buffer_data[0], GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, mesh->indexBuffer); glBufferData(GL_ARRAY_BUFFER, ring_index_buffer_data.size() * sizeof(GLuint), &ring_index_buffer_data[0], GL_STATIC_DRAW); mesh->indexSize = ring_index_buffer_data.size(); mesh->mode = Mesh::DRAW_TRIANGLE_STRIP; return mesh; }
Mesh* MeshBuilder::GenerateCylinder(const std::string &meshName, Color color, unsigned numStacks, unsigned numSlices) { std::vector<Vertex> cylinder_vertex_buffer_data; std::vector<GLuint> cylinder_index_buffer_data; Vertex cylinder; float height = 5.f; float stackHeight = height / numStacks; float angleperslice = 360.f / numSlices; for (unsigned stack = 0; stack < numStacks + 1; ++stack) // stack { for (float theta = 0; theta <= 360; theta += 10) // slice { //add_normal(x(theta), 0, z(theta)) cylinder.normal.Set(CircleX(theta), 0, CircleZ(theta)); cylinder.pos.Set(CircleX(theta), (-height / 2) + stack * stackHeight, CircleZ(theta)); cylinder.normal.Set(CircleX(theta), 0, CircleZ(theta)); cylinder.pos.Set(CircleX(theta), (-height / 2) + (stack + 1) * stackHeight, CircleZ(theta)); cylinder_vertex_buffer_data.push_back(cylinder); cylinder.color = color; } } for (float theta = 0; theta <= 360; theta += 10) // top { cylinder.normal.Set(0, 1, 0); cylinder.pos.Set(0, -height / 2, 0); cylinder.normal.Set(0, 1, 0); cylinder.pos.Set(CircleX(theta), height / 2, CircleZ(theta)); cylinder_vertex_buffer_data.push_back(cylinder); cylinder.color = color; } for (float theta = 0; theta <= 360; theta += 10) // bottom { cylinder.normal.Set(0, -1, 0); cylinder.pos.Set(0, -height / 2, 0); cylinder.normal.Set(0, -1, 0); cylinder.pos.Set(CircleX(theta), -height / 2, CircleZ(theta)); cylinder_vertex_buffer_data.push_back(cylinder); cylinder.color = color; } for (unsigned stack = 0; stack < numStacks + 1; stack += 1) // stack { for (unsigned slice = 0; slice * angleperslice <= 360; slice += 1) // slice { cylinder_index_buffer_data.push_back(stack * (numSlices + 1) + slice); // Note: numslice is used because it represent one 'round' cylinder_index_buffer_data.push_back((stack + 1) * (numSlices + 1) + slice); } } for (unsigned slice = numSlices + 1; slice > 0; --slice) { cylinder_index_buffer_data.push_back(slice); cylinder_index_buffer_data.push_back(numSlices + 1); } for (unsigned slice = numSlices + 1; slice > 0; --slice) { cylinder_index_buffer_data.push_back(slice); cylinder_index_buffer_data.push_back(numSlices + 1); } //OTHER CODE //float anglePerSlice = 360.f / numSlices; //for (unsigned slice = 0; slice < numSlices + 1; ++slice) //{ // float theta = slice * anglePerSlice; // cylinder.pos.Set(CircleX(theta), -0.5f, CircleZ(theta)); // cylinder.color = color; // cylinder.normal.Set(CircleX(theta), 0, CircleZ(theta)); // cylinder_vertex_buffer_data.push_back(cylinder); // cylinder.pos.Set(CircleX(theta), 0.5f, CircleZ(theta)); // cylinder.color = color; // cylinder.normal.Set(CircleX(theta), 0, CircleZ(theta)); // cylinder_vertex_buffer_data.push_back(cylinder); //} // //for (unsigned slice = 0; slice < numSlices + 1; ++slice) //{ // cylinder_index_buffer_data.push_back(2 * slice); // cylinder_index_buffer_data.push_back(2 * slice + 1); //} Mesh *mesh = new Mesh(meshName); glBindBuffer(GL_ARRAY_BUFFER, mesh->vertexBuffer); glBufferData(GL_ARRAY_BUFFER, cylinder_vertex_buffer_data.size() * sizeof(Vertex), &cylinder_vertex_buffer_data[0], GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, mesh->indexBuffer); glBufferData(GL_ARRAY_BUFFER, cylinder_index_buffer_data.size() * sizeof(GLuint), &cylinder_index_buffer_data[0], GL_STATIC_DRAW); mesh->indexSize = cylinder_index_buffer_data.size(); mesh->mode = Mesh::DRAW_TRIANGLE_STRIP; return mesh; }
Mesh* MeshBuilder::GenerateHemisphere(const std::string &meshName, Color color, unsigned numStacks, unsigned numSlices) { std::vector<Vertex> hemisphere_vertex_buffer_data; std::vector<GLuint> hemisphere_index_buffer_data; Vertex hemisphere; float anglePerStack = 180.f / numStacks; float anglePerSlice = 360.f / numSlices; //Hemisphere for (unsigned stack = 0; stack < numStacks + 1; ++stack) { float phi = 0.f + stack * anglePerStack; for (unsigned slice = 0; slice < numSlices + 1; ++slice) { float theta = slice * anglePerSlice; hemisphere.pos.Set(SphereX(phi, theta), SphereY(phi, theta), SphereZ(phi, theta)); hemisphere.normal.Set(SphereX(phi, theta), SphereY(phi, theta), SphereZ(phi, theta)); hemisphere.color = color; hemisphere_vertex_buffer_data.push_back(hemisphere); hemisphere.normal.Set(SphereX(phi + 10, theta), SphereY(phi + 10, theta), SphereZ(phi + 10, theta)); } } //Bottom for (float theta = 0; theta <= 360; theta += 10) { hemisphere.normal.Set(0, -1, 0); hemisphere.pos.Set(0, 0, 0); hemisphere.normal.Set(0, -1, 0); hemisphere.pos.Set(CircleX(theta), 0, CircleZ(theta)); hemisphere.color = color; hemisphere_vertex_buffer_data.push_back(hemisphere); } for (unsigned stack = 0; stack < numStacks; ++stack) { for (unsigned slice = 0; slice < numSlices + 1; ++slice) { hemisphere_index_buffer_data.push_back(stack * (numSlices + 1) + slice); hemisphere_index_buffer_data.push_back((stack + 1) * (numSlices + 1) + slice); } } for (unsigned slice = 0; slice < numSlices + 1; ++slice) { hemisphere_index_buffer_data.push_back(slice); hemisphere_index_buffer_data.push_back(numSlices + 1); } Mesh *mesh = new Mesh(meshName); glBindBuffer(GL_ARRAY_BUFFER, mesh->vertexBuffer); glBufferData(GL_ARRAY_BUFFER, hemisphere_vertex_buffer_data.size() * sizeof(Vertex), &hemisphere_vertex_buffer_data[0], GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, mesh->indexBuffer); glBufferData(GL_ARRAY_BUFFER, hemisphere_index_buffer_data.size() * sizeof(GLuint), &hemisphere_index_buffer_data[0], GL_STATIC_DRAW); mesh->indexSize = hemisphere_index_buffer_data.size(); mesh->mode = Mesh::DRAW_TRIANGLE_STRIP; return mesh; }
Mesh* MeshBuilder::GenerateCylinder(const std::string &meshName, Color color, unsigned numStacks) { std::vector<Vertex> vertex_buffer_data; std::vector<GLuint> index_buffer_data; Vertex v; float height = 1; float radius = 1; int stackHeight = height / numStacks; float angleperSlice = 360.f / numStacks; for (unsigned slice = 0; slice < numStacks + 1; ++slice) { float theta = angleperSlice * slice; v.pos.Set(CircleX(theta), 0, CircleZ(theta)); v.normal.Set(CircleX(theta), 0, CircleZ(theta)); v.color = color; vertex_buffer_data.push_back(v); v.pos.Set(CircleX(theta), height, CircleZ(theta)); v.color = color; v.normal.Set(CircleX(theta), height, CircleZ(theta)); vertex_buffer_data.push_back(v); }; for (int theta = 0; theta <= 360; theta += 10) //top { v.pos.Set(radius * CircleX(theta), height, radius * CircleZ(theta)); v.color = color; v.normal.Set(0, 1, 0); vertex_buffer_data.push_back(v); v.pos.Set(0, height, 0); v.color = color; v.normal.Set(0, 1, 0); vertex_buffer_data.push_back(v); } for (int theta = 0; theta <= 360; theta += 10) //bottom { v.pos.Set(0, 0, 0); v.color = color; v.normal.Set(0, -1, 0); vertex_buffer_data.push_back(v); v.color = color; v.pos.Set(radius * CircleX(theta), 0, radius * CircleZ(theta)); v.normal.Set(0, -1, 0); vertex_buffer_data.push_back(v); } for (unsigned slice = 0; slice < vertex_buffer_data.size() - 3; ++slice) { index_buffer_data.push_back((2 * slice)); index_buffer_data.push_back((2 * slice) + 1); }; for (unsigned i = 0; i < vertex_buffer_data.size(); ++i) { index_buffer_data.push_back(2 * i); index_buffer_data.push_back(2 * i + 1); } Mesh *mesh = new Mesh(meshName); glBindBuffer(GL_ARRAY_BUFFER, mesh->vertexBuffer); glBufferData(GL_ARRAY_BUFFER, vertex_buffer_data.size() * sizeof(Vertex), &vertex_buffer_data[0], GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh->indexBuffer); glBufferData(GL_ELEMENT_ARRAY_BUFFER, index_buffer_data.size() * sizeof(GLuint), &index_buffer_data[0], GL_STATIC_DRAW); mesh->indexSize = index_buffer_data.size(); mesh->mode = Mesh::DRAW_TRIANGLE_STRIP; return mesh; }