重绘时更改JOGL / OpenGL TextRenderer Wierd Rainbow Gradient

时间:2018-11-05 19:26:35

标签: java opengl jogl textrenderer

我正在尝试使用JOGL Java库中的TextRenderer在窗口上绘制文本,但是它不断获得这种怪异的彩虹般的色彩效果,该效果在重新绘制每个窗口时都会发生变化,这是唯一具有正确颜色的字母是第一个字母。

以下是一些屏幕截图:

Screenshot 1

Screenshot 2

Screenshot 3

我已经尝试过使用谷歌搜索,但这对Google来说并不是一件容易的事……我只是得到了有关如何向TextRenderer添加渐变的响应。

这是我当前正在使用的代码:

import com.jogamp.opengl.*;
import com.jogamp.opengl.awt.GLCanvas;
import com.jogamp.opengl.util.awt.TextRenderer;

import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;

class OpenGLRenderEngine extends RenderEngine implements GLEventListener{

    private TextRenderer textRenderer;

    OpenGLRenderEngine(Window window) {
        super(window);
    }

    @Override
    Component createComponent() {
        GLProfile profile = GLProfile.get(GLProfile.GL2);
        GLCapabilities capabilities = new GLCapabilities(profile);
        capabilities.setDepthBits(32);
        capabilities.setStencilBits(32);

        GLCanvas canvas = new GLCanvas(capabilities);
        canvas.addGLEventListener(this);

        return canvas;
    }

    public void init(GLAutoDrawable glAutoDrawable) {
        GL2 gl = glAutoDrawable.getGL().getGL2();

        textRenderer = new TextRenderer(new Font("SansSerif", Font.BOLD, 36), true, true);

        gl.glClearDepth(1.0f);
        gl.glEnable(GL2.GL_DEPTH_TEST);
        gl.glDepthFunc(GL2.GL_LEQUAL);
        gl.glEnable(GL2.GL_BLEND);
        gl.glBlendFunc(GL2.GL_SRC_ALPHA, GL2.GL_ONE_MINUS_SRC_ALPHA);
        gl.glEnable(GL2.GL_ALPHA_TEST);
        gl.glAlphaFunc(GL.GL_GREATER, 0);
    }

    public void dispose(GLAutoDrawable glAutoDrawable) {
        GL2 gl = glAutoDrawable.getGL().getGL2();
        gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT | GL2.GL_STENCIL_BUFFER_BIT);
        gl.glLoadIdentity();
    }

    public void display(GLAutoDrawable glAutoDrawable) {
        GL2 gl = glAutoDrawable.getGL().getGL2();
        Dimension size = window.size;

        gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT | GL2.GL_STENCIL_BUFFER_BIT);
        gl.glColorMask(false, false, false, false);
        gl.glLoadIdentity();

        OpenGLRenderer renderer = new OpenGLRenderer(gl, size);

        Point[] backgroundPoints = new Point[]{
            new Point(0, 0),
            new Point(size.width, 0),
            new Point(size.width, size.height),
            new Point(0, size.height)
        };
        Color backgroundColor = (Color) window.frame.getBackground();
        renderer.drawPolygon(backgroundPoints, backgroundColor, new Point(0, 0), Integer.MIN_VALUE);

        Layout rootLayout = window.getRootLayout();
        if(rootLayout != null){
            rootLayout.render(renderer, new Point(0, 0), size);
        }

        ArrayList<OpenGLRendererPolygon> polygonsList = renderer.getPolygons();
        OpenGLRendererPolygon[] polygons = new OpenGLRendererPolygon[polygonsList.size()];
        polygonsList.toArray(polygons);
        Arrays.sort(polygons);
        for(OpenGLRendererPolygon polygon : polygons){
            renderer.renderPolygon(polygon);
        }

        gl.glColorMask(true, true, true, true);
        textRenderer.beginRendering(size.width, size.height);
        textRenderer.draw("Text to draw", 0, 0);
        textRenderer.endRendering();

        gl.glFlush();
    }

    public void reshape(GLAutoDrawable glAutoDrawable, int x, int y, int width, int height) {
        window.size.width = width;
        window.size.height = height;
    }

}

0 个答案:

没有答案