Mesh * Mesh::GenerateQuad() { Mesh * m = new Mesh(); m->numVertices = 4; m->type = GL_TRIANGLE_STRIP; m->vertices = new Vector3[m->numVertices]; m->textureCoords = new Vector2[m->numVertices]; m->colours = new Vector4[m->numVertices]; m->normals = new Vector3[m->numVertices]; m->tangents = new Vector3[m->numVertices]; m->vertices[0] = Vector3(-1.0f, -1.0f, 0.0f); m->vertices[1] = Vector3(-1.0f, 1.0f, 0.0f); m->vertices[2] = Vector3(1.0f, -1.0f, 0.0f); m->vertices[3] = Vector3(1.0f, 1.0f, 0.0f); m->textureCoords[0] = Vector2(0.0f, 0.0f); m->textureCoords[1] = Vector2(0.0f, 1.0f); m->textureCoords[2] = Vector2(1.0f, 0.0f); m->textureCoords[3] = Vector2(1.0f, 1.0f); for (int i = 0; i < 4; ++i) { m->colours[i] = Vector4(1.0f, 1.0f, 1.0f, 1.0f); m->normals[i] = Vector3(0.0f, 0.0f, -1.0f); m->tangents[i] = Vector3(1.0f, 0.0f, 0.0f); } m->BufferData(); return m; }
Mesh* Mesh :: GenerateCircle() { Mesh* m = new Mesh(); m->type = GL_TRIANGLE_STRIP; float r = 0.05f; m->numVertices = 122; m->vertices = new Vector3[122]; m->textureCoords = new Vector2[122]; m->colours = new Vector4[122]; for(int i=0;i<61;i++) { float x = sin(2*PI/60*i)*r; float y = cos(2*PI/60*i)*r; m->vertices[i*2] = Vector3(x,y,0.0f); m->textureCoords[i*2] = Vector2(x,0.0f); m->colours[i*2] = Vector4(1.0f, 1.0f, 1.0f ,1.0f); } for(int i=0;i<61;i++) { float x = sin(2*PI/60*i)*r; float y = cos(2*PI/60*i)*r; m->vertices[i*2+1] = Vector3(x,y,0.2f); m->textureCoords[i*2+1] = Vector2(x,0.2f); m->colours[i*2+1] = Vector4(1.0f, 1.0f, 1.0f ,1.0f); } m->BufferData(); return m; }
Mesh* Mesh::GenerateQuad() { Mesh* mesh = new Mesh(); mesh->m_NumVertices = 4; mesh->m_Type = GL_TRIANGLE_STRIP; mesh->m_Vertices = new Vector3[mesh->m_NumVertices]; mesh->m_TextureCoords = new Vector2[mesh->m_NumVertices]; mesh->m_Colours = new Vector4[mesh->m_NumVertices]; mesh->m_Tangents = new Vector3[mesh->m_NumVertices]; mesh->m_Normals = new Vector3[mesh->m_NumVertices]; mesh->m_Vertices[0] = Vector3(-1.0f, -1.0f, 0.0f); mesh->m_Vertices[1] = Vector3(-1.0f, 1.0f, 0.0f); mesh->m_Vertices[2] = Vector3(1.0f, -1.0f, 0.0f); mesh->m_Vertices[3] = Vector3(1.0f, 1.0f, 0.0f); mesh->m_TextureCoords[0] = Vector2(0.0f, 1.0f); mesh->m_TextureCoords[1] = Vector2(0.0f, 0.0f); mesh->m_TextureCoords[2] = Vector2(1.0f, 1.0f); mesh->m_TextureCoords[3] = Vector2(1.0f, 0.0f); for (int i = 0; i < 4; ++i) { mesh->m_Colours[i] = Vector4(1.0f, 1.0f, 1.0f, 1.0f); mesh->m_Normals[i] = Vector3(0.0f, 0.0f, -1.0f); mesh->m_Tangents[i] = Vector3(1.0f, 0.0f, 0.0f); } //mesh->GenerateNormals(); //mesh->GenerateTangents(); mesh->BufferData(); return mesh; }
Mesh* Mesh::GenerateSphereMesh(float radius, unsigned int rings, unsigned int sectors) { Mesh* sphereMesh = new Mesh(); sphereMesh->m_Type = GL_TRIANGLES; // Generate a sphere float const RingsRecip = 1.0 / (float)(rings - 1); float const SectorsRecip = 1.0 / (float)(sectors - 1); int countRings, countSectors; sphereMesh->m_NumVertices = rings * sectors; sphereMesh->m_NumIndices = (rings - 1) * (sectors - 1) * 6; sphereMesh->m_Vertices = new Vector3[sphereMesh->m_NumVertices]; sphereMesh->m_TextureCoords = new Vector2[sphereMesh->m_NumVertices]; sphereMesh->m_Colours = new Vector4[sphereMesh->m_NumVertices]; sphereMesh->m_Indices = new unsigned int[sphereMesh->m_NumIndices]; int at = 0; // Calculate vertices' position and their respective texture coordinates for (countRings = 0; countRings < rings; countRings++) { float const y = sin(-PI / 2 + PI * countRings * RingsRecip) * radius; for (countSectors = 0; countSectors < sectors; countSectors++) { float const x = cos(2 * PI * countSectors * SectorsRecip) * sin(PI * countRings * RingsRecip); float const z = sin(2 * PI * countSectors * SectorsRecip) * sin(PI * countRings * RingsRecip); sphereMesh->m_TextureCoords[at].x = countSectors * SectorsRecip; sphereMesh->m_TextureCoords[at].y = countRings * RingsRecip; sphereMesh->m_Vertices[at].x = x * radius; sphereMesh->m_Vertices[at].y = y; sphereMesh->m_Vertices[at].z = z * radius; sphereMesh->m_Colours[at] = Vector4(1.0f, 0.0f, 0.0f, 1.0f); at++; } } at = 0; for (countRings = 0; countRings < rings - 1; countRings++) { for (countSectors = 0; countSectors < sectors - 1; countSectors++) { sphereMesh->m_Indices[at++] = (countRings + 0) * sectors + countSectors; // added for half-symmetry sphereMesh->m_Indices[at++] = (countRings + 0) * sectors + (countSectors + 1); sphereMesh->m_Indices[at++] = (countRings + 1) * sectors + (countSectors + 1); sphereMesh->m_Indices[at++] = (countRings + 0) * sectors + countSectors; sphereMesh->m_Indices[at++] = (countRings + 1) * sectors + countSectors; sphereMesh->m_Indices[at++] = (countRings + 1) * sectors + (countSectors + 1); // since we're using GL_TRIANGLE with indices to draw the mesh } } sphereMesh->GenerateNormals(); sphereMesh->GenerateTangents(); sphereMesh->BufferData(); return sphereMesh; }
Mesh* Mesh::GenerateTriangle() { Mesh* m = new Mesh(); m->numVertices = 3; //insert default vertices m->vertices = new glm::vec3[m->numVertices]; m->vertices[0] = glm::vec3(0.0f, 0.5f, 0.0f); m->vertices[1] = glm::vec3(0.5f, -0.5f, 0.0f); m->vertices[2] = glm::vec3(-0.5f, -0.5f, 0.0f); //add colors for the vertices m->colours = new glm::vec4[m->numVertices]; m->colours[0] = glm::vec4(1.0f, 0.0f, 0.0f, 1.0f); m->colours[1] = glm::vec4(0.0f, 1.0f, 0.0f, 1.0f); m->colours[2] = glm::vec4(0.0f, 0.0f, 1.0f, 1.0f); m->BufferData(); return m; }
Mesh* Mesh::GenerateTriangle() { Mesh* mesh = new Mesh(); mesh->m_NumVertices = 3; mesh->m_Vertices = new Vector3[mesh->m_NumVertices]; mesh->m_Vertices[0] = Vector3(0.0f, 0.5f, 0.0f); mesh->m_Vertices[1] = Vector3(0.5f, -0.5f, 0.0f); mesh->m_Vertices[2] = Vector3(-0.5f, -0.5f, 0.0f); mesh->m_TextureCoords = new Vector2[mesh->m_NumVertices]; mesh->m_TextureCoords[0] = Vector2(0.5f, 0.0f); mesh->m_TextureCoords[1] = Vector2(1.0f, 1.0f); mesh->m_TextureCoords[2] = Vector2(0.0f, 1.0f); mesh->m_Colours = new Vector4[mesh->m_NumVertices]; mesh->m_Colours[0] = Vector4(1.0f, 0.0f, 0.0f, 1.0f); mesh->m_Colours[1] = Vector4(0.0f, 1.0f, 0.0f, 1.0f); mesh->m_Colours[2] = Vector4(0.0f, 0.0f, 1.0f, 1.0f); mesh->BufferData(); return mesh; }
Mesh* Mesh::GenerateCubeMesh(Vector3 O, float len) { Mesh* cube = new Mesh(); cube->m_Type = GL_TRIANGLES; cube->m_NumVertices = 24; cube->m_NumIndices = 36; cube->m_Vertices = new Vector3[cube->m_NumVertices]; cube->m_Indices = new unsigned int[cube->m_NumIndices]; cube->m_Colours = new Vector4[cube->m_NumVertices]; std::vector<Vector3> vertices; std::vector<Vector4> color; std::vector<unsigned int> indices = { // Front face 0, 1, 2, 2, 3, 0, // Right face 7, 6, 5, 5, 4, 7, // Back face 11, 10, 9, 9, 8, 11, // Left face 15, 14, 13, 13, 12, 15, // Top Face 19, 18, 17, 17, 16, 19, // Bottom Face 23, 22, 21, 21, 20, 23 }; //front for (int j = 0; j < indices.size(); j++) { cube->m_Indices[j] = indices[j]; } float dim = len / 2.0f; //front cube->m_Vertices[0] = Vector3(O.x - dim, O.y - dim, O.z + dim); cube->m_Vertices[1] = Vector3(O.x + dim, O.y - dim, O.z + dim); cube->m_Vertices[2] = Vector3(O.x + dim, O.y + dim, O.z + dim); cube->m_Vertices[3] = Vector3(O.x - dim, O.y + dim, O.z + dim); // Right face cube->m_Vertices[4] = Vector3(O.x + dim, O.y - dim, O.z + dim); cube->m_Vertices[5] = Vector3(O.x + dim, O.y - dim, O.z - dim); cube->m_Vertices[6] = Vector3(O.x + dim, O.y + dim, O.z - dim); cube->m_Vertices[7] = Vector3(O.x + dim, O.y + dim, O.z + dim); // Back face cube->m_Vertices[8] = Vector3(O.x + dim, O.y - dim, O.z - dim); cube->m_Vertices[9] = Vector3(O.x - dim, O.y - dim, O.z - dim); cube->m_Vertices[10] = Vector3(O.x - dim, O.y + dim, O.z - dim); cube->m_Vertices[11] = Vector3(O.x + dim, O.y + dim, O.z - dim); // Left face cube->m_Vertices[12] = Vector3(O.x - dim, O.y - dim, O.z - dim); cube->m_Vertices[13] = Vector3(O.x - dim, O.y - dim, O.z + dim); cube->m_Vertices[14] = Vector3(O.x - dim, O.y + dim, O.z + dim); cube->m_Vertices[15] = Vector3(O.x - dim, O.y + dim, O.z - dim); // Top Face cube->m_Vertices[16] = Vector3(O.x - dim, O.y + dim, O.z + dim); cube->m_Vertices[17] = Vector3(O.x + dim, O.y + dim, O.z + dim); cube->m_Vertices[18] = Vector3(O.x + dim, O.y + dim, O.z - dim); cube->m_Vertices[19] = Vector3(O.x - dim, O.y + dim, O.z - dim); // Bottom Face cube->m_Vertices[20] = Vector3(O.x + dim, O.y - dim, O.z + dim); cube->m_Vertices[21] = Vector3(O.x - dim, O.y - dim, O.z + dim); cube->m_Vertices[22] = Vector3(O.x - dim, O.y - dim, O.z - dim); cube->m_Vertices[23] = Vector3(O.x + dim, O.y - dim, O.z - dim); for (int i = 0; i < cube->m_NumVertices; i++) { cube->m_Colours[i] = Vector4(0.0f, 0.0f, 1.0f, 1.0f); //blue } cube->BufferData(); return cube; }