octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: OpenGL lighting fixes


From: Kai Habel
Subject: Re: OpenGL lighting fixes
Date: Wed, 04 Feb 2009 18:28:55 +0100
User-agent: Thunderbird 2.0.0.19 (X11/20081227)

Michael Goffioul schrieb:
> ...
>
>   
>> 2.) The surface normals should be calculated when facelighting is set to
>> 'flat' too.
>>     
>
> That's not my understanding of Matlab documentation. When facelighting
> is flat, the ligthening should be uniform across the each quad and equal
> to the lightening of the first vertex. That's why I don't update the normal on
> the usbsequent vertices.
>
>   
> ...

Thanks,
for the reply and brief explanation. I did some tests myself, and can
confirm the behavior you have described above. So, I have reworked the
patch that only the calculation of diffuse reflection and surface
normals is modified.

Kai
# HG changeset patch
# User Kai Habel
# Date 1233761149 -3600
# Node ID 7d5d6dec3f4ffa16b50ee4d0087ca7e6c17fc32d
# Parent  98fb4f837962c70e0b7f357fc17914f87d110cf5
Fix calculation of diffuse reflectance. Calculate surface normals here now.

diff -r 98fb4f837962 -r 7d5d6dec3f4f src/ChangeLog
--- a/src/ChangeLog     Fri Jan 30 18:38:44 2009 +0100
+++ b/src/ChangeLog     Wed Feb 04 16:25:49 2009 +0100
@@ -1,3 +1,9 @@
+2009-02-02  Kai Habel <address@hidden>
+        * gl-render.cc (opengl_renderer::draw(surface::properties)): Normalize
+        surface normals. Correct calculation of diffuse lighting.
+        * gl-render.cc (opengl_renderer::draw(patch::properties)): Correct
+        calculation of diffuse lighting.
+
 2009-01-29  John W. Eaton  <address@hidden>
 
        * pt-stmt.cc (tree_statement::eval): Check
diff -r 98fb4f837962 -r 7d5d6dec3f4f src/gl-render.cc
--- a/src/gl-render.cc  Fri Jan 30 18:38:44 2009 +0100
+++ b/src/gl-render.cc  Wed Feb 04 16:25:49 2009 +0100
@@ -1742,7 +1742,8 @@
   float ds = props.get_diffusestrength ();
   float ss = props.get_specularstrength ();
   float se = props.get_specularexponent ();
-  float cb[4] = { 0, 0, 0, 1 };
+  float cb[4] = { 0.0, 0.0, 0.0, 1.0 };
+  double d = 1.0;
 
   opengl_texture tex;
 
@@ -1800,11 +1801,11 @@
              if (fl_mode > 0)
                {
                  for (int i = 0; i < 3; i++)
-                   cb[i] = (as * fcolor(i));
+                   cb[i] = as * fcolor(i);
                  glMaterialfv (LIGHT_MODE, GL_AMBIENT, cb);
 
                  for (int i = 0; i < 3; i++)
-                   cb[i] *= (ds / as);
+                   cb[i] = ds * fcolor(i);
                  glMaterialfv (LIGHT_MODE, GL_DIFFUSE, cb);
                }
            }
@@ -1855,12 +1856,17 @@
                          glMaterialfv (LIGHT_MODE, GL_AMBIENT, cb);
                          
                          for (int k = 0; k < 3; k++)
-                           cb[k] *= (ds / as);
+                           cb[k] = ds * c(j-1, i-1, k);
                          glMaterialfv (LIGHT_MODE, GL_DIFFUSE, cb);
                        }
                    }
-                 if (fl_mode > 0)
-                   glNormal3d (n(j-1,i-1,0), n(j-1,i-1,1), n(j-1,i-1,2));
+                  if (fl_mode > 0)
+                   {
+                     d = sqrt(n(j-1,i-1,0) * n(j-1,i-1,0)
+                            + n(j-1,i-1,1) * n(j-1,i-1,1)
+                            + n(j-1,i-1,2) * n(j-1,i-1,2));
+                     glNormal3d (n(j-1,i-1,0)/d, n(j-1,i-1,1)/d, 
n(j-1,i-1,2)/d);
+                   }
                  glVertex3d (x(j1,i-1), y(j-1,i1), z(j-1,i-1));
 
                  // Vertex 2
@@ -1879,12 +1885,19 @@
                          glMaterialfv (LIGHT_MODE, GL_AMBIENT, cb);
                          
                          for (int k = 0; k < 3; k++)
-                           cb[k] *= (ds / as);
+                           cb[k] = ds * c(j-1, i, k);
                          glMaterialfv (LIGHT_MODE, GL_DIFFUSE, cb);
                        }
                    }
-                 if (fl_mode == 2)
-                   glNormal3d (n(j-1,i,0), n(j-1,i,1), n(j-1,i,2));
+
+                  if (fl_mode == 2)
+                   {
+                     d = sqrt(n(j-1,i,0) * n(j-1,i,0)
+                            + n(j-1,i,1) * n(j-1,i,1)
+                            + n(j-1,i,2) * n(j-1,i,2));
+                     glNormal3d (n(j-1,i,0)/d, n(j-1,i,1)/d, n(j-1,i,2)/d);
+                   }
+
                  glVertex3d (x(j1,i), y(j-1,i2), z(j-1,i));
                  
                  // Vertex 3
@@ -1903,14 +1916,19 @@
                          glMaterialfv (LIGHT_MODE, GL_AMBIENT, cb);
                          
                          for (int k = 0; k < 3; k++)
-                           cb[k] *= (ds / as);
+                           cb[k] = ds * c(j, i, k);
                          glMaterialfv (LIGHT_MODE, GL_DIFFUSE, cb);
                        }
                    }
                  if (fl_mode == 2)
-                   glNormal3d (n(j,i,0), n(j,i,1), n(j,i,2));
+                   {
+                     d = sqrt(n(j,i,0) * n(j,i,0)
+                            + n(j,i,1) * n(j,i,1)
+                            + n(j,i,2) * n(j,i,2));
+                     glNormal3d (n(j,i,0)/d, n(j,i,1)/d, n(j,i,2)/d);
+                   }
                  glVertex3d (x(j2,i), y(j,i2), z(j,i));
-                 
+
                  // Vertex 4
                  if (fc_mode == 3)
                    tex.tex_coord (double (i-1) / (zc-1), double (j) / (zr-1));
@@ -1927,12 +1945,17 @@
                          glMaterialfv (LIGHT_MODE, GL_AMBIENT, cb);
                          
                          for (int k = 0; k < 3; k++)
-                           cb[k] *= (ds / as);
+                           cb[k] = ds * c(j, i-1, k);
                          glMaterialfv (LIGHT_MODE, GL_DIFFUSE, cb);
                        }
                    }
-                 if (fl_mode == 2)
-                   glNormal3d (n(j,i-1,0), n(j,i-1,1), n(j,i-1,2));
+                  if (fl_mode == 2)
+                   {
+                     d = sqrt(n(j,i-1,0) * n(j,i-1,0)
+                            + n(j,i-1,1) * n(j,i-1,1)
+                            + n(j,i-1,2) * n(j,i-1,2));
+                     glNormal3d (n(j,i-1,0)/d, n(j,i-1,1)/d, n(j,i-1,2)/d);
+                   }
                  glVertex3d (x(j2,i-1), y(j,i1), z(j,i-1));
 
                  glEnd ();
@@ -1962,11 +1985,11 @@
              if (fl_mode > 0)
                {
                  for (int i = 0; i < 3; i++)
-                   cb[i] = (as * ecolor(i));
+                   cb[i] = as * ecolor(i);
                  glMaterialfv (LIGHT_MODE, GL_AMBIENT, cb);
 
                  for (int i = 0; i < 3; i++)
-                   cb[i] *= (ds / as);
+                   cb[i] = ds * ecolor(i);
                  glMaterialfv (LIGHT_MODE, GL_DIFFUSE, cb);
                }
            }
@@ -2017,12 +2040,17 @@
                              glMaterialfv (LIGHT_MODE, GL_AMBIENT, cb);
 
                              for (int k = 0; k < 3; k++)
-                               cb[k] *= (ds / as);
+                               cb[k] = ds * c(j-1, i, k);
                              glMaterialfv (LIGHT_MODE, GL_DIFFUSE, cb);
                            }
                        }
                      if (el_mode > 0)
-                       glNormal3d (n(j-1,i,0), n(j-1,i,1), n(j-1,i,2));
+                       {
+                         d = sqrt(n(j-1,i,0) * n(j-1,i,0)
+                                + n(j-1,i,1) * n(j-1,i,1)
+                                + n(j-1,i,2) * n(j-1,i,2));
+                         glNormal3d (n(j-1,i,0)/d, n(j-1,i,1)/d, n(j-1,i,2)/d);
+                       }
                      glVertex3d (x(j1,i), y(j-1,i2), z(j-1,i));
 
                      // Vertex 2
@@ -2039,12 +2067,17 @@
                              glMaterialfv (LIGHT_MODE, GL_AMBIENT, cb);
 
                              for (int k = 0; k < 3; k++)
-                               cb[k] *= (ds / as);
+                               cb[k] = ds * c(j, i, k);
                              glMaterialfv (LIGHT_MODE, GL_DIFFUSE, cb);
                            }
                        }
                      if (el_mode == 2)
-                       glNormal3d (n(j,i,0), n(j,i,1), n(j,i,2));
+                       {
+                         d = sqrt(n(j,i,0) * n(j,i,0)
+                                + n(j,i,1) * n(j,i,1)
+                                + n(j,i,2) * n(j,i,2));
+                         glNormal3d (n(j,i,0)/d, n(j,i,1)/d, n(j,i,2)/d);
+                       }
                      glVertex3d (x(j2,i), y(j,i2), z(j,i));
 
                      glEnd ();
@@ -2091,12 +2124,17 @@
                              glMaterialfv (LIGHT_MODE, GL_AMBIENT, cb);
 
                              for (int k = 0; k < 3; k++)
-                               cb[k] *= (ds / as);
+                               cb[k] = ds * c(j, i-1, k);
                              glMaterialfv (LIGHT_MODE, GL_DIFFUSE, cb);
                            }
                        }
                      if (el_mode > 0)
-                       glNormal3d (n(j,i-1,0), n(j,i-1,1), n(j,i-1,2));
+                        {
+                         d = sqrt(n(j,i-1,0) * n(j,i-1,0)
+                                + n(j,i-1,1) * n(j,i-1,1)
+                                + n(j,i-1,2) * n(j,i-1,2));
+                         glNormal3d (n(j,i-1,0)/d, n(j,i-1,1)/d, n(j,i-1,2)/d);
+                       }
                      glVertex3d (x(j2,i-1), y(j,i1), z(j,i-1));
                      
                      // Vertex 2
@@ -2113,12 +2151,17 @@
                              glMaterialfv (LIGHT_MODE, GL_AMBIENT, cb);
 
                              for (int k = 0; k < 3; k++)
-                               cb[k] *= (ds / as);
+                               cb[k] = ds * c(j, i, k);
                              glMaterialfv (LIGHT_MODE, GL_DIFFUSE, cb);
                            }
                        }
                      if (el_mode == 2)
-                       glNormal3d (n(j,i,0), n(j,i,1), n(j,i,2));
+                       {
+                         d = sqrt(n(j,i,0) * n(j,i,0)
+                                + n(j,i,1) * n(j,i,1)
+                                + n(j,i,2) * n(j,i,2));
+                         glNormal3d (n(j,i,0)/d, n(j,i,1)/d, n(j,i,2)/d);
+                       }
                      glVertex3d (x(j2,i), y(j,i2), z(j,i));
                      
                      glEnd ();
@@ -2364,7 +2407,7 @@
                  glMaterialfv (LIGHT_MODE, GL_AMBIENT, cb);
 
                  for (int i = 0; i < 3; i++)
-                   cb[i] *= (ds / as);
+                   cb[i] = ds * fcolor(i);
                  glMaterialfv (LIGHT_MODE, GL_DIFFUSE, cb);
                }
            }
@@ -2420,7 +2463,7 @@
                  glMaterialfv (LIGHT_MODE, GL_AMBIENT, cb);
 
                  for (int i = 0; i < 3; i++)
-                   cb[i] *= (ds / as);
+                   cb[i] = ds * ecolor(i);
                  glMaterialfv (LIGHT_MODE, GL_DIFFUSE, cb);
                }
            }

reply via email to

[Prev in Thread] Current Thread [Next in Thread]