JOGL程序只显示黑色

时间:2017-09-04 09:52:16

标签: java opengl jogl

我正在尝试编写一个可以在.obj文件中读取的小程序(" airboat.obj"),然后绘制每个程序的面孔屏幕。不要担心文件中的任何纹理。

读取文件似乎工作正常,但在运行时,窗口只显示黑色。

import com.jogamp.opengl.*;
import com.jogamp.opengl.awt.GLCanvas;
import com.jogamp.opengl.glu.GLU;
import com.jogamp.opengl.util.FPSAnimator;

import javax.swing.*;
import java.awt.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;


public class Main implements GLEventListener{
    private GLU glu;
    private float angle = 0;
    private float dx = 0f, dy = 0f, dz = 0f;
    private float rotX = 0f, rotY = 0f, rotZ = 0f;

    private ArrayList<int []> f;
    private ArrayList<float []> v;

    private Main(){

        f = new ArrayList<>();
        v = new ArrayList<>();

        loadObj(v, f);
    }

    private void loadObj(ArrayList<float []> V, ArrayList<int []> F){
        try{
            BufferedReader br = new BufferedReader( new FileReader("airboat.obj"));
            String line;
            while (br.ready()){
                line = br.readLine();
                int counter = 0;
                if(line.startsWith("v")){
                    String[] lineParts = line.split(" ");
                    float[] vertex = new float[3];
                    for (String number: lineParts) {
                        if (!number.equals("v")){
                            float numint = Float.parseFloat(number);
                            vertex[counter] = numint;
                            counter++;
                        }
                    }
                    V.add(vertex);

                }else if (line.startsWith("f")){
                    String[] lineParts = line.split(" ");
                    int[] face = new int[lineParts.length-1];
                    for (String number: lineParts){
                        if (!number.equals("f")){
                            int numint = Integer.parseInt(number);
                            face[counter] = numint;
                            counter++;
                        }
                    }
                    F.add(face);
                }
            }
            br.close();
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    @Override
    public void init(GLAutoDrawable drawable){
        // TODO Auto-generated method stub
        GL2 gl = drawable.getGL().getGL2();
        glu = new GLU(); 
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        gl.glClearDepth(1.0f);
        gl.glEnable(GL2.GL_DEPTH_TEST);
        //gl.glDepthFunc(GL2.GL_LEQUAL);
        gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST);
        gl.glShadeModel(GL2.GL_SMOOTH); 
    }

    @Override
    public void display(GLAutoDrawable autoDrawable){
        final GL2 gl  = autoDrawable.getGL().getGL2();

        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        gl.glClearDepth(1.0f);  
        gl.glEnable(GL2.GL_DEPTH_TEST);
        gl.glDepthFunc(GL2.GL_LEQUAL);
        gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST);
        gl.glShadeModel(GL2.GL_SMOOTH);

        gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);

        gl.glLoadIdentity();
        draw(gl);
    }

    private void draw(GL2 gl){
        for(int i = 0; i < f.size(); i++){
            final int temp [] = (int []) f.get(i);
            gl.glColor3f(0f,1f,0f);
            if (temp.length == 3){
                gl.glBegin(GL2.GL_TRIANGLES);
            }else if (temp.length == 4){
                gl.glBegin(GL2.GL_QUADS);
            }else if (temp.length == 5){
                gl.glBegin(GL2.GL_POLYGON);
            }
            for (int j: temp) {
                final float[] temp2 = (float[]) v.get(j-1);
                gl.glVertex3f(temp2[0], temp2[1], temp2[2]);
            }
            gl.glEnd();
            gl.glFlush();
        }
    }

    @Override
    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height){
        GL2 gl = drawable.getGL().getGL2(); context
        if (height <= 0) height = 1;  // prevent divide by zero

        final float h = ( float ) width / ( float ) height;
        gl.glViewport( 0, 0, width, height );
        gl.glMatrixMode( GL2.GL_PROJECTION );
        gl.glLoadIdentity();

        glu.gluPerspective(45.0, h, 45, 100.0);
        gl.glMatrixMode( GL2.GL_MODELVIEW );
        gl.glLoadIdentity();
    }

    @Override
    public void dispose(GLAutoDrawable autoDrawable){

    }

    public static void main(String[] args) {
        final GLCanvas glCanvas = new GLCanvas();
        Main app = new Main();
        glCanvas.addGLEventListener(app);
        glCanvas.setSize(1280, 720);
        glCanvas.setBackground(Color.white);


        final JFrame aFrame = new JFrame("Airboat Example");
        aFrame.getContentPane().add(glCanvas);
        aFrame.pack();
        aFrame.setVisible(true);

        final FPSAnimator animator = new FPSAnimator(glCanvas, 60, true);
        animator.start();
    }
}

关于如何在将来调试此类事情的任何建议也将非常受欢迎。 提前感谢您的帮助。

0 个答案:

没有答案