3D模型不会翻译

时间:2014-10-20 01:19:51

标签: java opengl lwjgl

我正在尝试让模型非常靠近相机以便可以看得更清楚,但它被绘制得如此遥远,我不知道如何将其移近。我尝试使用GL11.glTranslatef(0,0,1);让它靠近,但由于一些奇怪的原因,对象只是保持相同的距离,永远不会靠近。然后它在近/远平面视图之外消失。关于如何解决这个问题的任何想法?下面有一个班级的副本。

package engine;

import model.Face;
import model.Model;
import model.ObjLoader;

import org.lwjgl.opengl.GL11;
import org.lwjgl.util.vector.Vector3f;

public class Render {

    Model m = null;

    public Render() {

    }

    public void draw(){
        GL11.glClearColor(0, 0, 0, 1);
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
        GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_LINE);
        GL11.glBegin(GL11.GL_TRIANGLES);
            for (Face f : m.faces){
                //This code draws the model.
                Vector3f n1 = m.normals.get((int)f.normalIndicies.x - 1);
                GL11.glNormal3f(n1.x, n1.y, n1.z);
                Vector3f v1 = m.vertecies.get((int)f.indicies.x - 1);
                GL11.glVertex3f(v1.x, v1.y, v1.z); //First vertex


                Vector3f n2 = m.normals.get((int)f.normalIndicies.y - 1);
                GL11.glNormal3f(n2.x, n2.y, n2.z);
                Vector3f v2 = m.vertecies.get((int)f.indicies.y - 1);
                GL11.glVertex3f(v2.x, v2.y, v2.z); //second vertex

                Vector3f n3 = m.normals.get((int)f.normalIndicies.z - 1);
                GL11.glNormal3f(n3.x, n3.y, n3.z);
                Vector3f v3 = m.vertecies.get((int)f.indicies.z - 1);
                GL11.glVertex3f(v3.x, v3.y, v3.z); // third vertex
            }
        GL11.glEnd();

        GL11.glTranslatef(0, 0, 1); // <---- Here is the issue!!!

    }

    public void initOpenGL(){
        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GL11.glOrtho(0, 800, 600, 0, 1, -100);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);


        try{
            m = ObjLoader.loadModel("bunnyWithNormals.obj");
        }catch (Exception e){
            e.printStackTrace();
        }
        GL11.glTranslatef(400f, 300f, 0); // moves it onto the screen
    }

}

1 个答案:

答案 0 :(得分:1)

使用glFrustum() / glPerspective()代替glOrtho()