我的游戏在帧率上非常滞后。我不确定它到底是什么,但我的相机愚蠢地跳来跳去。我想知道是否有人可以帮助我。它们只渲染屏幕上的内容,而不渲染您看不到的侧面。
此代码适用于我的第一堂课 GE,这是我的主课
import static org.lwjgl.opengl.GL11.*;
import java.text.Format.Field;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.newdawn.slick.opengl.Texture;
public class GE {
public static void main(String[] args) {
initDisplay();
gameLoop();
cleanUp();
}
public static void gameLoop() {
Texture front = Block.loadTexture(Block.getFront());
Texture back = Block.loadTexture(Block.getBack());
Texture Tright = Block.loadTexture(Block.getTright());
Texture Tleft = Block.loadTexture(Block.getTleft());
Texture top = Block.loadTexture(Block.getTop());
Texture bottom = Block.loadTexture(Block.getBottom());
Camera cam = new Camera(70, (float) Display.getWidth()
/ (float) Display.getHeight(), 0.3f, 1000);
while (!Display.isCloseRequested()) {
boolean forward = Keyboard.isKeyDown(Keyboard.KEY_W)
|| Keyboard.isKeyDown(Keyboard.KEY_UP);
boolean backward = Keyboard.isKeyDown(Keyboard.KEY_S)
|| Keyboard.isKeyDown(Keyboard.KEY_DOWN);
boolean left = Keyboard.isKeyDown(Keyboard.KEY_A);
boolean right = Keyboard.isKeyDown(Keyboard.KEY_D);
boolean crouch = Keyboard.isKeyDown(Keyboard.KEY_LSHIFT);
boolean jump = Keyboard.isKeyDown(Keyboard.KEY_SPACE);
if (forward)
cam.moveZ(2f);
if (backward)
cam.moveZ(-2f);
if (left)
cam.moveX(0.2f);// cam.rotateY(-0.1f);
if (right)
cam.moveX(-0.2f);// cam.rotateY(0.1f);
if (crouch)
cam.moveY(0.2f);
if (jump)
cam.moveY2(-0.2f);
if (Keyboard.isKeyDown(Keyboard.KEY_LEFT))
cam.rotateY(-2f);
if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT))
cam.rotateY(2f);
if (Keyboard.isKeyDown(Keyboard.KEY_Q))
cam.rotateX(-0.2f);
if (Keyboard.isKeyDown(Keyboard.KEY_Z))
cam.rotateX(0.2f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
cam.useView();
for(int x = 0; x < 100; x++){
for(int z = 0; z <100 ;z++){
glPushMatrix();
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_NEAREST);
front.bind();
glBegin(GL_QUADS);
{
// FrontFace
glTexCoord2f(1, 1);
glVertex3f(x-1, -2, z);
glTexCoord2f(0, 1);
glVertex3f(x, -2, z);
glTexCoord2f(0, 0);
glVertex3f(x, -1, z);
glTexCoord2f(1, 0);
glVertex3f(x-1, -1, z);
}
glEnd();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_NEAREST);
back.bind();
glBegin(GL_QUADS);
{
// BackFace
glTexCoord2f(1, 1);
glVertex3f(x, -2, z-1);
glTexCoord2f(0, 1);
glVertex3f(x-1, -2, z-1);
glTexCoord2f(0, 0);
glVertex3f(x-1, -1, z-1);
glTexCoord2f(1, 0);
glVertex3f(x, -1, z-1);
}
glEnd();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_NEAREST);
top.bind();
glBegin(GL_QUADS);
{
// Top Face
glTexCoord2f(0, 0);
glVertex3f(x-1, -1, z-1);
glTexCoord2f(0, 1);
glVertex3f(x, -1, z-1);
glTexCoord2f(1, 1);
glVertex3f(x, -1, z);
glTexCoord2f(1, 0);
glVertex3f(x-1, -1, z);
}
glEnd();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_NEAREST);
Tright.bind();
glBegin(GL_QUADS);
{
// RightFace
glTexCoord2f(0, 0);
glVertex3f(x, -1, z-1);
glTexCoord2f(0, 1);
glVertex3f(x, -2, z-1);
glTexCoord2f(1, 1);
glVertex3f(x, -2, z);
glTexCoord2f(1, 0);
glVertex3f(x, -1, z);
}
glEnd();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_NEAREST);
Tleft.bind();
glBegin(GL_QUADS);
{
// LeftFace
glTexCoord2f(0, 0);
glVertex3f(x-1, -1, z);
glTexCoord2f(0, 1);
glVertex3f(x-1, -2, z);
glTexCoord2f(1, 1);
glVertex3f(x-1, -2, z-1);
glTexCoord2f(1, 0);
glVertex3f(x-1, -1, z-1);
}
glEnd();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_NEAREST);
bottom.bind();
glBegin(GL_QUADS);
{
// BottomFace
glTexCoord2f(0, 0);
glVertex3f(x-1, -2, z-1);
glTexCoord2f(0, 1);
glVertex3f(x, -2, z-1);
glTexCoord2f(1, 1);
glVertex3f(x, -2, z);
glTexCoord2f(1, 0);
glVertex3f(x-1, -2, z);
}
glEnd();
}
glPopMatrix();
}
}
Display.update();
}
}
public static void cleanUp() {
Display.destroy();
}
public static void initDisplay() {
try {
Display.setDisplayMode(new DisplayMode(800, 600));
Display.setInitialBackground(135, 206, 260 );
Display.setTitle("#NervousBreakdown - Alpha 0.01");
Display.setResizable(true);
Display.create();
} catch (LWJGLException ex) {
Logger.getLogger(GE.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
这是我的二等相机
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.util.glu.GLU.*;
public class Camera {
private float x;
private float y;
private float z;
private float rx;
private float ry;
private float rz;
private float fov;
private float aspect;
private float near;
private float far;
public Camera(float fov, float aspect, float near, float far) {
x = 0;
y = 0;
z = 0;
rx = 0;
ry = 0;
rz = 0;
this.fov = fov;
this.aspect = aspect;
this.near = near;
this.far = far;
initProjection();
}
private void initProjection() {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fov, aspect, near, far);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
}
public void useView() {
glRotatef(rx, 1, 0, 0);
glRotatef(ry, 0, 1, 0);
glRotatef(rz, 0, 0, 1);
glTranslatef(x, y, z);
}
public float getX() {
return x;
}
public float getY() {
return y;
}
public float getZ() {
return z;
}
public void setX(float x) {
this.x = x;
}
public void setY(float y) {
this.y = y;
}
public void setZ(float z) {
this.z = z;
}
public float getRX() {
return rx;
}
public float getRY() {
return ry;
}
public float getRZ() {
return rz;
}
public void setRX(float rx) {
this.rx = rx;
}
public void setRY(float ry) {
this.ry = ry;
}
public void setRZ(float rz) {
this.rz = rz;
}
public void moveZ(float amt) {
z += amt * Math.sin(Math.toRadians(ry + 90));// *
// Math.sin(Math.toRadians(rx
// + 90));
x += amt * Math.cos(Math.toRadians(ry + 90));
// y += amt * Math.sin(Math.toRadians(rx));
}
public void moveX(float amt) {
z += amt * Math.sin(Math.toRadians(ry));
x += amt * Math.cos(Math.toRadians(ry));
}
public void moveY(float amt){
y= (float) (y + 0.002);
}
public void moveY2(float amt){
y= (float) (y - 0.002);
}
public void rotateY(float amt) {
ry += amt;
}
public void rotateX(float amt) {
rx += amt;
}
public void rotateZ(float amt) {
rz += amt;
}
最后是我的最后一个类块,它设置了主块中每个侧面的纹理:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
public class Block {
static String block = "Grass";
static String Front;
static String Back;
static String Top;
static String Tright;
static String Tleft;
static String Bottom;
public static Texture loadTexture(String key) {
try {
return TextureLoader.getTexture("png", new FileInputStream(
new File("res/textures/" + key + ".png")));
} catch (IOException ex) {
Logger.getLogger(GE.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
public static String getFront(){
if(block == "Grass"){
Front = "SideGrass";
}
if(block == "Wood"){
Front = "Wood";
}
return Front;
}
public static String getBack(){
if(block == "Grass"){
Back = "SideGrass";
}
if(block == "Wood"){
Back = "Wood";
}
return Back;
}
public static String getBottom(){
if(block == "Grass"){
Bottom = "Dirt";
}
if(block == "Wood"){
Bottom = "TopWood";
}
return Bottom;
}
public static String getTleft(){
if(block == "Grass"){
Tleft = "SideGrass";
}
if(block == "Wood"){
Tleft = "Wood";
}
return Tleft;
}
public static String getTright(){
if(block == "Grass"){
Tright = "SideGrass";
}
if(block == "Wood"){
Tright = "Wood";
}
return Tright;
}
public static String getTop(){
if(block == "Grass"){
Top = "Grass";
}
if(block == "Wood"){
Top = "TopWood";
}
return Top;
}
}