Commits

Cliff Biffle committed d1d5126

Improved lighting and added normal calculation for the quad renderer (the easy one).

Comments (0)

Files changed (1)

   glEnable(GL_DEPTH_TEST);
   glEnable(GL_CULL_FACE);
   glFrontFace(GL_CCW);
-  glShadeModel(GL_FLAT);
+  glShadeModel(GL_SMOOTH);
   
-  GLfloat lightPos[4] = { 0, 0, 1, 1 };
-  glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
+  glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.05F);
   
   GLfloat ambientColor[4] = { 0, 0, 0, 1 };
   glLightfv(GL_LIGHT0, GL_AMBIENT, ambientColor);
   
-  GLfloat diffuseColor[4] = { 1.0f, 0.0f, 0.0f, 1.0f };
+  GLfloat diffuseColor[4] = { 1.f, 0.0f, 0.0f, 1.0f };
   glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseColor);
   
-  GLfloat globalAmbient[4] = { 0.2f, 0.2f, 0.2f, 1.0f };
+  GLfloat globalAmbient[4] = { 0.1f, 0.1f, 0.1f, 1.0f };
   glLightModelfv(GL_LIGHT_MODEL_AMBIENT, globalAmbient);
   
-  glEnable(GL_LIGHTING);
+  glEnable(GL_NORMALIZE);
   glEnable(GL_LIGHT0);
   
   glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
   glEnable(GL_COLOR_MATERIAL);
   
-  glPolygonOffset(1.0, 1.0);  // I believe this lets edge lines show on top of polys.
   glClearColor(0, 0, 0, 0);
   
   [self resetCamera];
             camera.target.x, camera.target.y, camera.target.z,
             camera.up.x, camera.up.y, camera.up.z);
   
-  GLfloat lightPos[4] = { camera.pos.x, camera.pos.y, 1, 0 };
-  glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
-
   glRotatef(trackballRotation[0], trackballRotation[1], trackballRotation[2], trackballRotation[3]);
   glRotatef(worldRotation[0], worldRotation[1], worldRotation[2], worldRotation[3]);
   glRotatef(spin, 0, 1, 0);
   glScalef(-1/640., -1/640., 1);
   glTranslatef(-320, -240, 0);
+  GLfloat lightPos[4] = { 0, 0, -1, 1 };
+  glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
 }
 
 - (void) computeLinearDepthField {
   };
 }
 
+static void vector_subtract(const vec3f_t *a, const vec3f_t *b, vec3f_t *out) {
+	*out = (vec3f_t) {
+		a->x - b->x,
+		a->y - b->y,
+		a->z - b->z
+	};
+}
+
+static void cross_product(const vec3f_t *a, const vec3f_t *b, vec3f_t *out) {
+	*out = (vec3f_t) {
+		a->y * b->z - a->z * b->y,
+		a->z * b->x - a->x * b->z,
+		a->x * b->y - a->y * b->x
+	};		
+}
+
+static void compute_normal(const vec3f_t *p1, const vec3f_t *p2, const vec3f_t *p3, vec3f_t *out) {
+	vec3f_t a, b;
+	vector_subtract(p3, p2, &a);
+	vector_subtract(p1, p2, &b);
+	
+	cross_product(&a, &b, out);
+}
+
 - (void) drawDepthFieldAsTrisUsingMode: (GLenum)mode {
   /*
    * Theory: each horizontal raster row of the image is converted to a
   axes();
   
   const GLfloat *depths = [linearDepthField bytes];
-
+  
   glPolygonMode(GL_FRONT, mode);
   for (int y = 0; y < 479; y++) {
     glBegin(GL_TRIANGLE_STRIP);
   axes();
   
   const GLfloat *depths = [linearDepthField bytes];
-  
+  vec3f_t normal;
+
   glPolygonMode(GL_FRONT, mode);
   glBegin(GL_QUADS);
+  glColor3f(1,1,1);
+
   for (int y = 0; y < 479; y++) {
     for (int x = 0; x < 639; x++) {
       int xs[4] = { x, x, x + 1, x + 1 };
       
       if (skip) continue;
       
+      compute_normal(&coords[0], &coords[1], &coords[2], &normal);
+      glNormal3fv(&normal.x);
+      
       for (int i = 0; i < 4; i++) {
-        glColor3f(colors[i], colors[i], colors[i]);
+        //glColor3f(colors[i], colors[i], colors[i]);
         glVertex3fv((GLfloat *) &coords[i]);
       }
     }