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

我无法使用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(); 

}


尚方宝剑之说
浏览 706回答 3
3回答

凤凰求蛊

我不知道datenwolf的索引生成如何正确。但是我仍然发现他的解决方案相当明确。这是我经过一番思考后得到的:inline void push_indices(vector<GLushort>& indices, int sectors, int r, int s) {&nbsp; &nbsp; int curRow = r * sectors;&nbsp; &nbsp; int nextRow = (r+1) * sectors;&nbsp; &nbsp; indices.push_back(curRow + s);&nbsp; &nbsp; indices.push_back(nextRow + s);&nbsp; &nbsp; indices.push_back(nextRow + (s+1));&nbsp; &nbsp; indices.push_back(curRow + s);&nbsp; &nbsp; indices.push_back(nextRow + (s+1));&nbsp; &nbsp; indices.push_back(curRow + (s+1));}void createSphere(vector<vec3>& vertices, vector<GLushort>& indices, vector<vec2>& texcoords,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;float radius, unsigned int rings, unsigned int sectors){&nbsp; &nbsp; float const R = 1./(float)(rings-1);&nbsp; &nbsp; float const S = 1./(float)(sectors-1);&nbsp; &nbsp; for(int r = 0; r < rings; ++r) {&nbsp; &nbsp; &nbsp; &nbsp; for(int s = 0; s < sectors; ++s) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; float const y = sin( -M_PI_2 + M_PI * r * R );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; float const x = cos(2*M_PI * s * S) * sin( M_PI * r * R );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; float const z = sin(2*M_PI * s * S) * sin( M_PI * r * R );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; texcoords.push_back(vec2(s*S, r*R));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vertices.push_back(vec3(x,y,z) * radius);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; push_indices(indices, sectors, r, s);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP