LWJGL 使用 slick-util 渲染文本

发布于 2024-12-02 13:26:34 字数 9805 浏览 3 评论 0原文

我想使用 slick-util 的教程在 openGL 应用程序顶部渲染一串文本:http://lwjgl.org/wiki/index.php?title=Slick-Util_Library_-_Part_3_-_TrueType_Fonts_for_LWJGL

这是我的初始化代码:

private void init() {

    try {
        Display.setDisplayMode(new DisplayMode(640, 480));
        Display.setVSyncEnabled(true);
        Display.setTitle(this.windowTitle);
        Display.create();

        Keyboard.create();

    } catch (LWJGLException e) {
        Sys.alert("Error", "Initialization failed!\n\n" + e.getMessage());
        System.exit(0);
    }

    /* OpenGL */
    int width = Display.getDisplayMode().getWidth();
    int height = Display.getDisplayMode().getHeight();

    GL11.glViewport(0, 0, width, height); // Reset The Current Viewport
    GL11.glMatrixMode(GL11.GL_PROJECTION); // Select The Projection Matrix
    GL11.glLoadIdentity(); // Reset The Projection Matrix
    GLU.gluPerspective(45.0f, ((float) width / (float) height), 0.1f, 100.0f); // Calculate The Aspect Ratio Of The Window
    GL11.glMatrixMode(GL11.GL_MODELVIEW); // Select The Modelview Matrix
    GL11.glLoadIdentity(); // Reset The Modelview Matrix

    GL11.glEnable(GL11.GL_TEXTURE_2D);
    //GL11.glShadeModel(GL11.GL_SMOOTH); // Enables Smooth Shading
    GL11.glShadeModel(GL11.GL_FLAT);
    GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background
    GL11.glClearDepth(1.0f); // Depth Buffer Setup
    GL11.glEnable(GL11.GL_DEPTH_TEST); // Enables Depth Testing
    GL11.glDepthFunc(GL11.GL_LEQUAL); // The Type Of Depth Test To Do
    GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST); // Really Nice Perspective Calculations
    //GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE); // Set The Blending Function For Translucency
    //GL11.glEnable(GL11.GL_BLEND);


    /* Set up the light */
    // Ambient Light
    lightAmbient.put(0, 1.0f);
    lightAmbient.put(1, 1.0f);
    lightAmbient.put(2, 1.0f);
    lightAmbient.put(3, 1.0f);

    // Diffuse Light
    lightDiffuse.put(0, 1.0f);
    lightDiffuse.put(1, 1.0f);
    lightDiffuse.put(2, 1.0f);
    lightDiffuse.put(3, 1.0f);

    // Light Position
    lightPosition.put(0, 25.0f);
    lightPosition.put(1, 0.0f);
    lightPosition.put(2, 0.0f);
    lightPosition.put(3, 1.0f);

    lightDir.put(0, 0.0f);
    lightDir.put(1, 0.0f);
    lightDir.put(2, 0.0f);
    lightDir.put(3, 1.0f);

    GL11.glLight(GL11.GL_LIGHT1, GL11.GL_AMBIENT, lightAmbient); // Setup The Ambient Light
    GL11.glLight(GL11.GL_LIGHT1, GL11.GL_DIFFUSE, lightDiffuse); // Setup The Diffuse Light
    GL11.glLight(GL11.GL_LIGHT1, GL11.GL_SPOT_DIRECTION, lightDir);
    GL11.glLight(GL11.GL_LIGHT1, GL11.GL_POSITION, lightPosition); // Position The Light
    GL11.glEnable(GL11.GL_LIGHT1); // Enable Light One

    if (light) // Enable/Disable Lighting
        GL11.glEnable(GL11.GL_LIGHTING);
    else
        GL11.glDisable(GL11.GL_LIGHTING);

    /* Initiating loader classes */

    new ColorList();
    new TextureManager();
    new TimersManager();
    lvLoader = new LevelLoader();

    GL11.glBindTexture(GL11.GL_TEXTURE_2D, TextureManager.getInstance().atlases[0].getTextureID());

    /* Initiating variables */
    lastFPS = getTime();

    /* Setting the spawn point */
    xpos = spawnPoint.x;
    zpos = spawnPoint.z;

    /* Loading a new level */
    loadLevel("testmap");

    /* Build static display lists */
    buildLists();   

    //create the font
    Font awtFont = new Font("Times New Roman", Font.BOLD, 24);
    font = new TrueTypeFont(awtFont, false);
}

渲染代码:

private void render() {

    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
    GL11.glLoadIdentity(); // Reset The View

    GL11.glRotatef(lookupdown, 1.0f, 0, 0);
    GL11.glRotatef(360.0f - yrot, 0, 1.0f, 0);

    GL11.glTranslatef(-xpos, 0, -zpos);

    GL11.glCallList(blocksList);
    GL11.glCallList(tilesList);
    GL11.glCallList(roofList);

    TexCoords tc = null;

    drawS();

    GL11.glBindTexture(GL11.GL_TEXTURE_2D, TextureManager.getInstance().atlases[0].getTextureID());

    /*                   RENDERING BLOCKS                      */
    for (Block block : lvLoader.currentLevel.blocks)
    {
        if (block.created)
        {       
            if (!block.isStatic)
            {
                if (block.texturePos != null)
                {
                    tc = getTexCoords(block.texturePos);
                }
                else
                {
                    tc = new TexCoords(new Vertex(1.0f, 0.875f, 0), new Vertex(0.75f, 0.875f, 0), new Vertex(0.75f, 1.0f, 0), new Vertex(1.0f, 1.0f, 0));
                }

                //GL11.glColor3b(block.color.getRedByte(), block.color.getGreenByte(), block.color.getBlueByte());
                GL11.glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
                GL11.glBegin(GL11.GL_QUADS);

                for (int i = 0; i < 6; i++)
                {
                    quadNormal(i);
                    for (int j = 0; j < 4; j++)
                    {
                        Vertex coord = tc.coords[j];
                        GL11.glTexCoord2d(coord.x, coord.y);

                        GL11.glVertex3f(block.walls[i].vertices[j].x, block.walls[i].vertices[j].y, block.walls[i].vertices[j].z);
                    }
                }

                GL11.glEnd();
            }
        }
    }


    /*                   RENDERING TILES                      */
    for (Tile tile : lvLoader.currentLevel.tiles)
    {
        if (tile.created)
        {
            if (!tile.isStatic)
            {
                if (tile.animation != null)
                {
                    tile.texturePos = tile.animation.frames[tile.animation.currentFrame];
                    tc = getTexCoords(tile.texturePos);
                }
                else
                {
                    if (tile.texturePos != null)
                    {
                        tc = getTexCoords(tile.texturePos);
                    }
                    else
                    {
                        tc = new TexCoords(new Vertex(1.0f, 0.875f, 0), new Vertex(0.75f, 0.875f, 0), new Vertex(0.75f, 1.0f, 0), new Vertex(1.0f, 1.0f, 0));
                    }
                }

                GL11.glColor3b(tile.color.getRedByte(), tile.color.getGreenByte(), tile.color.getBlueByte());
                //GL11.glColor4f(0, 0, 1.0f, 1.0f);
                GL11.glBegin(GL11.GL_QUADS);

                quadNormal(5);
                for (int jj = 0; jj < 4; jj++)
                {
                    Vertex coord = tc.coords[jj];
                    GL11.glTexCoord2f(coord.x, coord.y);

                    GL11.glVertex3f(tile.surface.vertices[jj].x, tile.surface.vertices[jj].y, tile.surface.vertices[jj].z);
                }



                GL11.glEnd();
            }
        }
    }


    /*                   RENDERING ROOF                      */ 
    for (Tile rTile : lvLoader.currentLevel.roof)
    {
        if (rTile != null)
        {
            if (rTile.created)
            {
                if (!rTile.isStatic)
                {
                    if (rTile.texturePos != null)
                    {
                        tc = getTexCoords(rTile.texturePos);
                    }
                    else
                    {
                        tc = new TexCoords(new Vertex(1.0f, 0.875f, 0), new Vertex(0.75f, 0.875f, 0), new Vertex(0.75f, 1.0f, 0), new Vertex(1.0f, 1.0f, 0));
                    }
                    GL11.glColor3ub(rTile.color.getRedByte(), rTile.color.getGreenByte(), rTile.color.getBlueByte());
                    GL11.glBegin(GL11.GL_QUADS);

                    quadNormal(4);
                    for (int k = 0; k < 4; k++)
                    {
                        Vertex coord = tc.coords[k];
                        GL11.glTexCoord2f(coord.x, coord.y);

                        GL11.glVertex3f(rTile.surface.vertices[k].x, rTile.surface.vertices[k].y, rTile.surface.vertices[k].z);
                    }

                    GL11.glEnd();
                }
            }
        }
    }
}

附加方法:

private void drawS()
{
    // disable depth testing and enable orthographic view
    GL11.glDisable(GL11.GL_DEPTH_TEST);
   // GL11.glDepthMask(false);
    enableOrthoView();

    // draw the credits
    GL11.glDisable(GL11.GL_LIGHTING);
    font.drawString(20.0f, 20.0f, "Test string qqqqqqqqqqqqqqqqqqqq ", Color.green);
    GL11.glEnable(GL11.GL_LIGHTING);

    // go back to the model view
    GL11.glEnable(GL11.GL_DEPTH_TEST);
  //  GL11.glDepthMask(true);
    disableOrthoView();
}

/**
 * Enables orthographic view.
 */
public static void enableOrthoView(){                // Set Up An Ortho View   
    GL11.glMatrixMode(GL11.GL_PROJECTION);      // Select Projection
    GL11.glPushMatrix();      // Push The Matrix
    GL11.glLoadIdentity();        // Reset The Matrix
    GL11.glOrtho( 0, 800 , 600 , 0, -1, 1 );  // Select Ortho Mode
    GL11.glMatrixMode(GL11.GL_MODELVIEW);  // Select Modelview Matrix
    GL11.glPushMatrix();    // Push The Matrix
    GL11.glLoadIdentity();    // Reset The Matrix
}

/**
 * Disables the orthographic view.
 */
public static void disableOrthoView() {
    GL11.glMatrixMode( GL11.GL_PROJECTION ); // Select Projection
    GL11.glPopMatrix();    // Pop The Matrix
    GL11.glMatrixMode( GL11.GL_MODELVIEW );  // Select Modelview
    GL11.glPopMatrix();    // Pop The Matrix
}

输出如下所示:图片 1 (它似乎与我的水纹理有纹理)

或者像这样,如果我在绘制文本之前禁用纹理: image 2

什么是我做错了吗?

I want to render a string of text on top of my openGL application using the slick-util's tutorial: http:// lwjgl.org/wiki/index.php?title=Slick-Util_Library_-_Part_3_-_TrueType_Fonts_for_LWJGL

This is my init code:

private void init() {

    try {
        Display.setDisplayMode(new DisplayMode(640, 480));
        Display.setVSyncEnabled(true);
        Display.setTitle(this.windowTitle);
        Display.create();

        Keyboard.create();

    } catch (LWJGLException e) {
        Sys.alert("Error", "Initialization failed!\n\n" + e.getMessage());
        System.exit(0);
    }

    /* OpenGL */
    int width = Display.getDisplayMode().getWidth();
    int height = Display.getDisplayMode().getHeight();

    GL11.glViewport(0, 0, width, height); // Reset The Current Viewport
    GL11.glMatrixMode(GL11.GL_PROJECTION); // Select The Projection Matrix
    GL11.glLoadIdentity(); // Reset The Projection Matrix
    GLU.gluPerspective(45.0f, ((float) width / (float) height), 0.1f, 100.0f); // Calculate The Aspect Ratio Of The Window
    GL11.glMatrixMode(GL11.GL_MODELVIEW); // Select The Modelview Matrix
    GL11.glLoadIdentity(); // Reset The Modelview Matrix

    GL11.glEnable(GL11.GL_TEXTURE_2D);
    //GL11.glShadeModel(GL11.GL_SMOOTH); // Enables Smooth Shading
    GL11.glShadeModel(GL11.GL_FLAT);
    GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background
    GL11.glClearDepth(1.0f); // Depth Buffer Setup
    GL11.glEnable(GL11.GL_DEPTH_TEST); // Enables Depth Testing
    GL11.glDepthFunc(GL11.GL_LEQUAL); // The Type Of Depth Test To Do
    GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST); // Really Nice Perspective Calculations
    //GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE); // Set The Blending Function For Translucency
    //GL11.glEnable(GL11.GL_BLEND);


    /* Set up the light */
    // Ambient Light
    lightAmbient.put(0, 1.0f);
    lightAmbient.put(1, 1.0f);
    lightAmbient.put(2, 1.0f);
    lightAmbient.put(3, 1.0f);

    // Diffuse Light
    lightDiffuse.put(0, 1.0f);
    lightDiffuse.put(1, 1.0f);
    lightDiffuse.put(2, 1.0f);
    lightDiffuse.put(3, 1.0f);

    // Light Position
    lightPosition.put(0, 25.0f);
    lightPosition.put(1, 0.0f);
    lightPosition.put(2, 0.0f);
    lightPosition.put(3, 1.0f);

    lightDir.put(0, 0.0f);
    lightDir.put(1, 0.0f);
    lightDir.put(2, 0.0f);
    lightDir.put(3, 1.0f);

    GL11.glLight(GL11.GL_LIGHT1, GL11.GL_AMBIENT, lightAmbient); // Setup The Ambient Light
    GL11.glLight(GL11.GL_LIGHT1, GL11.GL_DIFFUSE, lightDiffuse); // Setup The Diffuse Light
    GL11.glLight(GL11.GL_LIGHT1, GL11.GL_SPOT_DIRECTION, lightDir);
    GL11.glLight(GL11.GL_LIGHT1, GL11.GL_POSITION, lightPosition); // Position The Light
    GL11.glEnable(GL11.GL_LIGHT1); // Enable Light One

    if (light) // Enable/Disable Lighting
        GL11.glEnable(GL11.GL_LIGHTING);
    else
        GL11.glDisable(GL11.GL_LIGHTING);

    /* Initiating loader classes */

    new ColorList();
    new TextureManager();
    new TimersManager();
    lvLoader = new LevelLoader();

    GL11.glBindTexture(GL11.GL_TEXTURE_2D, TextureManager.getInstance().atlases[0].getTextureID());

    /* Initiating variables */
    lastFPS = getTime();

    /* Setting the spawn point */
    xpos = spawnPoint.x;
    zpos = spawnPoint.z;

    /* Loading a new level */
    loadLevel("testmap");

    /* Build static display lists */
    buildLists();   

    //create the font
    Font awtFont = new Font("Times New Roman", Font.BOLD, 24);
    font = new TrueTypeFont(awtFont, false);
}

Render code:

private void render() {

    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
    GL11.glLoadIdentity(); // Reset The View

    GL11.glRotatef(lookupdown, 1.0f, 0, 0);
    GL11.glRotatef(360.0f - yrot, 0, 1.0f, 0);

    GL11.glTranslatef(-xpos, 0, -zpos);

    GL11.glCallList(blocksList);
    GL11.glCallList(tilesList);
    GL11.glCallList(roofList);

    TexCoords tc = null;

    drawS();

    GL11.glBindTexture(GL11.GL_TEXTURE_2D, TextureManager.getInstance().atlases[0].getTextureID());

    /*                   RENDERING BLOCKS                      */
    for (Block block : lvLoader.currentLevel.blocks)
    {
        if (block.created)
        {       
            if (!block.isStatic)
            {
                if (block.texturePos != null)
                {
                    tc = getTexCoords(block.texturePos);
                }
                else
                {
                    tc = new TexCoords(new Vertex(1.0f, 0.875f, 0), new Vertex(0.75f, 0.875f, 0), new Vertex(0.75f, 1.0f, 0), new Vertex(1.0f, 1.0f, 0));
                }

                //GL11.glColor3b(block.color.getRedByte(), block.color.getGreenByte(), block.color.getBlueByte());
                GL11.glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
                GL11.glBegin(GL11.GL_QUADS);

                for (int i = 0; i < 6; i++)
                {
                    quadNormal(i);
                    for (int j = 0; j < 4; j++)
                    {
                        Vertex coord = tc.coords[j];
                        GL11.glTexCoord2d(coord.x, coord.y);

                        GL11.glVertex3f(block.walls[i].vertices[j].x, block.walls[i].vertices[j].y, block.walls[i].vertices[j].z);
                    }
                }

                GL11.glEnd();
            }
        }
    }


    /*                   RENDERING TILES                      */
    for (Tile tile : lvLoader.currentLevel.tiles)
    {
        if (tile.created)
        {
            if (!tile.isStatic)
            {
                if (tile.animation != null)
                {
                    tile.texturePos = tile.animation.frames[tile.animation.currentFrame];
                    tc = getTexCoords(tile.texturePos);
                }
                else
                {
                    if (tile.texturePos != null)
                    {
                        tc = getTexCoords(tile.texturePos);
                    }
                    else
                    {
                        tc = new TexCoords(new Vertex(1.0f, 0.875f, 0), new Vertex(0.75f, 0.875f, 0), new Vertex(0.75f, 1.0f, 0), new Vertex(1.0f, 1.0f, 0));
                    }
                }

                GL11.glColor3b(tile.color.getRedByte(), tile.color.getGreenByte(), tile.color.getBlueByte());
                //GL11.glColor4f(0, 0, 1.0f, 1.0f);
                GL11.glBegin(GL11.GL_QUADS);

                quadNormal(5);
                for (int jj = 0; jj < 4; jj++)
                {
                    Vertex coord = tc.coords[jj];
                    GL11.glTexCoord2f(coord.x, coord.y);

                    GL11.glVertex3f(tile.surface.vertices[jj].x, tile.surface.vertices[jj].y, tile.surface.vertices[jj].z);
                }



                GL11.glEnd();
            }
        }
    }


    /*                   RENDERING ROOF                      */ 
    for (Tile rTile : lvLoader.currentLevel.roof)
    {
        if (rTile != null)
        {
            if (rTile.created)
            {
                if (!rTile.isStatic)
                {
                    if (rTile.texturePos != null)
                    {
                        tc = getTexCoords(rTile.texturePos);
                    }
                    else
                    {
                        tc = new TexCoords(new Vertex(1.0f, 0.875f, 0), new Vertex(0.75f, 0.875f, 0), new Vertex(0.75f, 1.0f, 0), new Vertex(1.0f, 1.0f, 0));
                    }
                    GL11.glColor3ub(rTile.color.getRedByte(), rTile.color.getGreenByte(), rTile.color.getBlueByte());
                    GL11.glBegin(GL11.GL_QUADS);

                    quadNormal(4);
                    for (int k = 0; k < 4; k++)
                    {
                        Vertex coord = tc.coords[k];
                        GL11.glTexCoord2f(coord.x, coord.y);

                        GL11.glVertex3f(rTile.surface.vertices[k].x, rTile.surface.vertices[k].y, rTile.surface.vertices[k].z);
                    }

                    GL11.glEnd();
                }
            }
        }
    }
}

Additional methods:

private void drawS()
{
    // disable depth testing and enable orthographic view
    GL11.glDisable(GL11.GL_DEPTH_TEST);
   // GL11.glDepthMask(false);
    enableOrthoView();

    // draw the credits
    GL11.glDisable(GL11.GL_LIGHTING);
    font.drawString(20.0f, 20.0f, "Test string qqqqqqqqqqqqqqqqqqqq ", Color.green);
    GL11.glEnable(GL11.GL_LIGHTING);

    // go back to the model view
    GL11.glEnable(GL11.GL_DEPTH_TEST);
  //  GL11.glDepthMask(true);
    disableOrthoView();
}

/**
 * Enables orthographic view.
 */
public static void enableOrthoView(){                // Set Up An Ortho View   
    GL11.glMatrixMode(GL11.GL_PROJECTION);      // Select Projection
    GL11.glPushMatrix();      // Push The Matrix
    GL11.glLoadIdentity();        // Reset The Matrix
    GL11.glOrtho( 0, 800 , 600 , 0, -1, 1 );  // Select Ortho Mode
    GL11.glMatrixMode(GL11.GL_MODELVIEW);  // Select Modelview Matrix
    GL11.glPushMatrix();    // Push The Matrix
    GL11.glLoadIdentity();    // Reset The Matrix
}

/**
 * Disables the orthographic view.
 */
public static void disableOrthoView() {
    GL11.glMatrixMode( GL11.GL_PROJECTION ); // Select Projection
    GL11.glPopMatrix();    // Pop The Matrix
    GL11.glMatrixMode( GL11.GL_MODELVIEW );  // Select Modelview
    GL11.glPopMatrix();    // Pop The Matrix
}

The output looks like this: image 1 (It seems to be textured with my water texture)

Or like this, if I disable texturing before drawing the text: image 2

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心的憧憬 2024-12-09 13:26:34

TrueTypeFonts 已被废弃。请改用 UnicodeFont。

    font = new UnicodeFont(awtFont);
    font.getEffects().add(new ColorEffect(java.awt.Color.white));
    font.addAsciiGlyphs();
    try {
        font.loadGlyphs();
    } catch (SlickException ex) {
       // Logger.getLogger(Game.class.getName()).log(Level.SEVERE, null, ex);
    }

这应该有帮助

TrueTypeFonts are depraceted. Use UnicodeFont instead.

    font = new UnicodeFont(awtFont);
    font.getEffects().add(new ColorEffect(java.awt.Color.white));
    font.addAsciiGlyphs();
    try {
        font.loadGlyphs();
    } catch (SlickException ex) {
       // Logger.getLogger(Game.class.getName()).log(Level.SEVERE, null, ex);
    }

this should help

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文