我只是在尝试使用 LWJGL 进行不同的渲染方式。我遇到了这种方法glDrawPixels(width, height, format, type, bytebuffer)
。因此,使用这种方法,我决定尝试在 LWJGL 中仅渲染像素并提出以下建议:
package biz.boulter.pixTest;
import org.lwjgl.*;
import org.lwjgl.opengl.*;
import java.nio.ByteBuffer;
import static org.lwjgl.opengl.GL11.*;
public class Game
{
private static long secTimer;
private static int ticks = 0;
private static int frames = 0;
private static ByteBuffer buffer;
public static final int WIDTH = 1280;
public static final int HEIGHT = WIDTH / 16 * 9;
private static long lastFrame;
public static int[] pixels = new int[WIDTH * HEIGHT];
private static void tick()
{
}
private static void render()
{
for (int y = 0; y < HEIGHT; y++)
{
for (int x = 0; x < WIDTH; x++)
{
int color = pixels[y * WIDTH + x];
buffer.put((byte) ((color >> 16) & 0xFF));
buffer.put((byte) ((color >> 8) & 0xFF));
buffer.put((byte) ((color) & 0xFF));
buffer.put((byte) ((color >> 24) & 0xFF));
}
}
buffer.flip();
glDrawPixels(WIDTH, HEIGHT, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
}
public static int getDelta()
{
long now = getTime();
int delta = (int) (now - lastFrame);
lastFrame = now;
return delta;
}
public static long getTime()
{
return (Sys.getTime() * 100) / Sys.getTimerResolution();
}
public static long getMilliTime()
{
return (Sys.getTime() * 1000) / Sys.getTimerResolution();
}
public static void main(String[] args)
{
try
{
Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
Display.create();
}
catch (LWJGLException e)
{
e.printStackTrace();
System.exit(0);
}
getDelta();
secTimer = getMilliTime();
buffer = BufferUtils.createByteBuffer((WIDTH * HEIGHT) * 4);
while (!Display.isCloseRequested())
{
int delta = getDelta();
while (delta >= 1)
{
tick();
ticks++;
delta--;
}
render();
frames++;
if (getMilliTime() - secTimer >= 1000)
{
System.out.println(frames + " fps\t" + ticks + " ups");
ticks = 0;
frames = 0;
secTimer += 1000;
}
Display.update();
}
Display.destroy();
}
}
这非常有效,但是我注意到当我使用正常的 LWJGL 矩阵模式渲染时,fps 大约少了 500 帧。那么我怎样才能加快它的速度,这样我就不必遍历像素并将 RGBA 值添加到每一帧的字节缓冲区?