Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-09-28
3
Medium Priority
?
404 Views
Last Modified: 2006-11-17
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;
}
0
Comment
Question by:slamjam2000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 1

Author Comment

by:slamjam2000
ID: 12174441
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;
}
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 12393986
PAQed, with points refunded (70)

Computer101
E-E Admin
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about problems I had with the new location API and permissions in Marshmallow
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Six Sigma Control Plans

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question