Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   Related Pages  

Lesson 2

Lesson2: Using OpenGL drawing.
In this lesson you will learn how to draw a simple shape in the window, which you know how to create from the Lesson 1.
See also:
sjgui::CWnd::OnDraw()
// For this lesson, all we need to do is to draw a triangle,
// which means we need to take of only one event: OnDraw() event.
// Also note that the base class is sjgui::CWnd class, which has default
// handlers for all events. We will just override what we need to do.
class CSpcWnd : public sjgui::CWnd
{
public:
    // All we want to do it is drawing of a triangle,
    // so the only event we need to take care of is drawing.
    virtual void OnDraw()
    {
        // First we need to set viewport and tell OpenGL how
        // we want to look at our space (what kind of projection and so on).
        // There is an easy predefined macro, which will do the job for us
        // but you can put anything you want here to have your own settings.
        // This will put point of view at the origin of the space looking down Z axes.
        // Everything closer then 1.0f unit or farther then 1000.0f units is clipped.
        // Because it is set each time window is redrawn, the size of the window
        // is automatically considered.
        // Another reason is that in the feature we will need it, different
        // projection will be used to draw controls.
        SET_GL_SIMPLE_PROSPECTIVE;
        // We want to have our point of view up on Z axis, so we would look
        // down to the origin, where triangle is drawn. This is the same as
        // to translate our triangle down on Z axis!
        glTranslatef(0.0f,0.0f,-10.0f);
        // Let us set color to a blue
        glColor3f(0.0f,0.0f,1.0f);
        // this is our triangle in the center of the screen.
        glBegin(GL_TRIANGLES);
            glVertex3f( 0.0f, 1.0f, 0.0f);
            glVertex3f(-1.0f,-1.0f, 0.0f);
            glVertex3f( 1.0f,-1.0f, 0.0f);
        glEnd();
    }
}; // end of CSpcWnd class
Back to Lesson 1. Forward to Lesson 3.
The following is full source code for this lesson.
// This header includes all resources of the sjgui library
#include <sjgui/sjgui.h>

// For this lesson, all we need to do is to draw a triangle,
// which means we need to take of only one event: OnDraw() event.
// Also note that the base class is sjgui::CWnd class, which has default
// handlers for all events. We will just override what we need to do.
class CSpcWnd : public sjgui::CWnd
{
public:
    // All we want to do it is drawing of a triangle,
    // so the only event we need to take care of is drawing.
    virtual void OnDraw()
    {
        // First we need to set viewport and tell OpenGL how
        // we want to look at our space (what kind of projection and so on).
        // There is an easy predefined macro, which will do the job for us
        // but you can put anything you want here to have your own settings.
        // This will put point of view at the origin of the space looking down Z axes.
        // Everything closer then 1.0f unit or farther then 1000.0f units is clipped.
        // Because it is set each time window is redrawn, the size of the window
        // is automatically considered.
        // Another reason is that in the feature we will need it, different
        // projection will be used to draw controls.
        SET_GL_SIMPLE_PROSPECTIVE;
        // We want to have our point of view up on Z axis, so we would look
        // down to the origin, where triangle is drawn. This is the same as
        // to translate our triangle down on Z axis!
        glTranslatef(0.0f,0.0f,-10.0f);
        // Let us set color to a blue
        glColor3f(0.0f,0.0f,1.0f);
        // this is our triangle in the center of the screen.
        glBegin(GL_TRIANGLES);
            glVertex3f( 0.0f, 1.0f, 0.0f);
            glVertex3f(-1.0f,-1.0f, 0.0f);
            glVertex3f( 1.0f,-1.0f, 0.0f);
        glEnd();
    }
}; // end of CSpcWnd class

// Now we need to setup our window, we do it in the function main.
int main(int argc, char* argv[])
{
    // Events receiver
    CSpcWnd SpcWnd;
    // Position the window
    SpcWnd.PosWnd(20,50,320,240);
    // Create window
    if(sjgui::Create("Drawing something!",&SpcWnd))
    {
        // Run it. Now all events are transferred to our class.
        sjgui::GlobalMainLoop();
    }
    else
        // Print error message if something was wrong.
        printf("Could not create opengl window.\n");
    return 0;
} // the end of main
sjgui logo
Quick Links:

 News
 Description
 Screen Shots
 Projects
 Downloads
 Source Code
 Help/FAQ
 Want to help?
 Credits
 Disclaimer


Documentation:

 Documentation
 Reference
 Lessons


Useful links:

sjcomp logo
sjcomp

opengl logo

nehe logo

SourceForge.net Logo

Last modified:


Started by Alexander Shyrokov. Generated at Wed Apr 28 12:31:05 2004 for the sjgui by doxygen 1.3.1. SourceForge.net Logo