CHEN Yihui 5 жил өмнө
parent
commit
dc2f87e08e
3 өөрчлөгдсөн 39 нэмэгдсэн , 8 устгасан
  1. 1 1
      build.sh
  2. 34 4
      main.cc
  3. 4 3
      readme.txt

+ 1 - 1
build.sh

@@ -1,2 +1,2 @@
 #!/bin/bash
-g++ main.cc InitShader.cpp -I. -lGL -lGLEW -lglut -o main.out -g
+g++ main.cc InitShader.cpp -I. -lGL -lGLEW -lglut -o main.out

+ 34 - 4
main.cc

@@ -16,6 +16,9 @@ const int TextureHeight = 128;
 typedef Angel::vec4 point4;
 typedef Angel::vec4 color4;
 
+// terrain altitude array
+float ter_alt[128][256];
+
 // Texture objects and storage for texture image
 GLuint textures[1];
 
@@ -57,7 +60,28 @@ GLuint projection; // projection matrix uniform shader variable location
 
 //----------------------------------------------------------------------------
 
-void load_model(const std::string &file_name, bool no_tex = false,
+bool hit_check(GLfloat &x, GLfloat &y, GLfloat &z) {
+  int x_idx = x * 256;
+  int y_idx = y * 256;
+  if (x_idx > 255 || x_idx < 0 || y_idx < 0 || y_idx > 127) {
+    x = 0.0;
+    y = 0.25;
+    z = 0.1;
+    return false;
+  }
+  if (PARAMS_DEBUG_PRINT) {
+    printf("%f, %f\n", ter_alt[y_idx][x_idx], z);
+  }
+  if (ter_alt[y_idx][x_idx] >= z) {
+    x = 0.0;
+    y = 0.25;
+    z = 0.1;
+    return false;
+  }
+  return true;
+}
+
+void load_model(const std::string &file_name, bool not_tex = false,
                 float x_offset = 0.0f, float y_offset = 0.0f,
                 float z_offset = 0.0f) {
   std::vector<point4> vertices;
@@ -71,11 +95,16 @@ void load_model(const std::string &file_name, bool no_tex = false,
     float x, y, z;
     int a, b, c;
     int num = 0;
+    int idx = 0;
     while (s >> t) {
       if (t == "v") { // vertices
         s >> x >> y >> z;
         vertices.emplace_back(x / 50.0f + x_offset, z / 50.0f + z_offset,
                               y / 50.0f + y_offset, 1.0);
+        if (!not_tex) {
+          *(reinterpret_cast<float*>(ter_alt) + idx) = y / 50.0f;
+          idx++;
+        }
       } else if (t == "vt") {
         s >> x >> y;
         tex_cods.emplace_back(x, y);
@@ -85,21 +114,21 @@ void load_model(const std::string &file_name, bool no_tex = false,
         b--;
         c--;
         points[index] = vertices[a];
-        if (no_tex) {
+        if (not_tex) {
           tex_coords[index] = {20.0, 0.0};
         } else {
           tex_coords[index] = tex_cods[a];
         }
         index++;
         points[index] = vertices[b];
-        if (no_tex) {
+        if (not_tex) {
           tex_coords[index] = {20.0, 0.0};
         } else {
           tex_coords[index] = tex_cods[b];
         }
         index++;
         points[index] = vertices[c];
-        if (no_tex) {
+        if (not_tex) {
           tex_coords[index] = {20.0, 0.0};
         } else {
           tex_coords[index] = tex_cods[c];
@@ -295,6 +324,7 @@ void keyboard(unsigned char key, int mousex, int mousey) {
     printf("eye_x,y,z:%f, %f, %f, %f\n", eye_x, eye_y, eye_z, eye_w);
     printf("at_x,y,z:%f, %f, %f, %f\n", at_x, at_y, at_z, at_w);
   }
+  hit_check(eye_x, eye_y, eye_z);
   glutPostRedisplay();
 }
 

+ 4 - 3
readme.txt

@@ -43,7 +43,8 @@
     在片段着色器里计算雾化效果,雾的颜色写在片段着色器里
   模型的数据结构,效率和交互
     对于顶点,纹理坐标,纹素,颜色的数据上面已经说明
-    对于碰撞检测使用cpu计算,在二维数组[x][y]中存放地形高度z,
-    判断当前位置x,y,z是否陷入地下,z取值上可以选用双向线性插值biliner或者最近nearest,
-    如果陷入地下,则把z设为一定高度。对于飞机的实际大小,不做考虑。
+    对于碰撞检测使用cpu计算,在二维数组[128][256]中存放地形高度z,
+    判断当前位置x,y,z是否陷入地下,z取值上选用最近nearest,
+    如果陷入地下,则恢复原位。对于飞机的实际大小,不做考虑。
+    在x和y方向上,如果飞机飞出地形上空,则也会恢复原位。
     对于xy方向的碰装,和物体的碰撞,不做考虑。