学习learnopengl文章对应地址:https://learnopengl-cn.github.io/02%20Lighting/02%20Basic%20Lighting/
VS、qt版本分别给了多个文件和两个项目,都在同一个包内:https://download.csdn.net/download/zzjzmdx/18249322
展示图:
Visual Studio 2019
代码如下:
BasicLighting-01.cpp
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "shader.h"
#include "camera.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
void processInput(GLFWwindow* window);
Camera camera(glm::vec3(0.0f, 1.0f, 6.0f));
float lastX = SCR_WIDTH / 2.0f;
float lastY = SCR_HEIGHT / 2.0f;
bool firstMouse = true;
float deltaTime = 0.0f;
float lastFrame = 0.0f;
glm::vec3 lightPos(1.2f, 1.0f, 2.0f);
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
};
int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetCursorPosCallback(window, mouse_callback);
glfwSetScrollCallback(window, scroll_callback);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
glEnable(GL_DEPTH_TEST);
Shader lightingShader("./2.1cube.vs", "./2.1cube.fs");
Shader lightCubeShader("./2.1light.vs", "./2.1light.fs");
unsigned int VBO, cubeVAO;
glGenVertexArrays(1, &cubeVAO);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindVertexArray(cubeVAO);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// normal attribute
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
unsigned int lightCubeVAO;
glGenVertexArrays(1, &lightCubeVAO);
glBindVertexArray(lightCubeVAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
while (!glfwWindowShouldClose(window))
{
float currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
processInput(window);
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
lightingShader.use();
lightingShader.setVec3("objectColor", 1.0f, 0.5f, 0.31f);
lightingShader.setVec3("lightColor", 1.0f, 1.0f, 1.0f);
lightingShader.setVec3("lightPos", lightPos);
glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
glm::mat4 view = camera.GetViewMatrix();
lightingShader.setMat4("projection", projection);
lightingShader.setMat4("view", view);
glm::mat4 model = glm::mat4(1.0f);
lightingShader.setMat4("model", model);
glBindVertexArray(cubeVAO);
glDrawArrays(GL_TRIANGLES, 0, 36);
lightCubeShader.use();
lightCubeShader.setMat4("projection", projection);
lightCubeShader.setMat4("view", view);
model = glm::mat4(1.0f);
model = glm::translate(model, lightPos);
model = glm::scale(model, glm::vec3(0.2f));
lightCubeShader.setMat4("model", model);
glBindVertexArray(lightCubeVAO);
glDrawArrays(GL_TRIANGLES, 0, 36);
glfwSwapBuffers(window);
glfwPollEvents();
}
glDeleteVertexArrays(1, &cubeVAO);
glDeleteVertexArrays(1, &lightCubeVAO);
glDeleteBuffers(1, &VBO);
glfwTerminate();
return 0;
}
void processInput(GLFWwindow* window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
camera.ProcessKeyboard(FORWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
camera.ProcessKeyboard(BACKWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
camera.ProcessKeyboard(LEFT, deltaTime);
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
camera.ProcessKeyboard(RIGHT, deltaTime);
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
void mouse_callback(GLFWwindow* window, double xpos, double ypos)
{
if (firstMouse)
{
lastX = xpos;
lastY = ypos;
firstMouse = false;
}
float xoffset = xpos - lastX;
float yoffset = lastY - ypos;
lastX = xpos;
lastY = ypos;
camera.ProcessMouseMovement(xoffset, yoffset);
}
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
camera.ProcessMouseScroll(yoffset);
}
BasicLighting-02.cpp
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "shader.h"
#include "camera.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
void processInput(GLFWwindow* window);
Camera camera(glm::vec3(0.0f, 0.0f, 4.0f));
float lastX = SCR_WIDTH / 2.0f;
float lastY = SCR_HEIGHT / 2.0f;
bool firstMouse = true;
float deltaTime = 0.0f;
float lastFrame = 0.0f;
glm::vec3 lightPos(0.0f, 0.5f, 2.0f);
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
};
int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetCursorPosCallback(window, mouse_callback);
glfwSetScrollCallback(window, scroll_callback);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
glEnable(GL_DEPTH_TEST);
Shader lightingShader("./2.2cube.vs", "./2.2cube.fs");
Shader lightCubeShader("./2.2light.vs", "./2.2light.fs");
unsigned int VBO, cubeVAO;
glGenVertexArrays(1, &cubeVAO);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindVertexArray(cubeVAO);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// normal attribute
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
unsigned int lightCubeVAO;
glGenVertexArrays(1, &lightCubeVAO);
glBindVertexArray(lightCubeVAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
while (!glfwWindowShouldClose(window))
{
float currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
processInput(window);
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
lightingShader.use();
lightingShader.setVec3("objectColor", 1.0f, 0.5f, 0.31f);
lightingShader.setVec3("lightColor", 1.0f, 1.0f, 1.0f);
lightingShader.setVec3("lightPos", lightPos);
lightingShader.setVec3("viewPos", camera.Position);
glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
glm::mat4 view = camera.GetViewMatrix();
lightingShader.setMat4("projection", projection);
lightingShader.setMat4("view", view);
glm::mat4 model = glm::mat4(1.0f);
lightingShader.setMat4("model", model);
glBindVertexArray(cubeVAO);
glDrawArrays(GL_TRIANGLES, 0, 36);
lightCubeShader.use();
lightCubeShader.setMat4("projection", projection);
lightCubeShader.setMat4("view", view);
model = glm::mat4(1.0f);
model = glm::translate(model, lightPos);
model = glm::scale(model, glm::vec3(0.1f));
lightCubeShader.setMat4("model", model);
glBindVertexArray(lightCubeVAO);
glDrawArrays(GL_TRIANGLES, 0, 36);
glfwSwapBuffers(window);
glfwPollEvents();
}
glDeleteVertexArrays(1, &cubeVAO);
glDeleteVertexArrays(1, &lightCubeVAO);
glDeleteBuffers(1, &VBO);
glfwTerminate();
return 0;
}
void processInput(GLFWwindow* window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
camera.ProcessKeyboard(FORWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
camera.ProcessKeyboard(BACKWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
camera.ProcessKeyboard(LEFT, deltaTime);
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
camera.ProcessKeyboard(RIGHT, deltaTime);
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
void mouse_callback(GLFWwindow* window, double xpos, double ypos)
{
if (firstMouse)
{
lastX = xpos;
lastY = ypos;
firstMouse = false;
}
float xoffset = xpos - lastX;
float yoffset = lastY - ypos;
lastX = xpos;
lastY = ypos;
camera.ProcessMouseMovement(xoffset, yoffset);
}
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
camera.ProcessMouseScroll(yoffset);
}
qt 5.15.1
代码如下:
BasicLighting-01.cpp
#include "BasicLighting.h"
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
};
QVector3D lightPos(1.2f, 1.0f, 2.0f);
BasicLighting::BasicLighting(QWidget *parent)
: QOpenGLWidget(parent)
{
}
BasicLighting::~BasicLighting()
{
m_cubeVAO->destroy();
m_vbo->destroy();
m_lightVAO->destroy();
}
void BasicLighting::initializeGL()
{
initializeOpenGLFunctions();
glEnable(GL_DEPTH_TEST);
//物体的QOpenGLShaderProgram
m_cubeProgram = new QOpenGLShaderProgram();
//vertex shader 顶点着色器
m_cubeProgram->addShaderFromSourceFile(QOpenGLShader::Vertex, ":/vcube.glsl");
//fragment shader 片段着色器
m_cubeProgram->addShaderFromSourceFile(QOpenGLShader::Fragment, ":/fcube.glsl");
m_cubeProgram->link();
m_cubeProgram->bind();//激活Program对象
//物体的VAO
m_cubeVAO = new QOpenGLVertexArrayObject();
m_cubeVAO->create();
m_cubeVAO->bind();
m_vbo = new QOpenGLBuffer(QOpenGLBuffer::Type::VertexBuffer);
m_vbo->create();
m_vbo->bind();
m_vbo->allocate(vertices, sizeof(vertices));
m_vbo->setUsagePattern(QOpenGLBuffer::StreamDraw);
GLint cubePos = m_cubeProgram->attributeLocation("cubePos");
if(cubePos==-1)
{
return;
}
m_cubeProgram->setAttributeBuffer(cubePos, GL_FLOAT, 0, 3, 6*sizeof(GLfloat));
m_cubeProgram->enableAttributeArray(cubePos);
GLint cubeNormal = m_cubeProgram->attributeLocation("cubeNormal");
if(cubeNormal==-1)
{
return;
}
m_cubeProgram->setAttributeBuffer(cubeNormal, GL_FLOAT, 3*sizeof(GLfloat), 3, 6*sizeof(GLfloat));
m_cubeProgram->enableAttributeArray(cubeNormal);
//物体的QOpenGLShaderProgram
m_lightProgram = new QOpenGLShaderProgram();
//vertex shader 顶点着色器
m_lightProgram->addShaderFromSourceFile(QOpenGLShader::Vertex, ":/vlight.glsl");
//fragment shader 片段着色器
m_lightProgram->addShaderFromSourceFile(QOpenGLShader::Fragment, ":/flight.glsl");
m_lightProgram->link();
m_lightProgram->bind();//激活Program对象
m_lightVAO = new QOpenGLVertexArrayObject();
m_lightVAO->create();
m_lightVAO->bind();
m_vbo->bind();
GLint lightPos = m_lightProgram->attributeLocation("lightPos");
if(lightPos==-1)
{
return;
}
m_lightProgram->setAttributeBuffer(lightPos, GL_FLOAT, 0, 3, 6*sizeof(GLfloat));
m_lightProgram->enableAttributeArray(lightPos);
m_vbo->release();
}
void BasicLighting::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
QMatrix4x4 projection;
projection.perspective(fov, (float)SCR_WIDTH/(float)SCR_HEIGHT, 0.1f, 100.0f);
QMatrix4x4 view;
m_time = QTime::currentTime();
m_deltaTime = m_time.msecsTo(m_lasttime)/1000.0f;
m_lasttime = m_time;
//一个位置、目标和上向量
view.lookAt(cameraPos, cameraPos + cameraFront, cameraUp);
QMatrix4x4 model;
if(m_cubeProgram->isLinked())
{
m_cubeProgram->bind();
m_cubeProgram->setUniformValue("objectColor", 1.0f, 0.5f, 0.31f);
m_cubeProgram->setUniformValue("lightColor", 1.0f, 1.0f, 1.0f);
m_cubeProgram->setUniformValue("lightPos", lightPos);
m_cubeProgram->setUniformValue("cubemodel", model);
m_cubeProgram->setUniformValue("cubeview", view);
m_cubeProgram->setUniformValue("cubeprojection", projection);
}
m_cubeVAO->bind();
glDrawArrays(GL_TRIANGLES, 0, 36);
m_cubeVAO->release();
m_cubeProgram->release();
model.translate(lightPos);
model.scale(QVector3D(0.2f, 0.2f, 0.2f));
if(m_lightProgram->isLinked())
{
m_lightProgram->bind();
m_lightProgram->setUniformValue("lightmodel", model);
m_lightProgram->setUniformValue("lightview", view);
m_lightProgram->setUniformValue("lightprojection", projection);
}
m_lightVAO->bind();
glDrawArrays(GL_TRIANGLES, 0, 36);
m_lightVAO->release();
m_lightProgram->release();
update();
}
void BasicLighting::mouseMoveEvent(QMouseEvent *event)
{
int xpos = event->x();
int ypos = event->y();
if (firstMouse)
{
lastX = xpos;
lastY = ypos;
firstMouse = false;
}
float xoffset = xpos - lastX;
float yoffset = lastY - ypos;
lastX = xpos;
lastY = ypos;
float sensitivity = 0.1f;
xoffset *= sensitivity;
yoffset *= sensitivity;
yaw += xoffset;
pitch += yoffset;
if (pitch > 89.0f)
pitch = 89.0f;
if (pitch < -89.0f)
pitch = -89.0f;
QVector3D front = QVector3D(0.0f, 0.0f, 3.0f);
front.setX(cos(qDegreesToRadians(yaw)) * cos(qDegreesToRadians(pitch)));
front.setY(sin(qDegreesToRadians(pitch)));
front.setZ(sin(qDegreesToRadians(yaw)) * cos(qDegreesToRadians(pitch)));
cameraFront = front;
}
void BasicLighting::wheelEvent(QWheelEvent *event){
float flagNumber = 1;
if(event->angleDelta().y()>0){
flagNumber = 1;
}else{
flagNumber = -1;
}
fov -= flagNumber;
// if (fov < 1.0f)
// fov = 1.0f;
// if (fov > 45.0f)
// fov = 45.0f;
}
void BasicLighting::keyPressEvent(QKeyEvent *event)
{
float cameraSpeed = m_deltaTime;
if(event->key() == Qt::Key_Escape)
{
close();
}
if(event->key() == Qt::Key_W)
{
cameraPos += cameraSpeed * cameraFront;
}
if(event->key() == Qt::Key_S)
{
cameraPos -= cameraSpeed * cameraFront;
}
if(event->key() == Qt::Key_A)
{
cameraPos -= QVector3D::crossProduct(cameraFront, cameraUp) * cameraSpeed;
}
if(event->key() == Qt::Key_D)
{
cameraPos += (QVector3D::crossProduct(cameraFront, cameraUp)) * cameraSpeed;
}
}
void BasicLighting::resizeGL(int w, int h)
{
}
BasicLighting-02.cpp
#include "BasicLighting.h"
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
};
QVector3D lightPos(0.0f, 0.5f, 2.0f);
BasicLighting::BasicLighting(QWidget *parent)
: QOpenGLWidget(parent)
{
}
BasicLighting::~BasicLighting()
{
m_cubeVAO->destroy();
m_vbo->destroy();
m_lightVAO->destroy();
}
void BasicLighting::initializeGL()
{
initializeOpenGLFunctions();
glEnable(GL_DEPTH_TEST);
//物体的QOpenGLShaderProgram
m_cubeProgram = new QOpenGLShaderProgram();
//vertex shader 顶点着色器
m_cubeProgram->addShaderFromSourceFile(QOpenGLShader::Vertex, ":/vcube.glsl");
//fragment shader 片段着色器
m_cubeProgram->addShaderFromSourceFile(QOpenGLShader::Fragment, ":/fcube.glsl");
m_cubeProgram->link();
m_cubeProgram->bind();//激活Program对象
//物体的VAO
m_cubeVAO = new QOpenGLVertexArrayObject();
m_cubeVAO->create();
m_cubeVAO->bind();
m_vbo = new QOpenGLBuffer(QOpenGLBuffer::Type::VertexBuffer);
m_vbo->create();
m_vbo->bind();
m_vbo->allocate(vertices, sizeof(vertices));
m_vbo->setUsagePattern(QOpenGLBuffer::StreamDraw);
GLint cubePos = m_cubeProgram->attributeLocation("cubePos");
if(cubePos==-1)
{
return;
}
m_cubeProgram->setAttributeBuffer(cubePos, GL_FLOAT, 0, 3, 6*sizeof(GLfloat));
m_cubeProgram->enableAttributeArray(cubePos);
GLint cubeNormal = m_cubeProgram->attributeLocation("cubeNormal");
if(cubeNormal==-1)
{
return;
}
m_cubeProgram->setAttributeBuffer(cubeNormal, GL_FLOAT, 3*sizeof(GLfloat), 3, 6*sizeof(GLfloat));
m_cubeProgram->enableAttributeArray(cubeNormal);
//物体的QOpenGLShaderProgram
m_lightProgram = new QOpenGLShaderProgram();
//vertex shader 顶点着色器
m_lightProgram->addShaderFromSourceFile(QOpenGLShader::Vertex, ":/vlight.glsl");
//fragment shader 片段着色器
m_lightProgram->addShaderFromSourceFile(QOpenGLShader::Fragment, ":/flight.glsl");
m_lightProgram->link();
m_lightProgram->bind();//激活Program对象
m_lightVAO = new QOpenGLVertexArrayObject();
m_lightVAO->create();
m_lightVAO->bind();
m_vbo->bind();
GLint lightPos = m_lightProgram->attributeLocation("lightPos");
if(lightPos==-1)
{
return;
}
m_lightProgram->setAttributeBuffer(lightPos, GL_FLOAT, 0, 3, 6*sizeof(GLfloat));
m_lightProgram->enableAttributeArray(lightPos);
m_vbo->release();
}
void BasicLighting::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
QMatrix4x4 projection;
projection.perspective(fov, (float)SCR_WIDTH/(float)SCR_HEIGHT, 0.1f, 100.0f);
QMatrix4x4 view;
m_time = QTime::currentTime();
m_deltaTime = m_time.msecsTo(m_lasttime)/1000.0f;
m_lasttime = m_time;
//一个位置、目标和上向量
view.lookAt(cameraPos, cameraPos + cameraFront, cameraUp);
QMatrix4x4 model;
if(m_cubeProgram->isLinked())
{
m_cubeProgram->bind();
m_cubeProgram->setUniformValue("objectColor", 1.0f, 0.5f, 0.31f);
m_cubeProgram->setUniformValue("lightColor", 1.0f, 1.0f, 1.0f);
m_cubeProgram->setUniformValue("lightPos", lightPos);
m_cubeProgram->setUniformValue("viewPos", cameraPos);
m_cubeProgram->setUniformValue("cubemodel", model);
m_cubeProgram->setUniformValue("cubeview", view);
m_cubeProgram->setUniformValue("cubeprojection", projection);
}
m_cubeVAO->bind();
glDrawArrays(GL_TRIANGLES, 0, 36);
m_cubeVAO->release();
m_cubeProgram->release();
model.translate(lightPos);
model.scale(QVector3D(0.2f, 0.2f, 0.2f));
if(m_lightProgram->isLinked())
{
m_lightProgram->bind();
m_lightProgram->setUniformValue("lightmodel", model);
m_lightProgram->setUniformValue("lightview", view);
m_lightProgram->setUniformValue("lightprojection", projection);
}
m_lightVAO->bind();
glDrawArrays(GL_TRIANGLES, 0, 36);
m_lightVAO->release();
m_lightProgram->release();
update();
}
void BasicLighting::mouseMoveEvent(QMouseEvent *event)
{
int xpos = event->x();
int ypos = event->y();
if (firstMouse)
{
lastX = xpos;
lastY = ypos;
firstMouse = false;
}
float xoffset = xpos - lastX;
float yoffset = lastY - ypos;
lastX = xpos;
lastY = ypos;
float sensitivity = 0.1f;
xoffset *= sensitivity;
yoffset *= sensitivity;
yaw += xoffset;
pitch += yoffset;
if (pitch > 89.0f)
pitch = 89.0f;
if (pitch < -89.0f)
pitch = -89.0f;
QVector3D front = QVector3D(0.0f, 0.0f, 3.0f);
front.setX(cos(qDegreesToRadians(yaw)) * cos(qDegreesToRadians(pitch)));
front.setY(sin(qDegreesToRadians(pitch)));
front.setZ(sin(qDegreesToRadians(yaw)) * cos(qDegreesToRadians(pitch)));
cameraFront = front;
}
void BasicLighting::wheelEvent(QWheelEvent *event){
float flagNumber = 1;
if(event->angleDelta().y()>0){
flagNumber = 1;
}else{
flagNumber = -1;
}
fov -= flagNumber;
// if (fov < 1.0f)
// fov = 1.0f;
// if (fov > 45.0f)
// fov = 45.0f;
}
void BasicLighting::keyPressEvent(QKeyEvent *event)
{
float cameraSpeed = m_deltaTime;
if(event->key() == Qt::Key_Escape)
{
close();
}
if(event->key() == Qt::Key_W)
{
cameraPos += cameraSpeed * cameraFront;
}
if(event->key() == Qt::Key_S)
{
cameraPos -= cameraSpeed * cameraFront;
}
if(event->key() == Qt::Key_A)
{
cameraPos -= QVector3D::crossProduct(cameraFront, cameraUp) * cameraSpeed;
}
if(event->key() == Qt::Key_D)
{
cameraPos += (QVector3D::crossProduct(cameraFront, cameraUp)) * cameraSpeed;
}
}
void BasicLighting::resizeGL(int w, int h)
{
}