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

Initialize The Library /: NT Void

The code initializes an OpenGL context and window using GLFW. It defines vertex positions for a triangle and stores them in a buffer. Shaders are loaded from files and applied. The main loop renders the triangle by drawing arrays and swapping buffers before polling for events.

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)
26 views

Initialize The Library /: NT Void

The code initializes an OpenGL context and window using GLFW. It defines vertex positions for a triangle and stores them in a buffer. Shaders are loaded from files and applied. The main loop renders the triangle by drawing arrays and swapping buffers before polling for events.

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/ 2

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