octave-maintainers
[Top][All Lists]
Advanced

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

OpenGL lighting fixes


From: Kai Habel
Subject: OpenGL lighting fixes
Date: Mon, 02 Feb 2009 15:33:04 +0100
User-agent: Thunderbird 2.0.0.19 (X11/20081227)

I am currently working on enabling light (at least for the OpenGL
renderer).  I noticed the following problems with the current code.

1.) To set the material properties (ambient and diffuse reflection) we
have some instances in gl_render.cc with:

for (int i = 0; i < 3; i++)
  cb[i] = (as * fcolor(i));
glMaterialfv (LIGHT_MODE, GL_AMBIENT, cb);

for (int i = 0; i < 3; i++)
  cb[i] *= (ds / as);
glMaterialfv (LIGHT_MODE, GL_DIFFUSE, cb);

As you can see, the calculation of the diffuse reflection fails if as==0.


2.) The surface normals should be calculated when facelighting is set to
'flat' too. In addition we need to normalize here, since we don't do
this in graphics.cc --> surface::properties::update_normals (void) anymore.

So,

if (fl_mode == 2)
  glNormal3d (...);

should be changed into

if (fl_mode > 0)
  glNormal3d (...);

in all cases.

I am cc'ing Michael directly since I would like to have his comment
before someone is applying this changeset.

Kai

# HG changeset patch
# User Kai Habel
# Date 1233566648 -3600
# Node ID 9d003832a9a099eaf2a9487022af478c7193ae97
# Parent  98fb4f837962c70e0b7f357fc17914f87d110cf5
Correct calculation of diffuse lighting for surface and patch object. Calculate
 surface normals in all cases except for lighting mode 'none'.

diff -r 98fb4f837962 -r 9d003832a9a0 src/ChangeLog
--- a/src/ChangeLog     Fri Jan 30 18:38:44 2009 +0100
+++ b/src/ChangeLog     Mon Feb 02 10:24:08 2009 +0100
@@ -1,3 +1,10 @@
+2009-02-02  Kai Habel <address@hidden>
+        * gl-render.cc (opengl_renderer::draw(surface::properties)): Calculate
+        surface normals for all facelighting modes except 'none'. 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 9d003832a9a0 src/gl-render.cc
--- a/src/gl-render.cc  Fri Jan 30 18:38:44 2009 +0100
+++ b/src/gl-render.cc  Mon Feb 02 10:24:08 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 > 0)
+                   {
+                     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));
+                 if (fl_mode > 0)
+                   {
+                     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 > 0)
+                   {
+                     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,16 +2040,21 @@
                              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
-                     if (ec_mode == 2)
+                     if (ec_mode > 0)
                        {
                          for (int k = 0; k < 3; k++)
                            cb[k] = c(j, i, k);
@@ -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));
+                     if (el_mode > 0)
+                       {
+                         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,16 +2124,21 @@
                              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
-                     if (ec_mode == 2)
+                     if (ec_mode > 0)
                        {
                          for (int k = 0; k < 3; k++)
                            cb[k] = c(j, i, k);
@@ -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));
+                     if (el_mode > 0)
+                       {
+                         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]