Write the code to set the OpenGL framebuffer, just to have more control than the default…
- User
Write the code to set the OpenGL framebuffer, just to have more control than the default one.
- Assistant
- User
GLSL doesn't support C-style initializers. What do I do for: const vec3 vertex_positions[] = { vec3(-1.0f, 1.0f, 0.0f), vec3(1.0f, 1.0f, 0.0f), vec3(-1.0f, -1.0f, 0.0f), vec3(1.0f, 1.0f, 0.0f), vec3(1.0f, -1.0f, 0.0f), vec3(-1.0f, -1.0f, 0.0f) };
- Assistant
To initialize an array in GLSL, you can use a combination of
uniformandassignstatements. Here's how you can initialize thevertex_positionsarray in GLSL:glslconst vec3 vertex_positions[6]; uniform vec3 value0 = vec3(-1.0f, 1.0f, 0.0f); uniform vec3 value1 = vec3(1.0f, 1.0f, 0.0f); uniform vec3 value2 = vec3(-1.0f, -1.0f, 0.0f); uniform vec3 value3 = vec3(1.0f, 1.0f, 0.0f); uniform vec3 value4 = vec3(1.0f, -1.0f, 0.0f); uniform vec3 value5 = vec3(-1.0f, -1.0f, 0.0f); void initVertexPositions() { vertex_positions[0] = value0; vertex_positions[1] = value1; vertex_positions[2] = value2; vertex_positions[3] = value3; vertex_positions[4] = value4; vertex_positions[5] = value5; } void main() { // Call the initialization function initVertexPositions(); // ... } - User
I don't want uniforms. Nor do I want a gazillion variables like that
- Assistant
I apologize for the confusion. In GLSL, you can initialize an array directly using a constructor-like syntax. However, it requires GLSL version 4.20 or higher. Here's how you can initialize the
vertex_positionsarray in GLSL without using uniforms:glslconst vec3 vertex_positions[6] = vec3[]( vec3(-1.0f, 1.0f, 0.0f), vec3(1.0f, 1.0f, 0.0f), vec3(-1.0f, -1.0f, 0.0f), vec3(1.0f, 1.0f, 0.0f), vec3(1.0f, -1.0f, 0.0f), vec3(-1.0f, -1.0f, 0.0f) ); void main() { // ... }Here, the
vertex_positionsarray is initialized directly with the desired values using a constructor-like syntaxvec3[](...). Eachvec3element is provided inside parentheses, separated by commas.Please ensure that your GLSL version is at least 4.20 or higher for this syntax to work.
- User
If I do glDrawArrays(GL_TRIANGLES, 6, 0); without an actual VAO, will this work? #version 330 core
const vec2 vertex_positions[] = vec2[] ( vec2(-1.0f, 1.0f), vec2(1.0f, 1.0f), vec2(-1.0f, -1.0f), vec2(1.0f, 1.0f), vec2(1.0f, -1.0f), vec2(-1.0f, -1.0f) );
void main() { gl_Position = vec4(vertex_positions[gl_VertexID], 0.0f, 1.0f); }
- Assistant
Yes, the
glDrawArrays(GL_TRIANGLES, 6, 0)call will work without an actual VAO (Vertex Array Object).In your provided GLSL code, you are using the
gl_VertexIDvariable to index into thevertex_positionsarray and assign the resulting position togl_Position. This usage is valid and will work as expected.However, it's important to note that using VAOs is generally recommended and considered best practice. VAOs allow you to encapsulate all vertex attribute setup (such as vertex positions, colors, normals, etc.) in a single object, which can be bound with a single call, improving code organization and performance.
If you choose to use a VAO, you would need to bind a VAO before the
glDrawArrayscall and associate the vertex attribute data with the corresponding attribute locations usingglVertexAttribPointer. - User
Then why is the screen black? glBindFramebuffer(GL_FRAMEBUFFER, fbo_id); glEnable(GL_DEPTH_TEST);
codeglUseProgram(shader_program_id); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texture_id); glBindVertexArray(cube_vao); glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0); glBindFramebuffer(GL_FRAMEBUFFER, 0); glDisable(GL_DEPTH_TEST); glUseProgram(post_proccess_shader_program_id); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, fbo_colorbuffer_texture_id); glDrawArrays(GL_TRIANGLES, 6, 0); glfwSwapBuffers(main_window); glfwPollEvents();#version 330 core
out vec4 out_fColor;
void main() { out_fColor = vec4(1.0f); }
- Assistant
The screen is likely black because you are passing
0as the count parameter in theglDrawArrayscall (glDrawArrays(GL_TRIANGLES, 6, 0)). The count parameter specifies the number of vertices to be rendered, but you are passing0, indicating that you want to render zero vertices.To fix this issue, you need to provide the correct number of vertices to be rendered. Based on your GLSL code, it looks like you have 6 vertices defined in the
vertex_positionsarray. Therefore, you should change theglDrawArrayscall toglDrawArrays(GL_TRIANGLES, 0, 6), indicating that you want to render 6 vertices starting from index 0.Here is the corrected code snippet:
cppglUseProgram(post_proccess_shader_program_id); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, fbo_colorbuffer_texture_id); glDrawArrays(GL_TRIANGLES, 0, 6);This should render the vertices defined in the
vertex_positionsarray using the post-processing shader program and display the expected result on the screen.