所以,我正在开发一个使用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 coord deep或1 coord back
答案 0 :(得分:1)
当然它无法正常工作,您正在以这种方式构建ContextSettings
类对象:
contextSettings = new ContextSettings(0, 4, 2, 32, 32);
构造函数中每个参数的含义依次为:
您正在请求具有0位深度缓冲区,4位模板缓冲区,2x MSAA和OpenGL 32.32的渲染上下文!
考虑一下这样的事情:
contextSettings = new ContextSettings (24, 8, 0, 2, 1);
24位深度,8位模板,0xMSAA,OpenGL 2.1。