未有方向光的截图如下:

给模型加方向光后,截图如下:

关键源码如下:

light.h

#pragma once
#include "ggl.h"
class Light {
protected:GLenum mLightIdentifier;Light();
public:void SetAmbientColor(float r, float g, float b, float a);    //设置环境光void SetDiffuseColor(float r, float g, float b, float a);    //设置漫反射void SetSpecularColor(float r, float g, float b, float a);   //设置镜面反射void Enable();
};
class DirectionLight :public Light {
public:DirectionLight(GLenum light);void SetPosition(float x, float y, float z);
};

light.cpp

#include "light.h"
Light::Light() {}
void Light::SetAmbientColor(float r, float g, float b, float a) {float ambientColor[] = { r,g,b,a };glLightfv(mLightIdentifier, GL_AMBIENT, ambientColor);
}
void Light::SetDiffuseColor(float r, float g, float b, float a) {float diffuseColor[] = { r,g,b,a };glLightfv(mLightIdentifier, GL_DIFFUSE, diffuseColor);
}
void Light::SetSpecularColor(float r, float g, float b, float a) {float specularColor[] = { r,g,b,a };glLightfv(mLightIdentifier, GL_SPECULAR, specularColor);
}
void Light::Enable() {glEnable(GL_LIGHTING);glEnable(mLightIdentifier);
}
DirectionLight::DirectionLight(GLenum light) {mLightIdentifier = light;
}
void DirectionLight::SetPosition(float x, float y, float z) {float pos[] = { x,y,z,0.0f };glLightfv(mLightIdentifier, GL_POSITION, pos);
}

model.h

#pragma once#include "ggl.h"
struct VertexData {float position[3];float normal[3];float texcoord[2];
};
class Model {VertexData*mVertexes;unsigned short *mIndexes;int mIndexCount;//环境光材质,镜面光材质,漫反射的材质float mAmbientMaterial[4], mDiffuseMaterial[4], mSpecularMaterial[4];
public:GLuint mTexture;Model();void Init(const char*modelPath);void Draw();void SetAmbientMaterial(float r, float g, float b, float a);void SetDiffuseMaterial(float r, float g, float b, float a);void SetSpecularMaterial(float r, float g, float b, float a);
};

model.cpp

#include "model.h"
#include "utils.h"
Model::Model() {memset(mAmbientMaterial, 0, sizeof(mAmbientMaterial));memset(mDiffuseMaterial, 0, sizeof(mDiffuseMaterial));memset(mSpecularMaterial, 0, sizeof(mSpecularMaterial));
}void Model::Init(const char*modelPath) {struct FloatData {float v[3];};struct VertexDefine {int posIndex;int texcoordIndex;int normalIndex;};int nFileSize = 0;unsigned char*fileContent = LoadFileContent(modelPath, nFileSize);if (fileContent == nullptr) {return;}std::vector<FloatData> positions, texcoords, normals;std::vector<VertexDefine> vertexes;std::vector<int> indexes;std::stringstream ssFileContent((char*)fileContent);std::string temp;char szOneLine[256];while (!ssFileContent.eof()) {memset(szOneLine, 0, 256);ssFileContent.getline(szOneLine, 256);if (strlen(szOneLine) > 0) {if (szOneLine[0] == 'v') {std::stringstream ssOneLine(szOneLine);if (szOneLine[1] == 't') {ssOneLine >> temp;FloatData floatData;ssOneLine >> floatData.v[0];ssOneLine >> floatData.v[1];texcoords.push_back(floatData);printf("texcoord : %f,%f\n", floatData.v[0], floatData.v[1]);}else if (szOneLine[1] == 'n') {ssOneLine >> temp;FloatData floatData;ssOneLine >> floatData.v[0];ssOneLine >> floatData.v[1];ssOneLine >> floatData.v[2];normals.push_back(floatData);printf("normal : %f,%f,%f\n", floatData.v[0], floatData.v[1], floatData.v[2]);}else {ssOneLine >> temp;FloatData floatData;ssOneLine >> floatData.v[0];ssOneLine >> floatData.v[1];ssOneLine >> floatData.v[2];positions.push_back(floatData);printf("position : %f,%f,%f\n", floatData.v[0], floatData.v[1], floatData.v[2]);}}else if (szOneLine[0] == 'f') {std::stringstream ssOneLine(szOneLine);ssOneLine >> temp;std::string vertexStr;for (int i = 0; i < 3; i++) {ssOneLine >> vertexStr;size_t pos = vertexStr.find_first_of('/');std::string posIndexStr = vertexStr.substr(0, pos);size_t pos2 = vertexStr.find_first_of('/', pos + 1);std::string texcoordIndexStr = vertexStr.substr(pos + 1, pos2 - 1 - pos);std::string normalIndexStr = vertexStr.substr(pos2 + 1, vertexStr.length() - 1 - pos2);VertexDefine vd;vd.posIndex = atoi(posIndexStr.c_str());vd.texcoordIndex = atoi(texcoordIndexStr.c_str());vd.normalIndex = atoi(normalIndexStr.c_str());int nCurrentVertexIndex = -1;int nCurrentVertexCount = (int)vertexes.size();for (int j = 0; j < nCurrentVertexCount; ++j) {if (vertexes[j].posIndex == vd.posIndex&&vertexes[j].normalIndex == vd.normalIndex&&vertexes[j].texcoordIndex == vd.texcoordIndex) {nCurrentVertexIndex = j;break;}}if (nCurrentVertexIndex == -1) {nCurrentVertexIndex = (int)vertexes.size();vertexes.push_back(vd);}indexes.push_back(nCurrentVertexIndex);}}}}mIndexCount = (int)indexes.size();mIndexes = new unsigned short[mIndexCount];for (int i = 0; i < mIndexCount; ++i) {mIndexes[i] = indexes[i];}int vertexCount = (int)vertexes.size();mVertexes = new VertexData[vertexCount];for (int i = 0; i < vertexCount; ++i) {memcpy(mVertexes[i].position, positions[vertexes[i].posIndex - 1].v, sizeof(float) * 3);memcpy(mVertexes[i].texcoord, texcoords[vertexes[i].texcoordIndex - 1].v, sizeof(float) * 2);memcpy(mVertexes[i].normal, normals[vertexes[i].normalIndex - 1].v, sizeof(float) * 3);}delete fileContent;
}
void Model::Draw() {glEnable(GL_LIGHTING);glMaterialfv(GL_FRONT, GL_AMBIENT, mAmbientMaterial);glMaterialfv(GL_FRONT, GL_DIFFUSE, mDiffuseMaterial);glMaterialfv(GL_FRONT, GL_SPECULAR, mSpecularMaterial);glEnable(GL_TEXTURE_2D);glBindTexture(GL_TEXTURE_2D, mTexture);glEnable(GL_DEPTH_TEST);glPushMatrix();glTranslatef(0.0f, 0.0f, -5.0f);glBegin(GL_TRIANGLES);for (int i = 0; i < mIndexCount; ++i) {glTexCoord2fv(mVertexes[mIndexes[i]].texcoord);glNormal3fv(mVertexes[mIndexes[i]].normal);glVertex3fv(mVertexes[mIndexes[i]].position);}glEnd();glPopMatrix();
}void Model::SetAmbientMaterial(float r, float g, float b, float a) {mAmbientMaterial[0] = r;mAmbientMaterial[1] = g;mAmbientMaterial[2] = b;mAmbientMaterial[3] = a;
}void Model::SetDiffuseMaterial(float r, float g, float b, float a) {mDiffuseMaterial[0] = r;mDiffuseMaterial[1] = g;mDiffuseMaterial[2] = b;mDiffuseMaterial[3] = a;
}void Model::SetSpecularMaterial(float r, float g, float b, float a) {mSpecularMaterial[0] = r;mSpecularMaterial[1] = g;mSpecularMaterial[2] = b;mSpecularMaterial[3] = a;
}

这里要注意:

1.关照也是影响当前的矩阵;

2.如果在某一个绘制里面不想用光照,可以用glDisable(GL_LIGHTING);

C++ opengl 方向光相关推荐

  1. opengl对三种光源(方向光,点光源,聚光灯)进行特写并分屏渲染

    分屏特写渲染效果图 实现原理 1,创建3个fbo 2,分别将方向光,点光源,聚光灯的照射效果渲染到fbo 3,在将渲染好的三个fbo作为纹理贴到要绘制的三个四边形上. 渲染入口 #include &l ...

  2. Unity Shader - Custom DirectionalLight ShadowMap 自定义方向光的ShadowMap

    文章目录 思路 实践 在方向光的位置,放一个正交相机 调整光源相机参数 将光源投影空间的正交视锥体画出来 投射阴影 接收阴影 改进 超出Shadow map的默认为光照 添加光照处理 添加PCF柔滑整 ...

  3. OpenGL定向光的投影阴影

    OpenGL定向光的投影阴影 先上图,再解答. 完整主要的源代码 源代码剖析 先上图,再解答. 完整主要的源代码 #include <stdio.h> #include "GL/ ...

  4. python写h5网页前端_3D 图形 web 前端开发( OpenGL 方向) H5 Python 开发 招聘~~~

    之前的帖子被吐槽没有把薪资写清楚,所以这次特别把薪资标注了出来,而且除了 Python 之外,我们还在招聘其他的岗位呦~~快来看看吧,觉得合适就投简历啊~~ 3D 图形工程师 /3D 引擎开发工程师 ...

  5. 10.DirectionalLight(方向光)—— 模拟远处类似太阳的光源

    方向光光源和我们之前看过的聚光灯光源之间主要的差别是:方向光不像聚焦光那样离目标越远越暗淡.被方向光光源照亮的整个区域接收到的光强是一样的.

  6. three.js方向光DirectionalLight使用,调整方向光颜色、光源位置、光照强度、光照指向、是否可见、是否产生阴影属性(vue中使用three.js10)

    方向光DirectionalLight使用 一.方向光介绍 二.如何使用方向光 1.创建方向光 2.方向光的属性 2.1颜色-color 2.2是否可见-visible 2.3强度-intensity ...

  7. 【Android】openGL单位光的问题

    所谓单位光就是3D坐标内某光源在(x,y,z)点上,光向外散射.光源是一个点. 所谓单向光就是从一个方向来的平行光.光源是一个面. 最近在Android上用openGL来实现单向光和单位光,然后单位光 ...

  8. [OpenGL] 体积光效果实现

    reference: Volumetric Light Effects in Killzone: Shadow Fall [1] Interactive Rendering Method for Dis ...

  9. 【Unity,C#】控制方向光模拟昼夜变化的脚本

    Unity.C#.模拟昼夜变化的脚本 效果 如何实现 创建TextPro用于实时显示时间 简单配置 创建空对象 加入脚本 脚本代码 using System; using TMPro; using U ...

最新文章

  1. PAT (Advanced Level) 1014. Waiting in Line (30)
  2. 安卓设置菊花动画_Android Progressbar自定义菊花效果
  3. 基础练习 01字串(取位操作)
  4. Caliburn.Micro学习笔记(三)----事件聚合IEventAggregator和 IhandleT
  5. Android编译系统入门(二)
  6. 一种新的图像清晰度评价函数,数字图像清晰度评价函数的研究与改进
  7. 每日一笑 | 程序员的日常,这也太真实了......
  8. 将MongoDB集成到您的Spring项目
  9. 在pocket pc 2003上播放声音
  10. Linux移植随笔:让内核支持nor flash
  11. 这4点教你找到小程序专业开发公司
  12. 扫描问题 无法识别计算机,我的扫描枪插进去显示无法识别怎么办
  13. tmux鼠标配置出现错误unknown option: mode-mouse
  14. C/C++ 使用信号量控制线程运行顺序
  15. MongoDB系列六(聚合).
  16. 数学建模:9 多元线性回归分析
  17. 云服务器 架设传奇_阿里云虚拟主机,ECS服务器,轻量应用服务器有什么区别,该如何选择?...
  18. python分层抽样_基于列的sklearn分层抽样
  19. 详解独角兽应该具备的6个特点,创业者必看
  20. 数十位行业高管讲述:自动化测试优势及解决的现实问题

热门文章

  1. 有人说学了C语言,两天就能学会Java,两个星期就可以找工作?
  2. 大数据之路:阿里巴巴大数据实践,附339页PPT下载
  3. 10年经验+20个数据管理项目,我总结出这4个用数据改变企业的精华
  4. 如何为企业量身打造一套高可用系统?
  5. 来,我们谈谈怎么学好计算机科学与技术
  6. 正在这紧要关头的jdzyzwc
  7. 飞鸽传书2007的java学习感想
  8. 基于or1200最小sopc系统搭建(一)--搭建及仿真(DE2,DE2-70)
  9. 用VC写Assembly代码(4)
  10. 哈佛成功金句 -25则