Hello,
I have written code to draw a sphere. I have referred the code for drawing a sphere from swiftless tutorial.I am able to view the sphere only when i do orthographic projection.I am not able to view the sphere under perspective projection.
#define GLEW_STATIC
#include <stdio.h>
#include <stdlib.h>
#include <gl/glew.h>
#include <glfw.h>
#include <math.h>
#define space 10
#define vertexcount (90/space)*(360/space)*4
const double PI=3.1415926535897;
struct Vertices
{
double x;
double y;
double z;
} vertex [vertexcount];
void init(void)
{
int glewinitialize;
//Vertices vertex[vertexcount];
//float aspect_ratio;
glClearColor(0.0,0.0,0.0,0.0);
glClearDepth(1.0);
glShadeModel(GL_FLAT);
glewinitialize=glewInit();
if(glewinitialize==GLEW_OK)
{
printf("GLEW is available
");
}
}
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);
gluPerspective(60.0,width/height,0.1,100.0);
glMatrixMode(GL_MODELVIEW);
}
void createsphere(int R,int H,int K,int Z)
{
int n;
int a;
int b;
int debug=0;
double A;
double B;
n=0;
for(b=0;b<=90-space;b+=space)
{
for(a=0;a<=360-space;a+=space)
{
vertex[n].x=R*(sin((a*PI)/180))*(sin((b*PI)/180))-H;
vertex[n].y=R*(cos((a*PI)/180))*(sin((b*PI)/180))+K;
vertex[n].z=R*(cos((b*PI)/180))-Z;
n++;
vertex[n].x=R*(sin((a*PI)/180))*(sin(((b+space)*PI)/180))-H;
vertex[n].y=R*(cos((a*PI)/180))*(sin(((b+space)*PI)/180))+K;
vertex[n].z=R*(cos(((b+space)*PI)/180))-Z;
n++;
vertex[n].x=R*(sin(((a+space)*PI)/180))*(sin((b*PI)/180))-H;
vertex[n].y=R*(cos(((a+space)*PI)/180))*(sin((b*PI)/180))+K;
vertex[n].z=R*(cos((b*PI)/180))-Z;
n++;
vertex[n].x=R*(sin(((a+space)*PI)/180))*(sin(((b+space)*PI)/180))-H;
vertex[n].y=R*(cos(((a+space)*PI)/180))*(sin(((b+space)*PI)/180))+K;
vertex[n].z=R*(cos(((b+space)*PI)/180))-Z;
n++;
}
}
}
void displaysphere(double R)
{
int b;
glLoadIdentity();
gluLookAt(0.0,0.0,1.0,0.0,0.0,0.0,0.0,1.0,0.0);
glScalef(0.0125*R,0.0125*R,0.0125*R);
// glRotatef(90,1.0,0.0,0.0);
glBegin(GL_TRIANGLE_STRIP);
for( b=0;b<=vertexcount;b++)
{
glVertex3f(vertex[b].x,vertex[b].y,-vertex[b].z);
}
for( b=0;b<=vertexcount;b++)
{
glVertex3f(vertex[b].x,vertex[b].y,vertex[b].z);
}
glEnd();
}
void display(void)
{
GLfloat mat[16];
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f(1.0,0.5,0.0);
glLoadIdentity();
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glCullFace(GL_BACK);
glFrontFace(GL_CCW);
glEnable(GL_CULL_FACE);
createsphere(70,0,0,0);
displaysphere(5);
glfwSwapBuffers();
}
int main(int argc,char** argv)
{
int wndstate,wndopenstate;
wndstate=glfwInit();
if(wndstate==1)
{
wndopenstate=glfwOpenWindow(640,480,0,0,0,0,4,0,GLFW_WINDOW);
if(wndopenstate==0)
{
glfwTerminate();
}
else
{
glfwSetWindowTitle("Hello Opengl");
glfwSetWindowPos(30,30);
init();
glfwSetWindowSizeCallback(reshape);
while(1)
{
display();
if(glfwGetKey(GLFW_KEY_ESC))
{
glfwTerminate();
}
}
}
}
glfwTerminate();
return 0;
}
Where is the mistake?
-swetha