# 基于Qt的OpenGL编程（3.x以上GLSL可编程管线版）---(十)基础光照

40 篇文章 84 订阅

Vries的原教程地址如下，https://learnopengl-cn.github.io/02%20Lighting/02%20Basic%20Lighting/#_7 关于OpenGL函数的详细解析及OpenGL关于基础光照的知识点详情描述请看这个教程，本篇旨在对Vires基于visual studio的编程思想做Qt平台的移植，重在记录自身学习之用）

Qt开发平台：5.8.0

cube.frag

#version 330 core
out vec4 FragColor;

uniform vec3 objectColor;
uniform vec3 lightColor;

void main()
{
float ambientStrength = 0.1f;
vec3 ambient = ambientStrength * lightColor;
vec3 result = ambient * objectColor;
FragColor = vec4(result, 1.0f);
}

cube.vert

#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

out vec3 Normal;
out vec3 FragPos;

void main(){
gl_Position = projection * view * model * vec4(aPos, 1.0f);
FragPos = vec3(model * vec4(aPos, 1.0f));
Normal = mat3(transpose(inverse(model))) * aNormal;//预防scale对法线的干扰
} = mat3(transpose(inverse(model))) * aNormal;//预防scale对法线的干扰
}

cube.frag

#version 330 core
out vec4 FragColor;

uniform vec3 objectColor;
uniform vec3 lightColor;
uniform vec3 lightPos;

in vec3 Normal;
in vec3 FragPos;

void main()
{
//ambient
float ambient = 0.1f;

//diffuse
vec3 norm = normalize(Normal);
vec3 lightDir = normalize(lightPos - FragPos);
float diffuse = max(dot(norm, lightDir), 0.0f);

//all
vec3 result =  (ambient + diffuse) * lightColor * objectColor;
FragColor = vec4(result, 1.0f);
}

cube.cpp

#include "cube.h"
#include "resourcemanager.h"

Cube::Cube(){

}

Cube::~Cube(){
core->glDeleteVertexArrays(1, &cubeVAO);
core->glDeleteVertexArrays(1, &lightVAO);
}

void Cube::init(){
core = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_3_3_Core>();
float vertices[] = {
-0.5f, -0.5f, -0.5f,  0.0f,  0.0f, -1.0f,
0.5f, -0.5f, -0.5f,  0.0f,  0.0f, -1.0f,
0.5f,  0.5f, -0.5f,  0.0f,  0.0f, -1.0f,
0.5f,  0.5f, -0.5f,  0.0f,  0.0f, -1.0f,
-0.5f,  0.5f, -0.5f,  0.0f,  0.0f, -1.0f,
-0.5f, -0.5f, -0.5f,  0.0f,  0.0f, -1.0f,

-0.5f, -0.5f,  0.5f,  0.0f,  0.0f,  1.0f,
0.5f, -0.5f,  0.5f,  0.0f,  0.0f,  1.0f,
0.5f,  0.5f,  0.5f,  0.0f,  0.0f,  1.0f,
0.5f,  0.5f,  0.5f,  0.0f,  0.0f,  1.0f,
-0.5f,  0.5f,  0.5f,  0.0f,  0.0f,  1.0f,
-0.5f, -0.5f,  0.5f,  0.0f,  0.0f,  1.0f,

-0.5f,  0.5f,  0.5f, -1.0f,  0.0f,  0.0f,
-0.5f,  0.5f, -0.5f, -1.0f,  0.0f,  0.0f,
-0.5f, -0.5f, -0.5f, -1.0f,  0.0f,  0.0f,
-0.5f, -0.5f, -0.5f, -1.0f,  0.0f,  0.0f,
-0.5f, -0.5f,  0.5f, -1.0f,  0.0f,  0.0f,
-0.5f,  0.5f,  0.5f, -1.0f,  0.0f,  0.0f,

0.5f,  0.5f,  0.5f,  1.0f,  0.0f,  0.0f,
0.5f,  0.5f, -0.5f,  1.0f,  0.0f,  0.0f,
0.5f, -0.5f, -0.5f,  1.0f,  0.0f,  0.0f,
0.5f, -0.5f, -0.5f,  1.0f,  0.0f,  0.0f,
0.5f, -0.5f,  0.5f,  1.0f,  0.0f,  0.0f,
0.5f,  0.5f,  0.5f,  1.0f,  0.0f,  0.0f,

-0.5f, -0.5f, -0.5f,  0.0f, -1.0f,  0.0f,
0.5f, -0.5f, -0.5f,  0.0f, -1.0f,  0.0f,
0.5f, -0.5f,  0.5f,  0.0f, -1.0f,  0.0f,
0.5f, -0.5f,  0.5f,  0.0f, -1.0f,  0.0f,
-0.5f, -0.5f,  0.5f,  0.0f, -1.0f,  0.0f,
-0.5f, -0.5f, -0.5f,  0.0f, -1.0f,  0.0f,

-0.5f,  0.5f, -0.5f,  0.0f,  1.0f,  0.0f,
0.5f,  0.5f, -0.5f,  0.0f,  1.0f,  0.0f,
0.5f,  0.5f,  0.5f,  0.0f,  1.0f,  0.0f,
0.5f,  0.5f,  0.5f,  0.0f,  1.0f,  0.0f,
-0.5f,  0.5f,  0.5f,  0.0f,  1.0f,  0.0f,
-0.5f,  0.5f, -0.5f,  0.0f,  1.0f,  0.0f
};

core->glGenVertexArrays(1, &cubeVAO);//两个参数，第一个为需要创建的缓存数量。第二个为用于存储单一ID或多个ID的GLuint变量或数组的地址
core->glGenVertexArrays(1, &lightVAO);
core->glGenBuffers(1, &VBO);

core->glBindBuffer(GL_ARRAY_BUFFER, VBO);
core->glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

core->glBindVertexArray(cubeVAO);
core->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
core->glEnableVertexAttribArray(0);
core->glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3*sizeof(float)));
core->glEnableVertexAttribArray(1);
core->glBindVertexArray(0);

core->glBindVertexArray(lightVAO);
core->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
core->glEnableVertexAttribArray(0);
core->glBindVertexArray(0);

core->glDeleteBuffers(1, &VBO);
}

void Cube::drawCube(){
core->glBindVertexArray(cubeVAO);
core->glDrawArrays(GL_TRIANGLES, 0, 36);
}

void Cube::drawLight(){
core->glBindVertexArray(lightVAO);
core->glDrawArrays(GL_TRIANGLES, 0, 36);
}

oglmanager.cpp

#include "oglmanager.h"
#include <QKeyEvent>
#include <QDebug>
#include "resourcemanager.h"
#include "cube.h"

const QVector3D CAMERA_POSITION(0.0f, 0.0f, 3.0f);
const QVector3D LIGHT_POSITION(1.0f, 0.8f, 0.8f);

Cube *cube;

OGLManager::OGLManager(GLuint w, GLuint h){
this->width = w;
this->height = h;
for(GLuint i = 0; i != 1024; ++i)
keys[i] = GL_FALSE;

}

OGLManager::~OGLManager(){
delete this->camera;
ResourceManager::clear();
}

void OGLManager::init(){
cube = new Cube();

this->camera = new Camera(CAMERA_POSITION);
cube->init();
core = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_3_3_Core>();

//开启状态
core->glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
core->glEnable(GL_DEPTH_TEST);
}

void OGLManager::processInput(GLfloat dt){
if (keys[Qt::Key_W])
camera->processKeyboard(FORWARD, dt);
if (keys[Qt::Key_S])
camera->processKeyboard(BACKWARD, dt);
if (keys[Qt::Key_A])
camera->processKeyboard(LEFT, dt);
if (keys[Qt::Key_D])
camera->processKeyboard(RIGHT, dt);
if (keys[Qt::Key_E])
camera->processKeyboard(UP, dt);
if (keys[Qt::Key_Q])
camera->processKeyboard(DOWN, dt);
}

void OGLManager::update(GLfloat dt){
QMatrix4x4 projection, model;
projection.perspective(camera->zoom, (GLfloat)width/(GLfloat)height, 0.1f, 200.f);

model.translate(LIGHT_POSITION);
model.scale(0.2f);
}

void OGLManager::resize(GLuint w, GLuint h){
core->glViewport(0, 0, w, h);
}

void OGLManager::draw(GLfloat dt)
{
core->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

cube->drawCube();

cube->drawLight();
}


cube.frag

#version 330 core
out vec4 FragColor;

uniform vec3 objectColor;
uniform vec3 lightColor;

uniform vec3 lightPos;
uniform vec3 viewPos;

in vec3 Normal;
in vec3 FragPos;

void main()
{
//ambient
float ambient = 0.1f;

//diffuse
vec3 norm = normalize(Normal);
vec3 lightDir = normalize(lightPos - FragPos);
float diffuse = max(dot(norm, lightDir), 0.0f);

//specular
vec3 viewDir = normalize(viewPos - FragPos);
vec3 reflectDir = reflect(-lightDir, norm);
float specularStrength = 0.5f;
float specular = pow(max(dot(viewDir, reflectDir), 0.0f), 64) * specularStrength;

//all
vec3 result =  (ambient + diffuse + specular) * lightColor * objectColor;
FragColor = vec4(result, 1.0f);

}


oglmanager.cpp

#include "oglmanager.h"
#include <QKeyEvent>
#include <QDebug>
#include "resourcemanager.h"
#include "cube.h"

const QVector3D CAMERA_POSITION(0.0f, 0.0f, 3.0f);
const QVector3D LIGHT_POSITION(1.0f, 0.8f, 0.8f);

Cube *cube;

OGLManager::OGLManager(GLuint w, GLuint h){
this->width = w;
this->height = h;
for(GLuint i = 0; i != 1024; ++i)
keys[i] = GL_FALSE;

}

OGLManager::~OGLManager(){
delete this->camera;
ResourceManager::clear();
}

void OGLManager::init(){
cube = new Cube();

this->camera = new Camera(CAMERA_POSITION);
cube->init();
core = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_3_3_Core>();

//开启状态
core->glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
core->glEnable(GL_DEPTH_TEST);
}

void OGLManager::processInput(GLfloat dt){
if (keys[Qt::Key_W])
camera->processKeyboard(FORWARD, dt);
if (keys[Qt::Key_S])
camera->processKeyboard(BACKWARD, dt);
if (keys[Qt::Key_A])
camera->processKeyboard(LEFT, dt);
if (keys[Qt::Key_D])
camera->processKeyboard(RIGHT, dt);
if (keys[Qt::Key_E])
camera->processKeyboard(UP, dt);
if (keys[Qt::Key_Q])
camera->processKeyboard(DOWN, dt);
}

void OGLManager::update(GLfloat dt){
QMatrix4x4 projection, model;
projection.perspective(camera->zoom, (GLfloat)width/(GLfloat)height, 0.1f, 200.f);

model.translate(LIGHT_POSITION);
model.scale(0.2f);
}

void OGLManager::resize(GLuint w, GLuint h){
core->glViewport(0, 0, w, h);
}

void OGLManager::draw(GLfloat dt)
{
core->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

cube->drawCube();

cube->drawLight();
}


• 1
点赞
• 2
收藏
• 打赏
• 3
评论
07-26 1314
12-03 198
02-28 8502
07-18 194
07-26 100
01-18 808

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。