https://blog.youkuaiyun.com/Wang_Dou_Dou_/article/details/121065437
根据网页中的代码,使用qt中相关函数实现相同功能,在窗口中画出对应3D图像
class Point_Light
{
public:
Point_Light()
{
this->update();
}
void Draw(Shader &shader)
{
glBindVertexArray(light_VAO);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
}
~Point_Light()
{
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glDeleteVertexArrays(1, &light_VAO);
glDeleteBuffers(1, &light_VBO);
}
private:
GLuint light_VAO, light_VBO;
void update()
{GLfloat vertices_2[] =
{ // 坐标
// x、y、z 坐标 // color
-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, 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, 1.0f, 0.0f, //
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 1.0f,
0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 1.0f,
};
glGenVertexArrays(1, &light_VAO);
glGenBuffers(1, &light_VBO);
glBindVertexArray(light_VAO);
glBindBuffer(GL_ARRAY_BUFFER, light_VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices_2), vertices_2, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
}
};
着色器:class Shader
{
private:
GLuint vertex, fragment;
public:
GLuint Program;
Shader(const GLchar *vertexPath, const GLchar *fragmentPath)
{
string vertexCode;
string fragmentCode;
ifstream vShaderFile;
ifstream fShaderFile;
vShaderFile.exceptions(ifstream::badbit);
fShaderFile.exceptions(ifstream::badbit);
try
{
vShaderFile.open(vertexPath);
fShaderFile.open(fragmentPath);
stringstream vShaderStream, fShaderStream;
vShaderStream << vShaderFile.rdbuf();
fShaderStream << fShaderFile.rdbuf();
vShaderFile.close();
fShaderFile.close();
vertexCode = vShaderStream.str();
fragmentCode = fShaderStream.str();
}
catch (ifstream::failure e) {
cout << "ERROR::SHADER::FILE_NOT_SUCCESSFULLY_READ" << endl;
}
const GLchar *vShaderCode = vertexCode.c_str();
const GLchar *fShaderCode = fragmentCode.c_str();
vertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex, 1, &vShaderCode, NULL);
glCompileShader(vertex);
GLint flag;
GLchar infoLog[512];
glGetShaderiv(vertex, GL_COMPILE_STATUS, &flag);
if (!flag)
{
glGetShaderInfoLog(vertex, 512, NULL, infoLog);
cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << endl;
}
fragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment, 1, &fShaderCode, NULL);
glCompileShader(fragment);
glGetShaderiv(fragment, GL_COMPILE_STATUS, &flag);
if (!flag)
{
glGetShaderInfoLog(fragment, 512, NULL, infoLog);
cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << endl;
}
this->Program = glCreateProgram();
glAttachShader(this->Program, vertex);
glAttachShader(this->Program, fragment);
glLinkProgram(this->Program);
if (!flag)
{
glGetProgramInfoLog(this->Program, 512, NULL, infoLog);
cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << endl;
}
glDeleteShader(vertex);
glDeleteShader(fragment);
}
~Shader()
{
glDetachShader(this->Program, vertex);
glDetachShader(this->Program, fragment);
glDeleteShader(vertex);
glDeleteShader(fragment);
glDeleteProgram(this->Program);
}
void Use()
{
glUseProgram(this->Program);
}
};
观察相机:enum Camera_Movement { // 枚举类型
FORWARD, // 向前
BACKWARD, // 向后
LEFT, // 向左
RIGHT, // 向右
UPWARD, // 向上
DOWNWARD // 向下
};
const GLfloat SPEED = 6.0f; // 初始速度
class Camera
{
public:
// 构造函数
Camera(glm::vec3 position = glm::vec3(0.0f, 0.0f, 5.0f), glm::vec3 target = glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f)) :movementSpeed(SPEED)
{
this->position = position;
this->camera_Z_axis = target;
this->camera_Y_axis = up;
this->camera_X_axis = glm::normalize(glm::cross(this->camera_Z_axis, this->camera_Y_axis));
this->updateCameraVectors(); // 实时更新
}
// 观察矩阵
glm::mat4 GetViewMatrix()
{
return glm::lookAt(this->position, this->position + this->camera_Z_axis, this->camera_Y_axis);
}
void ProcessKeyboard(Camera_Movement direction, float deltaTime)
{
float velocity = this->movementSpeed * deltaTime;
if (direction == FORWARD)
this->position += this->camera_Z_axis * velocity;
if (direction == BACKWARD)
this->position -= this->camera_Z_axis * velocity;
if (direction == LEFT)
this->position -= this->camera_X_axis * velocity;
if (direction == RIGHT)
this->position += this->camera_X_axis * velocity;
if (direction == UPWARD)
this->position += this->camera_Y_axis * velocity;
if (direction == DOWNWARD)
this->position -= this->camera_Y_axis * velocity;
}
glm::vec3 GetPosition() // 下一篇文章使用
{
return this->position;
}
private:
// 摄影机的属性
glm::vec3 position; // 相机当前位置
glm::vec3 camera_Z_axis; // 摄影机的 Z 轴向量
glm::vec3 camera_X_axis; // 摄影机的 X 轴向量
glm::vec3 camera_Y_axis; // 摄影机的 Y 轴向量
GLfloat movementSpeed; // 镜头移动速度
void updateCameraVectors()
{
this->camera_Z_axis = glm::normalize(this->camera_Z_axis);
this->camera_X_axis = glm::normalize(glm::cross(this->camera_Z_axis, this->camera_Y_axis));
this->camera_Y_axis = glm::normalize(glm::cross(this->camera_X_axis, this->camera_Z_axis));
}
};
Shader ourShader = Shader("F:/test/OpenGLTest/shaders/shader_v.txt", "F:/test/OpenGLTest/shaders/shader_f.txt");
Shader lightShader = Shader("F:/test/OpenGLTest/shaders/light_v.txt", "F:/test/OpenGLTest/shaders/light_f.txt");
可以使用qt相关方式修改自定义着色器,也可以使用QT相关函数替代相关功能,去除自定义着色器,只有相关功能可以实现,之前着色器时从文件中读取的,依旧需要保留从文件中读取,请给出实现后的完整代码以及详细的中文注释,头文件和源文件需要分开,不同类在不同文件