From 9d5c7eaac6e14caa0c5640efbf973e603eee0cf8 Mon Sep 17 00:00:00 2001 From: Matt Strapp Date: Mon, 18 Oct 2021 20:43:13 -0500 Subject: do a3 --- dev/a3-earthquake/earth.cc | 93 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 65 insertions(+), 28 deletions(-) (limited to 'dev/a3-earthquake/earth.cc') diff --git a/dev/a3-earthquake/earth.cc b/dev/a3-earthquake/earth.cc index db43b10..e2c87da 100644 --- a/dev/a3-earthquake/earth.cc +++ b/dev/a3-earthquake/earth.cc @@ -17,6 +17,10 @@ Earth::Earth() { Earth::~Earth() { } +float lerp(float x, float y, float a) { + return x + a * (y - x); +} + void Earth::Init(const std::vector &search_path) { // init shader program shader_.Init(); @@ -25,37 +29,65 @@ void Earth::Init(const std::vector &search_path) { earth_tex_.InitFromFile(Platform::FindFile("earth-2k.png", search_path)); // init geometry - const int nslices = 10; - const int nstacks = 10; + + std::vector tex; // TODO: This is where you need to set the vertices and indiceds for earth_mesh_. - // As a demo, we'll add a square with 2 triangles. - std::vector indices; - std::vector vertices; - // four vertices - vertices.push_back(Point3(0,0,0)); - vertices.push_back(Point3(1,0,0)); - vertices.push_back(Point3(1,1,0)); - vertices.push_back(Point3(0,1,0)); + for (int i = 0; i < nslices + 1; i++) { + for (int j = 0; j < nstacks + 1; j++) { + Point3 sphere_point = Point3(LatLongToSphere(lerp(-M_PI / 2, M_PI / 2, j * 1.0 / nstacks), lerp(-M_PI, M_PI, i * 1.0 / nslices))); + Point3 plane_point = Point3(lerp(-M_PI, M_PI, i * 1.0 / nslices), lerp(-M_PI / 2, M_PI / 2, j * 1.0 / nstacks), 0); + vertices_.push_back(plane_point); + sphVertices_.push_back(sphere_point); + normals_.push_back(Vector3(0,0,1)); + sphNormals_.push_back(sphere_point - Point3(0,0,0)); + tex_.push_back(Point2(i * 1.0 / nslices, 1 - j * 1.0 / nstacks)); + } + } + + //Lower triangles + for (int i = 0; i < nslices; i++) { + for (int j = 0; j < nstacks; j++) { + indices_.push_back(0 + j + (nstacks + 1) * i); + indices_.push_back(nstacks + 1 + j + (nstacks + 1) * i); + indices_.push_back(1 + j + (nstacks + 1) * i); + } + } + + //Upper triangles + for (int i = 0; i < nslices; i++) { + for (int j = 0; j < nstacks; j++) { + indices_.push_back(1 + j + (nstacks + 1) * i); + indices_.push_back(nstacks + 1 + j + (nstacks + 1) * i); + indices_.push_back(nstacks + 2 + j + (nstacks + 1) * i); + } + } - // indices into the arrays above for the first triangle - indices.push_back(0); - indices.push_back(1); - indices.push_back(2); - // indices for the second triangle, note some are reused - indices.push_back(0); - indices.push_back(2); - indices.push_back(3); - earth_mesh_.SetVertices(vertices); - earth_mesh_.SetIndices(indices); - earth_mesh_.UpdateGPUMemory(); + earth_mesh_.SetVertices(vertices_); + earth_mesh_.SetIndices(indices_); + earth_mesh_.SetTexCoords(0, tex_); + earth_mesh_.SetNormals(normals_); + } - +void Earth::set_globe_mode(bool globe_mode) { + if (globe_mode) { + earth_mesh_.SetVertices(sphVertices_); + earth_mesh_.SetIndices(indices_); + earth_mesh_.SetTexCoords(0, tex_); + earth_mesh_.SetNormals(sphNormals_); + } else { + earth_mesh_.SetVertices(vertices_); + earth_mesh_.SetIndices(indices_); + earth_mesh_.SetTexCoords(0, tex_); + earth_mesh_.SetNormals(normals_); + } + earth_mesh_.UpdateGPUMemory(); +} void Earth::Draw(const Matrix4 &model_matrix, const Matrix4 &view_matrix, const Matrix4 &proj_matrix) { // Define a really bright white light. Lighting is a property of the "shader" @@ -86,13 +118,18 @@ void Earth::Draw(const Matrix4 &model_matrix, const Matrix4 &view_matrix, const Point3 Earth::LatLongToSphere(double latitude, double longitude) const { // TODO: We recommend filling in this function to put all your // lat,long --> sphere calculations in one place. - return Point3(0,0,0); + float x = cos(latitude) * sin(longitude); + float y = sin(latitude); + float z = cos(latitude) * cos(longitude); + + return Point3(x, y, z); } Point3 Earth::LatLongToPlane(double latitude, double longitude) const { // TODO: We recommend filling in this function to put all your // lat,long --> plane calculations in one place. - return Point3(0,0,0); + // return Point3(latitude,longitude,0); + return Point3(GfxMath::ToRadians(longitude), GfxMath::ToRadians(latitude), 0); } @@ -102,11 +139,11 @@ void Earth::DrawDebugInfo(const Matrix4 &model_matrix, const Matrix4 &view_matri // So it will be very slow if you have a large mesh, but it's quite useful when you are // debugging your mesh code, especially if you start with a small mesh. for (int t=0; t indices = earth_mesh_.read_triangle_indices_data(t); + std::vector indices_ = earth_mesh_.read_triangle_indices_data(t); std::vector loop; - loop.push_back(earth_mesh_.read_vertex_data(indices[0])); - loop.push_back(earth_mesh_.read_vertex_data(indices[1])); - loop.push_back(earth_mesh_.read_vertex_data(indices[2])); + loop.push_back(earth_mesh_.read_vertex_data(indices_[0])); + loop.push_back(earth_mesh_.read_vertex_data(indices_[1])); + loop.push_back(earth_mesh_.read_vertex_data(indices_[2])); quick_shapes_.DrawLines(model_matrix, view_matrix, proj_matrix, Color(1,1,0), loop, QuickShapes::LinesType::LINE_LOOP, 0.005f); } -- cgit v1.2.3