gpt4 book ai didi

java - 当代理移动时,旋转形状与相机一起移动

转载 作者:行者123 更新时间:2023-11-30 06:24:38 25 4
gpt4 key购买 nike

我创建了一个 3D 游戏,代理可以在其中向多个方向(北、南...)移动。

它工作正常,但是当我在代码中添加另一个形状 - 金字塔时,然后当我移动代理,金字塔会随着他移动,这意味着它会随着代理(又名相机)的移动而移动。

知道如何使形状静态化吗?

启用 gl2.glLoadIdentity(); 行会导致金字塔与相机保持一致。禁用 gl2.glLoadIdentity(); 会导致金字塔不出现。

代码:

类:Pyramid.java

import javax.media.opengl.GL2;


public class Pyramid {


private int m_pyramid;


public void createPyramid(GL2 gl2)
{
m_pyramid = gl2.glGenLists(1);
gl2.glNewList(m_pyramid, GL2.GL_COMPILE);

// set texture parameters
gl2.glTexParameteri ( GL2.GL_TEXTURE_2D,GL2.GL_TEXTURE_WRAP_T,GL2.GL_LINEAR_MIPMAP_LINEAR);
gl2.glTexParameteri( GL2.GL_TEXTURE_2D,GL2.GL_TEXTURE_WRAP_S,GL2.GL_LINEAR_MIPMAP_LINEAR);

gl2.glBegin(GL2.GL_TRIANGLES); // of the pyramid

// Font-face triangle
gl2.glTexCoord3f(1.0f, 0.0f, 0.0f); // Red
gl2.glVertex3d(0.0f, 1.0f, 0.0f);
gl2.glTexCoord3f(0.0f, 1.0f, 0.0f); // Green
gl2.glVertex3d(-1.0f, -1.0f, 1.0f);
gl2.glTexCoord3f(0.0f, 0.0f, 1.0f); // Blue
gl2.glVertex3d(1.0f, -1.0f, 1.0f);

// Right-face triangle
gl2.glTexCoord3f(1.0f, 0.0f, 0.0f); // Red
gl2.glVertex3d(0.0f, 1.0f, 0.0f);
gl2.glTexCoord3f(0.0f, 0.0f, 1.0f); // Blue
gl2.glVertex3d(1.0f, -1.0f, 1.0f);
gl2.glTexCoord3f(0.0f, 1.0f, 0.0f); // Green
gl2.glVertex3d(1.0f, -1.0f, -1.0f);

// Back-face triangle
gl2.glTexCoord3f(1.0f, 0.0f, 0.0f); // Red
gl2.glVertex3d(0.0f, 1.0f, 0.0f);
gl2.glTexCoord3f(0.0f, 1.0f, 0.0f); // Green
gl2.glVertex3d(1.0f, -1.0f, -1.0f);
gl2.glTexCoord3f(0.0f, 0.0f, 1.0f); // Blue
gl2.glVertex3d(-1.0f, -1.0f, -1.0f);

// Left-face triangle
gl2.glTexCoord3f(1.0f, 0.0f, 0.0f); // Red
gl2.glVertex3d(0.0f, 1.0f, 0.0f);
gl2.glTexCoord3f(0.0f, 0.0f, 1.0f); // Blue
gl2.glVertex3d(-1.0f, -1.0f, -1.0f);
gl2.glTexCoord3f(0.0f, 1.0f, 0.0f); // Green
gl2.glVertex3d(-1.0f, -1.0f, 1.0f);

gl2.glEnd(); // of the pyramid
gl2.glEndList();

}

}

世界 Controller .java

class WorldController extends GLCanvas implements GLEventListener, KeyListener 
{
private Point3D m_agent , m_center , m_coordinate1 , m_coordinate2 , m_coordinate3;
private GLU m_glu = new GLU();
private Pyramid m_pyra = new Pyramid();

public WorldController ()
{
this.addGLEventListener(this);

// initialize m_agent and axis
this.m_agent = new Point3D(750,200,1200);
this.m_coordinate1 = new Point3D(1,0,0);
this.m_coordinate2 = new Point3D(0,1,0);
this.m_coordinate3 = new Point3D(0,0,1);
this.m_center = new Point3D(0,0,0);
}

@Override
public void init (GLAutoDrawable gLDrawable)
{

GL2 gl2 = gLDrawable.getGL().getGL2(); // get the OpenGL graphics context
gl2.glEnable(GL2.GL_TEXTURE_2D); // get GL Utilities
gl2.glShadeModel(GL2.GL_SMOOTH);
gl2.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl2.glClearDepth(1.0f);
gl2.glEnable(GL2.GL_DEPTH_TEST);
gl2.glDepthFunc(GL2.GL_LEQUAL);
gl2.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST);
gl2.glShadeModel(GL_SMOOTH); // blends colors nicely, and smoothes out lighting


// bind the pyramid with the current GL2
this.m_pyra.createPyramid(gl2);

/**
* Creating the listener
*/
((Component) gLDrawable).addKeyListener(this);

}


@Override
public void display(GLAutoDrawable gLDrawable)
{
final GL2 gl2 = gLDrawable.getGL().getGL2();
gl2.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
gl2.glLoadIdentity();

// set m_center and look at
m_center.setPoint(m_agent.getX() - m_coordinate3.getX() ,
m_agent.getY() - m_coordinate3.getY() ,
m_agent.getZ() - m_coordinate3.getZ());

m_glu.gluLookAt(m_agent.getX(),m_agent.getY(),m_agent.getZ(),
m_center.getX(),m_center.getY(),m_center.getZ(),
m_coordinate2.getX(),m_coordinate2.getY(),m_coordinate2.getZ());


/**
* pyramid
*/

gl2.glPushMatrix();
// gl2.glLoadIdentity();
gl2.glTranslatef(-2f, 0.0f, -10.0f); // translate left and into the screen
gl2.glRotatef(anglePyramid, 0.1f, 1.0f, -0.1f); // rotate about the y-axis
gl2.glCallList(5);
gl2.glPopMatrix(); // <-- and here?
anglePyramid += speedPyramid;

}


@Override
public void reshape(GLAutoDrawable gLDrawable,int x,int y,int width,int height)
{

GL2 gl2 = gLDrawable.getGL().getGL2(); // get the OpenGL 2 graphics context
if(height <= 0) height = 1; // prevent divide by zero

float aspect = (float)width / (float)height;
gl2.glMatrixMode(GL_PROJECTION);
gl2.glLoadIdentity();
m_glu.gluPerspective(50.0f, aspect, 1.0, 10000.0);
gl2.glMatrixMode(GL_MODELVIEW);
gl2.glLoadIdentity();
}
}

以下是代理人移动时的一些照片: enter image description here

enter image description here

可能会解释问题的实时视频:http://www.youtube.com/watch?v=zCBqx69b_e4&feature=youtu.be

最佳答案

我建议您从显示函数中剥离生成显示列表的代码。将它放在 init 函数或您用来初始化对象的任何函数中。

其次,只将对构造您的对象(金字塔)很重要的生成代码、转换和定义放入此生成代码中。不要在其中放入任何与将其定位到世界系统相关的转换。请记住,它只需要生成一次,并且当您的列表名称有效时,您只需要一遍又一遍地调用它以将其显示到帧缓冲区中。

不要将 glLoadIdentity() 放在显示列表中,它们会重置您的模型 View 矩阵,我敢肯定,这不是您想要的。要保留模型 View 矩阵的内容,您可以使用矩阵堆栈子系统,通过使用 glPushMatrix 和 glPopMatrix 函数调用。

因此,关于您的代码,我建议进行以下修改:

@Override
public void init (GLAutoDrawable gLDrawable)
{

GL2 gl2 = gLDrawable.getGL().getGL2(); // get the OpenGL graphics context
gl2.glEnable(GL2.GL_TEXTURE_2D); // get GL Utilities
gl2.glShadeModel(GL2.GL_SMOOTH);
gl2.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl2.glClearDepth(1.0f);
gl2.glEnable(GL2.GL_DEPTH_TEST);
gl2.glDepthFunc(GL2.GL_LEQUAL);
gl2.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST);
gl2.glShadeModel(GL_SMOOTH); // blends colors nicely, and smoothes out lighting
((Component) gLDrawable).addKeyListener(this);

// ADDED HERE, OR IN ANY POSITION THAT MAKE MORE SENSE
int pyramid = gl2.glGenLists(1);
gl2.glNewList(pyramid, GL2.GL_COMPILE);
// gl2.glLoadIdentity(); // reset the model-view matrix <-- TAKE THIS OFF
gl2.glBegin(GL2.GL_TRIANGLES); // of the pyramid

// Font-face triangle
gl2.glTexCoord3f(1.0f, 0.0f, 0.0f); // Red
gl2.glVertex3d(0.0f, 1.0f, 0.0f);
gl2.glTexCoord3f(0.0f, 1.0f, 0.0f); // Green
gl2.glVertex3d(-1.0f, -1.0f, 1.0f);
gl2.glTexCoord3f(0.0f, 0.0f, 1.0f); // Blue
gl2.glVertex3d(1.0f, -1.0f, 1.0f);

// Right-face triangle
gl2.glTexCoord3f(1.0f, 0.0f, 0.0f); // Red
gl2.glVertex3d(0.0f, 1.0f, 0.0f);
gl2.glTexCoord3f(0.0f, 0.0f, 1.0f); // Blue
gl2.glVertex3d(1.0f, -1.0f, 1.0f);
gl2.glTexCoord3f(0.0f, 1.0f, 0.0f); // Green
gl2.glVertex3d(1.0f, -1.0f, -1.0f);

// Back-face triangle
gl2.glTexCoord3f(1.0f, 0.0f, 0.0f); // Red
gl2.glVertex3d(0.0f, 1.0f, 0.0f);
gl2.glTexCoord3f(0.0f, 1.0f, 0.0f); // Green
gl2.glVertex3d(1.0f, -1.0f, -1.0f);
gl2.glTexCoord3f(0.0f, 0.0f, 1.0f); // Blue
gl2.glVertex3d(-1.0f, -1.0f, -1.0f);

// Left-face triangle
gl2.glTexCoord3f(1.0f, 0.0f, 0.0f); // Red
gl2.glVertex3d(0.0f, 1.0f, 0.0f);
gl2.glTexCoord3f(0.0f, 0.0f, 1.0f); // Blue
gl2.glVertex3d(-1.0f, -1.0f, -1.0f);
gl2.glTexCoord3f(0.0f, 1.0f, 0.0f); // Green
gl2.glVertex3d(-1.0f, -1.0f, 1.0f);


gl2.glEnd(); // of the pyramid
gl2.glEndList();

// END HERE
}


@Override
public void display(GLAutoDrawable gLDrawable)
{
final GL2 gl2 = gLDrawable.getGL().getGL2();
gl2.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
gl2.glLoadIdentity();

// agent and center settings
m_center.setPoint(m_agent.getX() - m_coordinate3.getX() ,
m_agent.getY() - m_coordinate3.getY() ,
m_agent.getZ() - m_coordinate3.getZ());

m_glu.gluLookAt(m_agent.getX(),m_agent.getY(),m_agent.getZ(),
m_center.getX(),m_center.getY(),m_center.getZ(),
m_coordinate2.getX(),m_coordinate2.getY(),m_coordinate2.getZ());


/**
* pyramid
*/

glPushMatrix(); // <-- did you note this function here?

gl2.glTranslatef(-2f, 0.0f, -10.0f); // translate left and into the screen
gl2.glRotatef(anglePyramid, 0.1f, 1.0f, -0.1f); // rotate about the y-axis

gl2.glCallList(pyramid);
glPopMatrix(); // <-- and here?
anglePyramid += speedPyramid;

这不会完全解决您的问题,一旦您没有展示您是如何控制相机的。但是尝试按照代码中的指示放置 glPushMatrix 和 glPopMatrix

附言。再次查看您的代码后,我注意到了这段代码:

 // agent and center settings
m_center.setPoint(m_agent.getX() - m_coordinate3.getX() ,
m_agent.getY() - m_coordinate3.getY() ,
m_agent.getZ() - m_coordinate3.getZ());

m_glu.gluLookAt(m_agent.getX(),m_agent.getY(),m_agent.getZ(),
m_center.getX(),m_center.getY(),m_center.getZ(),
m_coordinate2.getX(),m_coordinate2.getY(),m_coordinate2.getZ());

m_center.setPoint... 的目的是什么?这个函数有什么作用?我猜你的问题可能出在这里。

关于java - 当代理移动时,旋转形状与相机一起移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16615802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com