CHEN Yihui 5 years ago
parent
commit
374674e9f0
4 changed files with 42 additions and 15 deletions
  1. 1 1
      build.sh
  2. 7 2
      fshader71.glsl
  3. 33 11
      main.cc
  4. 1 1
      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++ main.cc InitShader.cpp -I. -lGL -lGLEW -lglut -o main.out -g

+ 7 - 2
fshader71.glsl

@@ -9,7 +9,12 @@ out vec4 fColor;
 uniform sampler2D tex;
 
 void main() 
-{ 
-    fColor = color * texture( tex, texCoord );
+{
+    if (texCoord.x > 10) {
+      fColor = vec4(0.2, 0.2, 0.2, 1);
+    }
+    else {
+      fColor = color * texture(tex, texCoord);
+    }
 } 
 

+ 33 - 11
main.cc

@@ -3,7 +3,12 @@
 #include <iostream>
 #include <vector>
 
-const int NumTriangles = 64770; // (6 faces)(2 triangles/face)
+#define PARAMS_DEBUG_PRINT false
+
+const int texNumTriangles = 64770;
+const int cowNumTriangles = 5804;
+
+const int NumTriangles = texNumTriangles + cowNumTriangles;
 const int NumVertices = 3 * NumTriangles;
 const int TextureWidth = 256;
 const int TextureHeight = 128;
@@ -47,14 +52,16 @@ GLuint projection; // projection matrix uniform shader variable location
 
 //----------------------------------------------------------------------------
 
-void load_model(const std::string &file_name) {
+void load_model(const std::string &file_name, bool no_tex = false,
+                float x_offset = 0.0f, float y_offset = 0.0f,
+                float z_offset = 0.0f) {
   std::vector<point4> vertices;
   std::vector<vec2> tex_cods;
   std::fstream s(file_name, std::fstream::in);
+  static int index = 0;
   if (!s.is_open()) {
     std::cerr << "failed to open" << file_name << std::endl;
   } else {
-    int index = 0;
     std::string t;
     float x, y, z;
     int a, b, c;
@@ -62,7 +69,7 @@ void load_model(const std::string &file_name) {
     while (s >> t) {
       if (t == "v") { // vertices
         s >> x >> y >> z;
-        vertices.emplace_back(x / 50.0f, z / 50.0f, y / 50.0f, 1.0);
+        vertices.emplace_back(x / 50.0f + x_offset, z / 50.0f + z_offset, y / 50.0f + y_offset, 1.0);
       } else if (t == "vt") {
         s >> x >> y;
         tex_cods.emplace_back(x, y);
@@ -72,17 +79,30 @@ void load_model(const std::string &file_name) {
         b--;
         c--;
         points[index] = vertices[a];
-        tex_coords[index] = tex_cods[a];
+        if (no_tex) {
+          tex_coords[index] = {20.0, 0.0};
+        } else {
+          tex_coords[index] = tex_cods[a];
+        }
         index++;
         points[index] = vertices[b];
-        tex_coords[index] = tex_cods[b];
+        if (no_tex) {
+          tex_coords[index] = {20.0, 0.0};
+        } else {
+          tex_coords[index] = tex_cods[b];
+        }
         index++;
         points[index] = vertices[c];
-        tex_coords[index] = tex_cods[c];
+        if (no_tex) {
+          tex_coords[index] = {20.0, 0.0};
+        } else {
+          tex_coords[index] = tex_cods[c];
+        }
         index++;
         num++;
       }
     }
+    std::cout << "Load model:" << file_name << std::endl;
     std::cout << "There are " << vertices.size() << "vertice(s)"
               << ", " << tex_cods.size() << "tex cor(s)" << std::endl
               << "There are " << num << "fragment(s)" << std::endl;
@@ -108,7 +128,7 @@ void load_tex(const std::string &file_name) {
 void init() {
   load_tex("gc_tex.ppm");
   load_model("gc_tex.obj");
-
+  load_model("cow.obj", true, 0.90, 0.05, 0.25); // no tex data in cow obj
   // Initialize texture objects
   glGenTextures(1, textures);
 
@@ -251,8 +271,10 @@ void keyboard(unsigned char key, int mousex, int mousey) {
     phi += 0.01;
     break;
   }
-  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);
+  if (PARAMS_DEBUG_PRINT) {
+    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);
+  }
   glutPostRedisplay();
 }
 
@@ -276,7 +298,7 @@ int main(int argc, char **argv) {
   glutDisplayFunc(display);
   glutKeyboardFunc(keyboard);
   glutMouseFunc(mouse);
-  //glutIdleFunc(idle);
+  // glutIdleFunc(idle);
 
   glutMainLoop();
   return 0;

+ 1 - 1
readme.txt

@@ -29,7 +29,7 @@
     在host端维护飞机坐标x,y,z以及观察的方向theta,phi,侧看的角度tau
     在cpu端用lookat和Frustum计算model_view和projection,使用uniform传给gpu
   物体绘制
-    设置了物体,物体没有纹理,有颜色,
+    设置了物体,物体没有纹理,有颜色,
     物体的纹理坐标都设成一个坐标,这样物体不会有纹理,颜色稍有影响
   光照
     顶点法向用cpu预先计算,存在glBuffer里,