Solved

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

Posted on 2004-09-28
3
402 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

Technology Partners: 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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Introduction to Processes

695 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