JOGL和Swing的问题

时间:2016-04-03 07:28:49

标签: java swing opengl jogl

我正在使用JOGL并且有一个glcanvas我正在渲染。然而,它只是闪红色然后变黑。我尝试使用不同的示例代码,它也是如此。 这是我的显示功能,如果有任何问题请告诉我!

public static void display(GLAutoDrawable drawable)
{
     System.out.println("Runnning display");
     GL2 gl = drawable.getGL().getGL2();



     // Clear screen
     gl.glClearColor(1, 0, 1, 0.5f);  // Purple
     gl.glClear(GL2.GL_STENCIL_BUFFER_BIT | GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);

     int [] vbo_handles = new int[2];
     gl.glGenBuffers(2, vbo_handles, 0);


     gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);

     float[] vertices = {  0.0f,  1.0f, 0.0f, //Top
               -1.0f, -1.0f, 0.0f, //Bottom Left
                1.0f, -1.0f, 0.0f  //Bottom Right
                                };


     FloatBuffer fbVertices = Buffers.newDirectFloatBuffer(vertices);
     gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, vbo_handles[VERTICES_IDX]);
     int numBytes = vertices.length*4;
     gl.glBufferData(GL.GL_ARRAY_BUFFER, numBytes, fbVertices, GL.GL_STATIC_DRAW);

     // Associate Vertex attribute 0 with the last bound VBO
     gl.glVertexAttribPointer(0 /* the vertex attribute */, 3,
          GL2.GL_FLOAT, false /* normalized? */, 0 /* stride */,
                                 0 /* The bound VBO data offset */);

     // VBO
     gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, 0); // You can unbind the VBO after it have been associated using glVertexAttribPointer

     gl.glEnableVertexAttribArray(0);


     float[] colors = {    1.0f, 0.0f, 0.0f, 1.0f, //Top color (red)
         0.0f, 0.0f, 0.0f, 1.0f, //Bottom Left color (black)
         1.0f, 1.0f, 0.0f, 0.9f  //Bottom Right color (yellow) with 10% transparence
         };

    FloatBuffer fbColors = Buffers.newDirectFloatBuffer(colors);

    // Select the VBO, GPU memory data, to use for colors
    gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, vboHandles[COLOR_IDX]);

    numBytes = colors.length * 4;
    gl.glBufferData(GL.GL_ARRAY_BUFFER, numBytes, fbColors, GL.GL_STATIC_DRAW);
    fbColors = null; // It is OK to release CPU color memory after transfer to GPU

     //Associate Vertex attribute 1 with the last bound VBO
    gl.glVertexAttribPointer(1 /* the vertex attribute */, 4 /* four possitions used for each vertex */,
    GL2.GL_FLOAT, false /* normalized? */, 0 /* stride */,
            0 /* The bound VBO data offset */);

    gl.glEnableVertexAttribArray(1);

    gl.glDrawArrays(GL2.GL_TRIANGLES, 0, 3); //Draw the vertices as triangle

    gl.glDisableVertexAttribArray(0); // Allow release of vertex position memory

   }
}

It seems like it isn't calling the display function for the GLEventListener.


public class UserInterface implements GLEventListener{
    private JTree tree;
    JPopupMenu popupmenu;
    JFrame frame = new JFrame("NecroTEK 3D Game Graphics Engine");

    public void initUI()
    {
        System.out.println("Initializing UI");
        TreeDemo();

        //JFrame frame = new JFrame("NecroTek 3D Game Modeling Engine");
        JPopupMenu popupMenu = new JPopupMenu();
        popupMenu.setSize(200, 400);
        popupMenu.setBackground(Color.black);
        popupMenu.setForeground(Color.white);
        popupMenu.setBorder(BorderFactory.createLineBorder(Color.white));



        /**********MENUBAR - TOP MENU***************/

        JMenuBar menuBar = new JMenuBar();

        menuBar.setLayout(new BoxLayout(menuBar, BoxLayout.X_AXIS));



        JMenu fileMenu = new JMenu("File");
        fileMenu.setFont(new Font("Arial", Font.BOLD, 10));


        fileMenu.setToolTipText("Load and Save Projects and Scenes");

        JMenu EditMenu = new JMenu("Edit");
        EditMenu.setFont(new Font("Arial", Font.BOLD, 10));
        EditMenu.setToolTipText("Cut, Copy and Paste");

        JMenu ViewMenu = new JMenu("View");
        ViewMenu.setFont(new Font("Arial", Font.BOLD, 10));
        JMenuItem clear = new JMenuItem("Clear");
        clear.setFont(new Font("Arial", Font.BOLD, 10));
        JMenuItem fullscreen = new JMenuItem("Fullscreen");
        fullscreen.setFont(new Font("Arial", Font.BOLD, 10));
        JMenu imagemode = new JMenu("Image Mode");
        imagemode.setFont(new Font("Arial", Font.BOLD, 10));
        JMenuItem bitmap = new JMenuItem("Bitmap");
        bitmap.setFont(new Font("Arial", Font.BOLD, 10));
        JMenuItem greyscale = new JMenuItem("Greyscale");
        greyscale.setFont(new Font("Arial", Font.BOLD, 10));
        JMenuItem rgbcolor = new JMenuItem("RGB Color");
        rgbcolor.setFont(new Font("Arial", Font.BOLD, 10));
        JMenuItem cmykcolor = new JMenuItem("CMYK Color");
        cmykcolor.setFont(new Font("Arial", Font.BOLD, 10));
        JMenuItem eightbitchannel = new JMenuItem("8 bit channel");
        eightbitchannel.setFont(new Font("Arial", Font.BOLD, 10));
        JMenuItem sixteenbitchannel = new JMenuItem("16 bit channel");
        sixteenbitchannel.setFont(new Font("Arial", Font.BOLD, 10));
        JMenuItem thirtytwobitchannel = new JMenuItem("32 bit channel");
        thirtytwobitchannel.setFont(new Font("Arial", Font.BOLD, 10));
        imagemode.add(bitmap);
        imagemode.add(greyscale);
        imagemode.add(rgbcolor);
        imagemode.add(cmykcolor);
        imagemode.add(eightbitchannel);
        imagemode.add(sixteenbitchannel);
        imagemode.add(thirtytwobitchannel);
        ViewMenu.add(imagemode);

        JMenu shadingMenu = new JMenu("Shading");
        shadingMenu.setFont(new Font("Arial", Font.BOLD, 10));
        JMenuItem smooth = new JMenuItem("Smooth");
        smooth.setFont(new Font("Arial", Font.BOLD, 10));
        JMenuItem flat = new JMenuItem("Flat");
        flat.setFont(new Font("Arial", Font.BOLD, 10));
        shadingMenu.add(smooth );
        smooth.setFont(new Font("Arial", Font.BOLD, 10));
        shadingMenu.add(flat );
        ViewMenu.add(shadingMenu);

        JMenuItem renderpresets = new JMenuItem("Render Presets");
        renderpresets.setFont(new Font("Arial", Font.BOLD, 10));
        ViewMenu.add(renderpresets);

        JMenu performance = new JMenu("Performance");
        performance.setFont(new Font("Arial", Font.BOLD, 10));

        JMenu mathperformance = new JMenu("Math");
        mathperformance.setFont(new Font("Arial", Font.BOLD, 10));

So that's a stub of the UI function. The end of it is..

        /**********************/

        frame.setBounds(0, 0, 640, 480);
        frame.setPreferredSize(new Dimension(1200,800));
        frame.setResizable(false);
        frame.setVisible( true );



        mainScreen.setLayout(new FlowLayout());
        left.setLayout(new FlowLayout());
        right.setLayout(new FlowLayout());
        bottom.setLayout(new FlowLayout());


        /*
        GLProfile glprofile = GLProfile.getDefault();
        GLCapabilities glcapabilities = new GLCapabilities( glprofile );
        final ` glcanvas = new GLCanvas( glcapabilities );
         glcanvas.setMinimumSize(new Dimension(100,100));
         glcanvas.setMaximumSize(new Dimension(100,100));
        int z = 300;
        int h = 300;

        JPanel canvasPanel = new JPanel();
        canvasPanel.add(glcanvas);
        frame.getContentPane().add(glcanvas);
        frame.setSize(frame.getContentPane().getPreferredSize());
        frame.setVisible(true);
        frame.setName("3D Line");

         frame.setTitle("Necro3D World Editor");
         frame.pack();
         frame.setVisible(true);        


        */











frame.setVisible(true);
frame.setName("3D Line");

 frame.setTitle("Necro3D World Editor");
 frame.pack();
 frame.setVisible(true);


//addWindowListener(this);

JPanel toolPanel = new JPanel();
toolPanel.setLayout(new FlowLayout());
toolPanel.add(toolbar);


JPanel rightsidePanel = new JPanel();
rightsidePanel.add(right);
rightsidePanel.add(toolPanel);


frame.add(mainScreen, BorderLayout.CENTER);
frame.add(left, BorderLayout.LINE_START);
frame.add(rightsidePanel, BorderLayout.LINE_END);
frame.add(bottom, BorderLayout.PAGE_END);
frame.setBackground(Color.RED);

/ ************* OPENGL STUFF ****************** /

GLProfile glprofile = GLProfile.getDefault();
GLCapabilities glcapabilities = new GLCapabilities( glprofile );
final GLCanvas glcanvas = new GLCanvas( glcapabilities );

glcanvas.setMinimumSize(new Dimension(100,100));
 glcanvas.setMaximumSize(new Dimension(100,100));
 glcanvas.setBounds(0,0, 520, 320);
int z = 300;
int h = 300;

JPanel canvasPanel = new JPanel();
canvasPanel.add(glcanvas);
mainScreen.add(canvasPanel);

/*****************************************/




    /**************DIALOGUES*************/

    layersButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

            JPanel testPanel = new JPanel();
            testPanel.setLayout(new GridLayout(20,20));

            JLabel title = new JLabel("Select layer");
            testPanel.add(title);
            for (int i=0;i<40;i++)
            {
                JButton newbutton = new JButton(" ");
                newbutton.setSize(new Dimension(10,10));
                newbutton.setLayout(new FlowLayout());
                testPanel.add(newbutton);
            }

...

frame.add(mainScreen, BorderLayout.CENTER);
    //frame.add(canvasPanel, BorderLayout.SOUTH);
    frame.add(left, BorderLayout.LINE_START);
    frame.add(rightsidePanel, BorderLayout.LINE_END);
    frame.add(bottom, BorderLayout.PAGE_END);
    frame.setBackground(Color.RED);



    frame.setSize(500, 300);
    //center the JFrame on the screen
    //centerWindow(frame);

    //
    //4. Size the frame.

    frame.pack();

    //5. Show it.
    frame.setVisible(true);
    glcanvas.requestFocus();
    System.out.println("Frame Set Visible")


            JOptionPane.showMessageDialog(frame, testPanel);

        }

    });

...

@Override
    public void display(GLAutoDrawable arg0) {
        // TODO Auto-generated method stub
        System.out.println("Display");
        GLRenderer.display(arg0);

    }
    @Override
    public void dispose(GLAutoDrawable arg0) {
        // TODO Auto-generated method stub
        System.out.println("Dispose");
    }
    @Override
    public void init(GLAutoDrawable arg0) {
        // TODO Auto-generated method stub
        System.out.println("Init");
        GLRenderer.init(arg0);
    }
    @Override
    public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4) {
        // TODO Auto-generated method stub
        System.out.println("Reshape");
    }


    /**************/
    // An inner class to check whether mouse events are the popup trigger
    class MousePopupListener extends MouseAdapter {
        public void mousePressed(MouseEvent e) {
            checkPopup(e);
        }

        public void mouseClicked(MouseEvent e) {
            checkPopup(e);
        }

        public void mouseReleased(MouseEvent e) {
            checkPopup(e);
        }

        private void checkPopup(MouseEvent e) {
            if (e.isPopupTrigger()) {
                popupmenu.show(popupmenu, e.getX(), e.getY());
            }
        }
    }


    // An inner class to show when popup events occur
    class PopupPrintListener implements PopupMenuListener {
        public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
            System.out.println("Popup menu will be visible!");
        }

        public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
            System.out.println("Popup menu will be invisible!");
        }

        public void popupMenuCanceled(PopupMenuEvent e) {
            System.out.println("Popup menu is hidden!");
        }`
    }

}

没有GL侦听器正在执行。

1 个答案:

答案 0 :(得分:1)

有些事似乎是错的:

  • 您正在使用display()
  • 生成每个glGenBuffers的新缓冲区
  • glEnableClientState已弃用
  • 您正在使用display()vertices vbo_handles[VERTICES_IDX]glBufferData数据加载到vertices。如果vboHandles[COLOR_IDX]是静态的,则您不需要
  • fbColors
  • 相同
  • 由于null是直接缓冲区,因此您无法通过将其分配给{{1}}来清除它,您必须执行this
  • 之类的操作