游戏事件运行正常,但场景无法渲染

时间:2012-05-28 16:15:44

标签: java rendering game-engine lwjgl

我正在创建自己的简单游戏引擎,用于我即将开展的一些项目。我有一个世界对象,它存储了一堆扩展基类RenderObject的对象。

RenderObject包含诸如缩放旋转位置平滑着色等内容,并强制所有扩展类实现自己的方法render()。

为了导航我的世界,我有一个FPS风格的摄像机控制设置,我通过system.out.println()测试它来检查俯仰和偏航并且它正常工作。

但是我的对象都没有渲染。 我的world objet有一个名为renderObjects()的方法,它循环遍历世界上的所有对象并调用它们的render()函数。 FIXED - 新问题...... 现在我得到4个2x2像素点,在特定的地方随机飞来飞去。然后我移动我的鼠标整个群集移动。每个游戏勾选onUpdate()方法被调用,然后lookThrough()

FPSCameraControl.java

public class FPSCameraControl
{
    private boolean DEBUG = false;

    public Vector3f position = null;
    public float yaw = 0.0f;
    public float pitch = 0.0f;

    public FPSCameraControl(float x, float y, float z)
    {
        position = new Vector3f(x, y, z);
    }
    public void yaw_(float amount)
    {
        yaw -= amount;
        if(yaw>360){
            yaw=0+(yaw-360);
        }
        if(yaw<1){
            yaw=360-(1-yaw);
        }
    }
    public void pitch_(float amount)
    {
        pitch -= amount;
        if(pitch>=155)
        {
            pitch=155;
        }
        if(pitch<=25)
        {
            pitch=25;
        }
    }
    public void walkForward(float distance)
    {
        position.x -= -distance * (float)Math.sin(Math.toRadians(yaw));
        position.z += -distance * (float)Math.cos(Math.toRadians(yaw));
    }
    public void walkBackwards(float distance)
    {
        position.x += -distance * (float)Math.sin(Math.toRadians(yaw));
        position.z -= -distance * (float)Math.cos(Math.toRadians(yaw));
    }
    public void strafeLeft(float distance)
    {
        position.x -= distance * (float)Math.sin(Math.toRadians(yaw-90));
        position.z += distance * (float)Math.cos(Math.toRadians(yaw-90));
    }
    public void strafeRight(float distance)
    {
        position.x -= distance * (float)Math.sin(Math.toRadians(yaw+90));
        position.z += distance * (float)Math.cos(Math.toRadians(yaw+90));
    }
    public void lookThrough()
    {
        GL11.glRotatef(pitch, 1.0f, 0.0f, 0.0f);
        GL11.glRotatef(yaw, 0.0f, 1.0f, 0.0f);
        GL11.glTranslatef(position.x, position.y, position.z);
    }
    public void onUpdate()
    {
        debug("Updating View");

        //Calculate Sensitivity

        float sen = (0.02F*10);

        debug("Sensitivity: " + sen);

        //Get Center Of Screen

        int cx = Display.getDisplayMode().getWidth()/2;
        int cy = Display.getDisplayMode().getHeight()/2;

        //Get Mouse Position From Center

        int x = Mouse.getX()-cx;
        int y = Mouse.getY()-cy;

        debug("Mouse Moved: " + x + ", " + y);

        //Apply Inverting If Set



        //Apply Sensitivity

        float _yaw = (x*sen);
        float _pitch = (y*sen);

        yaw_(_yaw);
        pitch_(_pitch);

        debug("New Yaw: " + yaw);
        debug("New Pitch: " + pitch);

        UpdatePosition();

        //SET MOUSE TO CENTER

        Mouse.setCursorPosition(cx, cy);

        lookThrough();
    }
    public void printROT()
    {
        System.out.println("Pitch : "+pitch);
        System.out.println("Yaw : "+yaw);
        System.out.println();
        System.out.println("XYZ : "+position.x+", "+position.y+", "+position.z);
    }

    public void debug(String msg)
    {
        if(DEBUG)
        {
            System.out.println(msg);
        }
    }

    public void UpdatePosition()
    {
        float f=0;
        float b=0;
        float r=0;
        float l=0;
        boolean moved =false;
        boolean shift = Keyboard.isKeyDown(Keyboard.KEY_LSHIFT);
        if(Keyboard.isKeyDown(Keyboard.KEY_W))
        {
            if(shift)
            {
                f=0.04F;
            }
            else
            {
                f=0.02F;
            }
            moved=true;
        }
        if(Keyboard.isKeyDown(Keyboard.KEY_S))
        {
            if(shift)
            {
                b=0.04F;
            }
            else
            {
                b=0.02F;
            }
            moved=true;
        }
        if(Keyboard.isKeyDown(Keyboard.KEY_D))
        {
            if(shift)
            {
                r=0.025F;
            }
            else
            {
                r=0.016F;
            }
            moved=true;
        }
        if(Keyboard.isKeyDown(Keyboard.KEY_A))
        {
            if(shift)
            {
                l=0.02F;
            }
            else
            {
                l=0.016F;
            }
            moved=true;
        }
        if(moved)
        {
            walkForward(f);
            walkBackwards(b);
            strafeLeft(l);
            strafeRight(r);
        }
    }
}

RenderObject.java

public abstract class RenderObject
{
    public  double []  location  =  new  double [] {0, 0, 0};
    public  double []  rotation  =  new  double [] {0, 0, 0};
    public  double []     scale  =  new  double [] {1, 1, 1};
    public Color color = new Color(180, 180, 180);

    public boolean smooth = false;

    public RenderObject parent = null;
    public LinkedList<RenderObject> children = new LinkedList<RenderObject>();

    public abstract void render();

    public void setScale(double x, double y, double z)
    {
        scale = new double[]{x, y, z};
    }

    public void setParent(RenderObject object)
    {
        parent = object;
    }
    public void removeParent()
    {
        parent = null;
    }
    public RenderObject getParent()
    {
        return parent;
    }
    public void addChild(RenderObject object)
    {
        object.setParent(this);
        children.add(object);
    }
    public void removeChild(RenderObject object)
    {
        if(children.contains(object))
        {
            object.removeParent();
            children.remove(object);
        }
    }
}

和RenderCube.java

public class RenderCube extends RenderObject
{
    @Override
    public void render()
    {
        if(smooth)
        {
            GL11.glShadeModel(GL11.GL_SMOOTH);
        }
        else
        {
            GL11.glShadeModel(GL11.GL_FLAT);
        }
        GL11.glBegin(GL11.GL_QUADS);

        GL11.glPushMatrix();

        GL11.glColor3b( color.getRedByte(),color.getGreenByte(), color.getBlueByte() );

        GL11.glRotated(rotation[0], 1.0, 0.0, 0.0);
        GL11.glRotated(rotation[1], 0.0, 1.0, 0.0);
        GL11.glRotated(rotation[2], 0.0, 0.0, 1.0);

        //TOP
        GL11.glNormal3d(0.0, 1.0, 0.0);
        GL11.glVertex3d(location[0]-scale[0], location[1]+scale[1], location[2]-scale[2]);
        GL11.glVertex3d(location[0]-scale[0], location[1]+scale[1], location[2]+scale[2]);
        GL11.glVertex3d(location[0]+scale[0], location[1]+scale[1], location[2]+scale[2]);
        GL11.glVertex3d(location[0]+scale[0], location[1]+scale[1], location[2]-scale[2]);

        //BOTTOM
        GL11.glNormal3d(0.0, -1.0, 0.0);
        GL11.glVertex3d(location[0]-scale[0], location[1]-scale[1], location[2]-scale[2]);
        GL11.glVertex3d(location[0]-scale[0], location[1]-scale[1], location[2]+scale[2]);
        GL11.glVertex3d(location[0]+scale[0], location[1]-scale[1], location[2]+scale[2]);
        GL11.glVertex3d(location[0]+scale[0], location[1]-scale[1], location[2]-scale[2]);

        //FRONT
        GL11.glNormal3d(0.0, 0.0, 1.0);
        GL11.glVertex3d(location[0]-scale[0], location[1]+scale[1], location[2]+scale[2]);
        GL11.glVertex3d(location[0]+scale[0], location[1]+scale[1], location[2]+scale[2]);
        GL11.glVertex3d(location[0]+scale[0], location[1]-scale[1], location[2]+scale[2]);
        GL11.glVertex3d(location[0]-scale[0], location[1]-scale[1], location[2]+scale[2]);

        //BACK
        GL11.glNormal3d(0.0, 0.0, -1.0);
        GL11.glVertex3d(location[0]-scale[0], location[1]+scale[1], location[2]-scale[2]);
        GL11.glVertex3d(location[0]+scale[0], location[1]+scale[1], location[2]-scale[2]);
        GL11.glVertex3d(location[0]+scale[0], location[1]-scale[1], location[2]-scale[2]);
        GL11.glVertex3d(location[0]-scale[0], location[1]-scale[1], location[2]-scale[2]);

        //RIGHT
        GL11.glNormal3d(1.0, 0.0, 0.0);
        GL11.glVertex3d(location[0]+scale[0], location[1]+scale[1], location[2]+scale[2]);
        GL11.glVertex3d(location[0]+scale[0], location[1]+scale[1], location[2]-scale[2]);
        GL11.glVertex3d(location[0]+scale[0], location[1]-scale[1], location[2]-scale[2]);
        GL11.glVertex3d(location[0]+scale[0], location[1]-scale[1], location[2]+scale[2]);

        //LEFT
        GL11.glNormal3d(-1.0, 0.0, 0.0);
        GL11.glVertex3d(location[0]-scale[0], location[1]+scale[1], location[2]+scale[2]);
        GL11.glVertex3d(location[0]-scale[0], location[1]+scale[1], location[2]-scale[2]);
        GL11.glVertex3d(location[0]-scale[0], location[1]-scale[1], location[2]-scale[2]);
        GL11.glVertex3d(location[0]-scale[0], location[1]-scale[1], location[2]+scale[2]);

        Color _color = new Color(180, 180, 180);

        GL11.glColor3b( _color.getRedByte(),_color.getGreenByte(), _color.getBlueByte() );

        GL11.glPopMatrix();

        GL11.glEnd();
    }
}

有人看到可能不起作用的东西吗?因为我没有。

1 个答案:

答案 0 :(得分:0)

根本没有渲染。

当我添加:

GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(-500, 500, -281, 281, -1, 1);
GL11.glMatrixMode(GL11.GL_MODELVIEW); 

它开始渲染,我没有想到这一点,我在正交视图中进行了渲染。我将其替换为:

GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();      
GLU.gluPerspective(45.0f, ((float) 800) / ((float) 600), 0.1f, 100.0f);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);

完全渲染场景。然而,我的相机有点凌乱,但这证明我的发动机到目前为止工作正常。相机不是引擎的一部分,它是我为测试做的,所以“引擎”工作正常。

为了简单起见,我将让World包含一个Camera对象,它将控制观察位置,角度,FoV等。

感谢@pwny的帮助 - 您提供了帮助,只需进行一次简单的更改。