PDA

View Full Version : Perspective problem



swethasharma
03-02-2012, 08:10 AM
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\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);

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,GL FW_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

BionicBytes
03-02-2012, 09:31 AM
Try drawing the sphere into the projected screen a bit more so it is not clipped by the camera fustrum.

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

add
gltranslatef (0,0,-10.0);

swethasharma
03-02-2012, 08:40 PM
Hi BionicBytes,

Got it . Thanks a lot.
-swetha