OpenGL 2D mode

// 
// left Top default setting here is to the upper left corner
 //
 void push_view2d ( int left, int Top, int width, int height) 
{ 
    // glPushAttrib (GL_TRANSFORM_BIT | GL_VIEWPORT_BIT); 
    glPushAttrib (GL_ALL_ATTRIB_BITS); 
    glMatrixMode (GL_PROJECTION); 
    glPushMatrix (); 
    glLoadIdentity (); 
 
    // Cartesian coordinates
     // glOrtho (left, width, Top, height, 0, INT_MAX);
     // cross coordinates
     // glOrtho (-width / 2, width / 2, -height / 2, height / 2, 0, INT_MAX); // 
 
    // Windows coordinate
     //glOrtho (left, width, height, Top, -1.0, 1,000,000.0);
     // -1 here to be removed, otherwise there will be drawn rounding bias
     // glOrtho (left, left-width +. 1, Top +-height. 1, Top, 0, INT_MAX); 
    glOrtho (left, left + width, height + Top, Top, 0 , INT_MAX); 
 
    // glOrtho (0,. 1-width, -height +. 1, 0, 0, INT_MAX);
     // glTranslatef (0, -height + 1, 0); 
 
    // reset the front, the default GL_CCW
     // glFrontFace (GL_CW); 
 
    // Invert the screen
     // glScalef (1.0f, -1.0F, 1.0f);
     // glTranslatef (0.0f, -height, 0.0f); 
 
    glMatrixMode (GL_MODELVIEW); 
    glLoadIdentity (); 
    glDisable (GL_DEPTH_TEST); // Close depth test
    glDisable (GL_CULL_FACE); // Close culling 
} 
 
// restore the viewport mapping settings 
void pop_view2d () 
{ 
    glMatrixMode (the GL_PROJECTION); 
    glPopMatrix (); 
    glMatrixMode (GL_MODELVIEW); 
    glPopAttrib (); 
}

 

Function to use:

glClear (...); // clear screen

// first set to 800x600
// This is a virtual screen size. To the true resolution, and you would like to set the size of glViewport
push_view2d (0, 0, 800, 600);
Draw (...);
pop_view2d ();

// then set to 1024x768 mode, rendering something
push_view2d (0, 0, 1024, 768);
Draw (...);
pop_view2d ();

swapbuffers (); // inverting buffer, a display

In particular it should be noted that, OpenGL default starting in the lower left corner on the left, such as Windows coordinates after mapping, the texture is drawn upside down. There are two solutions, it is time to draw a swap v1, v2 coordinates, and the other is the time to make textures look upside down.

#define CGL_LOWER_LEFT 0     // lower left corner of the initial (default) 
#define CGL_UPPER_LEFT. 1     // upper left corner of the start (the Windows) 
#define CGL_COORDINATE CGL_LOWER_LEFT     // define the upper left corner of the start 
 
/ * 
struct vec4f 
{ 
    a float R & lt, G, B, A ; 
}; 
struct vertex_type 
{ 
    a float X, Y, Z; 
    a float U, V; 
    vec4f Color; 
}; 
* / 
 
void GLContext :: draw_image (GLuint Image, a float X, a float Y, a float width, a float height,
     a float U1, a float v1,float u2, float v2)
{
    vertex_type vtx[] = {
        #if CGL_COORDINATE == CGL_LOWER_LEFT//左下角为原点
        vertex_type(x,         y,          0.0f, u1, v2, color),
        vertex_type(x + width, y,          0.0f, u2, v2, color),
        vertex_type(x + width, y + height, 0.0f, u2, v1, color),
        vertex_type(x        , y + height, 0.0f, u1, v1, color)
        #else//右下角为原点,OpenGL默认
        vertex_type(x,         y,          0.0f, u1, v1, color),
        vertex_type(x + width, y,          0.0f, u2, v1, color),
        vertex_type(x + width, y + height, 0.0f, u2, v2, color),
        vertex_type(x,         y + height, 0.0f, u1, v2, color)
        #endif
    };
    this->bind_texture(image);
    this->draw_arrays(GL_TRIANGLE_FAN, vtx, 0, 4);
}

 

Guess you like

Origin www.cnblogs.com/sdragonx/p/10989862.html