diff --git a/Debug/Window_practice.exe b/Debug/Window_practice.exe index 64a5dcf..ca32b66 100644 Binary files a/Debug/Window_practice.exe and b/Debug/Window_practice.exe differ diff --git a/Debug/Window_practice.pdb b/Debug/Window_practice.pdb index 069b753..31d9684 100644 Binary files a/Debug/Window_practice.pdb and b/Debug/Window_practice.pdb differ diff --git a/Window_practice/Debug/Main.obj b/Window_practice/Debug/Main.obj index 8e814e9..80b5237 100644 Binary files a/Window_practice/Debug/Main.obj and b/Window_practice/Debug/Main.obj differ diff --git a/Window_practice/Debug/Window_practice.ilk b/Window_practice/Debug/Window_practice.ilk index af40635..6642736 100644 Binary files a/Window_practice/Debug/Window_practice.ilk and b/Window_practice/Debug/Window_practice.ilk differ diff --git a/Window_practice/Debug/Window_practice.tlog/link.read.1.tlog b/Window_practice/Debug/Window_practice.tlog/link.read.1.tlog index 6a526e9..d98202f 100644 Binary files a/Window_practice/Debug/Window_practice.tlog/link.read.1.tlog and b/Window_practice/Debug/Window_practice.tlog/link.read.1.tlog differ diff --git a/Window_practice/Debug/vc142.idb b/Window_practice/Debug/vc142.idb index bb3b281..bf69fe7 100644 Binary files a/Window_practice/Debug/vc142.idb and b/Window_practice/Debug/vc142.idb differ diff --git a/Window_practice/Debug/vc142.pdb b/Window_practice/Debug/vc142.pdb index 8f50eb9..f17e1ba 100644 Binary files a/Window_practice/Debug/vc142.pdb and b/Window_practice/Debug/vc142.pdb differ diff --git a/Window_practice/src/Main.cpp b/Window_practice/src/Main.cpp index 9cd0966..f290d48 100644 --- a/Window_practice/src/Main.cpp +++ b/Window_practice/src/Main.cpp @@ -2,6 +2,50 @@ #include #include +static unsigned int CompileShader(unsigned int type, const std::string& source) { + unsigned int id = glCreateShader(type); + const char* src = source.c_str(); + glShaderSource(id, 1, &src, nullptr); + glCompileShader(id); + + //Error handling + int result; + glGetShaderiv(id, GL_COMPILE_STATUS, &result); + + if (result == GL_FALSE) { + int length; + glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length); + char* message = (char*)_malloca(length * sizeof(char)); //_malloca is more secure than alloca + glGetShaderInfoLog(id, length, &length, message); + + std::cout << "Failed to compile " << + (type == GL_VERTEX_SHADER ? "vertex" : "fragment") << " shader" << std::endl; + std::cout << message << std::endl; + + glDeleteShader(id); + + return 0; + } + + return id; +} + +static unsigned int CreateShader(const std::string& vertexShader, const std::string& fragmentShader) { + unsigned int program = glCreateProgram(); + unsigned int vs = CompileShader(GL_VERTEX_SHADER, vertexShader); + unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader); + + glAttachShader(program, vs); + glAttachShader(program, fs); + glLinkProgram(program); + glValidateProgram(program); + + glDeleteShader(vs); + glDeleteShader(fs); + + return program; +} + int main(void){ // GLFW init if (!glfwInit()) { @@ -45,6 +89,28 @@ int main(void){ glEnableVertexAttribArray(0); glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0); + std::string vertexShader = + "#version 330 core\n" + "\n" + "layout(location = 0) in vec4 position;\n" + "\n" + "void main(){\n" + " gl_Position = position;\n" + "}\n"; + + std::string fragmentShader = + "#version 330 core\n" + "\n" + "layout(location = 0) out vec4 color;\n" + "\n" + "void main(){\n" + " color = vec4(1.0, 0.0, 0.0, 1.0);\n" + "}\n"; + + + unsigned int shader = CreateShader(vertexShader, fragmentShader); + glUseProgram(shader); + glBindBuffer(GL_ARRAY_BUFFER, 0); //unbinding the buffer //Main loop @@ -61,6 +127,8 @@ int main(void){ glfwPollEvents(); } + glDeleteProgram(shader); + glfwTerminate(); return 0; } \ No newline at end of file