Solved

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

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
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 …

803 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