Advertisement

Drawing a quad using glOrtho work fine alone, and so does my shader-based quad, but together it's disaster

Started by April 12, 2020 08:34 PM
14 comments, last by taby 4 years, 9 months ago

As the topic title says: drawing a quad using glOrtho work fine alone, and so does a shader-based quad, but together it's disaster.

Can you spot an error in my code / logic? Thank you so much for any advice that you might have.

The lines that are commented out start at line 581 in ssao.h:

https://github.com/sjhalayka/julia4d3/blob/8a3105d68b119f209195bf8e2900c80918cd70dc/ssao.h#L581

Way too much code to skim here. Copy the relevant lines here. What version of openGL?

NBA2K, Madden, Maneater, Killing Floor, Sims

Advertisement

Here is my draw function. When I uncomment out the lines, I enable the shader-based quad draw. After that, I do a glOrtho call:

[code]

void display_func(void)

{

glClearColor(1, 0.5f, 0, 1);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glEnable(GL_TEXTURE_2D);

//glDisable(GL_BLEND);

//

//const GLfloat black[] = { 0.0f, 0.0f, 0.0f, 0.0f };

// static const GLfloat one = 1.0f;

// static const GLenum draw_buffers[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 };

// glViewport(0, 0, win_x, win_y);

// glBindFramebuffer(GL_FRAMEBUFFER, render_fbo);

// glEnable(GL_DEPTH_TEST);

//

// glClearBufferfv(GL_COLOR, 0, black);

// glClearBufferfv(GL_COLOR, 1, black);

// glClearBufferfv(GL_DEPTH, 0, &one);

// glBindBufferBase(GL_UNIFORM_BUFFER, 0, points_buffer);

//glUseProgram(render.get_program());

//main_camera.calculate_camera_matrices(win_x, win_y);

//glUniformMatrix4fv(uniforms.render.proj_matrix, 1, GL_FALSE, main_camera.projection_mat);

//glUniformMatrix4fv(uniforms.render.mv_matrix, 1, GL_FALSE, main_camera.view_mat);

// glUniform1f(uniforms.render.shading_level, show_shading ? (show_ao ? 0.7f : 1.0f) : 0.0f);

//glBindVertexArray(fractal_vao);

//glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(triangle_indices.size()*3), GL_UNSIGNED_INT, 0);

// glBindFramebuffer(GL_FRAMEBUFFER, 0);

//glUseProgram(ssao.get_program());

// glUniform1f(uniforms.ssao.ssao_radius, ssao_radius * float(win_x) / 1000.0f);

// glUniform1f(uniforms.ssao.ssao_level, show_ao ? (show_shading ? 0.3f : 1.0f) : 0.0f);

// glUniform1i(uniforms.ssao.weight_by_angle, weight_by_angle ? 1 : 0);

// glUniform1i(uniforms.ssao.randomize_points, randomize_points ? 1 : 0);

// glUniform1ui(uniforms.ssao.point_count, point_count);

// glActiveTexture(GL_TEXTURE0);

// glBindTexture(GL_TEXTURE_2D, fbo_textures[0]);

// glActiveTexture(GL_TEXTURE1);

// glBindTexture(GL_TEXTURE_2D, fbo_textures[1]);

// glDisable(GL_DEPTH_TEST);

// glBindVertexArray(quad_vao);

// glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

// Draw fullscreen quad

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

glOrtho(0, win_x, 0, win_y, 0, 1);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

glEnable(GL_BLEND);

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glBegin(GL_QUADS);

glTexCoord2f(0, 0);

glVertex2f(0, 0);

glTexCoord2f(1, 0);

glVertex2f(win_x, 0);

glTexCoord2f(1, 1);

glVertex2f(win_x, win_y);

glTexCoord2f(0, 1);

glVertex2f(0, win_y);

glEnd();

glutSwapBuffers();

}

[/code]

One way you are using glBegin(GL_QUADS) and the other is with vertex array objects.
Steps to debug:

Add a basic shader and apply it to the glBegin(GL_QUADS) code you have. Leave everything else uncommented how you have it now. Does a simple shader work?

Once you have a simple shader working, then try using your vertex array object without all the FBO stuff. Render to screen with the vertex array and see if it works. glGetError() is a nice thing to inject after every opengl call to test what errors might be occuring.

NBA2K, Madden, Maneater, Killing Floor, Sims

OK, thank you for the suggestions!

P.S. I have a code that draws a textured quad using a shader, so I just have to fit it into this code.

So I added the freetype2 library to my code, and it also fails to draw text if I re-enable the SSAO code. So, it's not just me, it's OpenGL. If I leave the SSAO code disabled, it draws text just fine.

What's a hack to do this? Any ideas?

Advertisement

I can use glReadPixels, but how to I write to the framebuffer? Sorry, just at my wit's end lol

Drawing text over a scene works just fine. It's something in your setup/code.

Maybe my recent dabbling with text rendering helps (OpenGL 4.5) ?

https://github.com/BaronVerde/VeryVerySimpleGUI. Just add a font.

Like I was saying, freetype works fine… if i don't re-enable the SSAO code. It's not freetype, it's OpenGL..

P.S. I have a code that draws a textured quad using a shader, so I just have to fit it into this code.

So the code you have is copy/pasted? Not the best way to learn without other concepts.

but how to I write to the framebuffer

The framebuffer is the buffer that your screen displays. By default you are rendering to the screen. The SSAO code is using an FBO, an offline texture used for other processing (rendering shadow maps, rendering to HDR buffers, etc). If you remove the FBO code completely, you might see something work. Don't have time to debug each line what is going on but that is the idea.

NBA2K, Madden, Maneater, Killing Floor, Sims

This topic is closed to new replies.

Advertisement