glRasterPos2i 和 glBitmap繪製位圖

openGL中繪製位圖時,首先要調用glRasterPos2i設置光柵繪圖的起點位置,glBitmap中的xorig和yorig指向光柵的起點

而glBitmap中的xMove和yMove表明下次繪圖時光柵移動的距離。

 

#if 1
#include <GLFW/glut.h>
#include <stdlib.h>

GLubyte rasters[24] = {
    0xc0, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0xc0, 0x00,
    0xff, 0x00, 0xff, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0xc0, 0x00,
    0xff, 0xc0, 0xff, 0xc0 
};

GLubyte rastersx55[24] = {
    0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
    0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
    0x55, 0x55, 0x55, 0x55
};
void init(void)
{
    glPixelStorei(GL_UNPACK_ALIGNMENT/*GL_UNPACK_SWAP_BYTES*//*GL_PACK_SWAP_BYTES*//*GL_UNPACK_ALIGNMENT*/, 1);
    glClearColor(0.0, 0.0, 0.0, 0.0);
}

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0, 1.0, 1.0);
    glRasterPos2i(20, 20);
    glBitmap(10, 12, 0.0, 0.0, 11.0, 0.0, rasters);

    glRasterPos2i(120, 120);
    glBitmap(10, 12, 0.0, 0.0, 11.0, 60, rasters);

    glBitmap(10, 12, 0.0, 0.0, 11.0, 11.0, rasters);

    glColor3f(1.0, 0, 0);
    glRasterPos2i(150, 20);
    glBitmap(10, 12, 0.0, 0.0, 11.0, 0.0, rastersx55);
    //繪製由bitmap指定的位圖,bitmap是一個指向位圖圖像的指針,位圖的原點是當前光柵位置,如果當前光柵位置無效,則這個函數不會繪製任何東西。
    //width和height表示位圖的寬度和高度,xorig和yorig定義了位圖的原點,他是根據當期光柵位置確定的,右上爲正。
    //xmove和ymove表示位圖光柵化之後光柵座標的x增加值和y增加值
    glFlush();
}

void reshape(int w, int h)
{
    glViewport(0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, w, 0, h, -1.0, 1.0);
    glMatrixMode(GL_MODELVIEW);
}

void keyboard(unsigned char key, int x, int y)
{
    switch (key) {
    case 27:
        exit(0);
    }
}
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(300, 100);
    glutInitWindowPosition(100, 100);
    glutCreateWindow(argv[0]);
    init();
    glutReshapeFunc(reshape);
    glutKeyboardFunc(keyboard);
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}

#endif

 

運行結果:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章