0% found this document useful (0 votes)
64 views

Static Int Unsigned Int Const Unsigned Int Const Char Nullptr

This C++ code defines functions for compiling and linking OpenGL shaders from strings. The CompileShader function compiles a shader, checks for errors, and returns the shader ID. The CreateShader function calls CompileShader, attaches the shaders to a program, links it, and returns the program ID. It is used in the main function to create and use a shader program to render a triangle primitive to the screen.

Uploaded by

iDenis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

Static Int Unsigned Int Const Unsigned Int Const Char Nullptr

This C++ code defines functions for compiling and linking OpenGL shaders from strings. The CompileShader function compiles a shader, checks for errors, and returns the shader ID. The CreateShader function calls CompileShader, attaches the shaders to a program, links it, and returns the program ID. It is used in the main function to create and use a shader program to render a triangle primitive to the screen.

Uploaded by

iDenis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

static 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);

int result;
glGetShaderiv(id, GL_COMPILE_STATUS, &result);
if (result == GL_FALSE) {
int length;
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
char* message = (char*)malloc(length * sizeof(char));
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 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)
{
GLFWwindow* window;

/* Initialize the library */


if (!glfwInit())
return -1;

/* Create a windowed mode window and its OpenGL context */


window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}

/* Make the window's context current */


glfwMakeContextCurrent(window);

if (glewInit() != GLEW_OK) {
std::cout << "Error!";
}

std::cout << glGetString(GL_VERSION) << std::endl;

/*
float positions[] = {
-0.5f, -0.5f,
-0.5f, 0.5f,
0.5f, -0.5f

-0.5f, 0.5f,
0.5f, 0.5f,
0.5f, -0.5f
};
*/

float positions[] = {
0.0f, 0.5f,
-0.5f, -0.5f,
0.5f, -0.5f
};

unsigned int indices[] = {


0, 1, 3,
1, 2, 3
};

unsigned int buffer;


glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), positions, GL_STATIC_DRAW);

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), 0);

ShaderProgramSource source = ParseShader("res/shaders/Basic.shader");


std::cout << source.VertexSource << std::endl;
std::cout << source.FragmentSource << std::endl;

unsigned int shader = CreateShader(source.VertexSource, source.FragmentSource);


glUseProgram(shader);

/* Loop until the user closes the window */


while (!glfwWindowShouldClose(window))
{
/* Render here */
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);

/*
glBegin(GL_TRIANGLES);
glVertex2f(-0.5f, -0.5f);
glVertex2f( 0.0f, 0.5f);
glVertex2f( 0.5f, -0.5f);
glEnd();
*/
glDrawArrays(GL_TRIANGLES, 0, 3);

/* Swap front and back buffers */


glfwSwapBuffers(window);

/* Poll for and process events */


glfwPollEvents();
}

glDeleteProgram(shader);

glfwTerminate();
return 0;
}

You might also like