这章内容作为OpenGL的光照体系的一个起点,简单说明如何在OpenGL中应用颜色
(Vries的原教程地址如下,https://learnopengl-cn.github.io/02%20Lighting/01%20Colors/ 关于OpenGL函数的详细解析及OpenGL关于颜色这里的知识点详情描述请看这个教程,本篇旨在对Vires基于visual studio的编程思想做Qt平台的移植,重在记录自身学习之用)
实现Vries的第一个简单的颜色应用
项目组织如下,在我上一节,已上传了这个框架代码的百度云连接:
https://pan.baidu.com/s/196FAnQylehc9XHOUR8MLIg
因为有了框架,所以仅修改cube.h, cube.cpp, oglmanager.cpp与两个shader文件
cube.vert
#version 330 core
layout (location = 0) in vec3 aPos;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main(){
gl_Position = projection * view * model * vec4(aPos, 1.0f);
}
cube.frag
#version 330 core
out vec4 FragColor;
uniform vec3 objectColor;
uniform vec3 lightColor;
void main()
{
FragColor = vec4(objectColor * lightColor, 1.0f);
}
light.vert
#version 330 core
layout (location = 0) in vec3 aPos;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main(){
gl_Position = projection * view * model * vec4(aPos, 1.0f);
}
light.frag
#version 330 core
out vec4 FragColor;
void main()
{
FragColor = vec4(1.0f);
}
cube.h
#ifndef CUBE_H
#define CUBE_H
#include <QOpenGLFunctions_3_3_Core>
class Cube
{
public:
Cube();
~Cube();
void init();
void drawLight();
void drawCube();
private:
QOpenGLFunctions_3_3_Core *core;
GLuint VBO, cubeVAO, lightVAO;
};
#endif // CUBE_H
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>();
GLfloat vertices[] = {
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, -0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, -0.5f
};
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, 3 * sizeof(float), (void*)0);
core->glEnableVertexAttribArray(0);
core->glBindVertexArray(0);
core->glBindVertexArray(lightVAO);
core->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * 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>();
ResourceManager::loadShader("cube", ":/shaders/res/shaders/cube.vert", ":/shaders/res/shaders/cube.frag");
ResourceManager::loadShader("light", ":/shaders/res/shaders/light.vert", ":/shaders/res/shaders/light.frag");
ResourceManager::getShader("cube").use().setVector3f("objectColor", QVector3D(1.0f, 0.5f, 0.31f));
ResourceManager::getShader("cube").use().setVector3f("lightColor", QVector3D(1.0f, 1.0f, 1.0f));
//开启状态
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);
ResourceManager::getShader("cube").use().setMatrix4f("projection", projection);
ResourceManager::getShader("cube").use().setMatrix4f("view", camera->getViewMatrix());
ResourceManager::getShader("cube").use().setMatrix4f("model", model);
model.translate(LIGHT_POSITION);
model.scale(0.2f);
ResourceManager::getShader("light").use().setMatrix4f("projection", projection);
ResourceManager::getShader("light").use().setMatrix4f("view", camera->getViewMatrix());
ResourceManager::getShader("light").use().setMatrix4f("model", model);
}
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);
ResourceManager::getShader("cube").use();
cube->drawCube();
ResourceManager::getShader("light").use();
cube->drawLight();
}