<bdo id='SZV4x'></bdo><ul id='SZV4x'></ul>

    <i id='SZV4x'><tr id='SZV4x'><dt id='SZV4x'><q id='SZV4x'><span id='SZV4x'><b id='SZV4x'><form id='SZV4x'><ins id='SZV4x'></ins><ul id='SZV4x'></ul><sub id='SZV4x'></sub></form><legend id='SZV4x'></legend><bdo id='SZV4x'><pre id='SZV4x'><center id='SZV4x'></center></pre></bdo></b><th id='SZV4x'></th></span></q></dt></tr></i><div id='SZV4x'><tfoot id='SZV4x'></tfoot><dl id='SZV4x'><fieldset id='SZV4x'></fieldset></dl></div>

    1. <small id='SZV4x'></small><noframes id='SZV4x'>

    2. <legend id='SZV4x'><style id='SZV4x'><dir id='SZV4x'><q id='SZV4x'></q></dir></style></legend>

    3. <tfoot id='SZV4x'></tfoot>

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

      时间:2023-09-18
      <i id='LOWgL'><tr id='LOWgL'><dt id='LOWgL'><q id='LOWgL'><span id='LOWgL'><b id='LOWgL'><form id='LOWgL'><ins id='LOWgL'></ins><ul id='LOWgL'></ul><sub id='LOWgL'></sub></form><legend id='LOWgL'></legend><bdo id='LOWgL'><pre id='LOWgL'><center id='LOWgL'></center></pre></bdo></b><th id='LOWgL'></th></span></q></dt></tr></i><div id='LOWgL'><tfoot id='LOWgL'></tfoot><dl id='LOWgL'><fieldset id='LOWgL'></fieldset></dl></div>

          <bdo id='LOWgL'></bdo><ul id='LOWgL'></ul>
              <tbody id='LOWgL'></tbody>

            <tfoot id='LOWgL'></tfoot>

              <small id='LOWgL'></small><noframes id='LOWgL'>

            1. <legend id='LOWgL'><style id='LOWgL'><dir id='LOWgL'><q id='LOWgL'></q></dir></style></legend>

              • 本文介绍了使用 Visual C++ 在 Opengl 中创建 3D 球体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我无法使用 C++ 中的 OpenGL 库函数 glutSolidSphere() 创建一个简单的 3D 球体.

                I am not able to create a simple 3D sphere using the OpenGL library function glutSolidSphere() in C++.

                这是我尝试过的:

                #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(); 
                }
                

                推荐答案

                在 OpenGL 中,您不创建对象,您只绘制它们.绘制完成后,OpenGL 不再关心您发送的几何图形.

                In OpenGL you don't create objects, you just draw them. Once they are drawn, OpenGL no longer cares about what geometry you sent it.

                glutSolidSphere 只是向 OpenGL 发送绘图命令.然而,它并没有什么特别之处.而且由于它与 GLUT 相关联,因此我不会使用它.相反,如果您的代码中确实需要一些领域,那么为自己创建 if 怎么样?

                glutSolidSphere is just sending drawing commands to OpenGL. However there's nothing special in and about it. And since it's tied to GLUT I'd not use it. Instead, if you really need some sphere in your code, how about create if for yourself?

                #define _USE_MATH_DEFINES
                #include <GL/gl.h>
                #include <GL/glu.h>
                #include <vector>
                #include <cmath>
                
                // your framework of choice here
                
                class SolidSphere
                {
                protected:
                    std::vector<GLfloat> vertices;
                    std::vector<GLfloat> normals;
                    std::vector<GLfloat> texcoords;
                    std::vector<GLushort> indices;
                
                public:
                    SolidSphere(float radius, unsigned int rings, unsigned int sectors)
                    {
                        float const R = 1./(float)(rings-1);
                        float const S = 1./(float)(sectors-1);
                        int r, s;
                
                        vertices.resize(rings * sectors * 3);
                        normals.resize(rings * sectors * 3);
                        texcoords.resize(rings * sectors * 2);
                        std::vector<GLfloat>::iterator v = vertices.begin();
                        std::vector<GLfloat>::iterator n = normals.begin();
                        std::vector<GLfloat>::iterator t = texcoords.begin();
                        for(r = 0; r < rings; r++) for(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 );
                
                                *t++ = s*S;
                                *t++ = r*R;
                
                                *v++ = x * radius;
                                *v++ = y * radius;
                                *v++ = z * radius;
                
                                *n++ = x;
                                *n++ = y;
                                *n++ = z;
                        }
                
                        indices.resize(rings * sectors * 4);
                        std::vector<GLushort>::iterator i = indices.begin();
                        for(r = 0; r < rings; r++) for(s = 0; s < sectors; s++) {
                                *i++ = r * sectors + s;
                                *i++ = r * sectors + (s+1);
                                *i++ = (r+1) * sectors + (s+1);
                                *i++ = (r+1) * sectors + s;
                        }
                    }
                
                    void draw(GLfloat x, GLfloat y, GLfloat z)
                    {
                        glMatrixMode(GL_MODELVIEW);
                        glPushMatrix();
                        glTranslatef(x,y,z);
                
                        glEnableClientState(GL_VERTEX_ARRAY);
                        glEnableClientState(GL_NORMAL_ARRAY);
                        glEnableClientState(GL_TEXTURE_COORD_ARRAY);
                
                        glVertexPointer(3, GL_FLOAT, 0, &vertices[0]);
                        glNormalPointer(GL_FLOAT, 0, &normals[0]);
                        glTexCoordPointer(2, GL_FLOAT, 0, &texcoords[0]);
                        glDrawElements(GL_QUADS, indices.size(), GL_UNSIGNED_SHORT, &indices[0]);
                        glPopMatrix();
                    }
                };
                
                SolidSphere sphere(1, 12, 24);
                
                void display()
                {
                    int const win_width  = …; // retrieve window dimensions from
                    int const win_height = …; // framework of choice here
                    float const win_aspect = (float)win_width / (float)win_height;
                
                    glViewport(0, 0, win_width, win_height);
                
                    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
                
                    glMatrixMode(GL_PROJECTION);
                    glLoadIdentity();
                    gluPerspective(45, win_aspect, 1, 10);
                
                    glMatrixMode(GL_MODELVIEW);
                    glLoadIdentity();
                
                #ifdef DRAW_WIREFRAME
                    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
                #endif
                    sphere.draw(0, 0, -5);
                
                    swapBuffers();
                }
                
                int main(int argc, char *argv[])
                {
                    // initialize and register your framework of choice here
                    return 0;
                }
                

                这篇关于使用 Visual C++ 在 Opengl 中创建 3D 球体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:法线贴图严重错误 下一篇:变换模型矩阵

                相关文章

                最新文章

                <tfoot id='DI0wU'></tfoot>
                <i id='DI0wU'><tr id='DI0wU'><dt id='DI0wU'><q id='DI0wU'><span id='DI0wU'><b id='DI0wU'><form id='DI0wU'><ins id='DI0wU'></ins><ul id='DI0wU'></ul><sub id='DI0wU'></sub></form><legend id='DI0wU'></legend><bdo id='DI0wU'><pre id='DI0wU'><center id='DI0wU'></center></pre></bdo></b><th id='DI0wU'></th></span></q></dt></tr></i><div id='DI0wU'><tfoot id='DI0wU'></tfoot><dl id='DI0wU'><fieldset id='DI0wU'></fieldset></dl></div>
                <legend id='DI0wU'><style id='DI0wU'><dir id='DI0wU'><q id='DI0wU'></q></dir></style></legend>

                1. <small id='DI0wU'></small><noframes id='DI0wU'>

                    <bdo id='DI0wU'></bdo><ul id='DI0wU'></ul>