-1

I have drawn a house with a slanted roof in opengl. I am having problem in defining the normals for the slanted roofs.It wont be parallel to the axes so this is what I have used:

glBindTexture(GL_TEXTURE_2D,texture[3]);
glBegin(GL_QUADS);
glColor3f(1,1,0);
glNormal3f(0,cos(th),sin(th));

glTexCoord2f(0,0);
glVertex3f(2,1.25,1);
glTexCoord2f(1,0);
glVertex3f(2,2.2,0);
glTexCoord2f(1,1);
glVertex3f(-2,2.2,0);
glTexCoord2f(0,1);
glVertex3f(-2,1.25,1);
glEnd();
glBindTexture(GL_TEXTURE_2D,texture[3]);
glBegin(GL_QUADS);

glColor3f(1,1,0);
glNormal3f(0,cos(-th),sin(-th));
//glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex3f(-2,1.25,-1);
glTexCoord2f(1,0);
glVertex3f(-2,2.2,0);
glTexCoord2f(1,1);
glVertex3f(2,2.2,0);
glTexCoord2f(0,1);
glVertex3f(2,1.25,-1);
glEnd();

My program allows the user to choose the angle from which he wants see the object th is that angle..

It seems the lighting is not right.

Can anyone please help me?

datenwolf
  • 159,371
  • 13
  • 185
  • 298

1 Answers1

1

My program allows the user to choose the angle from which he wants see the object th is that angle..

Then this is the wrong value to use for a normal. You should calculate the normal from the vertices. A quad consists of two triangles. A triangles has 3 edges. Taking the cross product of two edges of a triangle gives you the normal of the triangle.

datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • Can you show me one as an example using the vertices i have.I am pretty new to graphics and still trying to get a grasp of it. – user3600372 Jun 24 '14 at 01:18
  • 2
    @user3600372: I could of course write it down with exactly the numbers you have there, but then you'd learn nothing from it. First things first you must get yourself familiar with the basics of linear algebra; if you don't you'll have a very hard time doing any kind of graphics programming. One particular operation possible with vectors of dimension 3 is the so called "cross product" which yields the vector perpendicular to two given vectors. If your vectors are (x,y,z) and (X,Y,Z) then the cross product is (yZ-zY, zX-xZ, xY-yX). Put the vectors between vertex positions into the cross product. – datenwolf Jun 24 '14 at 09:18
  • I have calculated the normals for the 2 sets of points..they are (0,4,3.8) and (0,4,-3.8).Could you please tell me if they are correct? – user3600372 Jun 25 '14 at 04:41
  • @user3600372: Definitely not. Normals to be used for OpenGL illumination must be unit length, which means none of their elements must be larger than 1. – datenwolf Jun 25 '14 at 08:34
  • ohh ok...so i had not normalised...the normalised vectors will be (0,0.73,0.69) and (0,0.73,-0.69) – user3600372 Jun 25 '14 at 14:52