refactor: clean up shader file headers and improve error messages
This commit is contained in:
parent
aecc61b0a6
commit
9070510957
52
main.cpp
52
main.cpp
@ -1,19 +1,16 @@
|
||||
// main.cpp
|
||||
#include <GL/glew.h>
|
||||
#include <GL/glut.h>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <cmath> // For sin, cos
|
||||
#include <cmath>
|
||||
|
||||
// GLM Headers
|
||||
#define GLM_ENABLE_EXPERIMENTAL
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include <glm/gtx/transform.hpp>
|
||||
#include <glm/gtc/constants.hpp> // 包含 glm::pi
|
||||
|
||||
#include "shader_utils.hpp" // 我们创建的着色器加载工具
|
||||
#include "shader_utils.hpp" // 着色器加载工具
|
||||
|
||||
// 窗口尺寸
|
||||
int windowWidth = 1024;
|
||||
@ -21,19 +18,18 @@ int windowHeight = 768;
|
||||
|
||||
// 着色器程序ID
|
||||
GLuint programID;
|
||||
GLuint axesProgramID; // Shader program for axes
|
||||
// GLuint pickingProgramID; // (可选,如果实现拾取)
|
||||
GLuint axesProgramID;
|
||||
|
||||
|
||||
// Uniform ID
|
||||
GLuint MatrixID_MVP, MatrixID_M, MatrixID_V;
|
||||
GLuint LightPosID, LightColorID, LightPowerID, ViewPosID;
|
||||
GLuint TextureSamplerID;
|
||||
GLuint AxesMatrixID_MVP; // MVP uniform for axes shader
|
||||
GLuint AxesMatrixID_MVP;
|
||||
|
||||
// VAO 和 VBO
|
||||
GLuint CubeVAO, CubeVBO_vertices, CubeVBO_normals, CubeVBO_uvs;
|
||||
GLuint AxesVAO, AxesVBO_vertices, AxesVBO_colors;
|
||||
// GLuint LightVAO; // 用于表示光源的球体 (使用 GLUT 绘制,可能不需要单独VAO)
|
||||
|
||||
// 纹理
|
||||
GLuint checkerboardTextureID;
|
||||
@ -70,8 +66,8 @@ void mouseMotion(int x, int y);
|
||||
void idle();
|
||||
void createGradientTexture();
|
||||
void setupCubeBuffers();
|
||||
void setupAxesBuffers(); // Added
|
||||
void renderAxes(const glm::mat4& projectionMatrix, const glm::mat4& viewMatrix); // Added
|
||||
void setupAxesBuffers();
|
||||
void renderAxes(const glm::mat4& projectionMatrix, const glm::mat4& viewMatrix);
|
||||
void renderText(const char* text, float x, float y, float r = 1.0f, float g = 1.0f, float b = 1.0f);
|
||||
|
||||
// 立方体顶点数据 (位置, 法线, UV)
|
||||
@ -243,7 +239,7 @@ void createGradientTexture() {
|
||||
|
||||
for (int i = 0; i < texHeight; i++) {
|
||||
for (int j = 0; j < texWidth; j++) {
|
||||
// 创建漂亮的蓝紫色渐变
|
||||
// 创建蓝紫色渐变
|
||||
float u = (float)j / texWidth;
|
||||
float v = (float)i / texHeight;
|
||||
|
||||
@ -326,7 +322,7 @@ void setupAxesBuffers() {
|
||||
void initGL() {
|
||||
glewExperimental = GL_TRUE;
|
||||
if (glewInit() != GLEW_OK) {
|
||||
std::cerr << "Failed to initialize GLEW" << std::endl;
|
||||
std::cerr << "初始化 GLEW 失败" << std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
std::cout << "Using GLEW Version: " << glewGetString(GLEW_VERSION) << std::endl;
|
||||
@ -342,18 +338,18 @@ void initGL() {
|
||||
glDepthFunc(GL_LESS);
|
||||
glDepthMask(GL_TRUE);
|
||||
|
||||
// 禁用背面剔除,这样我们可以看到立方体的所有面
|
||||
// 禁用背面剔除,这样可以看到立方体的所有面
|
||||
glDisable(GL_CULL_FACE);
|
||||
|
||||
programID = LoadShaders("shaders/phong.vert", "shaders/phong.frag");
|
||||
if (programID == 0) {
|
||||
std::cerr << "Failed to load shaders. Exiting." << std::endl;
|
||||
std::cerr << "Failed to load shaders. QWQ" << std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
axesProgramID = LoadShaders("shaders/axes.vert", "shaders/axes.frag");
|
||||
if (axesProgramID == 0) {
|
||||
std::cerr << "Failed to load axes shaders. Exiting." << std::endl;
|
||||
std::cerr << "Failed to load axes shaders. QWQ" << std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
@ -429,7 +425,7 @@ void renderText(const char* text, float x, float y, float r, float g, float b) {
|
||||
void renderAxes(const glm::mat4& projectionMatrix, const glm::mat4& viewMatrix) {
|
||||
glUseProgram(axesProgramID);
|
||||
|
||||
glm::mat4 ModelMatrixAxes = glm::mat4(1.0); // Identity matrix, axes at origin
|
||||
glm::mat4 ModelMatrixAxes = glm::mat4(1.0);
|
||||
glm::mat4 MVP_Axes = projectionMatrix * viewMatrix * ModelMatrixAxes;
|
||||
|
||||
if (AxesMatrixID_MVP != -1) {
|
||||
@ -437,10 +433,10 @@ void renderAxes(const glm::mat4& projectionMatrix, const glm::mat4& viewMatrix)
|
||||
}
|
||||
|
||||
glBindVertexArray(AxesVAO);
|
||||
glLineWidth(2.0f); // Make axes lines thicker
|
||||
glDrawArrays(GL_LINES, 0, 6); // 3 lines, 2 vertices each
|
||||
glLineWidth(2.0f);
|
||||
glDrawArrays(GL_LINES, 0, 6);
|
||||
glBindVertexArray(0);
|
||||
glLineWidth(1.0f); // Reset line width
|
||||
glLineWidth(1.0f);
|
||||
}
|
||||
|
||||
void display() {
|
||||
@ -478,8 +474,8 @@ void display() {
|
||||
up
|
||||
);
|
||||
|
||||
// Render Cube
|
||||
glUseProgram(programID); // Ensure correct shader is used for the cube
|
||||
|
||||
glUseProgram(programID);
|
||||
|
||||
glm::mat4 ModelMatrixCube = glm::mat4(1.0);
|
||||
ModelMatrixCube = glm::translate(ModelMatrixCube, glm::vec3(0.0f, 0.0f, 0.0f));
|
||||
@ -540,7 +536,7 @@ void display() {
|
||||
// 解绑VAO
|
||||
glBindVertexArray(0);
|
||||
|
||||
// Render Axes
|
||||
// 画坐标轴
|
||||
renderAxes(ProjectionMatrix, ViewMatrix);
|
||||
|
||||
// 检查绘制后的OpenGL错误
|
||||
@ -559,7 +555,7 @@ void display() {
|
||||
glColor3f(lightColor.r, lightColor.g, lightColor.b);
|
||||
glutSolidSphere(0.1, 10, 10);
|
||||
|
||||
// 渲染操作说明文字
|
||||
// 渲染操作说明文字(emmm看来不能非ASCII)
|
||||
renderText("Controls:", 10, windowHeight - 20, 1.0f, 1.0f, 0.0f);
|
||||
renderText("W/S: Move Forward/Backward", 10, windowHeight - 40);
|
||||
renderText("A/D: Strafe Left/Right", 10, windowHeight - 60);
|
||||
@ -606,7 +602,7 @@ void keyboard(unsigned char key, int x, int y) {
|
||||
glDeleteVertexArrays(1, &CubeVAO);
|
||||
glDeleteProgram(programID);
|
||||
glDeleteTextures(1, &checkerboardTextureID);
|
||||
// Cleanup for axes
|
||||
// 删除坐标轴相关资源
|
||||
glDeleteBuffers(1, &AxesVBO_vertices);
|
||||
glDeleteBuffers(1, &AxesVBO_colors);
|
||||
glDeleteVertexArrays(1, &AxesVAO);
|
||||
@ -632,9 +628,6 @@ void mouseButton(int button, int state, int x, int y) {
|
||||
void mouseMotion(int x, int y) {
|
||||
// 仅当鼠标在窗口内时处理,避免跳动
|
||||
if (x < 0 || x >= windowWidth || y < 0 || y >= windowHeight) {
|
||||
// 当鼠标移出窗口时,可以选择不更新视角或者将鼠标重置回中心
|
||||
// 如果选择重置,可能需要更复杂的逻辑来避免在窗口边缘的抖动
|
||||
// mouseLeftDown = false; // 取消注释此行可以在鼠标移出时停止拖动视角
|
||||
return;
|
||||
}
|
||||
|
||||
@ -651,7 +644,7 @@ void mouseMotion(int x, int y) {
|
||||
lastMouseX = x;
|
||||
lastMouseY = y;
|
||||
|
||||
// FPS游戏风格的鼠标控制 (可选):
|
||||
// FPS游戏风格的鼠标控制:
|
||||
// if (x != windowWidth/2 || y != windowHeight/2) {
|
||||
// glutWarpPointer(windowWidth/2, windowHeight/2);
|
||||
// lastMouseX = windowWidth/2;
|
||||
@ -683,7 +676,6 @@ int main(int argc, char** argv) {
|
||||
glutKeyboardFunc(keyboard);
|
||||
glutMouseFunc(mouseButton);
|
||||
glutMotionFunc(mouseMotion);
|
||||
// glutPassiveMotionFunc(mouseMotion); // 如果希望鼠标不按下也触发 warp (如果使用)
|
||||
glutIdleFunc(idle);
|
||||
|
||||
glutMainLoop();
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
// shader_utils.cpp
|
||||
#include "shader_utils.hpp"
|
||||
|
||||
#include <vector>
|
||||
@ -17,7 +16,7 @@ GLuint LoadShaders(const char * vertex_file_path, const char * fragment_file_pat
|
||||
VertexShaderCode = sstr.str();
|
||||
VertexShaderStream.close();
|
||||
} else {
|
||||
std::cerr << "无法打开 " << vertex_file_path << ". 你是否在正确的目录下运行程序?" << std::endl;
|
||||
std::cerr << "无法打开 " << vertex_file_path << std::endl;
|
||||
getchar();
|
||||
return 0;
|
||||
}
|
||||
@ -31,7 +30,7 @@ GLuint LoadShaders(const char * vertex_file_path, const char * fragment_file_pat
|
||||
FragmentShaderCode = sstr.str();
|
||||
FragmentShaderStream.close();
|
||||
} else {
|
||||
std::cerr << "无法打开 " << fragment_file_path << ". 你是否在正确的目录下运行程序?" << std::endl;
|
||||
std::cerr << "无法打开 " << fragment_file_path << std::endl;
|
||||
getchar();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
// shaders/axes.frag
|
||||
#version 330 core
|
||||
|
||||
in vec3 fragmentColor;
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
// shaders/axes.vert
|
||||
#version 330 core
|
||||
|
||||
layout(location = 0) in vec3 vertexPosition_modelspace;
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
// shaders/phong.frag
|
||||
#version 330 core
|
||||
|
||||
// 从顶点着色器插值传入的变量
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
// shaders/phong.vert
|
||||
#version 330 core
|
||||
|
||||
// 输入顶点属性
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user