三个不同材质球体

这篇博客通过OpenGL展示了如何创建并展示三个具有不同材质和光照效果的球体。使用了glutSolidSphere函数绘制球体,并通过glMaterialfv设置材质参数,包括漫射、镜面反射和光泽度。此外,还设置了光源位置并启用光照和深度测试。
摘要由CSDN通过智能技术生成

#include <gl/glut.h>
#include <windows.h>

GLfloat light_position[]={0.0,3.0,6.0,0.0};
GLfloat material_diffuse[]={0.8,0.2,0.5,1.0};
GLfloat material_specular[]={1.0,1.0,1.0,1.0};

GLfloat no_shininess[]={0.0};
GLfloat low_shininess[]={5.0};
GLfloat high_shininess[]={100.0};

GLfloat no_mat[]={0.0,0.0,0.0,1.0};
GLfloat mat_grey_ambient[]={0.5,0.5,0.5,1.0};
GLfloat mat_red_ambient[]={0.0,0.0,1.0,1.0};

void Initial(void)
{
    glLightfv(GL_LIGHT2,GL_POSITION,light_position);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glShadeModel(GL_SMOOTH);
}

void myDisplay(void)
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();

    glMaterialfv(GL_FRONT,GL_DIFFUSE,material_diffuse);
    glMaterialfv(GL_FRONT,GL_SPECULAR,material_specular);
    glMaterialfv(GL_FRONT,GL_SHININESS,high_shininess);
    glMaterialfv(GL_FRONT,GL_EMISSION,no_mat);

    glColorMaterial(GL_FRONT,GL_AMBIENT);
    glEnable(GL_COLOR_MATERIAL);

    glPushMatrix();
    glColor3fv(no_mat);//glColor3f(1.0,0.0,0.0);等价于
                //float color_array[]={1.0,0.0,0.0};
                //glColor3fv(color_array);
    glTranslatef(-2.5,1.5,0.0);
    glRotatef(15.0,1.0,0.0,0.0);//void glRotatef(float angle,float x,float y,float z)分别代表 角度 X ,Y Z的旋转
    glutSolidSphere(1.2,20.0,20.0);
    glPopMatrix();

    glPushMatrix();
    glColor3fv(mat_grey_ambient);
    glRotatef(15.0,1.0,0.0,0.0);
    glutSolidSphere(1.2,20.0,20.0);
    glPopMatrix();

    glPushMatrix();
    glColor3fv(mat_red_ambient);
    glTranslatef(2.5,-1.5,0.0);
    glRotatef(15.0,1.0,0.0,0.0);
    glutSolidSphere(1.2,20.0,20.0);
    glPopMatrix();

    glPopMatrix();

    glFlush();
}

void myReshape(GLsizei w,GLsizei h)
{
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    glOrtho(-5.5,5.5,-5.5,5.5,-5.5,5.5);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

int main(int argc,char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowSize(400,400);
    glutInitWindowPosition(100,100);
    glutCreateWindow("LIGHT&Material");

    Initial();
    glutReshapeFunc(myReshape);
    glutDisplayFunc(myDisplay);
   
    glutMainLoop();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值