Solved

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

Posted on 2004-09-28
3
397 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
3 Comments
 
LVL 1

Author Comment

by:slamjam2000
Comment Utility
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
Comment Utility
PAQed, with points refunded (70)

Computer101
E-E Admin
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This is an explanation of a simple data model to help parse a JSON feed
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now