Skip to content

PEN_2_2

Francisco Zavala edited this page Jul 25, 2024 · 2 revisions

Constant Definitions and Global Variables:

#define numVAOs 1: Defines the number of Vertex Array Objects (VAOs) to be used in the program. In this case, only one is used.

GLuint renderingProgram; Global variable to store the ID of the shader program that will be used for rendering.

GLuint vao[numVAOs]; Array to store the IDs of the VAOs. In this case, the array has a single element, corresponding to the number of VAOs defined.

Function to Create a Shader Program:

GLuint glCreateShaderProgram() { ... } This function creates a shader program in OpenGL.

  • const char *vshaderSource = "..."; Contains the source code for the vertex shader. In this case, the shader simply assigns a fixed position to all vertices.

  • const char *fshaderSource = "..."; Contains the source code for the fragment shader. Here, the shader assigns a blue color to all fragments.

  • GLuint vShader = glCreateShader(GL_VERTEX_SHADER); Creates a shader object for the vertex shader.

  • GLuint fShader = glCreateShader(GL_FRAGMENT_SHADER); Creates a shader object for the fragment shader.

  • glShaderSource(vShader, 1, &vshaderSource, NULL); Attaches the vertex shader source code to the shader object.

  • glShaderSource(fShader, 1, &fshaderSource, NULL); Attaches the fragment shader source code to the shader object.

  • glCompileShader(vShader); Compiles the vertex shader.

  • glCompileShader(fShader); Compiles the fragment shader.

  • GLint vfProgram = glCreateProgram(); Creates a shader program.

  • glAttachShader(vfProgram, vShader); Attaches the vertex shader to the shader program.

  • glAttachShader(vfProgram, fShader); Attaches the fragment shader to the shader program.

  • glLinkProgram(vfProgram); Links the shader program, combining the shaders into a single executable program.

  • return vfProgram; Returns the ID of the created shader program.

Initialization Function:

void init(GLFWwindow *window) { ... } This function handles the initialization of the program.

  • renderingProgram = glCreateShaderProgram(); Calls the glCreateShaderProgram function to create a shader program and stores the ID in the renderingProgram variable.

  • glGenVertexArrays(numVAOs, vao); Generates the VAOs specified by numVAOs and stores their IDs in the vao array.

  • glBindVertexArray(vao[0]); Binds the first VAO from the array to the OpenGL state machine.

Function to Draw the Scene:

void displaw(GLFWwindow* window, double currentTime) { ... } This function handles rendering the scene.

  • glUseProgram(renderingProgram); Activates the shader program for rendering.

  • glPointSize(50.0f); Sets the size of the point to be drawn.

  • glDrawArrays(GL_POINTS, 0, 1); Draws a point using the data from the current VAO.

Enable V-Sync:

glfwSwapInterval(1); Enables vertical synchronization (V-Sync), which limits the window's update rate to the monitor's refresh rate. This helps to avoid screen tearing.

Main Loop:

while (!glfwWindowShouldClose(window)) { ... } Loop that runs until the window is closed.

  • displaw(window, glfwGetTime()); Calls the displaw function to render the scene.

  • glfwSwapBuffers(window); Swaps the window's buffers, showing the updated content.

programa_2_2

program_2_6

Clone this wiki locally