YaleCheung
YaleCheung

Reputation: 620

How to show the result of opengl?

I'm very new to OpenGL. I want to learn it from the Blue Book (OpenGL superbible 6th edition).

When I compile the first program with Visual Studio 2013, everything goe well, except that a white window appears and then the program quit with code 0.

The program is:

// Include the "sb6.h" header file
#include "sb6.h"
// Derive my_application from sb6::application
class my_application : public sb6::application
{
public:
    // Our rendering function
    void render(double currentTime)
    {
        // Simply clear the window with red
        static const GLfloat red[] = { 1.0f, 0.0f, 0.0f, 1.0f };
        glClearBufferfv(GL_COLOR, 0, red);
    }
};
// Our one and only instance of DECLARE_MAIN
DECLARE_MAIN(my_application);

I think both the compiling and building process are working fine because the result code 0. But I cannot figure out why I don't see a red output window. Please help me.

Upvotes: 0

Views: 344

Answers (1)

user18490
user18490

Reputation: 3810

You are basically dealing with a real-time application which means it takes very little time to render a frame. Rendering a frame means, OpenGL will take all the commands you used to define the scene (the geometry, the settings of the buffers, the shaders, etc.) and render 1 frame as quickly as it can. Generally, if your application needs to be real-time, it will have to be able to render this scene is more than 30 frame per second.

So what basically OpenGL does is render this frame, and then quit. Normally (you don't say in your post which framework you use to create your application, GLUT, GLFW? Well you say you use the code the blue book but not sure which OpenGL lib it uses, it's obviously a wrapper around something else), but a typical OpenGL app does this (in pseudo c/c++ code, assuming some arbitrary framework to deal with keyboard/mouse events, etc.):

bool run = true;

main() {
    ...
    while (run) {
        event e = get_event(e); // this is a key, or mouse event
        process_event(e);
        render_frame();
    }
    ...
}

void processe_event(event e)
{
    if (e.type == KEYBOARD_EVENT && e.value == ESC) { run = false; return; }
    ...
}

The idea is that you run the render function within an infinite loop. So each time the program iterates over the loop, it will render to the screen the content of your openGL scene. Of course, since it's an infinite loop, the window stays on the screen, until your decide to kill the program (or implement some mechanism in which you escape the loop when some specific keys are used, typically the escape key).

The most basic way of getting this to work, is to use an infinity loop:

while (1) {
    render_frame();
}

and do a ctrl+c or interrupt/kill your program. That way you don't have to deal with keys, etc. and then you can at least see what your program does and then move on learning how to use keys.

Aslo I am not sure your code will do anything. First, if you use a double buffer (generally the default these days), you will need to switch buffers to see even the clear function doing something. Second, you will need to add some geometry to your scene. However, note that if you use OpenGL4 for example, you will need to declare and use shaders to see anything, and this is not easy to get this working the first time.

Note that maybe the infinite loop is embedded within the macro DECLARE_MAIN, however this is the problem with using code/framework like the one you are showing in your example, you don't know what's happening elsewhere in the code and how are things coded. For example maybe the buffer swap is happening in the macro DECLARE_MAIN. Generally, I understand why they use macro like that for teaching people (because it hides from you all the complexity of how to get the OpenGL app working) but the downfall is that it stops you from truly understanding the principle of how things work. I personally think this is not the best way to teach graphics especially OpenGL.

The blue book is great, but I would also recommend that you find some example on the web, which shows how to render for example a simple triangle in GL, for example:

http://www.opengl-tutorial.org/beginners-tutorials/tutorial-2-the-first-triangle/

They are quite a few on the web, which are simple, well explained, etc.

You will need to choose your framework first though. I recommend GLFW if you can.

Also, while lessons on OpenGL haven't been written for this yet, I would recommend you check www.scratchapixel.com in the future. It will explain how OpenGL works and guide you step by step to get a simple app running.

If you have more questions please add them in your comments.

Upvotes: 1

Related Questions