0% found this document useful (0 votes)
62 views2 pages

OpenGL Triangle Rendering Guide

The code defines vertex position data and generates and binds a buffer object to store it. It creates a shader program from vertex and fragment shader sources. In a render loop, it clears the screen, draws triangles using the shader and buffer data, and swaps buffers at each frame.

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)
62 views2 pages

OpenGL Triangle Rendering Guide

The code defines vertex position data and generates and binds a buffer object to store it. It creates a shader program from vertex and fragment shader sources. In a render loop, it clears the screen, draws triangles using the shader and buffer data, and swaps buffers at each frame.

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

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;
}

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

You might also like