我的游戏在帧速率上非常滞后。我不确定它究竟是什么,但我的镜头傻傻地跳了起来。我想知道是否有人可以帮助我。它们只渲染屏幕上的内容,不会渲染出你看不到的边。
此代码适用于我的第一个名为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;
}
}
答案 0 :(得分:2)
一开始你正在使用立即模式。请改用显示列表或VBO ......
http://www.youtube.com/watch?v=-89BM2y3WIA&list=SP19F2453814E0E315&index=27 (这家伙有一整套LWJGL教程BTW)
您还在块的每一侧使用单独的纹理。部分原因是Minecraft渲染如此之快并使用如此少的内存,因为每个块共享相同的纹理。每个块类型应该具有不同的纹理坐标,而不是具有不同的纹理。
虽然它是一种更高级的优化,但您可能还想考虑剔除。简单的视锥体剔除(即不画出你无法看到的东西)应该会有显着的改善。
答案 1 :(得分:1)
尝试在渲染方法时尽可能多地删除,例如将纹理加载到类方法中,例如:
public GE() {
//load textures and stuff you don't need to continuously loop
答案 2 :(得分:0)
我看到你正在使用立即模式。这种编码方式已被弃用是有充分理由的。可以使用它,因为我认为不会很快支持它,但现在开始学习它是一个坏主意。使用OpengGl参考卡查找已弃用的内容和未弃用的内容。另外,我鼓励您将上下文设置为向前兼容性(org.lwjgl.opengl.ContextAttribs),并使用正确的教程。请记住,使用OpenGl的唯一原因是性能,所以不要浪费时间去学习太慢的界面。使用glBegin / glEnd绘制单个qual与绘制完整网格一样昂贵。
答案 3 :(得分: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";
}
事实上,当表演很重要时,你根本不应该使用弦乐。您可以尝试使用枚举类型。