1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
/** CSci-4611 Assignment 3: Earthquake
*/
#include "earth.h"
#include "config.h"
#include <vector>
// for M_PI constant
#define _USE_MATH_DEFINES
#include <math.h>
Earth::Earth() {
}
Earth::~Earth() {
}
void Earth::Init(const std::vector<std::string> &search_path) {
// init shader program
shader_.Init();
// init texture: you can change to a lower-res texture here if needed
earth_tex_.InitFromFile(Platform::FindFile("earth-2k.png", search_path));
// init geometry
const int nslices = 10;
const int nstacks = 10;
// 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));
// 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();
}
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"
DefaultShader::LightProperties light;
light.position = Point3(10,10,10);
light.ambient_intensity = Color(1,1,1);
light.diffuse_intensity = Color(1,1,1);
light.specular_intensity = Color(1,1,1);
shader_.SetLight(0, light);
// Adust the material properties, material is a property of the thing
// (e.g., a mesh) that we draw with the shader. The reflectance properties
// affect the lighting. The surface texture is the key for getting the
// image of the earth to show up.
DefaultShader::MaterialProperties mat;
mat.ambient_reflectance = Color(0.5, 0.5, 0.5);
mat.diffuse_reflectance = Color(0.75, 0.75, 0.75);
mat.specular_reflectance = Color(0.75, 0.75, 0.75);
mat.surface_texture = earth_tex_;
// Draw the earth mesh using these settings
if (earth_mesh_.num_triangles() > 0) {
shader_.Draw(model_matrix, view_matrix, proj_matrix, &earth_mesh_, mat);
}
}
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);
}
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);
}
void Earth::DrawDebugInfo(const Matrix4 &model_matrix, const Matrix4 &view_matrix, const Matrix4 &proj_matrix) {
// This draws a cylinder for each line segment on each edge of each triangle in your mesh.
// 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<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]));
quick_shapes_.DrawLines(model_matrix, view_matrix, proj_matrix,
Color(1,1,0), loop, QuickShapes::LinesType::LINE_LOOP, 0.005f);
}
}
|