Link to home
Start Free TrialLog in
Avatar of slamjam2000
slamjam2000

asked on

my program doesn't show the half cone....

My c program should show me some white lines around the y-axis between X = 1 to X = -1.  But how come it doesn't show anything, but a black screen???

/* Compile:  gcc -o cone cone.c -I /usr/X11R6/include/ -L /usr/X11R6/lib/ -lglut -lGL -lGLU -lX11 -lXmu -lXi -lm */

#include <stdlib.h>
#include <GL/glut.h>
#include <math.h>

void init()
{
      glClearColor(0.0, 0.0, 0.0, 0.0);
      glShadeModel(GL_FLAT);
}

void myCone()
{
      
      glBegin(GL_TRIANGLE_FAN);
      
      glColor3f(1, 1, 1);
      glVertex3i(0,0,0);    // Center vertex
      
      glColor3f(1,1,1);
        glVertex3i(0,0,2);      // Z vertex
      glColor3f(1,1,1);
        glVertex3i(1,0,0);
      
      double x1 = 1.0;
      
      for(x1; x1 >= -1.0; x1 = x1 - 0.1)
      {
            double y = sqrt(1 - (x1*x1));
            glColor3f(1, 1, 1);      
            glVertex3i(x1,y,0.0);    // point on the circle      
      }      
      
      glEnd();                    // Triangles Ended
                         
}

void display()
{
      glClear(GL_COLOR_BUFFER_BIT);
      glColor3f(0.0, 0.0, 0.0);
      
      glPushMatrix();
      GLfloat m[16];
      
      glLoadMatrixf(m);
      glMultMatrixf(m);
      gluPerspective(90.0, 1.3, 1.0, 100);
      
      myCone();
      
      glPopMatrix();
      glFlush();
}

void reshape(int w, int h)
{
      glViewport(0, 0, 512, 512);
      glMatrixMode(GL_PROJECTION);
      if (w <= h)
            glOrtho(-1.5, 1.5, -1.5*(GLfloat)h/(GLfloat)w,1.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
          else
            glOrtho(-1.5*(GLfloat)w/(GLfloat)h,1.5*(GLfloat)w/(GLfloat)h, -1.5, 1.5, -10.0, 10.0);
      
      GLfloat matrixFrom_gluLookAt[16];
      
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
      
      
      gluLookAt(0.0, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
      glGetFloatv(GL_MODELVIEW_MATRIX, matrixFrom_gluLookAt);      
}


void keyboard( unsigned char key, int x, int y )
{
  switch ( key )
    {
    default:
      break;
    case 'c':
          glutDisplayFunc(display);
          glEnable(GL_SMOOTH);
      break;
     
    case 'q':
         printf("doing something");
      break;
    }
}

int main(int argc, char** argv)
{
      glutInit(&argc, argv);
      glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
      glutInitWindowSize(640, 480);
      glutInitWindowPosition(100, 100);
      glutCreateWindow("3D Cone");
      init();

      glutKeyboardFunc(keyboard);
      glutDisplayFunc(display);
      glutReshapeFunc(reshape);

      glutMainLoop();
      return 0;
}
Avatar of slamjam2000
slamjam2000

ASKER

I got it!  Stupid me, I need to have the glVertex3f instead of glVertex3i....  Waste me an hour to figure this out....

Now I got a another question, how can I filp the cone and did my ortho function satiasifies for 3D ???  Because, when I run the program, it's still in 2D.

Thanks for suggestions.


/* Compile:  gcc -o cone cone.c -I /usr/X11R6/include/ -L /usr/X11R6/lib/ -lglut -lGL -lGLU -lX11 -lXmu -lXi -lm */

#include <stdlib.h>
#include <GL/glut.h>
#include <math.h>

void init()
{
      glClearColor(0.0, 0.0, 0.0, 0.0);
      glShadeModel(GL_FLAT);
}

void myCone()
{
      EnableOpenGL( hWnd, &hDC, &hRC );
      glBegin(GL_TRIANGLE_FAN);
      
      glColor3f(1.0, 1.0, 1.0);
      glVertex3f(0.0,0.0,0.0);    // Center vertex
      
      double x1 = 1.0;
      
      while(x1 >= -1.0)
      {
            double y = sqrt(1 - (x1*x1));
            glColor3f(1.0, 1.0, 1.0);      
            glVertex3f(x1,y,0.0);    // point on the circle
            x1 = x1 - 0.05;
            y = sqrt(1 - (x1*x1));
            glColor3f(1.0, 1.0, 1.0);      
            glVertex3f(x1,y,0.0);    // point on the circle
            x1 = x1 - 0.05;
      }      
      SwapBuffers(hDC);
      glEnd();                    // Triangles Ended
                         
}

void display()
{
      glClear(GL_COLOR_BUFFER_BIT);
      glColor3f(0.0, 0.0, 0.0);
      
      glPushMatrix();
      
      myCone();
      
      GLfloat m[16];
      
      glLoadMatrixf(m);
      glMultMatrixf(m);
      gluPerspective(90.0, 1.3, 1.0, 100);
      
      
      
      glPopMatrix();
      glFlush();
}

void reshape(int w, int h)
{
      glViewport(0, 0, 512, 512);
      glMatrixMode(GL_PROJECTION);
      if (w <= h)
            glOrtho(-1.5, 1.5, -1.5*(GLfloat)h/(GLfloat)w,1.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
          else
            glOrtho(-1.5*(GLfloat)w/(GLfloat)h,1.5*(GLfloat)w/(GLfloat)h, -1.5, 1.5, -10.0, 10.0);
      
      GLfloat matrixFrom_gluLookAt[16];
      
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
      
      
      gluLookAt(0.0, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
      glGetFloatv(GL_MODELVIEW_MATRIX, matrixFrom_gluLookAt);      
}

int main(int argc, char** argv)
{
      glutInit(&argc, argv);
      glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
      glutInitWindowSize(640, 480);
      glutInitWindowPosition(100, 100);
      glutCreateWindow("Nuo Neil Xu - 3D Cone");
      init();

      glutDisplayFunc(display);
      glutReshapeFunc(reshape);

      glutMainLoop();
      return 0;
}
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial