
【OpenGL】ADS光照模型
发布日期:2021-05-10 02:20:58
浏览次数:20
分类:精选文章
本文共 6217 字,大约阅读时间需要 20 分钟。
ADS是Ambient、Diffuse、Specular意思,用环境光、漫反射、高光反射进行渲染物体。
uniform vec3 vAmbientMaterial;
uniform vec3 vAmbientLight; vec3 vAmbientColor = vAmbientMaterial * vAmbientLight; //即环境光颜色 * 环境光亮度 得到环境光颜色uniform vec3 vDiffuseMaterial;
uniform vec3 vDiffuseLight; float fDotProduct = max(0.0, dot(vNormal, vLightDir); vec3 vDiffuseColor = vDiffuseMaterial * vDiffuseLight * fDotProduct;uniform vec3 vSpecularMaterial;
uniform vec3 vSpecularLigth; float shininess = 128.0; vec3 vReflection = reflect(-vLightDir, vEyeNormal); float EyeReflectionAngle = max(0.0, dot(vEyeNormal, vReflection); fSpec = pow(EyeReflectionAngle * shininess); vec3 vSpecularColor = vSpecularLight * vSpecularMaterial * fSpec;以上三段代码分别是环境光、漫反射、高光反射内容。具体代码如下:
// ADS Point lighting Shader// Vertex Shader// Richard S. Wright Jr.// OpenGL SuperBible#version 130// Incoming per vertex... position and normalin vec4 vVertex;in vec3 vNormal;// Set per batchuniform vec4 ambientColor;uniform vec4 diffuseColor; uniform vec4 specularColor;uniform vec3 vLightPosition;uniform mat4 mvpMatrix;uniform mat4 mvMatrix;uniform mat3 normalMatrix;// Color to fragment programsmooth out vec4 vVaryingColor;void main(void) { // Get surface normal in eye coordinates vec3 vEyeNormal = normalMatrix * vNormal; // Get vertex position in eye coordinates vec4 vPosition4 = mvMatrix * vVertex; vec3 vPosition3 = vPosition4.xyz / vPosition4.w; // Get vector to light source vec3 vLightDir = normalize(vLightPosition - vPosition3); // Dot product gives us diffuse intensity float diff = max(0.0, dot(vEyeNormal, vLightDir)); // Multiply intensity by diffuse color, force alpha to 1.0 vVaryingColor = diff * diffuseColor; // Add in ambient light vVaryingColor += ambientColor; // Specular Light vec3 vReflection = normalize(reflect(-vLightDir, vEyeNormal)); float spec = max(0.0, dot(vEyeNormal, vReflection)); if(diff != 0) { float fSpec = pow(spec, 128.0); vVaryingColor.rgb += vec3(fSpec, fSpec, fSpec); } // Don't forget to transform the geometry! gl_Position = mvpMatrix * vVertex; }
// ADS Point lighting Shader// Fragment Shader// Richard S. Wright Jr.// OpenGL SuperBible#version 130out vec4 vFragColor;smooth in vec4 vVaryingColor;void main(void) { vFragColor = vVaryingColor; }
// DiffuseLight.cpp// OpenGL SuperBible// Demonstrates simple diffuse lighting// Program by Richard S. Wright Jr.#pragma comment(lib,"gltools.lib")#include// OpenGL toolkit#include #include #include #include #include #include #ifdef __APPLE__#include #else#define FREEGLUT_STATIC#include #endifGLFrame viewFrame;GLFrustum viewFrustum;GLTriangleBatch sphereBatch;GLMatrixStack modelViewMatrix;GLMatrixStack projectionMatrix;GLGeometryTransform transformPipeline;GLShaderManager shaderManager;GLuint ADSLightShader; // The diffuse light shaderGLint locAmbient; // The location of the ambient colorGLint locDiffuse; // The location of the diffuse colorGLint locSpecular; // The location of the specular colorGLint locLight; // The location of the Light in eye coordinatesGLint locMVP; // The location of the ModelViewProjection matrix uniformGLint locMV; // The location of the ModelView matrix uniformGLint locNM; // The location of the Normal matrix uniform// This function does any needed initialization on the rendering// context. void SetupRC(void){ // Background glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); shaderManager.InitializeStockShaders(); viewFrame.MoveForward(4.0f); // Make the sphere gltMakeSphere(sphereBatch, 1.0f, 26, 13); ADSLightShader = shaderManager.LoadShaderPairWithAttributes("ADSGouraud.vp", "ADSGouraud.fp", 2, GLT_ATTRIBUTE_VERTEX, "vVertex", GLT_ATTRIBUTE_NORMAL, "vNormal"); locAmbient = glGetUniformLocation(ADSLightShader, "ambientColor"); locDiffuse = glGetUniformLocation(ADSLightShader, "diffuseColor"); locSpecular = glGetUniformLocation(ADSLightShader, "specularColor"); locLight = glGetUniformLocation(ADSLightShader, "vLightPosition"); locMVP = glGetUniformLocation(ADSLightShader, "mvpMatrix"); locMV = glGetUniformLocation(ADSLightShader, "mvMatrix"); locNM = glGetUniformLocation(ADSLightShader, "normalMatrix");}// Cleanupvoid ShutdownRC(void){}// Called to draw scenevoid RenderScene(void){ static CStopWatch rotTimer; // Clear the window and the depth buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); modelViewMatrix.PushMatrix(viewFrame); modelViewMatrix.Rotate(rotTimer.GetElapsedSeconds() * 10.0f, 0.0f, 1.0f, 0.0f); GLfloat vEyeLight[] = { -100.0f, 100.0f, 100.0f }; GLfloat vAmbientColor[] = { 0.1f, 0.1f, 0.1f, 1.0f }; GLfloat vDiffuseColor[] = { 0.0f, 0.0f, 1.0f, 1.0f }; GLfloat vSpecularColor[] = { 1.0f, 1.0f, 1.0f, 1.0f }; glUseProgram(ADSLightShader); glUniform4fv(locAmbient, 1, vAmbientColor); glUniform4fv(locDiffuse, 1, vDiffuseColor); glUniform4fv(locSpecular, 1, vSpecularColor); glUniform3fv(locLight, 1, vEyeLight); glUniformMatrix4fv(locMVP, 1, GL_FALSE, transformPipeline.GetModelViewProjectionMatrix()); glUniformMatrix4fv(locMV, 1, GL_FALSE, transformPipeline.GetModelViewMatrix()); glUniformMatrix3fv(locNM, 1, GL_FALSE, transformPipeline.GetNormalMatrix()); sphereBatch.Draw(); modelViewMatrix.PopMatrix(); glutSwapBuffers(); glutPostRedisplay();}void ChangeSize(int w, int h){ // Prevent a divide by zero if (h == 0) h = 1; // Set Viewport to window dimensions glViewport(0, 0, w, h); viewFrustum.SetPerspective(35.0f, float(w) / float(h), 1.0f, 100.0f); projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix()); transformPipeline.SetMatrixStacks(modelViewMatrix, projectionMatrix);}///// Main entry point for GLUT based programsint main(int argc, char* argv[]){ gltSetWorkingDirectory(argv[0]); glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL); glutInitWindowSize(800, 600); glutCreateWindow("ADS Lighting, Gouraud Shading"); glutReshapeFunc(ChangeSize); glutDisplayFunc(RenderScene); GLenum err = glewInit(); if (GLEW_OK != err) { fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err)); return 1; } SetupRC(); glutMainLoop(); ShutdownRC(); return 0;}
发表评论
最新留言
逛到本站,mark一下
[***.202.152.39]2025年04月11日 23时39分35秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
python 文件操作 open()与with open() as的区别(打开文件)
2021-05-10
python中列表 元组 字典 集合的区别
2021-05-10
python struct 官方文档
2021-05-10
Docker镜像加速
2021-05-10
静态数组类的封装(泛型)
2021-05-10
操作记录-2021-03-15: sunxiaoyu_project
2021-05-10
Android DEX加固方案与原理
2021-05-10
Android Retrofit2.0 上传单张图片和多张图片
2021-05-10
vue 导出Excel乱码问题解决方案
2021-05-10
eggjs validate no function 解决方案
2021-05-10
Permission denied 解决方案
2021-05-10
iOS_Runtime3_动态添加方法
2021-05-10
Docker配置文件
2021-05-10
PNFT邮票数字资产化,科技、美学与价值的完美融合
2021-05-10
Leetcode第557题---翻转字符串中的单词
2021-05-10
IDEA:修改编辑区背景色
2021-05-10