Opengl:why is YOUR CODE producing A BLACK WINDOW?

Source: Internet
Author: User

?

Introduction

one of the most common problems for Novice, and sometimes experienced, OpenGL programmers is that they write new rendering code, run it, and it displays ... Noth ing at all. Being more active on message boards again lately ( stack Overflow ?and? opengl Discussion Boards), ? I noticed that this still covers a large part of the issues people ask about.

Diagnosing and solving these types of issues is often tricky because they?can being caused by mistakes in almost all part of The OpenGL pipeline. This article explains some common reasons for the infamous ' Black Window ' problem, as well as approaches SE types of issues.

The following sections is mostly based on working with the Core profile, which includes writing your own shaders. Much of it would apply to the fixed pipeline as well. If you want to do the transition to the Core profiles, check out my? article? about the topic.

Check for Errors

with hundreds of APIs calls, it can easily happen that a wrong argument are passed for one of them. Or less obviously, the that API calls is made while not meeting all preconditions. You can check the for these types of errors, and should does so routinely. The primary function for doing? Glgeterror () . A useful approach is to has an error check, which is all executed for the debug Builds?at strategic places in your code. If you use an Assert macros, add a line like this for example at the end of rendering a frame:

ASSERT (glgeterror () = = Gl_no_error);

If the assert triggers, temporarily add more than them across your code, gradually narrowing down which call causes the Erro R based on the the fact that the error is triggered between the last check that does not report an error, and the first one th At did. Once you have isolated the "error to a", reading the documentation for the call, and looking at the exact error Code that is returned, should normally make it clear what went wrong.

Another important area of the error checking is shader compilation and linking. At least for debug builds, check the shader compilation status after each shader compilation, using:

Glint status = 0; Glgetshaderiv (Shaderid, Gl_compile_status, &status);

If status is? Gl_false , you can retrieve the error messages using:

Glint Loglen = 0; Glgetshaderiv (Shaderid, Gl_info_log_length, &loglen); glchar* logstr = new Glchar[loglen]; Glgetshaderinfolog (Shaderid, Loglen, 0, LOGSTR); Report Logstr delete[] logstr;

Checking Success after linking the program looks similar, except? GLGETPROGRAMIV () ,? Gl_link_status ,? and? Glgetprograminfolog () .

When working with Frame Buffer Objects (FBO), the There is another useful error check. After your finished setting up your FBO, check for success with:

ASSERT (Glcheckframebufferstatus (gl_draw_framebuffer) = = Gl_framebuffer_complete));

Geometry not in Field of View

This is a by-far one of the most common mistakes, and unfortunately don't easy-to-track down. In this case, the whole rendering pipeline are correctly set up, but the the the geometry are not in the coordinate range being Mapped to the window.

The only good the "to fix," or ideally avoid, this issue is a solid understanding of OpenGL coordinate systems and transform Ations. It's often best-to-start simple by placing the geometry around the origin, and placing the camera on the positive z-axis, Pointing towards the origin. There is no need to?use a?projection transformation to get something to appear on the screen. Once This works, you can progress to setting?up more advanced transformations, like perspective projections.

Common causes for the geometry isn't being in the field of view include:

  • The geometry has coordinates that is far away from the origin and while the camera is points at the origin. In this case, you'll either has to modify the coordinates of your geometry, and apply a translation to move the geometry to The origin, or point your camera in the direction at the geometry.
  • The camera is inside the object, and Backface culling is enabled. This can easily happen if the no viewing transformation is set to all. Make sure, which is set up a viewing transformation.
  • The geometry is behind the camera. A variation of the same problem as the previous, but this typically happens if the viewing transformation are not set up CO rrectly.
  • The clipping planes is set wrong. When using one of the common projection transformations, make sure that range between the near and far clipping planes is consistent with the distance of your geometry from the camera. When isn't using a projection transformation, make sure that the z-coordinates after applying the viewing transformation is between-1.0 and 1.0.
  • Less common, but possible:the range of coordinates are much too small, so, and the extreme case,?you end up drawing the Entire geometry in a single pixel.
Black on Black

if There is a problem in the fragment part of the Pipel INE, it'll often produce black pixels. When using shaders, a typical example is if the fragment shader uses texturing, and the texture was not properly set up. With the fixed pipeline, it can mean this no material color was set.

one very useful method to diagnose if the IS Happening is to set the clear color to something other than the default of black. I mostly set the clear color to something like yellow during development. If you do this, and see the outline of your geometry show on black, you know that the problem are with the color of the Fragments produced by your pipeline. This can is taken one step farther when using FBOs that is rendered to the primary framebuffer in the end. If you clear each of the render target with a different color, you can see where the things break is down.

Another useful approach can is applied when using relatively complex fragment shaders. To verify if there might is a problem with the output of the fragment shader, you can temporarily change it to simply prod UCE a fixed color. If that color shows up and while you previously rendered All black, your problem are with the fragment shader.

? Vertex Data not properly Set up

Current OpenGL requires vertex data to is in vertex buffers. If something goes wrong while setting up the data in those vertex buffers, it can result with no rendering at all. Verify that:

    • The vertex data itself that's store in the vertex buffers?contains the correct coordinates for your geometry.
    • The correct vertex buffer is a bound with? glbindbuffer ()? When setting the vertex buffer data with a call like? glbufferdata ().
    • All arguments to the vertex setup calls is correct. For example, do sure that the arguments to? glvertexattribpointer ()? Match the format, sizes, etc. of your vertex data.
Faces is culled

By default, OpenGL expects the vertices for each of the arranged in a counter-clockwise orientation. If you get the this wrong, and has backface culling enabled, your faces can disappear. If you have any kind of suspicion the this might be happening, disable Backface culling:

Gldisable (Gl_cull_face);

Not everything is Bound and Enabled

There is a number of objects this need to being bound, and features that need to being enabled, for rendering to happen. If any of them is missing, you'll often get no rendering at all.

there is no "around code inspection, or stepping Through the code in a debugger, to make sure that everything was properly bound and enabled when the draw calls is execute D. Items to look out for include:

    • The correct program was bound with? Gluseprogram ().
    • Rendering goes to the primary framebuffer when intended, using? Glbindframebuffer (gl_draw_framebuffer, 0).
    • Vertex Array object is bound with? Glbindvertexarray ().
    • Vertex attributes is properly set up and enabled, using calls? glvertexattribpointer ()? Glenablevertexattribarray ().
uniforms were not Set

Make sure, the uniforms used in your shaders is set to valid values before starting to draw. For example, if you miss to set a value for a uniform matrix used for transformations, it can result in your geometry not Showing up.

Code Inspection and Debugging?is the only reasonable-to-find this. If you suspect this certain uniforms might not being set correctly, you can also try to temporarily simplify the shader to No t use the value, and see if something changes.

Depth Buffer is not cleared

If you use a depth buffer, and having depth testing enabled, make sure then the depth buffer at the start of each Frame.

A slight variation of this is so if you don't need a depth buffer for your rendering, make sure so you configure your Context/surface without a depth buffer during initialization.

Frame is not Displayed

This problem are so trivial it's almost embarrassing, but it does happen:if you use a double buffered visual (which You should-almost all cases), make sure that the buffers is swapped when you finish rendering the frame, so, the F Rame rendered is actually displayed.

How exactly the is very system dependent. Some higher level frameworks handle this automatically after they invoked your rendering method. If that was not the case, look for a function that typically have? swapbuffers , or something similar as part of its name, and can is found in the Windows system interface, or the toolkit you use.

The symptom of this is so nothing at all are displayed, not even the clear color. Like in other cases, setting the clear color to something different from black or white helps recognizing so this might Be your problem.

Context is not a properly Set up, or isn't current

When nothing renders, it's possible that there were a problem while setting up the context, pixel format, rendering Su Rface, etc. How it is-highly platform dependent. Fortunately, it's at least easy to diagnose if the problem are in this area. Set your clear color to something other than black, using? Glclearcolor () , and change your rendering method to only do a? glclear () .? If The window does not show your clear color, chances is that your context setup failed.

?

from:http://retokoradi.com/2014/04/21/opengl-why-is-your-code-producing-a-black-window/

Opengl:why is YOUR CODE producing A BLACK WINDOW?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.