有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java JSFML深度缓冲区不工作

所以,我正在用OpenGL和Java开发一个游戏,我决定使用JSFML进行窗口、文本、图像加载等。。。我有一些小问题

OpenGL绘图工作得很好,直到我尝试玩深度,然后它就不工作了

以下是我目前的代码:

import java.io.*;

import org.jsfml.graphics.*;
import org.jsfml.window.*;

import static org.lwjgl.opengl.GL11.*;
//import static org.lwjgl.util.glu.GLU.*;

public class Main
{
    static Image icon;
    static Texture playert;
    static long a, b, c, d;
    static float e;
    public static void main(String[] args) throws Exception
    {
        c = 0l;
        icon = new Image();
        icon.loadFromStream(new FileInputStream("Icon.png"));
        ContextSettings contextSettings;

        contextSettings = new ContextSettings (24, 8, 0, 2, 1);

        VideoMode v = new VideoMode(800, 600);

        RenderWindow window = new RenderWindow(new VideoMode(800, 600), "SFML Game", org.jsfml.window.WindowStyle.DEFAULT, contextSettings);
        window.setIcon(icon);
        window.setFramerateLimit(120);
        Context.getContext().setActive(true);
        Text pauseMessage = new Text();
        Font font = new Font();
        font.loadFromStream(new FileInputStream("Minecraftia.ttf"));
        pauseMessage.setFont(font);
        pauseMessage.setCharacterSize(20);
        pauseMessage.setPosition(0.f, 0.f);
        pauseMessage.setColor(new Color(250, 250, 250));
        pauseMessage.setString("This is Mission:Iinfinity, prototyped in JSFML");
        Text fps = new Text();
        fps.setFont(font);
        fps.setCharacterSize(20);
        fps.setPosition(0.f, 20.f);
        fps.setColor(new Color(250, 250, 250));
        fps.setString("FPS: " + c);

        org.lwjgl.opengl.GLContext.useContext(Context.getContext());
        glDepthMask(true);
        glClearDepth(1.f);
        glEnable(GL_DEPTH_TEST);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        float ratio = window.getSize().x / window.getSize().y;
        glFrustum(-ratio, ratio, -1.f, 1.f, 1.f, 500.f);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        a = System.currentTimeMillis();
        d = System.currentTimeMillis();
        while (true)
        {
            window.clear();
            if(b - a >= 1000)
            {
                a = b;
                fps.setString("FPS: " + c);
                c = 0;
            }
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            window.pushGLStates();
            window.draw(pauseMessage);
            window.draw(fps);
            window.popGLStates();
            glPushMatrix();
            glTranslatef(0.0f, 0.0f, -10f);
            //glRotatef(10f, 0.f, 0.f, 1.f);
            glBegin(GL_POLYGON);
            glVertex3f(-0.5f, -0.5f, 0);
            glVertex3f(-0.5f, 0.5f, 0);
            glVertex3f(0.5f, 0.5f, 0);
            glVertex3f(0.5f, -0.5f, 0);
            glEnd();
            glPopMatrix();
            window.display();
            if(Keyboard.isKeyPressed(Keyboard.Key.ESCAPE))
            {
                window.close();
                System.exit(0);
            }
            b = System.currentTimeMillis();
            c++;
        }
    }
}

编辑:好的,修复了ContextSettings,但立方体仍然顽固地拒绝绘制超过1坐标深或1坐标回的图形


共 (1) 个答案

  1. # 1 楼答案

    当然,它不起作用,您正在以以下方式构造ContextSettings类对象:

    contextSettings = new ContextSettings(0, 4, 2, 32, 32);
    

    构造函数中每个参数的含义依次为:

    1. 整数深度位
    2. int模具位
    3. int抗锯齿级别
    4. 内部主版本
    5. 内部监控

    您正在请求具有0位深度缓冲区、4位模具缓冲区、2x MSAA和OpenGL 32.32的渲染上下文

    考虑一下这样一个更理智的事情:

    contextSettings = new ContextSettings (24, 8, 0, 2, 1);
    

    24位深度,8位模具,0x-MSAA,OpenGL 2.1