c++ - 用OpenGL绘制位图字体,glRasterPos2i()有什么作用?

标签 c++ c opengl bitmap

这是另一个“我有黑屏,请帮我解决”的时刻。

此示例来自 The OpenGL Programming Guide,2.1 版,第 311-312 页。

该示例应该在屏幕上绘制 2 行文本。

我认为过去的问题是我不明白 glRasterPos2i() 是如何工作的。可以: A :) 设置要在 3D 世界中以齐次/“OpenGL 坐标”绘制的位图的位置 B :) 以像素坐标设置要绘制的位图在屏幕上的位置

这是我目前的代码:您几乎可以忽略定义位图的第一个大块。

#include <GL/glut.h>
#include <cstdlib>
#include <iostream>
#include <cstring>


// This first bit is kind of irreverent, it sets up some fonts in memory as bitmaps
GLubyte space[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

GLubyte letters[][13] = {
                        { 0x00, 0x00, 0xc3, 0xc3, 0xc3, 0xc3, 0xff, 0xc3, 0xc3, 0xc3, 0x66, 0xc3, 0x18 },
                        { 0x00, 0x00, 0xfe, 0xc7, 0xc3, 0xc3, 0xc7, 0xfe, 0xc7, 0xc3, 0xc3, 0xc7, 0xfe },
                        { 0x00, 0x00, 0x7e, 0xe7, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xe7, 0x7e },
                        { 0x00, 0x00, 0xfc, 0xce, 0xc7, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc7, 0xce, 0xfc },
                        { 0x00, 0x00, 0xff, 0xc0, 0xc0, 0xc0, 0xc0, 0xfc, 0xc0, 0xc0, 0xc0, 0xc0, 0xff },
                        { 0x00, 0x00, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xfc, 0xc0, 0xc0, 0xc0, 0xff },
                        { 0x00, 0x00, 0x7e, 0xe7, 0xc3, 0xc3, 0xcf, 0xc0, 0xc0, 0xc0, 0xc0, 0xe7, 0x7e },
                        { 0x00, 0x00, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xff, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3 },
                        { 0x00, 0x00, 0x7e, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x7e },
                        { 0x00, 0x00, 0x7c, 0xee, 0xc6, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06 },
                        { 0x00, 0x00, 0xc3, 0xc6, 0xcc, 0xd8, 0xf0, 0xe0, 0xf0, 0xd8, 0xcc, 0xc6, 0xc3 },
                        { 0x00, 0x00, 0xff, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0 },
                        { 0x00, 0x00, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xdb, 0xff, 0xff, 0xe7, 0xc3 },
                        { 0x00, 0x00, 0xc7, 0xc7, 0xcf, 0xcf, 0xdf, 0xdb, 0xfb, 0xf3, 0xf3, 0xe3, 0xe3 },
                        { 0x7e, 0xe7, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xe7, 0x7e },
                        { 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xfe, 0xc0, 0xf3, 0xc7, 0xc3, 0xc3, 0xc7, 0xfe },
                        { 0x00, 0x00, 0x3f, 0x6e, 0xdf, 0xdb, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0x66, 0x3c },
                        { 0x00, 0x00, 0xc3, 0xc6, 0xcc, 0xd8, 0xf0, 0xfe, 0xc7, 0xc3, 0xc3, 0xc7, 0xfe },
                        { 0x00, 0x00, 0x7e, 0xe7, 0x03, 0x03, 0x07, 0x7e, 0xe0, 0xc0, 0xc0, 0xe7, 0x7e },
                        { 0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0xff },
                        { 0x00, 0x00, 0x7e, 0xe7, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3 },
                        { 0x00, 0x00, 0x18, 0x3c, 0x3c, 0x66, 0x66, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3 },
                        { 0x00, 0x00, 0xc3, 0xe7, 0xff, 0xff, 0xdb, 0xdb, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3 },
                        { 0x00, 0x00, 0xc3, 0x66, 0x66, 0xc3, 0xc3, 0x18, 0xc3, 0xc3, 0x66, 0x66, 0xc3 },
                        { 0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0xc3, 0xc3, 0x66, 0x66, 0xc3 },
                        { 0x00, 0x00, 0xff, 0xc0, 0xc0, 0x60, 0x30, 0x7e, 0x0c, 0x06, 0x03, 0x03, 0xff }
                        };


// This is just copying from the book
GLuint fontOffset;

void makeRasterFont()
{
    GLuint i, j;
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    fontOffset = glGenLists(128);
    for(i = 0, j = 'A'; i < 26; i ++, j ++)
    {
        glNewList(fontOffset + ' ', GL_COMPILE);
            glBitmap(8, 13, 0.0, 2.0, 10.0, 0.0, letters[i]);
        glEndList();
    }
    glNewList(fontOffset + ' ', GL_COMPILE);
        glBitmap(8, 13, 0.0, 2.0, 10.0, 0.0, space);
    glEndList();
}


void init()
{
    glShadeModel(GL_FLAT);
    makeRasterFont();
}


void printString(char* s)
{
    glPushAttrib(GL_LIST_BIT);
        glListBase(fontOffset);
        glCallLists(std::strlen(s), GL_UNSIGNED_BYTE, (GLubyte*)s);
    glPopAttrib();
}


void display()
{
    GLfloat white[3] = {1.0, 1.0, 1.0 };

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glColor3fv(white);

    // Print some text on the screen at (20,60) and (20,40)
    glRasterPos2i(20, 60);
    printString("THE QUICK BROWN FOX JUMPS");
    glRasterPos2i(20, 40);
    printString("OVER A LAZY DOG");

    glFlush();
}


void reshape(int w, int h)
{
    // Set the viewport
    glViewport(0, 0, (GLsizei)w, (GLsizei)h);

    // Set viewing mode
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0, (GLfloat)w / (GLfloat)h, 0.01, 100.0);
    glMatrixMode(GL_MODELVIEW);
}



int main(int argc, char** argv)
{

    /* Init glut with a single buffer display mode,
     * window size, position and title */
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(100, 100);
    glutCreateWindow(argv[0]);

    // Call init routine to set OpenGL specific initialization values
    init();

    // Set callback function
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);

    // Enter main loop
    glutMainLoop();

    return EXIT_SUCCESS;
}

对于这种类型的问题感到抱歉 - 我讨厌只问“请修复我的代码”,因为我真的应该能够自己修复它。这一次,我发现自己基本上被困住了。感谢您的宝贵时间和帮助。

解决方案:

对于那些感兴趣的人,为了“让它工作”,所做的更改是:

1: 将 gluPerspective 更改为 gluOrtho2D(0, width, 0, height)。

2:将 glnewList(fontOffset + ' ', GL_COMPILE) 更改为 glnewList(fontOffset + j, GL_COMPILE) - 不是两者,只是循环中的第一个。

3: 将 glRasterPos2i 设置为 glOrtho2D 指定区域内的任意位置。我的宽度和高度都是 500,所以我使用坐标 (20, 60) 然后 (20, 40)。

您可以将其保留为 gluPerspective,并使用大约 (0,0) 的坐标而不指定任何变换。但是,由于位图是二维的,我认为这不太直观。

最佳答案

关于你的渲染问题,提示,你没有使用j...

在for循环中:

glNewList(fontOffset + ' ', GL_COMPILE);

用你想要的字母替换你的空格。

关于c++ - 用OpenGL绘制位图字体,glRasterPos2i()有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17748263/

相关文章:

c++ - 按键时更改 GLFWwindow 的背景

c++ - 视觉 C++ : #include files from other projects in the same solution

c++ - Crypto++ 对称算法和经过身份验证的 block 模式组合

c++ - 不同编译器中C++和C之间无符号位域整数表达式的截断不一致

c - 为什么在这两个测试用例中我的输出是错误的?

c++ - 添加多个 QDockWidget

c++ - 为什么 -INT_MIN = INT_MIN 以带符号的二进制补码表示?

c++ - 如何查找并返回 vector 中的重复序列

c++ - 为 Visual Studio 2010 设置 OpenCV-2.3

c++ - 延迟渲染的特殊情况