myCanvas.addKeyListener(this) 在 JOGL 中抛出错误

问题描述 投票:0回答:0

我是 OpenGL 中 Java3D 的新手。如果我按左右箭头键分别移动水平轴和垂直轴,我想旋转立方体。因此我在这个程序中使用了 keyListener 接口。在实例化 keyListener 时会抛出错误,特此附上我的代码供您参考。

import java.io.*;
import java.lang.Math;
import java.nio.*;
import javax.swing.*;
import static com.jogamp.opengl.GL4.*;
import com.jogamp.opengl.*;
import com.jogamp.opengl.awt.GLCanvas;
import com.jogamp.opengl.util.texture.*;
import com.jogamp.common.nio.Buffers;
import com.jogamp.newt.event.KeyListener;

import org.joml.*;
import java.awt.event.KeyEvent;

public class Code extends JFrame implements GLEventListener,KeyListener
{   private GLCanvas myCanvas;
    
    private int renderingProgram;
    private int vao[] = new int[1];
    private int vbo[] = new int[3];
    private float cameraX, cameraY, cameraZ;
    private float objLocX, objLocY, objLocZ;
    
    // allocate variables for display() function
    private FloatBuffer vals = Buffers.newDirectFloatBuffer(16);
    private Matrix4f pMat = new Matrix4f();  // perspective matrix
    private Matrix4f vMat = new Matrix4f();  // view matrix
    private Matrix4f mMat = new Matrix4f();  // model matrix
    private Matrix4f mvMat = new Matrix4f(); // model-view matrix
    private int mvLoc, pLoc;
    private float aspect;
    
    private int shuttleTexture;
    private int rotateX = 0,rotateY = 0;
    
    private int numObjVertices;
    private ImportedModel myModel;
    

    public Code()
    {   setTitle("Chapter6 - program3");
        setSize(600, 600);
        myCanvas = new GLCanvas();
        myCanvas.addKeyListener(this); //this line throws an error.
        myCanvas.addGLEventListener(this);
        
        this.add(myCanvas);
        myCanvas.setFocusable(true);
        this.setVisible(true);
    }

    public void display(GLAutoDrawable drawable)
    {   GL4 gl = (GL4) GLContext.getCurrentGL();
        gl.glClear(GL_COLOR_BUFFER_BIT);
        gl.glClear(GL_DEPTH_BUFFER_BIT);

        gl.glUseProgram(renderingProgram);

        int mvLoc = gl.glGetUniformLocation(renderingProgram, "mv_matrix");
        int pLoc = gl.glGetUniformLocation(renderingProgram, "p_matrix");

        vMat.identity().setTranslation(-cameraX,-cameraY,-cameraZ);

        mMat.identity();
        mMat.translate(objLocX, objLocY, objLocZ);

        mMat.rotateX((float)Math.toRadians(20.0f));
        mMat.rotateY((float)Math.toRadians(130.0f));
        mMat.rotateZ((float)Math.toRadians(5.0f));

        mvMat.identity();
        mvMat.mul(vMat);
        mvMat.mul(mMat);

        gl.glUniformMatrix4fv(mvLoc, 1, false, mvMat.get(vals));
        gl.glUniformMatrix4fv(pLoc, 1, false, pMat.get(vals));

        gl.glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
        gl.glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
        gl.glEnableVertexAttribArray(0);

        gl.glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
        gl.glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0);
        gl.glEnableVertexAttribArray(1);

        gl.glActiveTexture(GL_TEXTURE0);
        gl.glBindTexture(GL_TEXTURE_2D, shuttleTexture);

        gl.glEnable(GL_DEPTH_TEST);
        gl.glDepthFunc(GL_LEQUAL);
        gl.glDrawArrays(GL_TRIANGLES, 0, myModel.getNumVertices());
    }

    public void init(GLAutoDrawable drawable)
    {   GL4 gl = (GL4) GLContext.getCurrentGL();
    
        myModel = new ImportedModel("cube1.obj");
        renderingProgram = Utils.createShaderProgram("src/code/vertShader.glsl", "src/code/fragShader.glsl");

        float aspect = (float) myCanvas.getWidth() / (float) myCanvas.getHeight();
        pMat.identity().setPerspective((float) Math.toRadians(60.0f), aspect, 0.1f, 1000.0f);

        setupVertices();
        cameraX = 0.0f; cameraY = 0.0f; cameraZ = 3.0f;
        objLocX = 0.0f; objLocY = 0.0f; objLocZ = 0.0f;

        shuttleTexture = Utils.loadTexture("Material5.jpg");
    }

    private void setupVertices()
    {   GL4 gl = (GL4) GLContext.getCurrentGL();
    
        numObjVertices = myModel.getNumVertices();
        Vector3f[] vertices = myModel.getVertices();
        Vector2f[] texCoords = myModel.getTexCoords();
        Vector3f[] normals = myModel.getNormals();
        
        float[] pvalues = new float[numObjVertices*3];
        float[] tvalues = new float[numObjVertices*2];
        float[] nvalues = new float[numObjVertices*3];
        
        for (int i=0; i<numObjVertices; i++)
        {   pvalues[i*3]   = (float) (vertices[i]).x() + rotateX;
            pvalues[i*3+1] = (float) (vertices[i]).y() + rotateY;
            pvalues[i*3+2] = (float) (vertices[i]).z();
            tvalues[i*2]   = (float) (texCoords[i]).x();
            tvalues[i*2+1] = (float) (texCoords[i]).y();
            nvalues[i*3]   = (float) (normals[i]).x();
            nvalues[i*3+1] = (float) (normals[i]).y();
            nvalues[i*3+2] = (float) (normals[i]).z();
        }
        
        gl.glGenVertexArrays(vao.length, vao, 0);
        gl.glBindVertexArray(vao[0]);
        gl.glGenBuffers(vbo.length, vbo, 0);
        
        gl.glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
        FloatBuffer vertBuf = Buffers.newDirectFloatBuffer(pvalues);
        gl.glBufferData(GL_ARRAY_BUFFER, vertBuf.limit()*4, vertBuf, GL_STATIC_DRAW);

        gl.glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
        FloatBuffer texBuf = Buffers.newDirectFloatBuffer(tvalues);
        gl.glBufferData(GL_ARRAY_BUFFER, texBuf.limit()*4, texBuf, GL_STATIC_DRAW);

        gl.glBindBuffer(GL_ARRAY_BUFFER, vbo[2]);
        FloatBuffer norBuf = Buffers.newDirectFloatBuffer(nvalues);
        gl.glBufferData(GL_ARRAY_BUFFER, norBuf.limit()*4,norBuf, GL_STATIC_DRAW);
    }
    
    

    public void keyTyped(KeyEvent e) {}
    

    public static void main(String[] args) { 
        new Code();}


    public void dispose(GLAutoDrawable drawable) {}
    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)
    {   float aspect = (float) myCanvas.getWidth() / (float) myCanvas.getHeight();
        pMat.identity().setPerspective((float) Math.toRadians(60.0f), aspect, 0.1f, 1000.0f);
    }

    public void keyPressed(com.jogamp.newt.event.KeyEvent arg0) {
        // TODO Auto-generated method stub
        int key = arg0.getKeyCode();
        System.out.println(arg0.getKeyCode());
        if ( key == KeyEvent.VK_LEFT )
        {
            rotateX +=2;
            System.out.println("Left arrow is pressed");
        }
        else if ( key == KeyEvent.VK_RIGHT )
            rotateY += 2;
        else if ( key == KeyEvent.VK_DOWN)
            rotateX += 2;
        else if ( key == KeyEvent.VK_UP )
            rotateX -= 2;
        else if ( key == KeyEvent.VK_HOME )
            rotateX = rotateY = 0;
        
    }

    public void keyReleased(com.jogamp.newt.event.KeyEvent arg0) {}
}

输出

Component 类型中的方法 addKeyListener(KeyListener) 不适用于参数 (Code)

请任何人告诉我为什么我无法在 opengl java 中添加 add keylistener。

java opengl events keylistener jogl
© www.soinside.com 2019 - 2024. All rights reserved.