#include "Dataset.h" void Dataset::sphere(float radius, GLuint slices, GLuint stacks) { clear(); // Generate vertexes and normals GLfloat theta, phi; GLfloat thetaFac = glm::two_pi() / slices; GLfloat phiFac = glm::pi() / stacks; GLfloat nx, ny, nz, s, t; GLuint idx = 0, tIdx = 0; for (GLuint i = 0; i <= slices; i++) { theta = i * thetaFac; s = (GLfloat)i / slices; for (GLuint j = 0; j <= stacks; j++) { phi = j * phiFac; t = (GLfloat)j / stacks; nx = sinf(phi) * cosf(theta); ny = sinf(phi) * sinf(theta); nz = cosf(phi); vertices.emplace_back(radius * nx, radius * ny, radius * nz); normals.emplace_back(nx, ny, nz); idx++; } } // Generate the element list idx = 0; for (GLuint i = 0; i < slices; i++) { GLuint stackStart = i * (stacks + 1); GLuint nextStackStart = (i + 1) * (stacks + 1); for (GLuint j = 0; j < stacks; j++) { if (j == 0) { GLuint a[3] = { stackStart, stackStart + 1, nextStackStart + 1 }; indexes.insert(indexes.end(), std::begin(a), std::end(a)); } else if (j == stacks - 1) { GLuint a[3] = { stackStart + j, stackStart + 1 + j, nextStackStart + j }; indexes.insert(indexes.end(), std::begin(a), std::end(a)); } else { GLuint a[6] = { stackStart + j, stackStart + 1 + j, nextStackStart + j + 1, nextStackStart + j, stackStart + j, nextStackStart + j + 1 }; indexes.insert(indexes.end(), std::begin(a), std::end(a)); } } } } void Dataset::simpleCube() { clear(); vertices = { { -1.0, -1.0, 1.0 },{ 1.0, -1.0, 1.0 },{ 1.0, 1.0, 1.0 }, { 1.0, 1.0, 1.0 },{ -1.0, 1.0, 1.0 },{ -1.0, -1.0, 1.0 }, { 1.0, -1.0, 1.0 },{ 1.0, -1.0, -1.0 },{ 1.0, 1.0, -1.0 }, { 1.0, 1.0, -1.0 },{ 1.0, 1.0, 1.0 },{ 1.0, -1.0, 1.0 }, { -1.0, 1.0, -1.0 },{ 1.0, 1.0, -1.0 },{ 1.0, -1.0, -1.0 }, { 1.0, -1.0, -1.0 },{ -1.0, -1.0, -1.0 },{ -1.0, 1.0, -1.0 }, { -1.0, -1.0, -1.0 },{ -1.0, -1.0, 1.0 },{ -1.0, 1.0, 1.0 }, { -1.0, 1.0, 1.0 },{ -1.0, 1.0, -1.0 },{ -1.0, -1.0, -1.0 }, { -1.0, -1.0, -1.0 },{ 1.0, -1.0, -1.0 },{ 1.0, -1.0, 1.0 }, { 1.0, -1.0, 1.0 },{ -1.0, -1.0, 1.0 },{ -1.0, -1.0, -1.0 }, { -1.0, 1.0, 1.0 },{ 1.0, 1.0, 1.0 },{ 1.0, 1.0, -1.0 }, { 1.0, 1.0, -1.0 },{ -1.0, 1.0, -1.0 },{ -1.0, 1.0, 1.0 } }; tex_mapping = { { 0.0f, 0.0f },{ 1.0f, 0.0f },{ 1.0f, 1.0f }, // one triangle { 0.0f, 0.0f },{ 1.0f, 1.0f },{ 0.0f, 1.0f }, //the other triangle { 0.0f, 0.0f },{ 1.0f, 0.0f },{ 1.0f, 1.0f }, // one triangle { 0.0f, 0.0f },{ 1.0f, 1.0f },{ 0.0f, 1.0f }, //the other triangle { 0.0f, 0.0f },{ 1.0f, 0.0f },{ 1.0f, 1.0f }, // one triangle { 0.0f, 0.0f },{ 1.0f, 1.0f },{ 0.0f, 1.0f }, //the other triangle { 0.0f, 0.0f },{ 1.0f, 0.0f },{ 1.0f, 1.0f }, // one triangle { 0.0f, 0.0f },{ 1.0f, 1.0f },{ 0.0f, 1.0f }, //the other triangle { 0.0f, 0.0f },{ 1.0f, 0.0f },{ 1.0f, 1.0f }, // one triangle { 0.0f, 0.0f },{ 1.0f, 1.0f },{ 0.0f, 1.0f }, //the other triangle { 0.0f, 0.0f },{ 1.0f, 0.0f },{ 1.0f, 1.0f }, // one triangle { 0.0f, 0.0f },{ 1.0f, 1.0f },{ 0.0f, 1.0f }, //the other triangle }; colors = { { 1.0, 0.0, 0.0 },{ 0.0, 1.0, 0.0 },{ 0.0, 0.0, 1.0 }, { 0.0, 0.0, 1.0 },{ 1.0, 1.0, 1.0 },{ 1.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0 },{ 0.0, 1.0, 0.0 },{ 0.0, 0.0, 1.0 }, { 0.0, 0.0, 1.0 },{ 0.0, 0.0, 1.0 },{ 0.0, 1.0, 0.0 }, { 1.0, 1.0, 1.0 },{ 0.0, 0.0, 1.0 },{ 0.0, 1.0, 0.0 }, { 0.0, 1.0, 0.0 },{ 1.0, 0.0, 0.0 },{ 1.0, 1.0, 1.0 }, { 1.0, 0.0, 0.0 },{ 1.0, 0.0, 0.0 },{ 1.0, 1.0, 1.0 }, { 1.0, 1.0, 1.0 },{ 1.0, 1.0, 1.0 },{ 1.0, 0.0, 0.0 }, { 1.0, 0.0, 0.0 },{ 0.0, 1.0, 0.0 },{ 0.0, 1.0, 0.0 }, { 0.0, 1.0, 0.0 },{ 1.0, 0.0, 0.0 },{ 1.0, 0.0, 0.0 }, { 1.0, 1.0, 1.0 },{ 0.0, 0.0, 1.0 },{ 0.0, 0.0, 1.0 }, { 0.0, 0.0, 1.0 },{ 1.0, 1.0, 1.0 },{ 1.0, 1.0, 1.0 } }, genNormals(); } void Dataset::simpleFloor() { clear(); vertices = { { -10.0, 0.0, -10.0 },{ -10.0, 0.0, 10.0 },{ 10.0, 0.0, -10.0 }, { 10.0, 0.0, 10.0 },{ 10.0, 0.0, -10.0 },{ -10.0, 0.0, 10.0 } }; colors = { { 0.7, 0.7, 0.7 },{ 0.7, 0.7, 0.7 },{ 0.7, 0.7, 0.7 }, { 0.7, 0.7, 0.7 },{ 0.7, 0.7, 0.7 },{ 0.7, 0.7, 0.7 } }; genNormals(); } void Dataset::checkeredFloor(float size, int squares, glm::vec3 light_color, glm::vec3 dark_color) { clear(); float maxX = size / 2.f, maxY = size / 2.f; float minX = -size / 2.f, minY = -size / 2.f; float side_size = size / (float)squares; int color = 1; for (float x = minX; x < maxX; x += side_size) { for (float y = minY; y < maxY; y += side_size) { color *= -1; glm::vec3 tile_color = (color > 0) ? light_color : dark_color; //gen 1 carré vertices.emplace_back(x, 0, y); //upleft vertices.emplace_back(x, 0, y + side_size); //downleft vertices.emplace_back(x + side_size, 0, y + side_size); //downright vertices.emplace_back(x + side_size, 0, y + side_size); //downright vertices.emplace_back(x + side_size, 0, y); //upright vertices.emplace_back(x, 0, y); //upleft for (int z = 0; z < 6; z++) colors.push_back(tile_color); } color *= -1; } genNormals(); } void Dataset::genNormals() { for (unsigned int i = 0; i < vertices.size() - 2; i += 3) { glm::vec3 norm = glm::cross(vertices[i + 1] - vertices[i], vertices[i + 2] - vertices[i]); normals.push_back(norm); normals.push_back(norm); normals.push_back(norm); } }