LWJGL使用3D场景转换的文本

时间:2016-05-04 00:51:24

标签: java opengl 3d lwjgl

我正在尝试使用LWJGL制作一个学校项目的游戏,并且最近遇到了文本问题。我的问题是,每当我尝试在屏幕上绘制文本时,它就会移动并随3D场景变换,就像它是一个3D对象一样。我不确定是否有一些明确的方法我应该用来重置正在应用的着色器,但是如果有的话会很棒。这是我与LWJGL的第一个项目,任何帮助将不胜感激。谢谢!

我在下面添加了一些相关的方法和类,但完整的源代码位于https://github.com/SomeRandomPerson9/3DT

我的渲染方法:

public static void renderScene(){
    if(selectedScene.sceneType == SceneType.THREE_DIMENSIONAL) {

        for (RenderObject renderObject : selectedScene.getObjects()) {

            for(Mesh meshyThing : renderObject.getMeshs().values()){        
                selectedScene.getRegShader().bind();
                selectedScene.getRegShader().updateUniforms(renderObject.getTransform().getTransformation(), renderObject.getTransform().getProjectedTransformation(new Matrix4f().initTranslation(renderObject.getLocation().GetX(), renderObject.getLocation().GetY(), renderObject.getLocation().GetZ())), renderObject.getMaterial(meshyThing.getRequiredMtl()));
                meshyThing.draw();
            }
        }

        glMatrixMode(GL_PROJECTION);
        glLoadMatrix(selectedScene.getOrthographicProjectionMatrix());
        glMatrixMode(GL_MODELVIEW);

        for(GenericObject genericObject : selectedScene.getOverlayObjects()){
            if(genericObject instanceof TextObject){
                TextObject textObj = (TextObject)genericObject;
                try {
                    glPushMatrix();
                    glLoadIdentity();
                    //glDisable(GL_LIGHTING);
                    ProgramRefrence.fonts.arialFont.drawString(10, 10, "EulerCamera: [x=" + ProgramRefrence.formatters.hundredths.format(selectedScene.getCamera().getPos().GetX()) +
                            ",y=" + ProgramRefrence.formatters.hundredths.format(selectedScene.getCamera().getPos().GetY()) + ",z=" + ProgramRefrence.formatters.hundredths.format(selectedScene.getCamera().getPos().GetZ()) + "]");
                    //glEnable(GL_LIGHTING);
                    glPopMatrix();
                }catch(NullPointerException e){

                }
            }                
        }

        glMatrixMode(GL_PROJECTION);
        glLoadMatrix(selectedScene.getPerspectiveProjectionMatrix());
        glMatrixMode(GL_MODELVIEW);

    }
    else if(selectedScene.sceneType == SceneType.TWO_DIMENSIONAL){
        if(selectedScene instanceof Scene2DVideo){

            ((Scene2DVideo) selectedScene).render();
        }
    }
}

定义了getPerspectiveProjectionMatrix()和getOrthographicProjectionMatrix()。

public void setCamera(Camera camera) {
    this.camera = camera;
    this.getCamera().applyPerspectiveMatrix();
    GL11.glGetFloat(GL11.GL_PROJECTION_MATRIX, perspectiveProjectionMatrix);
    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glLoadIdentity();
    GL11.glOrtho(0, Display.getWidth(), Display.getHeight(), 0, 1, -1);
    GL11.glGetFloat(GL11.GL_PROJECTION_MATRIX, orthographicProjectionMatrix);
    GL11.glLoadMatrix(perspectiveProjectionMatrix);
    GL11.glMatrixMode(GL11.GL_MODELVIEW_MATRIX);

}

getRegShader()返回的内容:

package com.harry9137.api.render.shaders;

import com.harry9137.api.render.Material;
import com.harry9137.api.util.ResourceLoader;
import com.harry9137.api.render.math.Matrix4f;
import com.harry9137.api.render.math.Vector3f;
import com.harry9137.api.render.lighting.BaseLight;
import com.harry9137.api.render.lighting.DirectionalLight;
import com.harry9137.api.util.RenderUtil;

import org.newdawn.slick.Color;

import java.awt.*;

public class PhongShader extends Shader {
    private static final PhongShader instance = new PhongShader();

public static PhongShader getInstance() {
    return instance;
}

private static Vector3f ambientLight = new Vector3f(0.1f,0.1f,0.1f);
private static DirectionalLight directionalLight = new DirectionalLight(new BaseLight(new Vector3f(1,1,1), 0f), new Vector3f(0,0,0));

private PhongShader() {
    super();

    addVertexShader(ResourceLoader.loadShader("phongVertex.vs"));
    addFragmentShader(ResourceLoader.loadShader("phongFragment.fs"));
    compileShader();

    addUniform("transform");
    addUniform("baseColor");
    addUniform("ambientLight");

    addUniform("directionalLight.base.color");
    addUniform("directionalLight.base.intensity");
    addUniform("directionalLight.direction");
}

public static DirectionalLight getDirectionalLight() {
    return directionalLight;
}

public static void setDirectionalLight(DirectionalLight directionalLight) {
    PhongShader.directionalLight = directionalLight;
}

public void updateUniforms(Matrix4f worldMatrix, Matrix4f projectedMatrix, Material material) {
    setUniform("transform", projectedMatrix.mul(worldMatrix));
    if(material != null && material.getColor() != null) {
        setUniform("baseColor", material.getColor());
    }
    else{
        setUniform("baseColor", new Vector3f(255,255,255));
        //Color.white.bind();
    }

    if (material != null && material.getTexture() != null) {
        material.getTexture().bind();
    }
    else
        RenderUtil.unbindTextures();

    setUniform("ambientLight", ambientLight);

    setUniform("directionalLight", directionalLight);
}

public static Vector3f getAmbientLight() {
    return ambientLight;
}

public static void setAmbientLight(Vector3f ambientLight) {
    PhongShader.ambientLight = ambientLight;
}
public void setUniform(String uniformName, BaseLight baseLight){
    setUniform(uniformName + ".color", baseLight.getColor());
    setUniformf(uniformName + ".intensity", baseLight.getIntensity());
}
public void setUniform(String uniformName, DirectionalLight directionalLight)
{
    setUniform(uniformName + ".base", directionalLight.getBase());
    setUniform(uniformName + ".direction", directionalLight.getDirection());
}
}

0 个答案:

没有答案