为了账号安全,请及时绑定邮箱和手机立即绑定

使用Visual C ++在Opengl中创建3D球体

使用Visual C ++在Opengl中创建3D球体

C++
尚方宝剑之说 2019-09-24 09:54:24
我无法使用C ++中的OpenGL库函数glutSolidSphere()创建简单的3D球体。这是我尝试过的:#include<GL/glu.h> void display() {     glClear(GL_COLOR_BUFFER_BIT);     glColor3f(1.0,0.0,0.0);     glLoadIdentity();     glutSolidSphere( 5.0, 20.0, 20.0);     glFlush(); } void myInit() {    glClearColor(1.0,1.0,1.0,1.0);     glColor3f(1.0,0.0,0.0);     glMatrixMode(GL_PROJECTION);     glLoadIdentity();     gluOrtho2D(0.0,499.0,0.0,499.0);     glMatrixMode(GL_MODELVIEW); } void main(int argc,char **argv) {     qobj = gluNewQuadric();     glutInit(&argc,argv);     glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);     glutInitWindowSize(500,500);     glutCreateWindow("pendulum");             glutDisplayFunc(display);     myInit();     glutMainLoop(); }
查看完整描述

3 回答

?
凤凰求蛊

TA贡献1825条经验 获得超4个赞

我不知道datenwolf的索引生成如何正确。但是我仍然发现他的解决方案相当明确。这是我经过一番思考后得到的:


inline void push_indices(vector<GLushort>& indices, int sectors, int r, int s) {

    int curRow = r * sectors;

    int nextRow = (r+1) * sectors;


    indices.push_back(curRow + s);

    indices.push_back(nextRow + s);

    indices.push_back(nextRow + (s+1));


    indices.push_back(curRow + s);

    indices.push_back(nextRow + (s+1));

    indices.push_back(curRow + (s+1));

}


void createSphere(vector<vec3>& vertices, vector<GLushort>& indices, vector<vec2>& texcoords,

             float radius, unsigned int rings, unsigned int sectors)

{

    float const R = 1./(float)(rings-1);

    float const S = 1./(float)(sectors-1);


    for(int r = 0; r < rings; ++r) {

        for(int s = 0; s < sectors; ++s) {

            float const y = sin( -M_PI_2 + M_PI * r * R );

            float const x = cos(2*M_PI * s * S) * sin( M_PI * r * R );

            float const z = sin(2*M_PI * s * S) * sin( M_PI * r * R );


            texcoords.push_back(vec2(s*S, r*R));

            vertices.push_back(vec3(x,y,z) * radius);

            push_indices(indices, sectors, r, s);

        }

    }

}


查看完整回答
反对 回复 2019-09-24
  • 3 回答
  • 0 关注
  • 703 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信