hello everyone, thanks for your reply.
I did what you all suggested but there wasn’t any change.
I’ll quickly tell you the overview of the program - firstly in the setupRC() callback i have done all the state enabling of GL and material and lighting creation and properties. In renderScene() which is used for display callback, i use vertex arrays for vertex definition and normal definition. So in order to get the all normal in an array, I have individually calculated the normals for each face and then combined them into a single array using the user defined combineNormals(). The error i get is not compile or linking error but a runtime error.The window shows the pyramid and when I try to rotate , i see changes in the shade a little bit and then i get an error saying :
unhandled exception in filname.exe 0x0000005 access violation.
I have been trying to correct this for two days without sleep.I don’t understand why this error occurs. I get 0 error and warning in the compile time.
I would be grateful to anyone who could compile this and tell me the mistake I made.
The code is given below after made the corrections i made from the first one.
Also can any one direct me to a working example of usage of glNormalPointer() program…
thanx. Please help.
#include<windows.h>
#include<GL/glut.h>
#include<math.h>
#include<stdio.h>
static float xRot,yRot;
static int x=0;
static float all_normals[12],bottom_normals[3],back_normals[3],lside_normals[3],front_normals[3];
GLfloat pyramid_vertices[][3] = {{20.0f,0.0f,0.0f},
{0.0f,0.0f,60.0f},
{-20.0f,0.0f,0.0f},
{20.0f,0.0f,0.0f},
{-20.0f,0.0f,0.0f},
{0.0f,40.0f,30.0f},
{-20.0f,0.0f,0.0f},
{0.0f,0.0f,60.0f},
{0.0f,40.0f,30.0f},
{0.0f,0.0f,60.0f},
{20.0f,0.0f,0.0f},
{0.0f,40.0f,30.0f}};
//vertices for each face
float bottom_vertices[][3] = { 20.0f,0.0f,0.0f , 0.0f,0.0f,60.0f , -20.0f,0.0f,0.0f};
float back_vertices[][3] = {20.0f,0.0f,0.0f , -20.0f,0.0f,0.0f , 0.0f,40.0f,30.0f};
float lside_vertices[][3] = {-20.0f,0.0f,0.0f , 0.0f,0.0f,60.0f , 0.0f,40.0f,30.0f};
float front_vertices[][3] = {0.0f,0.0f,60.0f , 20.0f,0.0f,0.0f , 0.0f,40.0f,30.0f};
static GLubyte bottom[]={0,1,2};
static GLubyte back[]={3,4,5};
static GLubyte lside[]={6,7,8};
static GLubyte front[]={9,10,11};
static GLubyte allindices[]={0,1,2,3,4,5,6,7,8,9,10,11};
//color for the vertices
GLfloat pyramid_vertex_colors[][3] = {{1.0f,0.0f,0.0f},
{0.0f,1.0f,0.0f},
{0.0f,0.0f,1.0f},
{1.0f,1.0f,0.0f},
{1.0f,0.0f,1.0f},
{0.0f,1.0f,1.0f},
{0.25f,0.25f,0.25f},
{0.75f,0.75f,0.75f},
{0.60f,0.40f,0.12f},
{0.98f,0.625f,0.12f},
{0.98f,0.04f,0.7f},
{0.60f,0.40f,0.70f}};
void setupRC()
{
GLfloat ambient[] = {0.3f,0.3f,0.3f,1.0f};
GLfloat diffuse[] = {0.7f,0.7f,0.7f,0.0f};
GLfloat specular[]= {1.0f,1.0f,1.0f,1.0f};
GLfloat specRef[] = {1.0f,1.0f,1.0f,1.0f};
GLfloat lightPos[] = { -50.f, 50.0f, 100.0f, 1.0f };
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0,GL_AMBIENT,ambient);
glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuse);
glLightfv(GL_LIGHT0,GL_SPECULAR,specular);
glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
glMaterialfv(GL_FRONT,GL_SPECULAR,specRef);;
glMateriali(GL_FRONT,GL_SHININESS,128);
glShadeModel(GL_SMOOTH);
glFrontFace(GL_CCW);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
glEnable(GL_AUTO_NORMAL);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluPerspective(60.0f,1.0f,80.0f,40.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluLookAt(0.0f,0.0f,80.0f,0.0f,0.0f,0.0f,0.0f,1.0f,0.0f);
glClearColor(0.0f,0.0f,0.0f,1.0f);
}
void calcUnitNormals(float out[3])
{
float length;
length = (float)sqrt((out[0]*out[0]) + (out[1]*out[1]) + (out[2]*out[2]));
if(length==0.0f)
length=1.0f;
out[0] /= length;
out[1] /= length;
out[2] /= length;
//printf("norm0: %f
norm1: %f
norm2: %f
",out[0],out[1],out[2]);
}
void calcNormals(GLfloat v[3][3],GLfloat out[3])
{
GLfloat v1[3],v2[3];
static const int x=0,y=1,z=2;
v1[x] = v[0][x] - v[1][x];
v1[y] = v[0][y] - v[1][y];
v1[z] = v[0][z] - v[1][z];
v2[x] = v[1][x] - v[2][x];
v2[y] = v[1][y] - v[2][y];
v2[z] = v[1][z] - v[2][z];
out[x] = v1[y]*v2[z] - v2[y]*v1[z];
out[y] = v1[z]*v2[x] - v1[x]*v2[z];
out[z] = v1[x]*v2[y] - v1[y]*v2[x];
calcUnitNormals(out);
}
void specialKeys(int key,int x,int y)
{
if(key==GLUT_KEY_UP)
xRot +=5.0f;
if(key==GLUT_KEY_DOWN)
xRot -= 5.0f;
if(key==GLUT_KEY_RIGHT)
yRot += 5.0f;
if(key==GLUT_KEY_LEFT)
yRot -= 5.0f;
if(key<0.0f)
xRot=359.0f;
if(key>359.0f)
xRot=0.0f;
if(key>359.0f)
yRot=0.0f;
if(key<0.0f)
yRot=359.0f;
glutPostRedisplay();
}
void combineNormals(float xNormal[])
{
for(int i=0;i<=2;i++)
{
//checking contents of all_normals[i] and index x
/*for(int s=0;s<=2;s++)
{printf("%f %d
",all_normals[s], s);}*/
all_normals[x] = xNormal[i];
x=x+1;
}
}
void renderScene()
{
calcNormals(bottom_vertices,bottom_normals);
calcNormals(back_vertices,back_normals);
calcNormals(lside_vertices,lside_normals);
calcNormals(front_vertices,front_normals);
combineNormals(bottom_normals);;
combineNormals(back_normals);
combineNormals(lside_normals);
combineNormals(front_normals);
// just checking contents of all_normals[]
/*int k;
for(k=0;k<=11;k++)
printf("%f
",all_normals[k]);*/
glPushMatrix();
glRotatef(xRot,1.0f,0.0f,0.0f);
glRotatef(yRot,0.0f,1.0f,0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(3,GL_FLOAT,0,pyramid_vertex_colors);
glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT,0,all_normals);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3,GL_FLOAT,0,pyramid_vertices);
glDrawElements(GL_TRIANGLES,12,GL_UNSIGNED_BYTE,allindices);
glPopMatrix();
glutSwapBuffers();
}
void changeSize(GLsizei w, GLsizei h)
{
glViewport(0,0,w,h);
if(h==0)
h=1;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glOrtho(-30.0f,30.0f,-10.0f,60.0f,-100.0f,100.0f);
//gluPerspective(60.0f,1.0f,80.0f,40.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//gluLookAt(0.0f,0.0f,80.0f,0.0f,0.0f,0.0f,0.0f,1.0f,0.0f);
}
void main()
{
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow(“Hello”);
glutDisplayFunc(renderScene);
glutReshapeFunc(changeSize);
glutSpecialFunc(specialKeys);
setupRC();
glutMainLoop();
}