PDA

View Full Version : Creating a sphere



swethasharma
03-18-2012, 03:51 AM
Hello,

I have created a sphere.I have actually modified a code taken from elsewhere.I am viewing the sphere with Polygon mode set to GL_LINE.I have enabled culling and set the culling to back face.I am getting a non-uniform sphere.I have attached the output.



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


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



const double PI=3.1415926535897;

struct Vertices
{
double x;
double y;
double z;
} vertex [vertexcount];

void init(void)
{
int glewinitialize;

glClearColor(0.0,0.0,0.0,0.0);
glClearDepth(1.0);
glShadeModel(GL_SMOOTH);



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();

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 createsphere(int R,int H,int K,int Z)
{
int n;
int a;
int b;
int debug=0;
int d=0;
double A;
double B;


n=0;
for(b=0;b<=180-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].z=R*(cos((b*PI)/180))*(sin((a*PI)/180))-Z;
vertex[n].y=R*(cos((a*PI)/180))-K;
n++;

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

n++;

}
}
}

void displaysphere(double R)
{
int b;
GLfloat mat[16];
glScalef(0.0125*R,0.0125*R,0.0125*R);
glGetFloatv(GL_MODELVIEW_MATRIX,mat);
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);

glBegin(GL_TRIANGLE_STRIP);
for(b=0;b<=vertexcount;b++)
{
glVertex3f(vertex[B].x,vertex[B].y,vertex[B].z);
}

glEnd();



}
void display(void)
{

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

glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glPushMatrix();
glTranslatef(0.0,0.0,-10.0);
glColor3f(1.0,0.5,0.0);
createsphere(20,0,0,0);
displaysphere(15);
glPopMatrix();

glPushMatrix();
glColor3f(0.0,1.0,0.0);
createsphere(20,0,0,0);
glTranslatef(10.0,10.0,-50.0);
//displaysphere(10);
glPopMatrix();

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;

}




I had taken this code from some other references in which culling was enabled.

Why am I getting an output like this?Where is the mistake

swethasharma
03-18-2012, 03:52 AM
Also how do i calculate the normal for this sphere?

mobeen
03-18-2012, 06:55 AM
Also how do i calculate the normal for this sphere?

Simple subtract the vertex position from the sphere's center and normalize this vector.

swethasharma
03-19-2012, 03:09 AM
Hello,
The non uniformity in the rendering of the sphere was due to perspective projection.I have calculated the normal but it looks faceted.How do i get a smooth surface?
-swetha

mobeen
03-19-2012, 07:53 AM
You must be using the face normal as the vertex normal.
For a vertex v, find all the faces containing vertex v and then average their normals. Then use this average normal as the vertex normal.

MaxH
03-19-2012, 11:10 AM
Using glColor to assign colors means that you are not using lighting. Lighting must be used to do smooth shading. Without lighting your sphere would just look like a filled in circle. How about posting a picture of what you have now with glPolygonMode to to GL_FILL instead of GL_LINES.