0

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

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

为了导航我的世界,我有一个 FPS 风格的相机控制设置,我已经通过 system.out.println() 对其进行了测试,以检查俯仰和偏航,它工作正常。

但是,我的所有对象都没有渲染。我的世界对象有一个名为 renderObjects() 的方法,它循环遍历世界中的所有对象并调用它们的 render() 函数。 已修复 - 新问题...现在我得到 4 个 2x2 像素点,它们在特定位置随机飞来飞去。然后我移动鼠标,整个集群移动。每个游戏tick都会调用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);
        }
    }
}

渲染对象.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();
    }
}

有谁看到什么可能不起作用?因为我没有。

4

1 回答 1

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 的帮助-您确实提供了帮助,只是一个简单的更改。

于 2012-05-30T19:51:17.927 回答