PDA

View Full Version : what is wrong?



swethasharma
03-28-2012, 11:09 PM
Hello,

I have been trying to light the sphere.It is lighting wrong.



#define GLEW_STATIC
#include <stdio.h>
#include <stdlib.h>
#include <gl/glew.h>
#include <glfw.h>
#include <math.h>


#define space 10
#define vertexcount (190/space)*(360/space)*2



const double PI=3.1415926535897;

struct Vertices
{
GLfloat x;
GLfloat y;
GLfloat z;
} vertex [vertexcount];
struct Vertices normalvec[vertexcount];
GLfloat modvec[vertexcount];
void init(void)
{
int glewinitialize;
GLfloat mat_specular[]={1.0,1.0,1.0,1.0};
GLfloat mat_shininess[]={50.0};
GLfloat light_position[]={1.0,1.0,1.0,0.0};
GLfloat white_light[]={1.0,1.0,1.0,1.0};
GLfloat lmodel_ambient[]={0.1,0.1,0.1,1.0};
glClearColor(0.0,0.0,0.0,0.0);
glShadeModel(GL_SMOOTH);
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_spe cular);
glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,mat_sh ininess);
glLightfv(GL_LIGHT0,GL_POSITION,light_position);
glLightfv(GL_LIGHT0,GL_DIFFUSE,white_light);
glLightfv(GL_LIGHT0,GL_SPECULAR,white_light);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT,lmodel_ambie nt);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
glewinitialize=glewInit();
if(glewinitialize==GLEW_OK)
{
printf("GLEW is available\n");

}

}

void GLFWCALL reshape(int width,int height)
{

glViewport(0,0,500,500);
glMatrixMode(GL_PROJECTION);

glLoadIdentity();
glOrtho(-10.0,(GLdouble)10.0,-10.0,(GLdouble)10.0,-10.0,200.0);
//glOrtho(-1.5*(GLfloat)width/(GLfloat)height,1.5*(GLfloat)width/(GLfloat)height,-1.5,1.5,-10.0,10.0);
// gluPerspective(60.0,width/height,0.1,100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

gluLookAt(0.0,0.0,1.0,0.0,0.0,0.0,0.0,1.0,0.0);

}

void setNormals(int n)
{
GLfloat mod;

mod=sqrt(vertex[n].x*vertex[n].x+vertex[n].y*vertex[n].y+vertex[n].z*vertex[n].z);
normalvec[n].x=vertex[n].x/mod;
normalvec[n].y=vertex[n].y/mod;
normalvec[n].z=vertex[n].z/mod;
modvec[n]=sqrt(normalvec[n].x*normalvec[n].x+normalvec[n].y*normalvec[n].y+normalvec[n].z*normalvec[n].z);

}
void createsphere(int R,int H,int K,int Z)
{
int n;
int a;
int b;
double mod;
double B;


n=0;
for(a=-180;a<=180-space;a+=space)
{
for(b=0;b<=190-space;b+=space)
{


vertex[n].x=R*(sin((a*PI)/180))*(sin((b*PI)/180))-H;
vertex[n].z=R*(cos((b*PI)/180))*(sin((a*PI)/180))-Z;
vertex[n].y=R*(cos((a*PI)/180))-K;
setNormals(n);
n++;



vertex[n].x=R*(sin(((a+space)*PI)/180))*(sin((b*PI)/180))-H;
vertex[n].z=R*(cos((b*PI)/180))*(sin(((a+space)*PI)/180))-Z;
vertex[n].y=R*(cos(((a+space)*PI)/180))-K;
setNormals(n);
n++;



}
}
}

void displaysphere(double R)
{
int b,b1,b2,b3,b4;
int theta;
int phi;
GLfloat mat[16];

glScalef(0.0125*R,0.0125*R,0.0125*R);
//glRotatef(90,0.0,0.0,1.0);
//glRotatef(90,1.0,0.0,0.0);



glColor3f(1.0,0.5,0.0);
// b3=sqrt(normalvec[3].x*normalvec[3].x+normalvec[3].y*normalvec[3].y+normalvec[3].z*normalvec[3].z);
//glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);



glColor3f(1.0,0.5,0.0);

b1=0;
while(b1<vertexcount)
{


glBegin(GL_QUAD_STRIP);
for(b=b1;b<(b1+38);b=b+1)
{
glNormal3f(normalvec[B].x,normalvec[B].y,normalvec[B].z);
glVertex3f(vertex[B].x,vertex[B].y,vertex[B].z);
}

glEnd();
b1=b1+38;
}



glEnd();




}
void display(void)
{

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f(1.0,0.5,0.0);


//glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
//
//glFrontFace(GL_CW);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glPushMatrix();
glTranslatef(0.0,0.0,-20.0);
glColor3f(1.0,0.5,0.0);
createsphere(20,0,0,0);
displaysphere(15);
glPopMatrix();


Just tell me what is wrong in this code.
-swetha

tonyo_au
03-29-2012, 12:18 AM
It is lighting wrong.

What do you mean?

tonyo_au
03-29-2012, 12:22 AM
Could your light be too bright?

swethasharma
03-29-2012, 12:25 AM
I am not able to get 3D shape.It looks like a circle.

swethasharma
03-29-2012, 12:33 AM
I checked with gluSolidsphere,it is lighting properly.I am getting that 3D shape.

swethasharma
03-29-2012, 01:24 AM
Finally solved,I had forgotten to enable rescale_normal.I used scaling operation.