OpenGL实现的一个wire-sphere的小程序


#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. */
}

自己写的一个小程序,运行之后如上图所示。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值