位置: 编程技术 - 正文
推荐整理分享OpenGL中光照的设置(一)(opengl实现光照效果),希望有所帮助,仅作参考,欢迎阅读内容。
文章相关热门搜索词:opengl insight,opengl环境光,opengl2.1光影,opengl光照设置,opengl 灯光,opengl 点光源,opengl环境光,opengl环境光,内容如对您有帮助,希望把文章链接给更多的朋友!
OpenGL这个系列的blog是我这个小白学习的笔记。
大部分内容是摘录的,少部分内容是自己的理解。
这篇文章转载自 ambient[] = { 0.0, 0.0, 0.0, 1.0 };GLfloat diffuse[] = { 1.0, 1.0, 1.0, 1.0 };GLfloat position[] = { 0.0, 3.0, 2.0, 0.0 };
(2)创建光源 glLightfv(GL_LIGHT1, GL_AMBIENT, ambient); // Setup The Ambient Light设置环境光 glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse); // Setup The Diffuse Light设置漫射光 glLightfv(GL_LIGHT1, GL_POSITION,position); // Position The Light设置光源位置
(3)启动光照和光源 glEnable(GL_LIGHTING); // 启动光照 glEnable(GL_LIGHT1); // Enable Light One启用一号光源
2.选择光照模型GLfloat lmodel_ambient[] = { 0.4, 0.4, 0.4, 1.0 };GLfloat local_view[] = { 0.0 }; glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient); glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view);
3.定义材质(1)材料属性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[] = { .0 };GLfloat mat_emission[] = {0.3, 0.2, 0.2, 0.0};
(2)指定材料属性glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
4.绘制物体glutSolidSphere(1.0, , );
小结:
即使用 glLightfv 设置光(环境光、散射光等等)的颜色,使用 glLightModelfv设置关照的模式,使用glMaterialfv设置物体材料的颜色。
引用OpenGL编程指南中的例子:
[cpp] view plaincopyvoid init(void) { GLfloat ambient[] = { 0.0, 0.0, 0.0, 1.0 }; GLfloat diffuse[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat position[] = { 0.0, 3.0, 2.0, 0.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); glEnable(GL_DEPTH_TEST); 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); } 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[] = { .0 }; GLfloat mat_emission[] = {0.3, 0.2, 0.2, 0.0}; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); /* draw sphere in first row, first column * diffuse reflection only; no ambient or specular */ glPushMatrix(); glTranslatef (-3., 3.0, 0.0); glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat); glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess); glMaterialfv(GL_FRONT, GL_EMISSION, no_mat); glutSolidSphere(1.0, , ); glPopMatrix(); /* draw sphere in first row, second column * diffuse and specular reflection; low shininess; no ambient */ glPushMatrix(); glTranslatef (-1., 3.0, 0.0); glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT, GL_SHININESS, low_shininess); glMaterialfv(GL_FRONT, GL_EMISSION, no_mat); glutSolidSphere(1.0, , ); glPopMatrix(); /* draw sphere in first row, third column * diffuse and specular reflection; high shininess; no ambient */ glPushMatrix(); glTranslatef (1., 3.0, 0.0); glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat); 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, no_mat); glutSolidSphere(1.0, , ); glPopMatrix(); /* draw sphere in first row, fourth column * diffuse reflection; emission; no ambient or specular reflection */ glPushMatrix(); glTranslatef (3., 3.0, 0.0); glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat); glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess); glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission); glutSolidSphere(1.0, , ); glPopMatrix(); /* draw sphere in second row, first column * ambient and diffuse reflection; no specular */ glPushMatrix(); glTranslatef (-3., 0.0, 0.0); glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat); glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess); glMaterialfv(GL_FRONT, GL_EMISSION, no_mat); glutSolidSphere(1.0, , ); glPopMatrix(); /* draw sphere in second row, second column * ambient, diffuse and specular reflection; low shininess */ glPushMatrix(); glTranslatef (-1., 0.0, 0.0); 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); glMaterialfv(GL_FRONT, GL_EMISSION, no_mat); glutSolidSphere(1.0, , ); glPopMatrix(); /* draw sphere in second row, third column * ambient, diffuse and specular reflection; high shininess */ glPushMatrix(); glTranslatef (1., 0.0, 0.0); 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); glMaterialfv(GL_FRONT, GL_EMISSION, no_mat); glutSolidSphere(1.0, , ); glPopMatrix(); /* draw sphere in second row, fourth column * ambient and diffuse reflection; emission; no specular */ glPushMatrix(); glTranslatef (3., 0.0, 0.0); glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat); glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess); glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission); glutSolidSphere(1.0, , ); glPopMatrix(); /* draw sphere in third row, first column * colored ambient and diffuse reflection; no specular */ glPushMatrix(); glTranslatef (-3., -3.0, 0.0); glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient_color); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat); glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess); glMaterialfv(GL_FRONT, GL_EMISSION, no_mat); glutSolidSphere(1.0, , ); glPopMatrix(); /* draw sphere in third row, second column * colored ambient, diffuse and specular reflection; low shininess */ glPushMatrix(); glTranslatef (-1., -3.0, 0.0); 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, low_shininess); glMaterialfv(GL_FRONT, GL_EMISSION, no_mat); glutSolidSphere(1.0, , ); glPopMatrix(); /* draw sphere in third row, third column * colored ambient, diffuse and specular reflection; high shininess */ glPushMatrix(); glTranslatef (1., -3.0, 0.0); 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, no_mat); glutSolidSphere(1.0, , ); glPopMatrix(); /* draw sphere in third row, fourth column * colored ambient and diffuse reflection; emission; no specular */ glPushMatrix(); glTranslatef (3., -3.0, 0.0); glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient_color); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat); glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess); glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission); glutSolidSphere(1.0, , ); glPopMatrix(); glFlush(); } void reshape(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (w <= (h * 2)) glOrtho (-6.0, 6.0, -3.0*((GLfloat)h*2)/(GLfloat)w, 3.0*((GLfloat)h*2)/(GLfloat)w, -.0, .0); else glOrtho (-6.0*(GLfloat)w/((GLfloat)h*2), 6.0*(GLfloat)w/((GLfloat)h*2), -3.0, 3.0, -.0, .0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize (, ); glutCreateWindow(argv[0]); init(); glutReshapeFunc(reshape); glutDisplayFunc(display); glutKeyboardFunc (keyboard); glutMainLoop(); return 0; }OpenGL入门 & VS 配置使用 OpenGL gult库 GLUT(英文全写:OpenGLUtilityToolkit)是一个处理OpenGL程式的工具库,负责处理和底层操作系统的呼叫以及I/O,并包括了以下常见的功能:定义以及控制视
OpenGL中光照的设置(二) OpenGL这个系列的blog是我这个小白学习的笔记。大部分内容是摘录的,少部分内容是自己的理解。这篇文章转载自
OpenGL--粒子系统 理论基础1,粒子系统的基本思想是用许多形状简单且赋予生命的微小粒子作为基本元素来表示物体(一般由点或很小的多边形通过纹理贴图表示),侧重
标签: opengl实现光照效果
本文链接地址:https://www.jiuchutong.com/biancheng/369480.html 转载请保留说明!友情链接: 武汉网站建设