| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 915 人关注过本帖
标题:请教:OpenGL 关于怎么把8面体分割成球的问题 ,大侠们帮忙看下
只看楼主 加入收藏
jsz60000
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2009-7-9
收藏
 问题点数:0 回复次数:0 
请教: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;
}
搜索更多相关主题的帖子: 成球 OpenGL 
2009-12-02 12:22
快速回复:请教:OpenGL 关于怎么把8面体分割成球的问题 ,大侠们帮忙看下
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.038054 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved