#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 voidinit(void){ getdata(&Data); //getdata(array); glCullFace(GL_BACK); //glEnable(GL_CULL_FACE); glDisable(GL_DITHER); glShadeModel(GL_FLAT); glEnable(GL_DEPTH_TEST);}
static voidkey(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. */}
自己写的一个小程序,运行之后如上图所示。
