关键源码如下:
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);
};
class PointLight :public Light {
public:
PointLight(GLenum light); //使用的OPenGL中哪一个光源
void SetPosition(float x, float y, float z); //设置位置,其次为1.0表示点光源
void SetConstAttenuation(float v); //设置常数的衰减系数
void SetLinearAttenuation(float v); //设置线性的衰减系数
void SetQuadricAttenuation(float v); //设置平方的衰减系数
};
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);
}
PointLight::PointLight(GLenum light)
{
mLightIdentifier = light;
}
void PointLight::SetPosition(float x, float y, float z)
{
float pos[] = { x,y,z,1.0 }; //其次为1.0表示点光源
glLightfv(mLightIdentifier, GL_POSITION, pos);
}
void PointLight::SetConstAttenuation(float v)
{
glLightf(mLightIdentifier, GL_CONSTANT_ATTENUATION, v);
}
void PointLight::SetLinearAttenuation(float v)
{
glLightf(mLightIdentifier, GL_LINEAR_ATTENUATION, v);
}
void PointLight::SetQuadricAttenuation(float v)
{
glLightf(mLightIdentifier, GL_QUADRATIC_ATTENUATION, v);
}
scene.cpp
#include "scene.h"
#include "utils.h"
#include "skybox.h"
#include "model.h"
#include "light.h"
#include "ground.h"
SkyBox skybox;
Model model;
Ground ground;
DirectionLight light(GL_LIGHT0);
PointLight light1(GL_LIGHT1);
void Init() {
glMatrixMode(GL_PROJECTION);
gluPerspective(50.0f, 800.0f / 600.0f, 0.1f, 1000.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
skybox.Init("Res/");
model.Init("Res/Sphere.obj");
model.mTexture = CreateTexture2DFromBMP("Res/earth.bmp");
light.SetAmbientColor(0.1f, 0.1f, 0.1f, 1.0f);
light.SetDiffuseColor(0.8f, 0.8f, 0.8f, 1.0f);
light.SetSpecularColor(1.0f, 1.0f, 1.0f, 1.0f);
light.SetPosition(0.0f, 1.0f, 0.0f);
light1.SetAmbientColor(0.1f, 0.1f, 0.1f, 1.0f);
light1.SetDiffuseColor(0.8f, 0.8f, 0.8f, 1.0f);
light1.SetSpecularColor(1.0f, 1.0f, 1.0f, 1.0f);
light1.SetPosition(0.0f, 0.0f, 0.0f); //与摄像机同位置
light1.SetConstAttenuation(1.0f);
light1.SetLinearAttenuation(0.2f);
model.SetAmbientMaterial(0.1f, 0.1f, 0.1f, 1.0f);
model.SetDiffuseMaterial(0.4f, 0.4f, 0.4f, 1.0f);
model.SetSpecularMaterial(1.0f, 1.0f, 1.0f, 1.0f);
ground.SetAmbientMaterial(0.1f, 0.1f, 0.1f, 1.0f);
ground.SetDiffuseMaterial(0.4f, 0.4f, 0.4f, 1.0f);
ground.SetSpecularMaterial(0.0f, 0.0f, 0.0f, 1.0f);
}
void Draw() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
light1.Enable();
skybox.Draw();
model.Draw();
ground.Draw();
}
接下来看下截图:
点光源的:
方向光的:
差别还是挺大的!!!!!!!!!