请教:OpenGL 关于怎么把8面体分割成球的问题 ,大侠们帮忙看下
这个是8面体分割成球体的代码,但是运行出来 什么图片也不出现。。。但是我看了半天 也不清楚错在哪了高手 大侠 帮忙解决一下~~~
#include <Windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glaux.h>
#include <GL/glut.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define X .525731112119133606*5
#define Z .850650808352039932*5
int i,j;
void NormalTriangle(GLfloat v1[3], GLfloat v2[3], GLfloat v3[3], GLfloat *normal)
{
GLfloat vc1[3],vc2[3];
GLfloat a,b,c;
GLdouble r;
vc1[0]= v2[0] - v1[0]; vc1[1]= v2[1] - v1[1]; vc1[2]= v2[2] - v1[2];
vc2[0]= v3[0] - v1[0]; vc2[1]= v3[1] - v1[1]; vc2[2]= v3[2] - v1[2];
a = vc1[1] * vc2[2] - vc2[1] * vc1[2];
b = vc2[0] * vc1[2] - vc1[0] * vc2[2];
c = vc1[0] * vc2[1] - vc2[0] * vc1[1];
r = sqrt( a * a + b* b + c * c);
normal[0] = a / r;
normal[1] = b / r;
normal[2] = c / r;
}
void DrawTriangle(float *v1, float *v2, float *v3)
{
GLfloat normal[3] = {0,0,0};
NormalTriangle(v1,v2,v3,normal);
glBegin(GL_TRIANGLES);
glNormal3fv(normal);
glVertex3fv(v1);
glVertex3fv(v2);
glVertex3fv(v3);
glEnd();
}
void Normalize(float v[3]) {
GLfloat d= sqrt(v[0]*v[1]+v[1]*v[1]+v[2]*v[2]);
if (d == 0.0) {
return;
}
v[0] /= d; v[1] /= d; v[2] /= d;
}
void SubDivide(float *v1, float *v2, float *v3, int count)
{
if(0 >= count)
{
DrawTriangle(v1,v2,v3);
return;
}
else
{
GLfloat v12[3],v23[3],v31[3];
GLint i;
for(i = 0; i < 3; i++){
v12 = (v1+v2)/2;
v23 = (v2+v3)/2;
v31 = (v3+v1)/2;
}
Normalize(v12);
Normalize(v23);
Normalize(v31);
SubDivide(v1,v12,v31,count-1);
SubDivide(v2,v23,v12,count-1);
SubDivide(v3,v31,v23,count-1);
SubDivide(v12,v23,v31,count-1);
}
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0, 0.0, 0.0, 0.0);
glLoadIdentity();
gluLookAt(3.0,3.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0);
GLfloat r=2.0;
static GLfloat vdata[6][3] = {
{r,0.0,0.0},{-r,0.0,0.0},
{0.0,r,0.0},{0.0,-r,0.0},
{0.0,0.0,r},{0.0,0.0,-r}
};
static GLint tindices[8][3] = {
{2,4,0},{2,0,5},{2,5,1},{2,1,4},
{3,0,4},{3,5,0},{3,1,5},{3,4,1} };
glColor3f(0,0.4,0);
for (i = 0; i < 20; i++) {
SubDivide(&vdata[tindices[0]][0],
&vdata[tindices[1]][0],
&vdata[tindices[2]][0],r);
}
}
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_SMOOTH);
GLfloat ambient[] = {0.5, 0.5, 0.5, 1.0};
GLfloat position[] = {0.0, 0.0, 2.0, 1.0};
GLfloat mat_diffuse[] = {1.0, 1.0, 1.0, 1.0};
GLfloat mat_specular[] = {1.0, 1.0, 1.0, 1.0};
GLfloat mat_shininess[] = {50.0};
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
glLightfv(GL_LIGHT0, GL_POSITION, position);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum( -1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
}
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow(argv[0]);
init();
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}