首页 > 其他分享 >Opengl___真实感技术1:光照

Opengl___真实感技术1:光照

时间:2022-12-01 21:56:13浏览次数:61  
标签:1.0 Opengl 0.0 GLfloat 真实感 glMaterialfv FRONT ___ GL

光照

设置glLightfv(灯光,属性,属性值);

例如:
1.glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
2.glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
3.glLightfv(GL_LIGHT0, GL_POSITION, position);

设置物体材质glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);

// 提示:在合适的地方修改或添加代码
#include <GL/glut.h>
#include<stdlib.h>
// 评测代码所用头文件-开始

using namespace std;
// 评测代码所用头文件-结束

void init(void)
{
    GLfloat position[] = { 0.0,3.0,2.0,0.0 };
    GLfloat ambient[] = { 0.0,0.0,0.0,1.0 };
    GLfloat diffuse[] = { 1.0,1.0,1.0,1.0 };
    GLfloat specular[] = { 1.0,1.0,1.0,1.0 };
    GLfloat lmodel_ambient[] = { 0.4,0.4,0.4,1.0 };
    GLfloat local_view[] = { 0.0 };

    glClearColor(0.0, 0.1, 0.1, 0.0);
    glShadeModel(GL_SMOOTH);

    glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
    glLightfv(GL_LIGHT0, GL_POSITION, position);
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
    glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view);

    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_DEPTH_TEST);

}

void display(void)
{
    GLfloat no_mat[] = { 0.0,0.0,0.0,1.0 };                     //没有光
    GLfloat mat_ambient[] = { 0.7,0.7,0.7,1.0 };                //环境光
    GLfloat mat_ambient_color[] = { 0.8, 0.8, 0.2, 1.0 };       //彩色环境光 
    GLfloat mat_diffuse[] = { 0.1,0.5,0.8,1.0 };                //漫反射
    GLfloat mat_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 mat_emission[] = { 0.3,0.2,0.2,0.0 };               //材料辐射光颜色 

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();
    glTranslatef(-3.0, 0.0, 0.0);
    // 请在此添加你的代码,左侧圆球只有漫反射
    /********** Begin ********/
    glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
    glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
    /********** End *********/
    glutSolidSphere(1.0, 50, 50);
    glPopMatrix();

    glPushMatrix();
    // 请在此添加你的代码,中间圆球有环境光和漫反射
    /********** Begin ********/

    glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);

    /********** End *********/
    glutSolidSphere(1.0, 50, 50);
    glPopMatrix();

    glPushMatrix();
    glTranslatef(3.0, 0.0, 0.0);
    // 请在此添加你的代码,右侧圆球有彩色环境光和漫反射
    /********** Begin ********/

    glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient_color);
    glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);

    /********** End *********/
    glutSolidSphere(1.0, 50, 50);
    glPopMatrix();

    glFlush();
    glutPostRedisplay();
}

void reshape(int w, int h)
{
    glViewport(0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60.0, (GLfloat)w / (GLfloat)h, 1.0, 20.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0.0, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}





int main(int argc, char* argv[])
{
    GLubyte* pPixelData = (GLubyte*)malloc(800 * 600 * 3);//分配内存
    GLint viewport[4] = { 0 };
    glutInit(&argc, argv);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(800, 600);
    glutCreateWindow("几何变换示例");
    init();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();
    return 0;
}


镜面反射

// 提示:在合适的地方修改或添加代码
#include <GL/glut.h>
#include<stdlib.h>

using namespace std;
// 评测代码所用头文件-结束

void init(void)
{
    GLfloat position[] = { 0.0,3.0,2.0,0.0 };
    GLfloat ambient[] = { 0.0,0.0,0.0,1.0 };
    GLfloat diffuse[] = { 1.0,1.0,1.0,1.0 };
    GLfloat specular[] = { 1.0,1.0,1.0,1.0 };
    GLfloat lmodel_ambient[] = { 0.4,0.4,0.4,1.0 };
    GLfloat local_view[] = { 0.0 };

    glClearColor(0.0, 0.1, 0.1, 0.0);
    glShadeModel(GL_SMOOTH);

    glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
    glLightfv(GL_LIGHT0, GL_POSITION, position);
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
    glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view);

    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_DEPTH_TEST);

}
void display(void)
{
    GLfloat no_mat[] = { 0.0,0.0,0.0,1.0 };                     //没有光
    GLfloat mat_ambient[] = { 0.7,0.7,0.7,1.0 };                //环境光
    GLfloat mat_ambient_color[] = { 0.8, 0.8, 0.2, 1.0 };       //彩色环境光 
    GLfloat mat_diffuse[] = { 0.1,0.5,0.8,1.0 };                //漫反射
    GLfloat mat_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 mat_emission[] = { 0.3,0.2,0.2,0.0 };               //材料辐射光颜色 

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glClearColor(0.0, 0.1, 0.1, 0.0);
    glPushMatrix();
    glTranslatef(-3.0, 0.0, 0.0);
    // 请在此添加你的代码,左侧圆球有环境光、漫反射镜面低反射
    /********** Begin ********/
    glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, low_shininess);
    /********** End *********/
    glutSolidSphere(1.0, 50, 50);
    glPopMatrix();

    glPushMatrix();
    // 请在此添加你的代码,中间圆球有环境光、漫反射镜面高反射
    /********** Begin ********/

    glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
    /********** End *********/
    glutSolidSphere(1.0, 50, 50);
    glPopMatrix();

    glPushMatrix();
    glTranslatef(3.0, 0.0, 0.0);
    // 请在此添加你的代码,右侧圆球有彩色环境光、漫反射、镜面高反射和材料辐射
    /********** Begin ********/

    glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient_color);
    glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
    glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);

    /********** End *********/
    glutSolidSphere(1.0, 50, 50);
    glPopMatrix();


 
    glFlush();
    //glutPostRedisplay();
}

void reshape(int w, int h)
{
    glViewport(0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60.0, (GLfloat)w / (GLfloat)h, 1.0, 20.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0.0, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}





int main(int argc, char* argv[])
{
    GLubyte* pPixelData = (GLubyte*)malloc(800 * 600 * 3);//分配内存
    GLint viewport[4] = { 0 };
    glutInit(&argc, argv);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(800, 600);
    glutCreateWindow("几何变换示例");
    init();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();

   
    return 0;
}

茶壶光照

// 提示:在合适的地方修改或添加代码
#include <GL/glut.h>
#include<stdlib.h>

using namespace std;
// 评测代码所用头文件-结束

//定义左边茶壶材质系数 
GLfloat left_ambient[] = { 5.0f, 0.0f, 0.0f, 1.0f };
GLfloat left_diffuse[] = { 0.8f, 0.8f, 0.0f, 1.0f };
GLfloat left_specular[] = { 1.0f, 1.0f, 0.0f, 1.0f };
GLfloat left_emission[] = { 0.1f, 0.0f, 0.0f, 1.0f };

//定义中间茶壶材质系数  
GLfloat mid_ambient[] = { 0.0f, 0.2f, 0.0f, 1.0f };
GLfloat mid_diffuse[] = { 0.0f, 0.8f, 0.0f, 1.0f };
GLfloat mid_specular[] = { 0.0f, 1.0f, 0.0f, 1.0f };
GLfloat mid_shininess[] = { 80.0f };
GLfloat mid_emission[] = { 0.0f, 0.1f, 0.0f, 1.0f };

//定义右边茶壶材质系数
GLfloat right_ambient[] = { 0.0f, 0.0f, 0.1f, 1.0f };
GLfloat right_diffuse[] = { 0.0f, 0.0f, 0.8f, 1.0f };
GLfloat right_specular[] = { 0.0f, 0.0f, 0.9f, 1.0f };
GLfloat right_shininess[] = { 50.0f };
GLfloat right_emission[] = { 0.0f, 0.1f, 0.0f, 1.0f };

void myInit(void)
{

    GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
    GLfloat white_light[] = { 1.0, 1.0, 1.0, 1.0 };
    GLfloat light_ambient[] = { 0.2 , 0.2 , 0.2 , 1.0 };


    glClearColor(0.0, 0.0, 0.0, 0.0);
    glShadeModel(GL_SMOOTH);

    glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light);
    glLightfv(GL_LIGHT0, GL_SPECULAR, white_light);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_DEPTH_TEST);

}

void myDisplay(void)
{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();
    // 请在此添加你的代码,中间茶壶
    /********** Begin ********/
    glMaterialfv(GL_FRONT, GL_AMBIENT, mid_ambient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE, mid_diffuse);
    //glMaterialfv(GL_FRONT, GL_SPECULAR, mid_specular);
    //glMaterialfv(GL_FRONT, GL_SHININESS, mid_shininess);
    glMaterialfv(GL_FRONT, GL_EMISSION, mid_emission);

    /********** End *********/
    glutSolidTeapot(0.5);
    glPopMatrix();
    glFlush();


    glPushMatrix();
    glTranslatef(2.0, 0.0, 0.0);
    // 请在此添加你的代码,右边茶壶
    /********** Begin ********/

    glMaterialfv(GL_FRONT, GL_AMBIENT, right_ambient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE, right_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR, right_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, right_shininess);
    glMaterialfv(GL_FRONT, GL_EMISSION, right_emission);

    /********** End *********/
    glutSolidTeapot(0.5);
    glPopMatrix();
    glFlush();


    glPushMatrix();
    glDisable(GL_LIGHT0);
    glTranslatef(-2.0, 0.0, 0.0);
    // 请在此添加你的代码,左边茶壶
    /********** Begin ********/


    glMaterialfv(GL_FRONT, GL_AMBIENT, left_ambient);
    //glMaterialfv(GL_FRONT, GL_DIFFUSE, left_diffuse);
    //glMaterialfv(GL_FRONT, GL_SPECULAR, left_specular);
    glMaterialfv(GL_FRONT, GL_EMISSION, left_emission);
    /********** End *********/
    glutSolidTeapot(0.5);
    glPopMatrix();
    glFlush();


}



void myReshape(int w, int h)
{
    glViewport(0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    if (w <= h)
        glOrtho(-1.5, 1.5, -1.5 * (GLfloat)h / (GLfloat)w,
            1.5 * (GLfloat)h / (GLfloat)w, -10.0, 10.0);
    else
        glOrtho(-1.5 * (GLfloat)w / (GLfloat)h,
            1.5 * (GLfloat)w / (GLfloat)h, -1.5, 1.5, -10.0, 10.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

int main(int argc, char* argv[])
{

    glutInit(&argc, argv);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(1000, 400);
    glutCreateWindow("几何变换示例");
    myInit();
    glutDisplayFunc(myDisplay);
    glutReshapeFunc(myReshape);
    glutMainLoop();

   
    return 0;
}


QUOTE

光照属性
Educoder计算机图形学实训

标签:1.0,Opengl,0.0,GLfloat,真实感,glMaterialfv,FRONT,___,GL
From: https://www.cnblogs.com/lxp-blog/p/16942908.html

相关文章

  • sqlserver共享锁与排它锁
    ​ 共享锁:(读取)操作创建的锁。其他用户可以并发读取数据,但任何事物都不能获取数据上的排它锁,直到已释放所有共享锁。共享锁(S锁)又称为读锁,若事务T对数据对象A加上S锁,......
  • Java 中经常被提到的 SPI 到底是什么?
    Java程序员在日常工作中经常会听到SPI,而且很多框架都使用了SPI的技术,那么问题来了,到底什么是SPI呢?今天阿粉就带大家好好了解一下SPI。SPI概念SPI全称是Service......
  • 实验六
    任务三: 任务四:#include<iostream>#include"Vector.hpp"voidtest(){usingnamespacestd;intn;cin>>n;Vector<double>x1(n);fo......
  • 前端开发:2、CSS、表单标签补充说明
    前端开发之CSS目录前端开发之CSS一、表单标签补充说明二、CSS层叠样式表三、CSS选择器基本选择器CSS组合选择器分组嵌套属性选择器伪装选择器伪元素选择器四、选择器优......
  • 题解 CF1703G Good Key, Bad Key
    先放个代码。intn,k;cin>>n>>k;vector<vector<int>>a(n+5,vector<int>(35));for(inti=1;i<=n;i++){cin>>a[i][0];for(intj=1;j......
  • ObjectDataProvider 利用链
    前置介绍ObjectDataProvider命名空间:System.Windows.Data程序集:PresentationFramework.dll包装和创建可以用作绑定源的对象。ObjectDataProvider,顾名思义就是把对......
  • Pinely Round 1 (Div. 1 + Div. 2)(持续更新)
    Preface其实这场上周一就补了ABC,但是由于各种事情的堆积一直到今天才开始接着补D再不写博客的话可能题意都要忘光光了,赶紧来Rush一发A.TwoPermutations简单观察发现......
  • 二叉树中序遍历非递归算法实现 cpp
    二叉树中序遍历非递归算法实现#include<iostream>//二叉树中序遍历非递归算法实现usingnamespacestd;#defineMAXSIZE100/*不让用递归,那就用栈!!*///树的结点......
  • 反悔贪心乱做
    解决历史遗留问题.jpg[APIO/CTSC2007]数据备份显然,选的一定是相邻的两个城市。所以问题转化成,给一个差分数组,选\(k\)个不相邻的数,使得和最小。这就是经典的种树的问......
  • vue局部刷新 (组件重载)
     组件重载的运用是当组件的数据产生了变化之时,需要通过重载来实现组件页面的刷新,而不是重新刷新URL进行重新请求,也可以理解为是局部刷新 步骤:在父级页面通过......