我试图在慢跑中移动相机,但是我的代码似乎什么也没做。如何通过键盘输入移动相机?我正在画一个旋转的六边形,我想移动相机,但似乎无法正常工作。这是我的代码:
import javax.media.opengl.*;
import javax.media.opengl.awt.*;
import com.sun.opengl.util.*;
import com.sun.opengl.util.gl2.GLUT;
import javax.media.opengl.glu.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class Rotation2 extends JFrame implements GLEventListener, KeyListener
{
GLCanvas canvas;
Animator an;
public Rotation2()
{
canvas=new GLCanvas();
an=new Animator(canvas);
add(canvas);
canvas.addGLEventListener(this);
canvas.addKeyListener(this);
setSize(600,400);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
an.start();
}
double eyeX = 0;
double eyeY = 0;
double eyeZ = 0;
public void init(GLAutoDrawable drawable)
{
GL2 gl=drawable.getGL().getGL2();
GLU glu = new GLU();
gl.glClearColor(0.0f,0.0f,0.0f,0.0f);
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glOrtho(-300,300,-200,200,200,-200);
//gl.glLoadIdentity();
gl.glMatrixMode(GL2.GL_MODELVIEW);
//glu.gluLookAt(5, 5, 5,0, 0, 0, 1, 100, 0);
gl.glClearDepth(1.0f); // Set background depth to farthest
gl.glEnable(GL2.GL_DEPTH_TEST); // Enable depth testing for z-culling
gl.glDepthFunc(GL2.GL_LEQUAL); // Set the type of depth-test
}
double x=0;
public void display(GLAutoDrawable drawable)
{
GL2 gl=drawable.getGL().getGL2();
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
gl.glEnable(GL2.GL_DEPTH_TEST);
gl.glLoadIdentity();
GLU glu=new GLU();
//gl.glPushMatrix();
gl.glScaled(2,2,2);
gl.glRotated(x,1,1,1);
x+=.05;
glu.gluLookAt(eyeX, eyeY, 2,0, 0, 0, 1, 100, 0);
gl.glColor3f(0,1f,0);
// front
gl.glBegin(GL2.GL_POLYGON);
gl.glVertex3d(25,50,25);
gl.glVertex3d(-25,50,25);
gl.glVertex3d(-50,0,25);
gl.glVertex3d(-25,-50,25);
gl.glVertex3d(25,-50,25);
gl.glVertex3d(50,0,25);
gl.glEnd();
//left
gl.glColor3f(1f,1f,0);
gl.glBegin(GL2.GL_POLYGON);
gl.glVertex3d(25,50,-25);
gl.glVertex3d(-25,50,-25);
gl.glVertex3d(-50,0,-25);
gl.glVertex3d(-25,-50,-25);
gl.glVertex3d(25,-50,-25);
gl.glVertex3d(50,0,-25);
gl.glEnd();
gl.glColor3f(1f,0f,0);
gl.glBegin(GL2.GL_POLYGON);
gl.glVertex3d(25,50,25);
gl.glVertex3d(25,50,-25);
gl.glVertex3d(-25,50,-25);
gl.glVertex3d(-25,50,25);
gl.glEnd();
gl.glColor3f(0f,0f,1f);
gl.glBegin(GL2.GL_POLYGON);
gl.glVertex3d(-25,50,25);
gl.glVertex3d(-25,50,-25);
gl.glVertex3d(-50,0,-25);
gl.glVertex3d(-50,0,25);
gl.glEnd();
gl.glColor3f(1f,1f,1f);
gl.glBegin(GL2.GL_POLYGON);
gl.glVertex3d(-50,0,25);
gl.glVertex3d(-50,0,-25);
gl.glVertex3d(-25,-50,-25);
gl.glVertex3d(-25,-50,25);
gl.glEnd();
gl.glColor3f(0,1f,1f);
gl.glBegin(GL2.GL_POLYGON);
gl.glVertex3d(-25,-50,25);
gl.glVertex3d(-25,-50,-25);
gl.glVertex3d(25,-50,-25);
gl.glVertex3d(25,-50,25);
gl.glEnd();
gl.glColor3f(1,0f,1f);
gl.glBegin(GL2.GL_POLYGON);
gl.glVertex3d(25,-50,25);
gl.glVertex3d(25,-50,-25);
gl.glVertex3d(50,0,-25);
gl.glVertex3d(50,0,25);
gl.glEnd();
gl.glColor3f(.2f,1f,.2f);
gl.glBegin(GL2.GL_POLYGON);
gl.glVertex3d(50,0,25);
gl.glVertex3d(50,0,-25);
gl.glVertex3d(25,50,-25);
gl.glVertex3d(25,50,25);
gl.glEnd();
//gl.glPopMatrix();
}
public void reshape(GLAutoDrawable drawable,int x,int y,int width,int height)
{}
public void dispose(GLAutoDrawable drawable)
{}
public static void main(String[] ar)
{
new Rotation2();
}
@Override
public void keyTyped(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_X) {
eyeX++;
}
else if (e.getKeyCode() == KeyEvent.VK_Y) {
eyeY++;
}
else if(e.getKeyCode() == KeyEvent.VK_Z) {
eyeZ++;
}
else {
}
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
}
最佳答案
那是不推荐使用的opengl,算了吧。
您还使用了非常古老的jogl,对其进行了更新,请继续jogamp.org,在“ Builds / Download”下单击“ zip”,然后下载jogamp-all-platforms.7z
提取它并按照here的说明设置jogl
采取这个Hello Triangle。
它已经使用了keyListener
,只需复制您为keyTyped
编写的代码并将其粘贴在keyPressed
下(您必须使用jogamp keyListener,示例中使用的是Java awt)。
让我们知道您是否有任何问题:)