[go: up one dir, main page]

0% found this document useful (0 votes)
25 views7 pages

Exposicion de Graficos

The document is a laboratory report for a graphics programming course at the Universidad Nacional de Ingeniería, detailing a C++ OpenGL program. It includes code for compiling shaders, creating a window, and rendering polygons with different colors. The program initializes OpenGL, sets up vertex data, and enters a loop to render shapes until the window is closed.

Uploaded by

Ultron
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)
25 views7 pages

Exposicion de Graficos

The document is a laboratory report for a graphics programming course at the Universidad Nacional de Ingeniería, detailing a C++ OpenGL program. It includes code for compiling shaders, creating a window, and rendering polygons with different colors. The program initializes OpenGL, sets up vertex data, and enters a loop to render shapes until the window is closed.

Uploaded by

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

Universidad nacional de ingeniería

Recinto universitario Simón Bolívar


RUSB

Programación Grafica
Laboratorio #2

Estudiante: Baltodano Villareyna Enyel Antonio


#2023-0809U
3T1-COM-S

Fecha: 02/04/25
Codigo:

#include <GL/glew.h> #include <GLFW/glfw3.h>

#include

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

int result;
glGetShaderiv(id, GL_COMPILE_STATUS, &result);
if (result == GL_FALSE) {
int length;
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
char* message = (char*)alloca(length * sizeof(char));
glGetShaderInfoLog(id, length, &length, message);
std::cout << "Fallo al compilar el sombreado de la figura"
<<
(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);

/*Combinando vertexShader con fragmentShader*/


glAttachShader(program, vs);
glAttachShader(program, fs);
glLinkProgram(program); /*Realizando la union*/
glValidateProgram(program); /*Validando el resultado final*/

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(800, 600, "Mi Primera Pantalla en
OpenGL", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}

/* Make the window's context current */


glfwMakeContextCurrent(window);

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

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

/*Declaramos un arreglo de posiciones para los vertices*/

float positions[] = {
//figura 1
-0.295f, -0.335f,
-0.735f, -0.275f,
-0.85f, -0.62f,
-0.415f, -0.635f,
//figura 2
-0.62f, 0.19f,
-0.495f, 0.535f,
-0.59f, 0.895f,
-0.001f, 0.565f,
//figura3
0.62f,0.19f,
0.73f,0.325f,
0.72f,0.68f,
0.5f,0.87f,
0.215f,0.675f,
0.235f,0.325f,
//figura4
0.67f,-0.855f,
0.855f,-0.5f,
0.555f,-0.24f,
0.135f,-0.575f,
0.031f,-0.865f,
0.6f,-0.3f,

};

/*Establecemos parámetros de Buffer para almacenar los datos*/

unsigned int buffer;


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

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

std::string vertexShader =
"#version 330 core\n"
"layout(location = 0) in vec4 position;\n"
"void main()\n"
"{\n"
" gl_Position = position;\n"
"}\n";

std::string fragmentShader =
"#version 330 core\n"
"layout(location = 0) out vec4 color;\n"
"uniform vec4 u_Color;\n" // Nuevo uniform para el color
"void main()\n"
"{\n"
" color = u_Color;\n"
"}\n";

unsigned int shader = CreateShader(vertexShader,


fragmentShader);
glUseProgram(shader);
int color = glGetUniformLocation(shader, "u_Color");
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);

glUniform4f(color, 0.0f, 0.0f, 1.0f, 1.0f);


glDrawArrays(GL_POLYGON, 0, 4);

glUniform4f(color, 1.0f, 0.0f, 0.0f, 1.0f);


glDrawArrays(GL_POLYGON, 4, 4);

glUniform4f(color, 1.0f, 0.0f, 0.0f, 1.0f);


glDrawArrays(GL_POLYGON, 9, 6);

glUniform4f(color, 1.0f, 0.0f, 25.0f, 1.0f);


glDrawArrays(GL_POLYGON, 15, 6);

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

/* Swap front and back buffers */


glfwSwapBuffers(window);

/* Poll for and process events */


glfwPollEvents();
}

glfwTerminate();
return 0;
}

You might also like