summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--dev/a3-earthquake/README.md7
-rw-r--r--dev/a3-earthquake/earth.cc93
-rw-r--r--dev/a3-earthquake/earth.h33
-rw-r--r--dev/a3-earthquake/quake_app.cc63
-rw-r--r--dev/a3-earthquake/quake_app.h8
5 files changed, 162 insertions, 42 deletions
diff --git a/dev/a3-earthquake/README.md b/dev/a3-earthquake/README.md
index 1b32e4c..9a1af8e 100644
--- a/dev/a3-earthquake/README.md
+++ b/dev/a3-earthquake/README.md
@@ -1 +1,8 @@
# CSci-4611 Assignment 3: Earthquake
+## By Matt Strapp
+I didn't forget to do this this time!
+
+### Design Decisions
+The mapping I chose for the colors of the earthquakes was based on a [graph](https://upload.wikimedia.org/wikipedia/commons/d/de/Earthquake_severity.jpg) I found on Wikipedia. The size is based on the magnitude of the earthquake, normalized to the largest one in the database and how recent the quake was.
+
+Most of them are red (high magnitude) which does not seem correct. \ No newline at end of file
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<std::string> &search_path) {
// init shader program
shader_.Init();
@@ -25,37 +29,65 @@ void Earth::Init(const std::vector<std::string> &search_path) {
earth_tex_.InitFromFile(Platform::FindFile("earth-2k.png", search_path));
// init geometry
- const int nslices = 10;
- const int nstacks = 10;
+
+ std::vector<Point2> 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<unsigned int> indices;
- std::vector<Point3> 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<earth_mesh_.num_triangles(); t++) {
- std::vector<unsigned int> indices = earth_mesh_.read_triangle_indices_data(t);
+ std::vector<unsigned int> indices_ = earth_mesh_.read_triangle_indices_data(t);
std::vector<Point3> 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);
}
diff --git a/dev/a3-earthquake/earth.h b/dev/a3-earthquake/earth.h
index 2091c11..877b8ab 100644
--- a/dev/a3-earthquake/earth.h
+++ b/dev/a3-earthquake/earth.h
@@ -15,14 +15,14 @@ class Earth {
public:
Earth();
virtual ~Earth();
-
+
/// Load texture and define geometry. Initializes the mesh to the planar
/// version of the earth. The searchPath is for finding the texture file.
- void Init(const std::vector<std::string> &search_path);
+ void Init(const std::vector<std::string>& search_path);
/// Draw the Earth to screen using the current version of the mesh set with
/// the last call to UpdateMesh.
- void Draw(const Matrix4 &model_matrix, const Matrix4 &view_matrix, const Matrix4 &proj_matrix);
+ void Draw(const Matrix4& model_matrix, const Matrix4& view_matrix, const Matrix4& proj_matrix);
/// Given latitude and longitude, calculate 3D position for the flat earth
/// model that lies on a plane
@@ -31,25 +31,38 @@ public:
/// Given latitude and longitude, calculate the 3D position for the spherical
/// earth model.
Point3 LatLongToSphere(double latitude, double longitude) const;
-
+
/// This can be a helpful debugging aid when creating your triangle mesh. It
/// draws the triangles and normals for the current earth mesh.
- void DrawDebugInfo(const Matrix4 &model_matrix, const Matrix4 &view_matrix, const Matrix4 &proj_matrix);
-
+ void DrawDebugInfo(const Matrix4& model_matrix, const Matrix4& view_matrix, const Matrix4& proj_matrix);
+
+ /// Drawing either plane or sphere
+ void set_globe_mode(bool mode);
+
protected:
-
+ const int nslices = 10;
+ const int nstacks = 10;
+
// Stores the earth texture map
Texture2D earth_tex_;
-
+
// Stores the earth geometry as a renderable mesh
Mesh earth_mesh_;
-
+
// Renders meshes with texture and some simple shading
DefaultShader shader_;
DefaultShader::MaterialProperties earth_material_;
-
+
// Used only for the DrawDebugInfo() routine
QuickShapes quick_shapes_;
+
+ //Helper values needed for making earth
+ std::vector<unsigned int> indices_;
+ std::vector<Point3> vertices_;
+ std::vector<Point3> sphVertices_;
+ std::vector<Vector3> normals_;
+ std::vector<Vector3> sphNormals_;
+ std::vector<Point2> tex_;
};
diff --git a/dev/a3-earthquake/quake_app.cc b/dev/a3-earthquake/quake_app.cc
index 7904bf3..ead8dca 100644
--- a/dev/a3-earthquake/quake_app.cc
+++ b/dev/a3-earthquake/quake_app.cc
@@ -78,6 +78,14 @@ void QuakeApp::OnLeftMouseDrag(const Point2 &pos, const Vector2 &delta) {
void QuakeApp::OnGlobeBtnPressed() {
// TODO: This is where you can switch between flat earth mode and globe mode
+ globe_mode_ = !globe_mode_;
+ earth_.set_globe_mode(globe_mode_);
+ if (globe_mode_)
+ globe_btn_->setCaption("Map");
+ else
+ globe_btn_->setCaption("Globe");
+
+
}
void QuakeApp::OnDebugBtnPressed() {
@@ -109,6 +117,11 @@ void QuakeApp::UpdateSimulation(double dt) {
// TODO: Any animation, morphing, rotation of the earth, or other things that should
// be updated once each frame would go here.
+ if (globe_mode_) {
+ rotation_angle_ += GfxMath::ToRadians(5) / 60;
+ } else {
+ rotation_angle_ = 0;
+ }
}
@@ -123,8 +136,9 @@ void QuakeApp::InitOpenGL() {
// Initialize the texture used for the background image
stars_tex_.InitFromFile(Platform::FindFile("iss006e40544.png", search_path_));
-}
+ globe_mode_ = false;
+}
void QuakeApp::DrawUsingOpenGL() {
quick_shapes_.DrawFullscreenTexture(Color(1,1,1), stars_tex_);
@@ -132,9 +146,10 @@ void QuakeApp::DrawUsingOpenGL() {
// You can leave this as the identity matrix and we will have a fine view of
// the earth. If you want to add any rotation or other animation of the
// earth, the model_matrix is where you would apply that.
- Matrix4 model_matrix;
+ Matrix4 model_matrix = Matrix4::RotationY(rotation_angle_);
// Draw the earth
+
earth_.Draw(model_matrix, view_matrix_, proj_matrix_);
if (debug_mode_) {
earth_.DrawDebugInfo(model_matrix, view_matrix_, proj_matrix_);
@@ -142,7 +157,49 @@ void QuakeApp::DrawUsingOpenGL() {
// TODO: You'll also need to draw the earthquakes. It's up to you exactly
// how you wish to do that.
-
+ // Earthquake eq;
+ float year_ago = current_time_ - PLAYBACK_WINDOW;
+ int start_index = quake_db_.FindMostRecentQuake(Date(year_ago));
+ int end_index = quake_db_.FindMostRecentQuake(Date(current_time_));
+
+ for (int i = start_index; i < end_index; i++) {
+ Earthquake eq = quake_db_.earthquake(i);
+ float time = eq.date().ToSeconds();
+ if (time > year_ago && time < current_time_) {
+ double magnitude_range = quake_db_.max_magnitude() - quake_db_.min_magnitude();
+ double magnitude = (quake_db_.max_magnitude() -eq.magnitude()) / magnitude_range * 10.0;
+ float size = magnitude * 0.01 * ((time - year_ago) / (current_time_ - year_ago));
+ if (size < 0.005)
+ size = 0.01;
+ if (size > 0.2)
+ size = 0.2;
+ // Set position of quake
+ // Depends on if in globe mode or not
+ Point3 pos;
+ if (globe_mode_)
+ pos = earth_.LatLongToSphere(eq.latitude(), eq.longitude());
+ else
+ pos = earth_.LatLongToPlane(eq.latitude(), eq.longitude());
+
+ Color qColor = Color(1,1,1); // black, default (SHOULD NEVER BE SEEN)
+ if (magnitude < 3.0)
+ qColor = Color(0,0,1); // blue
+ else if (magnitude < 4.0)
+ qColor = Color(0,1,0); // green
+ else if (magnitude < 5.0)
+ qColor = Color(1,1,0); // yellow
+ else
+ qColor = Color(1,0,0); // red
+
+ // Draw quake
+ // Vector3 translation = pos - Point3(0,0,0);
+ Matrix4 quake_matrix = Matrix4::Translation(Vector3(pos[0],
+ pos[1], pos[2]));
+ Matrix4 scale_matrix = Matrix4::Scale(Vector3(size, size, size));
+ Matrix4 transform = quake_matrix * scale_matrix;
+ quick_shapes_.DrawSphere(model_matrix * transform, view_matrix_, proj_matrix_, qColor);
+ }
+ }
}
diff --git a/dev/a3-earthquake/quake_app.h b/dev/a3-earthquake/quake_app.h
index 21d7a8d..bc8dcf9 100644
--- a/dev/a3-earthquake/quake_app.h
+++ b/dev/a3-earthquake/quake_app.h
@@ -54,13 +54,19 @@ private:
// true if drawing debugging info for the mesh
bool debug_mode_;
+
+ // true if the earth is in globe mode
+ bool globe_mode_;
// Database through which you can access the earthquakes
EarthquakeDatabase quake_db_;
// Object for rendering textured earth geometry
Earth earth_;
-
+
+ float rotation_angle_;
+ float old_rotation_angle_;
+
// Background image
Texture2D stars_tex_;