201807130950->关于shader的资料索引

https://blog.youkuaiyun.com/weixin_37994402/article/details/78217668 

这里对一些shader的基本含义作了相当详细的解析

https://blog.youkuaiyun.com/lly707649841/article/details/78736897

关于顶点着色器与片段着色的结合播放uv动画

https://blog.youkuaiyun.com/zjw1349547081/article/details/53924787

关于shader的深入了解,对一些api的记录

https://blog.youkuaiyun.com/zhangxiao13627093203/article/details/53163098

对shader的api例子


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() { 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相关函数替代相关功能,去除自定义着色器,只有相关功能可以实现,之前着色器时从文件中读取的,依旧需要保留从文件中读取,请给出实现后的完整代码以及详细的中文注释,头文件和源文件需要分开,不同类在不同文件
07-30
详细解释下面的代码,具体到各个参数的含义和作用#ifndef MESH_H #define MESH_H #include <QOpenGLShaderProgram> #include <QOpenGLFunctions_3_3_Core> #include <string> #include <vector> #include <QOpenGLTexture> using namespace std; struct Vertex { QVector3D Position; QVector3D Normal; QVector2D TexCoords; }; struct Texture { unsigned int id; string type; string path; }; class Mesh { public: Mesh(){}; // mesh data vector<Vertex> vertices; vector<unsigned int> indices; vector<Texture> textures; void Draw(QOpenGLShaderProgram &shader); void Draw(QOpenGLShaderProgram &shader, QString type); Mesh(QOpenGLFunctions_3_3_Core *glFuns, vector<Vertex> vertices, vector<unsigned int> indices, vector<Texture> textures); private: // render data unsigned int VAO, VBO, EBO; void setupMesh(); private: QOpenGLFunctions_3_3_Core *m_glFuns; QOpenGLTexture *m_STLDiffuseTex; }; #endif//MESH_H void Mesh::setupMesh() { //创建VBO和VAO对象,并赋予ID m_glFuns->glGenVertexArrays(1, &VAO); m_glFuns->glGenBuffers(1, &VBO); m_glFuns->glGenBuffers(1,&EBO); //绑定VBO和VAO对象 m_glFuns->glBindVertexArray(VAO); m_glFuns->glBindBuffer(GL_ARRAY_BUFFER, VBO); //为当前绑定到target的缓冲区对象创建一个新的数据存储。 //如果data不是NULL,则使用来自此指针的数据初始化数据存储 m_glFuns->glBufferData(GL_ARRAY_BUFFER, vertices.size()*sizeof(Vertex), &vertices[0], GL_STATIC_DRAW); m_glFuns->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); m_glFuns->glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int),&indices[0], GL_STATIC_DRAW); //告知显卡如何解析缓冲里的属性值 m_glFuns->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0); m_glFuns->glEnableVertexAttribArray(0); m_glFuns->glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Normal)); m_glFuns->glEnableVertexAttribArray(1); m_glFuns->glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, TexCoords)); m_glFuns->glEnableVertexAttribArray(2); }
06-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值