Created Vertex and Fragment Shader
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -2,6 +2,50 @@
|
|||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
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){
|
int main(void){
|
||||||
// GLFW init
|
// GLFW init
|
||||||
if (!glfwInit()) {
|
if (!glfwInit()) {
|
||||||
@@ -45,6 +89,28 @@ int main(void){
|
|||||||
glEnableVertexAttribArray(0);
|
glEnableVertexAttribArray(0);
|
||||||
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 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
|
glBindBuffer(GL_ARRAY_BUFFER, 0); //unbinding the buffer
|
||||||
|
|
||||||
//Main loop
|
//Main loop
|
||||||
@@ -61,6 +127,8 @@ int main(void){
|
|||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
glDeleteProgram(shader);
|
||||||
|
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user