OpenGL實現的一個wire-sphere的小程式

來源:互聯網
上載者:User


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

#include <GL/glut.h>

static float * Data = NULL;
static float lati_array[10404][3];
static float longi_array[5100][3];
static float angle = 2 * 3.1415 / 100;
static int radius = 60;
static char temp[80];

static void getdata(float **data)
{

static int i = 0, j = 0;
//static float angle_xy, angle_xz;
static FILE   *fp;

fp = fopen("latitude_sphere_data", "ab");

  for(i = 0; i < 102; i++)
     {
        //angle_xy = (i - 1) * angle;
        //angle_xz = (i - 1) * angle;
        for (j = 0; j < 102; j++)
             {
                   lati_array[j + (i  * 102)][0] = (radius * cos(i * angle)) * cos(j * angle);
                   sprintf(temp, " %f ,  ", lati_array[j + (i * 102)][0]);
                   fwrite(temp, strlen(temp), 1, fp);

                   lati_array[j + (i * 102)][1] = radius * sin(i * angle);
                   sprintf(temp, " %f ,  ", lati_array[j + (i * 102)][1]);                      
                   fwrite(temp, strlen(temp), 1, fp);

                   lati_array[j + (i * 102)][2] = (radius * cos(i * angle)) * sin(j * angle);
                   sprintf(temp, " %f ,  /n", lati_array[j + (i * 102)][2]);                      
                   fwrite(temp, strlen(temp), 1, fp);
              }
       
     }

fclose(fp);

fp = fopen("longitude_sphere_data", "ab");

  for(i = 0; i < 50; i++)
     {
        //angle_xy = (i - 1) * angle;
        //angle_xz = (i - 1) * angle;
        for (j = 0; j < 102; j++)
             {
                   longi_array[j + (i  * 102)][0] = (radius * cos(j * angle)) * cos(i * angle);
                   sprintf(temp, " %f ,  ", longi_array[j + (i * 102)][0]);
                   fwrite(temp, strlen(temp), 1, fp);

                   longi_array[j + (i * 102)][1] = radius * sin(j * angle);
                   sprintf(temp, " %f ,  ", longi_array[j + (i * 102)][1]);                      
                   fwrite(temp, strlen(temp), 1, fp);

                   longi_array[j + (i * 102)][2] = (radius * cos(j * angle)) * sin(i * angle);
                   sprintf(temp, " %f ,  /n", longi_array[j + (i * 102)][2]);                      
                   fwrite(temp, strlen(temp), 1, fp);
              }
       
     }

fclose(fp);

}

static void draw(float *data)
{
static int i = 0;

 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 glVertexPointer(3, GL_FLOAT, 0, lati_array);
 glEnableClientState(GL_VERTEX_ARRAY);
 glColor4f(0.0, 1.0, 0.0, 1.0);
 for (i = 0; i < 102; i++ )
     {
         glDrawArrays(GL_LINE_LOOP, (i * 102), 102);
     }

  glDisableClientState(GL_VERTEX_ARRAY);

 glVertexPointer(3, GL_FLOAT, 0, longi_array);
 glEnableClientState(GL_VERTEX_ARRAY);
 glColor4f(1.0, 0.0, 0.0, 1.0);
 for (i = 0; i < 50; i++ )
     {
         glDrawArrays(GL_LINE_LOOP, (i * 102), 102);
     }

  glDisableClientState(GL_VERTEX_ARRAY);

}

static void
reshape(int width, int height)
{
    float aspect = (float) width / (float) height;
    glViewport(0, 0, (GLint) width, (GLint) height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-100.0 * aspect, 100.0 * aspect, -100.0, 100.0, -100.0, 100.0);
    glMatrixMode(GL_MODELVIEW);
 
}

static void display(void)
{
    glRotatef(45.0, 1.0, 0.0, 0.0);
    //glRotatef(0, 0.0, 0.0, 1.0);
   
    draw(Data);
     //NSidedPolygon(n, cx, cy, radius); 
    //glPopMatrix();

}

static void
init(void)
{
     getdata(&Data);
    //getdata(array);
    glCullFace(GL_BACK);
    //glEnable(GL_CULL_FACE);
    glDisable(GL_DITHER);
    glShadeModel(GL_FLAT);
    glEnable(GL_DEPTH_TEST);
}

static void
key(unsigned char k, int x, int y)
{
  switch (k) {
  case 27:  /* Escape */
    exit(0);
    break;
  default:
    return;
  }
  glutPostRedisplay();
}

int main(int argc, char** argv)
{
   glutInit(&argc, argv);
   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
   glutInitWindowSize (800, 600);
   glutInitWindowPosition (100, 100);
   glutCreateWindow (argv[0]);
   init ();
   glutReshapeFunc(reshape);
   glutDisplayFunc(display);
   glutKeyboardFunc(key);
   glutMainLoop();
   glFinish();
   return 0;   /* ANSI C requires main to return int. */
}

自己寫的一個小程式,運行之後如所示。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.