在lwjgl(Java)中文本呈现初始化代码后无法绘制任何内容

时间:2014-01-24 15:50:07

标签: java opengl rendering lwjgl

你好我有两个单独的初始化代码,用于在lwjgl中渲染2d形状和(2d)文本之间切换。如果执行渲染文本的初始化代码,则不会绘制2d形状。我尝试了所有内容,然后找到问题行:GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

在使用glBlendFunc完成某些操作后,我只能渲染tekst,切换到渲染2d形状的初始化代码将不再起作用。

以下是我的2个代码:

简单的2d渲染:

GL11.glEnable(GL_BLEND);
GL11.glMatrixMode(GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, Display.getWidth(), Display.getHeight(), 0, 1, -1);

GL11.glMatrixMode(GL_MODELVIEW);
GL11.glLoadIdentity();

呈现文字的代码:

GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glShadeModel(GL11.GL_SMOOTH);
GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glDisable(GL11.GL_LIGHTING);

GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
GL11.glClearDepth(1);

GL11.glEnable(GL11.GL_BLEND);

// Problem line
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

GL11.glViewport(0,0, 800, 600);
GL11.glMatrixMode(GL11.GL_MODELVIEW);

GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 800, 600, 0, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);

我认为问题只是一个错误的OpenGl状态,但我怎样才能把状态正确?

3 个答案:

答案 0 :(得分:1)

如果不能访问整个代码就很难找到问题,因此我可以发布一些猜测:

  1. 您是否清除了颜色和深度缓冲区(glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);)?
  2. 在绘制纹理时是否启用GL_TEXTURE_2D,否则禁用它?
  3. 我使用相同的混合功能,可以很好地绘制图像(这是用于绘制具有透明度的图像的最常用的混合功能)。

    PS:这是我用来绘制任何类型的2D东西的唯一设置(我使用顶点数组对象和着色器进行渲染):

        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    
        glEnable(GL_MULTISAMPLE);
    
        glEnable(GL_CULL_FACE);
        glCullFace(GL_BACK);
    
        glDisable(GL_DEPTH_TEST);
    

答案 1 :(得分:1)

这是更多代码

<强> Renderer.java

package game;

import static org.lwjgl.opengl.GL11.GL_BLEND;
import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_DEPTH_TEST;
import static org.lwjgl.opengl.GL11.GL_MODELVIEW;
import static org.lwjgl.opengl.GL11.GL_PROJECTION;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D;
import static org.lwjgl.opengl.GL11.glClear;
import static org.lwjgl.opengl.GL11.glEnable;
import static org.lwjgl.opengl.GL11.glLoadIdentity;
import static org.lwjgl.opengl.GL11.glMatrixMode;
import static org.lwjgl.util.glu.GLU.gluPerspective;
import static org.lwjgl.opengl.GL11.GL_CONSTANT_COLOR;
import static org.lwjgl.opengl.GL11.GL_ONE;

import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;

public class Renderer {

    private static boolean in3d = false;

    public static void initText2D() {

        in3d = false;

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glLoadIdentity();

        //GL11.glEnable(GL11.GL_TEXTURE_2D);
        GL11.glShadeModel(GL11.GL_SMOOTH);
        GL11.glDisable(GL11.GL_DEPTH_TEST);
        GL11.glDisable(GL11.GL_LIGHTING);

        GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        GL11.glClearDepth(1);

        GL11.glEnable(GL11.GL_BLEND);
        GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

        GL11.glViewport(0,0, 800, 600);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);

        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GL11.glOrtho(0, 800, 600, 0, 1, -1);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
    }

    public static void init2D() {

        in3d = false;

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glLoadIdentity();

        GL11.glEnable(GL_BLEND);
        GL11.glMatrixMode(GL_PROJECTION);
        GL11.glLoadIdentity();
        GL11.glOrtho(0, Display.getWidth(), Display.getHeight(), 0, 1, -1);

        GL11.glMatrixMode(GL_MODELVIEW);
        GL11.glLoadIdentity();

        GL11.glDisable(GL_TEXTURE_2D);

        // Test
        //GL11.glBlendFunc(GL_CONSTANT_COLOR, GL_ONE);

    }

    public static void init3D(float fov, float aspect, float near, float far) {

        in3d = true;

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(fov, aspect, near, far);
        GL11.glMatrixMode(GL_MODELVIEW);

        glEnable(GL_DEPTH_TEST);
        glEnable(GL_TEXTURE_2D);

    }

    public static void begin(int shape) {

        GL11.glBegin(shape);
    }

    public static void end() {

        GL11.glEnd();
    }

    public static void setVertex3(float x, float y, float z) {

        if(!in3d) {

            System.out.println("[WARNING] > Adding 3d vertex but there is no 3d context");
        }

        GL11.glVertex3f(x, y, z);
    }

    public static void setColor(float r, float g, float b) {

        GL11.glColor3f(r, g, b);
    }

    public static void setVertex2(float x, float y) {

        if(in3d) {

            System.out.println("[WARNING] > Adding 2d vertex while in 3d context");
        }

        GL11.glVertex2f(x, y);

    }
}

Button.java(我在lwjgl中绘制按钮的示例)

package gui;
import static org.lwjgl.opengl.GL11.GL_QUADS;
import static org.lwjgl.opengl.GL11.glBegin;
import static org.lwjgl.opengl.GL11.glEnd;
import static org.lwjgl.opengl.GL11.glVertex2f;
import game.Renderer;
import game.TextRenderer;
import org.lwjgl.opengl.GL11;
public class Button extends AbstractButton {

    private String text;

    public Button(int x, int y, int w, int h, String text) {

        this.setX(x);
        this.setY(y);
        this.setWidth(w);
        this.setHeight(h);
        this.text = text;

        paintElement();
    }

    public Button(String text) {

        this.text = text;
        paintElement();
    }

    public Button() {

        paintElement();
    }

    public void paintElement() {

        Renderer.init2D();

        Renderer.begin(GL11.GL_QUADS);

        Renderer.setColor(this.getColorR(), this.getColorG(), this.getColorB());

        Renderer.setVertex2(this.getX(), this.getY());
        Renderer.setVertex2(this.getX() + this.getWidth(), this.getY());
        Renderer.setVertex2(this.getX() + this.getWidth(), this.getY() + this.getHeight());
        Renderer.setVertex2(this.getX(), this.getY() + this.getHeight());

        Renderer.end();

        Renderer.initText2D();
        TextRenderer.drawString(this.getX() + 10, this.getY() + 10, this.text);
    }

}

<强> Main.java

package game;

import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_QUADS;
import static org.lwjgl.opengl.GL11.glBegin;
import static org.lwjgl.opengl.GL11.glClear;
import static org.lwjgl.opengl.GL11.glColor3f;
import static org.lwjgl.opengl.GL11.glEnd;
import static org.lwjgl.opengl.GL11.glLoadIdentity;
import static org.lwjgl.opengl.GL11.glMatrixMode;
import static org.lwjgl.opengl.GL11.glOrtho;
import static org.lwjgl.opengl.GL11.glVertex2f;

import java.awt.Font;

import gui.Button;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;
import org.newdawn.slick.TrueTypeFont;

public class Main {

    private int gamestate;
    private boolean closeRequested;

    private static Camera cam;
    private static User user;

    public Main() {

        createUser();
        createDisplay();
        createCamera();
        gameLoop();
        cleanUp();
    }

    private void createUser() {


    }

    private void createDisplay() {

        try {

            Display.setDisplayMode(new DisplayMode(800, 600));
            Display.setResizable(true);
            Display.create();


        } catch (LWJGLException e) {

            e.printStackTrace();
        }
    }

    private void createCamera() {


    }

    private void gameLoop() {

        while(!closeRequested) {

            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            glLoadIdentity();

            //Renderer.init2D();
            /*
            // Test 2d line
            Renderer.init2D();
            Renderer.setColor(1.0f,  1.0f,  1.0f);
            Renderer.begin(GL11.GL_LINE);
            Renderer.setVertex2(0, 0);
            Renderer.setVertex2(100, 100);
            Renderer.end();
            */

            // set the color of the quad (R,G,B,A)
            glColor3f(0.7f, 0.5f, 1.0f);

            // draw quad
            glBegin(GL_QUADS);
                glVertex2f(100,100);
                glVertex2f(100+200,100);
                glVertex2f(100+200,100+200);
                glVertex2f(100,100+200);
            glEnd();

            checkInput();

            Button b = new Button(100, 100, 100, 30, "Test");

            Display.update();
            Display.sync(10);
        }

        return;
    }

    private void checkInput() {

        // Check keyboard, mouse and other input

        if(Display.isCloseRequested()) {

            closeRequested = true;
        }

        return;
    }

    private void cleanUp() {


    }

    public static void main(String[] args) {

        new Main();
    }
}

答案 2 :(得分:1)

我开始工作了!

这是我的新(工作)代码:

<强> init2d

GL11.glEnable(GL_BLEND);
GL11.glMatrixMode(GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, Display.getWidth(), Display.getHeight(), 0, 1, -1);
GL11.glMatrixMode(GL_MODELVIEW);
GL11.glLoadIdentity();

// solution
GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);

<强> initText2d

GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glShadeModel(GL11.GL_SMOOTH);
GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glDisable(GL11.GL_LIGHTING);

GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
GL11.glClearDepth(1);

GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

GL11.glViewport(0,0, 800, 600);
GL11.glMatrixMode(GL11.GL_MODELVIEW);

GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 800, 600, 0, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);

我认为问题在于光滑使用纹理进行文本渲染,我必须启用纹理或将它们绑定到0。 请参阅:http://lwjgl.org/forum/index.php?topic=4019.0