0

我的游戏在帧率上非常滞后。我不确定它到底是什么,但我的相机愚蠢地跳来跳去。我想知道是否有人可以帮助我。它们只渲染屏幕上的内容,而不渲染您看不到的侧面。

此代码适用于我的第一堂课 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;     
}




}
4

4 回答 4

2

首先,您使用的是立即模式。改用显示列表或 VBO...

http://www.youtube.com/watch?v=-89BM2y3WIA&list=SP19F2453814E0E315&index=27 (这家伙有一系列的 LWJGL 教程顺便说一句)

您还为块的每一侧使用单独的纹理。Minecraft 渲染速度如此之快并使用如此少内存的部分原因是因为每个块都共享相同的纹理。每个块类型应该具有不同的纹理坐标,而不是具有不同的纹理。

尽管它是一种更高级的优化,但您可能还需要考虑剔除。简单的平截头体剔除(即不要画出你无论如何都看不到的东西)应该会带来显着的改进。

于 2013-01-14T16:46:25.847 回答
1

尝试在渲染方法时尽可能多地删除,例如将纹理加载到类方法中,例如:

public GE() {
//load textures and stuff you don't need to continuously loop
于 2013-06-02T16:04:57.013 回答
0

你应该搬家:

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

加载纹理的位置,(每个纹理仅调用一次)。

另一件事可能会有所帮助,不要以这种方式比较字符串,.equals()而是使用:

if(block == "Wood"){
    Front = "Wood";
}

变成

if(block.equals("Wood")){
    Front = "Wood";
}

事实上,当性能很重要时,你根本不应该使用字符串。您可以尝试使用枚举类型。

于 2013-07-08T11:31:37.833 回答
0

我看到你正在使用即时模式。这种编码方式被弃用是有充分理由的。可以使用它,因为我认为不会很快支持它,但现在开始学习它是一个坏主意。拿一张 OpengGl 参考卡,看看哪些是弃用的,哪些不是。此外,我鼓励您将上下文设置为前向兼容性 (org.lwjgl.opengl.ContextAttribs),并使用正确的教程。请记住,使用 OpenGl 的唯一原因是性能,所以不要浪费时间来学习太慢的界面。使用 glBegin/glEnd 绘制单个 qual 与绘制完整网格一样昂贵。

这是一个:http ://www.arcsynthesis.org/gltut/

于 2013-07-07T23:11:02.400 回答