CHEN Yihui 5 years ago
parent
commit
f08abf918d
3 changed files with 89 additions and 74 deletions
  1. 2 0
      build.sh
  2. 83 43
      main.cc
  3. 4 31
      vshader71.glsl

+ 2 - 0
build.sh

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

+ 83 - 43
main.cc

@@ -20,11 +20,30 @@ GLubyte image[TextureWidth][TextureHeight][3];
 point4 points[NumVertices];
 vec2 tex_coords[NumVertices];
 
-// Array of rotation angles (in degrees) for each coordinate axis
-enum { Xaxis = 0, Yaxis = 1, Zaxis = 2, NumAxes = 3 };
-int Axis = Xaxis;
-GLfloat Theta[NumAxes] = {0.0, 0.0, 0.0};
-GLuint theta;
+// Viewing transformation parameters
+GLfloat theta = 0.0;
+GLfloat phi = 0.0;
+GLfloat tau = 0.0;
+
+GLfloat eye_x = 0.0;
+GLfloat eye_y = 0.25;
+GLfloat eye_z = 0.1;
+GLfloat eye_w = 1.0;
+
+GLfloat at_x;
+GLfloat at_y;
+GLfloat at_z;
+GLfloat at_w;
+
+GLuint model_view; // model-view matrix uniform shader variable location
+
+// Projection transformation parameters
+
+GLfloat left = -0.01, right = 0.01;
+GLfloat bottom = -0.01, top = 0.01;
+GLfloat zNear = 0.1, zFar = 2.0;
+
+GLuint projection; // projection matrix uniform shader variable location
 
 //----------------------------------------------------------------------------
 
@@ -98,8 +117,8 @@ void init() {
                GL_UNSIGNED_BYTE, image);
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
-  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
-  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
 
   glActiveTexture(GL_TEXTURE0);
   glBindTexture(GL_TEXTURE_2D, textures[0]);
@@ -147,8 +166,8 @@ void init() {
   //   zero, for GL_TEXTURE0 which was previously set by calling
   //   glActiveTexture().
   glUniform1i(glGetUniformLocation(program, "tex"), 0);
-
-  theta = glGetUniformLocation(program, "theta");
+  model_view = glGetUniformLocation(program, "model_view");
+  projection = glGetUniformLocation(program, "projection");
 
   glEnable(GL_DEPTH_TEST);
 
@@ -158,7 +177,18 @@ void init() {
 void display(void) {
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
-  glUniform3fv(theta, 1, Theta);
+  point4 eye(eye_x, eye_y, eye_z, 1.0);
+  at_x = cos(theta) * cos(phi) * 100.0;
+  at_y = sin(theta) * cos(phi) * 100.0;
+  at_z = sin(phi) * 100.0;
+  point4 at(at_x, at_y, at_z, 1.0);
+  vec4 up(0.0, 100 * sin(tau), 100 * cos(tau), 0.0);
+
+  mat4 mv = LookAt(eye, at, up);
+  glUniformMatrix4fv(model_view, 1, GL_TRUE, mv);
+
+  mat4 p = Frustum(left, right, bottom, top, zNear, zFar);
+  glUniformMatrix4fv(projection, 1, GL_TRUE, p);
 
   glDrawArrays(GL_TRIANGLES, 0, NumVertices);
 
@@ -167,52 +197,62 @@ void display(void) {
 
 //----------------------------------------------------------------------------
 
-void mouse(int button, int state, int x, int y) {
-  if (state == GLUT_DOWN) {
-    switch (button) {
-    case GLUT_LEFT_BUTTON:
-      Axis = Xaxis;
-      break;
-    case GLUT_MIDDLE_BUTTON:
-      Axis = Yaxis;
-      break;
-    case GLUT_RIGHT_BUTTON:
-      Axis = Zaxis;
-      break;
-    }
-  }
-}
+void mouse(int button, int state, int x, int y) {}
 
 //----------------------------------------------------------------------------
 
-void idle(void) {
-  Theta[Axis] += 0.5;
-
-  if (Theta[Axis] > 360.0) {
-    Theta[Axis] -= 360.0;
-  }
-
-  glutPostRedisplay();
-}
+void idle(void) { glutPostRedisplay(); }
 
 //----------------------------------------------------------------------------
 
 void keyboard(unsigned char key, int mousex, int mousey) {
   switch (key) {
   case 033: // Escape Key
-  case 'q':
-  case 'Q':
-    exit(EXIT_SUCCESS);
+  case '1':
+    tau += 0.01;
+    break;
+  case '3':
+    tau -= 0.01;
     break;
-  case 'b':
-  case 'B':
-    glutIdleFunc(idle);
+  case 'q':
+    theta += 0.01;
     break;
   case 'e':
-  case 'E':
-    glutIdleFunc(NULL);
+    theta -= 0.01;
+    break;
+  case 'A':
+    at_x += 0.01;
+    break;
+  case 'D':
+    at_x -= 0.001;
+    break;
+  case 'w':
+    eye_x += 0.01;
+    break;
+  case 's':
+    eye_x -= 0.01;
+    break;
+  case 'd':
+    eye_y -= 0.01;
+    break;
+  case 'a':
+    eye_y += 0.01;
+    break;
+  case '9':
+    eye_z -= 0.01;
+    break;
+  case '6':
+    eye_z += 0.01;
+    break;
+  case '8':
+    phi -= 0.01;
+    break;
+  case '5':
+    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);
   glutPostRedisplay();
 }
 
@@ -236,7 +276,7 @@ int main(int argc, char **argv) {
   glutDisplayFunc(display);
   glutKeyboardFunc(keyboard);
   glutMouseFunc(mouse);
-  glutIdleFunc(idle);
+  //glutIdleFunc(idle);
 
   glutMainLoop();
   return 0;

+ 4 - 31
vshader71.glsl

@@ -1,44 +1,17 @@
 #version 150
 
 in  vec4 vPosition;
-in  vec4 vColor;
 in  vec2 vTexCoord;
 
 out vec4 color;
 out vec2 texCoord;
 
-uniform vec3 theta;
+uniform mat4 model_view;
+uniform mat4 projection;
 
 void main() 
 {
-    const float  DegreesToRadians = 3.14159265 / 180.0;
-    
-    vec3 c = cos( DegreesToRadians * theta );
-    vec3 s = sin( DegreesToRadians * theta );
-
-    mat4 rx = mat4( 1.0, 0.0,  0.0, 0.0,
-		    0.0, c.x, -s.x, 0.0,
-		    0.0, s.x,  c.x, 0.0,
-		    0.0, 0.0,  0.0, 1.0);
-
-    mat4 ry = mat4(   c.y, 0.0, s.y, 0.0,
-		      0.0, 1.0, 0.0, 0.0,
-		     -s.y, 0.0, c.y, 0.0,
-		      0.0, 0.0, 0.0, 1.0 );
-
-    // Workaround for bug in ATI driver
-    ry[1][0] = 0.0;
-    ry[1][1] = 1.0;
-    
-    mat4 rz = mat4( c.z, -s.z, 0.0, 0.0,
-		    s.z,  c.z, 0.0, 0.0,
-		    0.0,  0.0, 1.0, 0.0,
-		    0.0,  0.0, 0.0, 1.0 );
-
-    // Workaround for bug in ATI driver
-    rz[2][2] = 1.0;
-    
-    color       = vec4(0.5, 0.5, 0.5, 1);
+    color       = vec4(1, 1, 1, 1);
     texCoord    = vTexCoord;
-    gl_Position = rz * ry * rx * vPosition;
+    gl_Position = projection * model_view *  vPosition / vPosition.w;
 }