|
|
@@ -8,70 +8,7 @@
|
|
|
#include "IndexBuffer.h"
|
|
|
#include "VertexArray.h"
|
|
|
#include "VertexBufferLayout.h"
|
|
|
-
|
|
|
-static GLuint CompiledShader(const std::string& source, GLenum inType) {
|
|
|
- GLuint id = glCreateShader(inType);
|
|
|
- const char* src = source.c_str();
|
|
|
- glShaderSource(id, 1, &src, nullptr);
|
|
|
- glCompileShader(id);
|
|
|
-
|
|
|
- // Shader 错误处理
|
|
|
- GLint result;
|
|
|
- glGetShaderiv(id, GL_COMPILE_STATUS, &result);
|
|
|
- if (result == GL_FALSE) {
|
|
|
- int length;
|
|
|
- glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
|
|
|
- // alloca 在栈上申请内存,不需要 free ,在作用域结束后自动释放
|
|
|
- char* msg = (char*)alloca(length * sizeof(char));
|
|
|
- glGetShaderInfoLog(id, length, &length, msg);
|
|
|
- std::cout << "Shader Compile " << (inType == GL_VERTEX_SHADER ? "vertex shader" : "fragment shader") << " Fail" << std::endl;
|
|
|
- std::cout << msg << std::endl;
|
|
|
-
|
|
|
- glDeleteShader(id);
|
|
|
- return GL_ZERO;
|
|
|
- }
|
|
|
-
|
|
|
- return id;
|
|
|
-}
|
|
|
-
|
|
|
-static unsigned int CreateShader(const std::string& vertexShader, const std::string& fragmentShader) {
|
|
|
- GLuint program = glCreateProgram();
|
|
|
- GLuint vs = CompiledShader(vertexShader, GL_VERTEX_SHADER);
|
|
|
- GLuint fs = CompiledShader(fragmentShader, GL_FRAGMENT_SHADER);
|
|
|
-
|
|
|
- glAttachShader(program, vs); // 绑定顶点着色器
|
|
|
- glAttachShader(program, fs); // 绑定片段着色器
|
|
|
- glLinkProgram(program); // 链接程序,将所有着色器合并为一个可执行的程序
|
|
|
- glValidateProgram(program); // 验证程序对象是否可以在当前的 OpenGL 状态下执行
|
|
|
-
|
|
|
- glDeleteShader(fs); // 删除着色器对象 因为一旦着色器被链接到程序对象,着色器的代码已经被链接到程序中,所以可以安全地删除着色器对象
|
|
|
- glDeleteShader(vs);
|
|
|
-
|
|
|
- return program;
|
|
|
-}
|
|
|
-
|
|
|
-static GLuint CreateShaderWithFile(const std::string& vertexShaderFilePath, const std::string& fragmentShaderFilePath) {
|
|
|
- std::ifstream ifs;
|
|
|
- ifs.open(vertexShaderFilePath, std::ios::in);
|
|
|
- if (!ifs.is_open()) {
|
|
|
- std::cout << "Compile Shader Fail: Can't Open File" << std::endl;
|
|
|
- return GL_ZERO;
|
|
|
- }
|
|
|
-
|
|
|
- std::string vertextShaderSrouce((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
|
|
|
- ifs.close();
|
|
|
-
|
|
|
- ifs.open(fragmentShaderFilePath, std::ios::in);
|
|
|
- if (!ifs.is_open()) {
|
|
|
- std::cout << "Compile Shader Fail: Can't Open File" << std::endl;
|
|
|
- return GL_ZERO;
|
|
|
- }
|
|
|
-
|
|
|
- std::string fragmentShaderSource((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
|
|
|
- ifs.close();
|
|
|
-
|
|
|
- return CreateShader(vertextShaderSrouce, fragmentShaderSource);
|
|
|
-}
|
|
|
+#include "Shader.h"
|
|
|
|
|
|
int main(void)
|
|
|
{
|
|
|
@@ -127,19 +64,13 @@ int main(void)
|
|
|
|
|
|
IndexBuffer ibo(indeices, 6);
|
|
|
|
|
|
- GLuint shader = CreateShaderWithFile("src/Vertex.vert", "src/Fragment.frag");
|
|
|
- glUseProgram(shader);
|
|
|
-
|
|
|
- GLint location = -1;
|
|
|
- GL_CALL(location = glGetUniformLocation(shader, "u_Color"));
|
|
|
- GL_CALL(glUniform4f(location, 1, 1, 0, 0));
|
|
|
+ auto shader = Shader("src/Vertex.vert", "src/Fragment.frag");
|
|
|
|
|
|
// 清除所有绑定关系
|
|
|
- glBindVertexArray(0);
|
|
|
- glUseProgram(0);
|
|
|
- glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
|
|
-
|
|
|
+ va.Unbind();
|
|
|
+ shader.Unbind();
|
|
|
+ vb.UnBind();
|
|
|
+ ibo.Unbind();
|
|
|
|
|
|
GLfloat r = 0.0f;
|
|
|
GLfloat increment = 0.05f;
|
|
|
@@ -149,17 +80,12 @@ int main(void)
|
|
|
/* Render here */
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
|
|
- //glDrawArrays(GL_TRIANGLES, 0, 6);
|
|
|
- //GL_CLEAR_ERROR;
|
|
|
- //glDrawElements(GL_TRIANGLES, 6, GL_UNIFORM, 0);
|
|
|
- //GL_CHECK_ERROR;
|
|
|
-
|
|
|
va.Bind();
|
|
|
ibo.Bind();
|
|
|
|
|
|
r += increment;
|
|
|
- GL_CALL(glUseProgram(shader));
|
|
|
- GL_CALL(glUniform4f(location, r, .5f, .5f, 1.0f));
|
|
|
+ shader.Bind();
|
|
|
+ shader.SetUniform4f("u_Color", r, .5f, .5f, 1.0f);
|
|
|
|
|
|
if (r > 1.0f || r < 0.0f) {
|
|
|
increment *= -1;
|
|
|
@@ -173,7 +99,6 @@ int main(void)
|
|
|
/* Poll for and process events */
|
|
|
glfwPollEvents();
|
|
|
}
|
|
|
- glDeleteProgram(shader);
|
|
|
}
|
|
|
glfwTerminate();
|
|
|
return 0;
|