PDA

View Full Version : lighting looks striped



siarck
06-02-2011, 07:40 AM
I am try to light a surface I created from a vertex array and it seems to have striped shadows. Here is the relevant code and a couple images.


static void realize (GtkWidget *widget, gpointer data)
{
GdkGLContext *glcontext = gtk_widget_get_gl_context (widget);
GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable (widget);

GLfloat ambient[] = {0.0, 0.0, 0.0, 1.0};
GLfloat diffuse[] = {0.8, 0.8, 0.8, 1.0};
GLfloat position[] = {0.0, 0.0, -5.0, 0.0};

GLfloat lmodel_ambient[] = {0.3, 0.3, 0.3, 1.0};
GLfloat local_view[] = {0.0};
GLfloat two_side[] = {GL_TRUE};

if (!gdk_gl_drawable_gl_begin (gldrawable, glcontext))
return;

glClearColor (0.5, 0.5, 0.8, 1.0);
glClearDepth (1.0);

glLightfv (GL_LIGHT0, GL_AMBIENT, ambient);
glLightfv (GL_LIGHT0, GL_DIFFUSE, diffuse);
glLightfv (GL_LIGHT0, GL_POSITION, position);
glLightModelfv (GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
glLightModelfv (GL_LIGHT_MODEL_LOCAL_VIEWER, local_view);
glLightModelfv (GL_LIGHT_MODEL_TWO_SIDE, two_side);

glFrontFace (GL_CW);
glEnable (GL_LIGHTING);
glEnable (GL_LIGHT0);
glEnable (GL_NORMALIZE);
glEnable (GL_DEPTH_TEST);
glDepthFunc (GL_LESS);
glShadeModel(GL_SMOOTH);

gdk_gl_drawable_gl_end (gldrawable);

return;
}

static gboolean configure_event (GtkWidget *widget, GdkEventConfigure *event, gpointer data)
{
GdkGLContext *glcontext = gtk_widget_get_gl_context (widget);
GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable (widget);

GLfloat w = widget->allocation.width;
GLfloat h = widget->allocation.height;
GLfloat aspect;

if (!gdk_gl_drawable_gl_begin (gldrawable, glcontext))
return FALSE;

glViewport (0, 0, w, h);

glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
if (w > h)
{
aspect = w / h;
glFrustum (-aspect, aspect, -1.0, 1.0, 5.0, 60.0);
}
else
{
aspect = h / w;
glFrustum (-1.0, 1.0, -aspect, aspect, 5.0, 60.0);
}

glMatrixMode (GL_MODELVIEW);
glLoadIdentity();

gdk_gl_drawable_gl_end (gldrawable);

return TRUE;
}

static gboolean expose_event (GtkWidget *widget, GdkEventExpose *event, gpointer data)
{
GdkGLContext *glcontext = gtk_widget_get_gl_context (widget);
GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable (widget);

if (!gdk_gl_drawable_gl_begin (gldrawable, glcontext))
return FALSE;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity ();

glTranslatef (0.0, 0.0, -10.0);
glMultMatrixf(tran);
glMultMatrixf(rot);
glMultMatrixf(scale);

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);

glVertexPointer(3, GL_FLOAT, 0, vertices);
glNormalPointer(GL_FLOAT, 0, normals);

float wcolor[] = { 1.0f, 1.0f, 1.0f, 1.0f };
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, wcolor);
glDrawElements(GL_TRIANGLES, (cols-1)*(rows-1)*6, GL_UNSIGNED_INT, indices);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);

if (gdk_gl_drawable_is_double_buffered (gldrawable))
gdk_gl_drawable_swap_buffers (gldrawable);
else
glFlush ();

gdk_gl_drawable_gl_end (gldrawable);

return TRUE;
}

void setNormal(int n, int ind1, int ind2,int ind3)
{
ind1 *= 3;
ind2 *= 3;
ind3 *= 3;

float ax = vertices[ind1] - vertices[ind2];
float ay = vertices[ind1+1] - vertices[ind2+1];
float az = vertices[ind1+2] - vertices[ind2+2];
float bx = vertices[ind2] - vertices[ind3];
float by = vertices[ind2+1] - vertices[ind3+1];
float bz = vertices[ind2+2] - vertices[ind3+2];

normals[n] = (ay * bz) - (az * by);
normals[n+1] = (az * bx) - (ax * bz);
normals[n+2] = (ax * by) - (ay * bx);

float len = (float)(sqrt((normals[n] * normals[n]) + (normals[n+1] * normals[n+1]) + (normals[n+2] * normals[n+2])));

if (len == 0.0f)
len = 1.0f;

normals[n] /= len;
normals[n+1] /= len;
normals[n+2] /= len;
}

void Shape::startRender(){
vertices = new GLfloat[cols*rows*3];
normals = new GLfloat[(cols-1)*(rows-1)*6];
coords = new GLfloat[cols*rows*2];
indices = new GLuint[(cols-1)*(rows-1)*6];

nvertices = new GLfloat[(cols-1)*(rows-1)*12];

for (int i=0; i<cols*rows; i++){
vertices[i*3] = (i%cols-cols/2)/200.0;
vertices[i*3+1] = (i/cols-rows/2)/200.0;
vertices[i*3+2] = z[i]/200.0;

coords[i*2] = (float)(i%cols)/cols;
coords[i*2+1] = (float)(i/cols)/rows;
}
for (int i=0; i<(cols-1)*(rows-1); i++){
int x = i%(cols-1);
int y = i/(cols-1);

indices[i*6] = y*cols+x;
indices[i*6+1] = y*cols+x+1;
indices[i*6+2] = (y+1)*cols+x;

indices[i*6+3] = (y+1)*cols+x;
indices[i*6+4] = y*cols+x+1;
indices[i*6+5] = (y+1)*cols+x+1;

setNormal(i*6, indices[i*6], indices[i*6+1], indices[i*6+2]);
setNormal(i*6+3, indices[i*6+3], indices[i*6+4], indices[i*6+5]);
}
}