Professional Documents
Culture Documents
Varios Programas de Practica en OpenGL
Varios Programas de Practica en OpenGL
With the vertex data defined we’d like to send it as input to the first process of the graphics pipeline: the vertex
shader. This is done by creating memory on the GPU where we store the vertex data, configure how OpenGL
should interpret the memory and specify how to send the data to the graphics card. The vertex shader then
processes as much vertices as we tell it to from its memory. We manage this memory via so called vertex
buffer objects (VBO) that can store a large number of vertices in the GPU’s memory. The advantage of using
those buffer objects is that we can send large batches of data all at once to the graphics card, and keep it there
if there’s enough memory left, without having to send data one vertex at a time. Sending data to the graphics
card from the CPU is relatively slow, so wherever we can we try to send as much data as possible at once. Once
the data is in the graphics card’s memory the vertex shader has almost instant access to the vertices making it
extremely fast.
A vertex array object (also known as VAO) can be bound just like a vertex buffer object and any subsequent
vertex attribute calls from that point on will be stored inside the VAO. This has the advantage that when
configuring vertex attribute pointers you only have to make those calls once and whenever we want to draw
the object, we can just bind the corresponding VAO. This makes switching between different vertex data and
attribute configurations as easy as binding a different VAO. All the state we just set is stored inside the VAO.
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
// settings
"void main()\n"
"{\n"
" gl_Position = vec4(aPos, 1.0);\n" //Podemos usar interpolacion de sombreado poniendo los
siguientes valores:aPos.x, -aPos.y, aPos.z, 1.0
"}\0";
"void main()\n"
"{\n"
"}\n\0";
int main()
// ------------------------------
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
// --------------------
if (window == NULL)
glfwTerminate();
return -1;
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
// ---------------------------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
return -1;
// ------------------------------------
// vertex shader
int success;
char infoLog[512];
if (!success)
// fragment shader
glCompileShader(fragmentShader);
if (!success)
// link shaders
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
if (!success) {
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
// ------------------------------------------------------------------
float vertices[] = {
// posiciones // colores
};
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then
configure vertex attributes(s).
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
// position attribute
glEnableVertexAttribArray(0);
// color attribute
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 *
sizeof(float)));
glEnableVertexAttribArray(1);
// as we only have a single shader, we could also just activate our shader once beforehand
if we want to
glUseProgram(shaderProgram);
// render loop
// -----------
while (!glfwWindowShouldClose(window))
// input
// -----
processInput(window);
// render
// ------
glClear(GL_COLOR_BUFFER_BIT);
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 3);
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
glfwSwapBuffers(window);
glfwPollEvents();
}
// optional: de-allocate all resources once they've outlived their purpose:
// ------------------------------------------------------------------------
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteProgram(shaderProgram);
// ------------------------------------------------------------------
glfwTerminate();
return 0;
// process all input: query GLFW whether relevant keys are pressed/released this frame and
react accordingly
//
-----------------------------------------------------------------------------------------------
----------
glfwSetWindowShouldClose(window, true);
// glfw: whenever the window size changed (by OS or user resize) this callback function
executes
//
---------------------------------------------------------------------------------------------
// make sure the viewport matches the new window dimensions; note that width and
/*TRIANGULOS ANIDADOS*/
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
// settings
"void main()\n"
"{\n"
"}\0";
"void main()\n"
"{\n"
"}\n\0";
int main()
// ------------------------------
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
// --------------------
if (window == NULL)
glfwTerminate();
return -1;
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
// ---------------------------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
// ------------------------------------
// vertex shader
glCompileShader(vertexShader);
int success;
char infoLog[512];
if (!success)
// fragment shader
glCompileShader(fragmentShader);
if (!success)
// link shaders
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
if (!success) {
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
// ------------------------------------------------------------------
// add a new set of vertices to form a second triangle (a total of 6 vertices); the vertex
attribute configuration remains the same (still one 3-float position vector per vertex)
float vertices[] = {
// first triangle
// second triangle
};
unsigned int VBO, VAO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then
configure vertex attributes(s).
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glEnableVertexAttribArray(0);
// note that this is allowed, the call to glVertexAttribPointer registered VBO as the
vertex attribute's bound vertex buffer object so afterwards we can safely unbind
glBindBuffer(GL_ARRAY_BUFFER, 0);
// You can unbind the VAO afterwards so other VAO calls won't accidentally modify this VAO,
but this rarely happens. Modifying other
// VAOs requires a call to glBindVertexArray anyways so we generally don't unbind VAOs (nor
VBOs) when it's not directly necessary.
glBindVertexArray(0);
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
// render loop
// -----------
while (!glfwWindowShouldClose(window))
{
// input
// -----
processInput(window);
// render
// ------
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(shaderProgram);
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
glfwSwapBuffers(window);
glfwPollEvents();
// ------------------------------------------------------------------------
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteProgram(shaderProgram);
glfwTerminate();
return 0;
// process all input: query GLFW whether relevant keys are pressed/released this frame and
react accordingly
//
-----------------------------------------------------------------------------------------------
----------
glfwSetWindowShouldClose(window, true);
// glfw: whenever the window size changed (by OS or user resize) this callback function
executes
//
---------------------------------------------------------------------------------------------
// make sure the viewport matches the new window dimensions; note that width and
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
// settings
"void main()\n"
"{\n"
"}\0";
"void main()\n"
"{\n"
"}\n\0";
"void main()\n"
"{\n"
"}\n\0";
int main()
{
// ------------------------------
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
// --------------------
if (window == NULL)
glfwTerminate();
return -1;
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
// ---------------------------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
// ------------------------------------
// we skipped compile log checks this time for readability (if you do encounter issues, add
the compile-checks! see previous code samples)
glCompileShader(vertexShader);
glCompileShader(fragmentShaderOrange);
glCompileShader(fragmentShaderYellow);
glAttachShader(shaderProgramOrange, vertexShader);
glAttachShader(shaderProgramOrange, fragmentShaderOrange);
glLinkProgram(shaderProgramOrange);
// then link the second program object using a different fragment shader (but same vertex
shader)
// this is perfectly allowed since the inputs and outputs of both the vertex and fragment
shaders are equally matched.
glAttachShader(shaderProgramYellow, vertexShader);
glAttachShader(shaderProgramYellow, fragmentShaderYellow);
glLinkProgram(shaderProgramYellow);
// set up vertex data (and buffer(s)) and configure vertex attributes
// ------------------------------------------------------------------
float firstTriangle[] = {
};
float secondTriangle[] = {
};
glGenVertexArrays(2, VAOs); // we can also generate multiple VAOs or buffers at the same
time
glGenBuffers(2, VBOs);
// --------------------
glBindVertexArray(VAOs[0]);
glBindBuffer(GL_ARRAY_BUFFER, VBOs[0]);
glEnableVertexAttribArray(0);
// ---------------------
glEnableVertexAttribArray(0);
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
// render loop
// -----------
while (!glfwWindowShouldClose(window))
// input
// -----
processInput(window);
// render
// ------
glClear(GL_COLOR_BUFFER_BIT);
// now when we draw the triangle we first use the vertex and orange fragment shader
from the first program
glUseProgram(shaderProgramOrange);
// draw the first triangle using the data from our first VAO
glBindVertexArray(VAOs[0]);
// then we draw the second triangle using the data from the second VAO
// when we draw the second triangle we want to use a different shader program so we
switch to the shader program with our yellow fragment shader.
glUseProgram(shaderProgramYellow);
glBindVertexArray(VAOs[1]);
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
glfwSwapBuffers(window);
glfwPollEvents();
// ------------------------------------------------------------------------
glDeleteVertexArrays(2, VAOs);
glDeleteBuffers(2, VBOs);
glDeleteProgram(shaderProgramOrange);
glDeleteProgram(shaderProgramYellow);
// ------------------------------------------------------------------
glfwTerminate();
return 0;
// process all input: query GLFW whether relevant keys are pressed/released this frame and
react accordingly
//
-----------------------------------------------------------------------------------------------
----------
// glfw: whenever the window size changed (by OS or user resize) this callback function
executes
//
---------------------------------------------------------------------------------------------
// make sure the viewport matches the new window dimensions; note that width and
/*
*/
void initGL() {
/* Handler for window-repaint event. Call back when the window first appears and
glVertex2f(-0.2f, 0.7f);
glVertex2f(-0.8f, 0.7f);
glVertex2f(-0.7f, -0.6f);
glVertex2f(-0.1f, -0.6f);
glVertex2f(-0.1f, 0.0f);
glVertex2f(-0.7f, 0.0f);
glVertex2f(-0.9f, -0.7f);
glVertex2f(-0.5f, -0.7f);
glVertex2f(-0.5f, -0.3f);
glVertex2f(-0.9f, -0.3f);
glEnd();
glVertex2f(0.7f, -0.6f);
glVertex2f(0.4f, -0.1f);
glVertex2f(0.3f, -0.4f);
glVertex2f(0.9f, -0.4f);
glVertex2f(0.6f, -0.9f);
glEnd();
glVertex2f(0.4f, 0.2f);
glVertex2f(0.6f, 0.2f);
glVertex2f(0.7f, 0.4f);
glVertex2f(0.6f, 0.6f);
glVertex2f(0.4f, 0.6f);
glVertex2f(0.3f, 0.4f);
glEnd();
glutCreateWindow("Vertex, Primitive & Color"); // Create window with the given title
glutInitWindowSize(1000, 800); // Set the window's initial width & height
return 0;
// Global variable
void initGL() {
void idle() {
}
/* Handler for window-repaint event. Call back when the window first appears and
void display() {
glVertex2f(-0.3f, -0.3f);
glVertex2f(0.3f, -0.3f);
glVertex2f(0.3f, 0.3f);
glVertex2f(-0.3f, 0.3f);
glEnd();
glBegin(GL_QUADS);
glVertex2f(-0.3f, -0.3f);
glVertex2f(0.3f, -0.3f);
glVertex2f(0.3f, 0.3f);
glVertex2f(-0.3f, 0.3f);
glEnd();
glBegin(GL_QUADS);
glVertex2f(-0.2f, -0.2f);
glVertex2f(0.2f, -0.2f);
glVertex2f(0.2f, 0.2f);
glVertex2f(-0.2f, 0.2f);
glEnd();
glBegin(GL_TRIANGLES);
glVertex2f(-0.3f, -0.2f);
glVertex2f(0.3f, -0.2f);
glVertex2f(0.0f, 0.3f);
glEnd();
glBegin(GL_TRIANGLES);
glVertex2f(-0.3f, -0.2f);
glVertex2f(0.3f, -0.2f);
glVertex2f(0.0f, 0.3f);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(-0.1f, -0.2f);
glVertex2f(0.1f, -0.2f);
glVertex2f(0.2f, 0.0f);
glVertex2f(0.1f, 0.2f);
glVertex2f(-0.1f, 0.2f);
glVertex2f(-0.2f, 0.0f);
glEnd();
angle += 0.9f;
angle1 -= 0.5f;
angle2 += 1.5f;
/* Handler for window re-size event. Called back when the window first appears and
whenever the window is re-sized with its new width and height */
// Set the aspect ratio of the clipping area to match the viewport
glLoadIdentity();
else {
glutInitWindowSize(640, 480); // Set the window's initial width & height - non-square
glutCreateWindow("Animation via Idle Function"); // Create window with the given title
return 0;
#define PI 3.14159265f
// Global variables
void initGL() {
void display() {
glBegin(GL_TRIANGLE_FAN);
GLfloat angle;
for (int i = 0; i <= numSegments; i++) { // Last vertex same as first vertex
glEnd();
glutSwapBuffers(); // Swap front and back buffers (of double buffered mode)
ballX += xSpeed;
ballY += ySpeed;
ballX = ballXMax;
xSpeed = -xSpeed;
ballX = ballXMin;
xSpeed = -xSpeed;
ballY = ballYMax;
ySpeed = -ySpeed;
ballY = ballYMin;
ySpeed = -ySpeed;
}
/* Call back when the windows is re-sized */
// Set the aspect ratio of the clipping area to match the viewport
clipAreaYBottom = -1.0;
clipAreaYTop = 1.0;
else {
clipAreaXLeft = -1.0;
clipAreaXRight = 1.0;
switch (key) {
windowPosY = glutGet(GLUT_WINDOW_Y);
windowWidth = glutGet(GLUT_WINDOW_WIDTH);
windowHeight = glutGet(GLUT_WINDOW_HEIGHT);
break;
}
/* Main function: GLUT runs as a console application starting at main() */
return 0;
/*AUMENTO DE VELOCIDAD CONTROLADO POR LAS TECLAS DIRECCIONALES PARA UNA PELOTA*/
#define PI 3.14159265f
// Global variables
void initGL() {
void display() {
glBegin(GL_TRIANGLE_FAN);
GLfloat angle;
for (int i = 0; i <= numSegments; i++) { // Last vertex same as first vertex
glEnd();
glutSwapBuffers(); // Swap front and back buffers (of double buffered mode)
ballX += xSpeed;
ballY += ySpeed;
ballX = ballXMax;
xSpeed = -xSpeed;
ballX = ballXMin;
xSpeed = -xSpeed;
ballY = ballYMax;
ySpeed = -ySpeed;
ballY = ballYMin;
ySpeed = -ySpeed;
}
// Set the aspect ratio of the clipping area to match the viewport
clipAreaYBottom = -1.0;
clipAreaYTop = 1.0;
else {
clipAreaXLeft = -1.0;
clipAreaXRight = 1.0;
switch (key) {
exit(0);
break;
switch (key) {
windowPosY = glutGet(GLUT_WINDOW_Y);
windowWidth = glutGet(GLUT_WINDOW_WIDTH);
windowHeight = glutGet(GLUT_WINDOW_HEIGHT);
break;
ballRadius *= 1.05f;
break;
ballRadius *= 0.95f;
break;
}
return 0;
/*CONTROL DE VELOCIDAD CON TECLAS DIRECCIONALES Y USO DEL MOUSE CON LA FUNCION CLICK DEL BOTON
DE DERECHO*/
#define PI 3.14159265f
// Global variables
char title[] = "Full-Screen & Windowed Mode"; // Windowed mode's title
void initGL() {
void display() {
glBegin(GL_TRIANGLE_FAN);
GLfloat angle;
for (int i = 0; i <= numSegments; i++) { // Last vertex same as first vertex
glEnd();
glutSwapBuffers(); // Swap front and back buffers (of double buffered mode)
ballX += xSpeed;
ballY += ySpeed;
ballX = ballXMax;
xSpeed = -xSpeed;
ballX = ballXMin;
xSpeed = -xSpeed;
}
ballY = ballYMax;
ySpeed = -ySpeed;
ballY = ballYMin;
ySpeed = -ySpeed;
// Set the aspect ratio of the clipping area to match the viewport
clipAreaYBottom = -1.0;
clipAreaYTop = 1.0;
}
else {
clipAreaXLeft = -1.0;
clipAreaXRight = 1.0;
switch (key) {
exit(0);
break;
switch (key) {
windowPosY = glutGet(GLUT_WINDOW_Y);
windowWidth = glutGet(GLUT_WINDOW_WIDTH);
windowHeight = glutGet(GLUT_WINDOW_HEIGHT);
break;
ballRadius *= 1.05f;
ballRadius *= 0.95f;
break;
if (paused) {
ySpeedSaved = ySpeed;
ySpeed = 0;
else {
ySpeed = ySpeedSaved;
return 0;