Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 6 of 6

Thread: Creating a sphere

  1. #1
    Intern Newbie
    Join Date
    Dec 2011
    Location
    India
    Posts
    36

    Creating a sphere

    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.

    Code :
    #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,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;
     
      }

    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
    swetha

  2. #2
    Intern Newbie
    Join Date
    Dec 2011
    Location
    India
    Posts
    36

    Re: Creating a sphere

    Also how do i calculate the normal for this sphere?
    swetha

  3. #3
    Advanced Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Karachi, Pakistan
    Posts
    810

    Re: Creating a sphere

    Quote Originally Posted by swethasharma
    Also how do i calculate the normal for this sphere?
    Simple subtract the vertex position from the sphere's center and normalize this vector.
    Regards,
    Mobeen

  4. #4
    Intern Newbie
    Join Date
    Dec 2011
    Location
    India
    Posts
    36

    Re: Creating a sphere

    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
    swetha

  5. #5
    Advanced Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Karachi, Pakistan
    Posts
    810

    Re: Creating a sphere

    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.
    Regards,
    Mobeen

  6. #6
    Member Regular Contributor
    Join Date
    Mar 2003
    Location
    Los Angeles
    Posts
    374

    Re: Creating a sphere

    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.
    Am I doing your homework for you?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •