简体   繁体   English

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

[英]Game Events run fine, but scene does not render

I am creating my own simple game engine for use in some of my upcoming projects. 我正在创建自己的简单游戏引擎,以用于我即将进行的一些项目。 I have a world Object that stores a bunch of objects that extend a base class RenderObject. 我有一个世界对象,它存储了一堆扩展基类RenderObject的对象。

RenderObject contains things like scale rotation position smooth shading or not, etc. and forces all extending classes to implement their own method render(). RenderObject包含缩放比例旋转位置是否平滑阴影等内容,并强制所有扩展类实现其自己的方法render()。

To navigate my world, I have an FPS style camera control setup, which I've tested via system.out.println() to check pitch and yaw and it's working correctly. 为了导航我的世界,我有一个FPS风格的相机控件设置,我已经通过system.out.println()测试了该设置以检查俯仰和偏航,并且它工作正常。

However none of my objects are rendering. 但是我的任何物体都没有渲染。 My world objet has a method called renderObjects() which cycles through all the objects in the world and calls the render() function of them. 我的世界对象有一个称为renderObjects()的方法,该方法循环遍历世界中的所有对象并调用它们的render()函数。 FIXED - New problem... Now I'm getting 4 2x2 pixel dots that randomly fly around, in a specific place. 已修正-新问题...现在我得到4个2x2像素点,在特定位置随机飞来飞去。 and then I move my mouse the entire cluster moves. 然后我移动鼠标,整个集群都移动了。 Each game tick the onUpdate() method is called, and then lookThrough() 每个游戏都勾选了onUpdate()方法,然后调用lookThrough()

FPSCameraControl.java 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 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);
        }
    }
}

And RenderCube.java 还有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();
    }
}

Does anyone see what might not be working? 有人看到什么可能行不通吗? Cause I don't. 因为我没有。

Nothing was rendering at all. 根本没有渲染。

When I added: 当我添加时:

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

It started rendering, I did not think about the fact that at this point I was rendining in Orthographic view. 它开始渲染,我没有考虑到这一点,我正在正交视图中进行渲染。 I replaced that with: 我将其替换为:

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);

And the scene rendered completely. 场景完全渲染。 However my camera is stilla little messy, but this proves that my engine is working correctly so far now. 但是我的相机还是有点凌乱,但这证明我的引擎到目前为止工作正常。 the camera isn't part of the engine, it's something I made for the test, so the "Engine" is working correctly. 相机不是引擎的一部分,这是我为测试而制作的,因此“引擎”可以正常工作。

To make things simple, I'm going to have the World contain a Camera object, which will control the viewing location, angle, FoV, etc. 为简单起见,我将使World包含Camera对象,该对象将控制查看位置,角度,FoV等。

Thanks for the help @pwny - you did help, just a simple change. 感谢@pwny的帮助-您做了帮助,只是一个简单的更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM