Skip to content

PEN_2_4

Francisco Zavala edited this page Sep 7, 2024 · 2 revisions

Function to read shader source code from a file:

std::string readShaderSource(const char *filePath); This function reads the content of the file specified by filePath and returns the shader source code as a string.

  • Parameters:

  • filePath: A character string representing the path to the file containing the shader source code.

  • Return:

Returns a std::string containing the shader source code.

  • Detailed description:

  • First, an empty string content is initialized to store the file content.

  • The file is opened using std::ifstream in input mode (std::ios::in).

  • If the file cannot be opened, an error message is printed and an empty string is returned.

  • The file is read line by line using getline and the content is accumulated in the content string, adding a newline at the end of each line.

  • Finally, the file is closed and the read content is returned.

Function to create a shader program:

GLuint createShaderProgram(); This function creates a shader program, compiling and linking a vertex shader and a fragment shader, and returns the program identifier.

  • Return:

Returns a GLuint which is the identifier of the created shader program.

  • Detailed description:

  • The source codes of the vertex and fragment shaders are read from the files ./vertex_shader4.glsl and ./fragment_shader4.glsl using the readShaderSource function.

  • The character strings (const char*) are obtained from the read contents.

  • Vertex and fragment shaders are created using glCreateShader.

  • The shader source code is set using glShaderSource.

  • Shaders are compiled using glCompileShader.

  • If there were compilation errors, the error logs are printed using checkOpenGLError and printShaderLog.

  • A shader program is created using glCreateProgram.

  • The shaders are attached to the program using glAttachShader.

  • The program is linked using glLinkProgram.

  • If there were linking errors, the error logs are printed using checkOpenGLError and printShaderLog.

  • Finally, the shader program identifier is returned.

program_2_6

Clone this wiki locally