C++ opengl 方向光
生活随笔
收集整理的這篇文章主要介紹了
C++ opengl 方向光
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
未有方向光的截圖如下:
?
給模型加方向光后,截圖如下:
?
關鍵源碼如下:
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 方向光的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Qt工作笔记-对connect的第五个参
- 下一篇: C++/Qt工作笔记-static_ca