OpenGL——二次曲面函數(球面-圓錐面-圓柱面)


 代碼:

 

#include<iostream>
#include <math.h>
#include<Windows.h>
#include <GL/glut.h>

using namespace std;

GLsizei winWidth = 500, winHeight = 500;

void init()
{
    glClearColor(1.0, 1.0, 1.0, 0.0);
}

void wireQuadSurfs()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(0.0, 0.0, 1.0);

    gluLookAt(2.0, 2.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);

    glPushMatrix();
    glTranslatef(1.0, 1.0, 1.0);
    //球面,半徑為0.5
    glutWireSphere(0.50, 8, 6);
    glPopMatrix();
    
    glPushMatrix();
    glTranslatef(1.0, -0.5, 0.5);
    //圓錐面,底面半徑為0.5,高位1.5
    glutWireCone(0.5, 1.5, 7, 6);
    glPopMatrix();

    GLUquadricObj *cylinder;
    glPushMatrix();
    glTranslatef(0.0, 1.5, 0.8);
    //激活二次曲面繪制器
    cylinder = gluNewQuadric();
    //每對頂點之間用線連接,按線框圖形式顯示
    gluQuadricDrawStyle(cylinder, GLU_LINE);
    //生成圓柱面
    gluCylinder(cylinder, 0.5, 0.5, 1.5, 6, 4);
    glPopMatrix();

    glFlush();
}



void winReshapeFcn(GLint newWidth, GLint newHeight)
{
    glViewport(0, 0, (GLsizei)newWidth, (GLsizei)newHeight);

    glMatrixMode(GL_PROJECTION);
    glOrtho(-2.0, 2.0, -2.0, 2.0, 0.0, 5.0);

    glMatrixMode(GL_MODELVIEW);

    glClear(GL_COLOR_BUFFER_BIT);
}


int main(int argc, char* argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowPosition(100, 100);

    glutInitWindowSize(winWidth, winHeight);
    glutCreateWindow("球面-圓錐面-圓柱面");
    init();
    glutDisplayFunc(wireQuadSurfs);
    glutReshapeFunc(winReshapeFcn);
    glutMainLoop();

    system("pause");
    return 0;
}

 

運行結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM