Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4598020
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:20:24+00:00 2026-05-21T23:20:24+00:00

i want to make a sphere and put a camera which focuses at the

  • 0

i want to make a sphere and put a camera which focuses at the center ,having a vector (VUP=0,1,0).I want to alter the spherical coordinates r,theta,phi.

The problem is that when i run the application,the sphere appears but when i press a key it disappears.
If i delete the glMatrixMode(GL_PROJECTION) from the setupmywindow,then at the beginning the sphere doesn’t appear ,but if i press a key it appears.Then , bu pressing “r” and “p” ,it changes its radius but pressing “u” and “f” it rotates only one step.

#define PI 3.1415f


float theta = 0, phi = 0, dtheta = PI / 20, dphi = PI / 20;
float r = 0.2;

void setupmywindow()
{
    glClearColor(1.0,1.0,1.0,0);
    glColor3f(0.0, 0.0, 0.0);
    glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
    glMatrixMode(GL_PROJECTION);
    gluPerspective(30,1,2,100);
    glMatrixMode(GL_MODELVIEW);
    glEnable(GL_DEPTH_TEST);
}


void myaxes(double size)
{
    glBegin(GL_LINES);
        glColor3f(0,0,0);glVertex3f(0,0,0); glColor3f(1,0,0);glVertex3f(size,0,0); //x-axis
        glColor3f(0,0,0);glVertex3f(0,0,0); glColor3f(0,1,0);glVertex3f(0,size,0); //y-axis
        glColor3f(0,0,0);glVertex3f(0,0,0); glColor3f(0,0,1);glVertex3f(0,0,size); //z-axis
    glEnd();
}

void sphere()
{
float z1, x1, y1, z2, x2, y2, z3, x3, y3, z4, x4, y4;

float angle = 0,translate;

 glTranslatef(0.0,0.0,translate);

 angle++;
 glRotatef(angle, 1.0, 1.0, 1.0);

 glBegin(GL_QUAD_STRIP);

 for(theta=0;theta<=2.0*PI;theta+=dtheta)
 {
        for(phi=0;phi<=PI;phi+=dphi)
            {

            z1 = r * sin(phi + dphi) * cos(theta + dtheta);
            x1 = r * sin(phi + dphi) * sin(theta + dtheta);
            y1 = r * cos(phi + dphi);

            z2 = r * sin(phi) * cos(theta + dtheta);
            x2 = r * sin(phi) * sin(theta + dtheta);
            y2 = r * cos(phi);

            z3 = r * sin(phi) * cos(theta);
            x3 = r * sin(phi) * sin(theta);
            y3 = r * cos(phi);

            z4 = r * sin(phi + dphi) * cos(theta);
            x4 = r * sin(phi + dphi) * sin(theta);
            y4 = r * cos(phi + dphi);


 glColor3f(1,0,0);
 glVertex3f(x4, y4, z4);
 glVertex3f(x1, y1, z1);
 glVertex3f(x2, y2, z2);
 glVertex3f(x3, y3, z3);

            }
 }
 glEnd();

}

void myDrawing()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(r,theta,phi,0,0,0,0,1,0);

    sphere();
    myaxes(1.5);
    glutSwapBuffers();
}

void mykeyboardcontrol(unsigned char key, int x, int y)
{
    switch(key){
        case 'r': r+=0.1;break; //increase radius
        case 'p': r-=0.1;break; //decrease radius
        case 'u': theta+=PI/20;break;//increase theta angle
        case 'f': phi+=PI/20;break;//increase phi angle

    }
    printf("r=%f  theta=%f  phi=%f\n",r,theta,phi);
    if(key==27) exit(0);

    if(theta>2*PI) theta-=2*PI;
    if(phi>PI) phi-=PI;

    printf("r=%f  theta=%f  phi=%f\n",r,theta,phi);

    glutPostRedisplay();
}

int main(int argc, char **argv)
 {

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
glutCreateWindow("Sphere");
setupmywindow();
glutDisplayFunc(myDrawing);
glutKeyboardFunc(mykeyboardcontrol);

glutMainLoop();
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T23:20:25+00:00Added an answer on May 21, 2026 at 11:20 pm

    Here you go:

    #include <GL/glut.h>
    #include <cmath>
    #include <cstdlib>
    #include <cstdio>
    
    const float PI = 3.14159f;
    float camera_theta = 2.042033;
    float camera_phi = 8.639376;
    float camera_r = 15;
    
    double aspect_ratio = 0;
    void reshape(int w, int h)
    {
        aspect_ratio = (double)w / (double)h;
        glViewport(0, 0, w, h);
    }
    
    void setupmywindow()
    {
        glClearColor(1,1,1,0);
        glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
    }
    
    void myaxes(double size)
    {
        glBegin(GL_LINES);
        glColor3f(0,0,0);
        glVertex3f(0,0,0); 
        glColor3f(1,0,0);
        glVertex3f(size,0,0); //x-axis
    
        glColor3f(0,0,0);
        glVertex3f(0,0,0); 
        glColor3f(0,1,0);
        glVertex3f(0,size,0); //y-axis
    
        glColor3f(0,0,0);
        glVertex3f(0,0,0);
        glColor3f(0,0,1);
        glVertex3f(0,0,size); //z-axis
        glEnd();
    }
    
    void sphere(float radius)
    {
        float z1, x1, y1, z2, x2, y2, z3, x3, y3, z4, x4, y4;
    
        float dtheta = PI / 20;
        float dphi = PI / 20;
    
        glBegin(GL_QUADS);
        for( float theta = 0; theta<=2.0*PI; theta+=dtheta )
        {
            for( float phi = 0; phi<=PI; phi+=dphi )
            {
                z1 = radius * sin(phi + dphi) * cos(theta + dtheta);
                x1 = radius * sin(phi + dphi) * sin(theta + dtheta);
                y1 = radius * cos(phi + dphi);
    
                z2 = radius * sin(phi) * cos(theta + dtheta);
                x2 = radius * sin(phi) * sin(theta + dtheta);
                y2 = radius * cos(phi);
    
                z3 = radius * sin(phi) * cos(theta);
                x3 = radius * sin(phi) * sin(theta);
                y3 = radius * cos(phi);
    
                z4 = radius * sin(phi + dphi) * cos(theta);
                x4 = radius * sin(phi + dphi) * sin(theta);
                y4 = radius * cos(phi + dphi);
    
                glColor3f(1,0,0);
                glVertex3f(x4, y4, z4);
                glVertex3f(x1, y1, z1);
                glVertex3f(x2, y2, z2);
                glVertex3f(x3, y3, z3);
            }
        }
        glEnd();
    }
    
    void display(void)
    {
        glEnable(GL_DEPTH_TEST);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(45, aspect_ratio, 1, 100);
    
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    
        // spherical coordinate camera transform, +Z is "up"
        glTranslatef(0 ,0 , -camera_r);
        glRotatef( (camera_theta - PI) * (180.0f/PI), 1,0,0 );
        glRotatef( -camera_phi * (180.0f/PI), 0,0,1 );
    
        sphere(2);
        myaxes(5);
        glutSwapBuffers();
    }
    
    void mykeyboardcontrol(unsigned char key, int x, int y)
    {
        switch(key){
            case 'r': camera_r+=0.1;break; //increase radius
            case 'p': camera_r-=0.1;break; //decrease radius
    
            case 'i': camera_theta+=PI/20;break;//increase theta angle
            case 'k': camera_theta-=PI/20;break;//increase theta angle
            case 'j': camera_phi-=PI/20;break;//increase phi angle
            case 'l': camera_phi+=PI/20;break;//increase phi angle
            }
        printf("r=%f  theta=%f  phi=%f\n",camera_r,camera_theta,camera_phi);
        if(key==27) exit(0);
    
        // clamp theta 
        if( camera_theta < 0 ) camera_theta = 0;
        if( camera_theta > PI ) camera_theta = PI;
    
        // wrap phi
        if( camera_phi > 2*PI ) camera_phi -= 2*PI;
        if( camera_phi < 2*PI ) camera_phi += 2*PI;
    
        printf("r=%f  theta=%f  phi=%f\n",camera_r,camera_theta,camera_phi);
    
        glutPostRedisplay();
    }
    
    int main(int argc, char **argv)
    {
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
    
        glutInitWindowSize(400,400);
        glutCreateWindow("Sphere");
    
        glutDisplayFunc(display);
        glutReshapeFunc(reshape);
        glutKeyboardFunc(mykeyboardcontrol);
    
        setupmywindow();
        glutMainLoop();
        return 0;
    }
    

    I have no clue why you were trying to pass r, theta, and phi into gluLookAt(), so I replaced that with a fixed oblique-ish camera angle looking at the origin. Also you seem to be overwriting theta and phi in sphere(), blasting away whatever mykeyboardcontrol() is trying to do. I couldn’t tell if you want to orbit the camera around the origin or draw a partial sphere.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want make a bash script which returns the position of an element from
I want make sql query which will insert values from one table to another
I want make an irssi script which will notice of new files on server,
I want make a SharedInterrupt class(B) which will serve in series many objects in
I want make a generic hibernate API which will create mapping class from hbm.xml
I want make binding effect in one place so I need to text which
i have this table in database which called data and i want make this
I want make datetimepicker in my project. Using jquery how it is possible? I
I want make interactive application where user launches it and can do various task
Let's say I want make some of my sources publicly available via my blog

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.