2017-09-01 68 views
-1

我想從「用Java語言編寫OpenGL計算機圖形編程」一書中學習JOGL。所以我顯然沒有工作知識。我精確地遵循了這個例子,花了很多時間來診斷我做錯了什麼。我最終試圖運行從本書提供的代碼,甚至沒有工作。我以前已經能夠得到一個三角形顯示,但一個三維立方體仍然躲過我。下面是這本書的逐字代碼:沒有顯示錯誤,屏幕顯示黑色,JOGL

public class Code extends JFrame implements GLEventListener 
{ private GLCanvas myCanvas; 
    private int rendering_program; 
    private int vao[] = new int[1]; 
    private int vbo[] = new int[2]; 
    private float cameraX, cameraY, cameraZ; 
    private float cubeLocX, cubeLocY, cubeLocZ; 
    private GLSLUtils util = new GLSLUtils(); 

    public Code() 
    { setTitle("Chapter4 - program1a"); 
     setSize(600, 600); 
     myCanvas = new GLCanvas(); 
     myCanvas.addGLEventListener(this); 
     getContentPane().add(myCanvas); 
     this.setVisible(true); 
    } 

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

     gl.glClear(GL_DEPTH_BUFFER_BIT); 

     gl.glUseProgram(rendering_program); 

     int mv_loc = gl.glGetUniformLocation(rendering_program, "mv_matrix"); 
     int proj_loc = gl.glGetUniformLocation(rendering_program, "proj_matrix"); 

     float aspect = (float) myCanvas.getWidth()/(float) myCanvas.getHeight(); 
     Matrix3D pMat = perspective(60.0f, aspect, 0.1f, 1000.0f); 

     Matrix3D vMat = new Matrix3D(); 
     vMat.translate(-cameraX, -cameraY, -cameraZ); 

     Matrix3D mMat = new Matrix3D(); 
     mMat.translate(cubeLocX, cubeLocY, cubeLocZ); 

     Matrix3D mvMat = new Matrix3D(); 
     mvMat.concatenate(vMat); 
     mvMat.concatenate(mMat); 

     gl.glUniformMatrix4fv(mv_loc, 1, false, mvMat.getFloatValues(), 0); 
     gl.glUniformMatrix4fv(proj_loc, 1, false, pMat.getFloatValues(), 0); 

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

     gl.glEnable(GL_DEPTH_TEST); 
     gl.glDepthFunc(GL_LEQUAL); 

     gl.glDrawArrays(GL_TRIANGLES, 0, 36); 
    } 

    public void init(GLAutoDrawable drawable) 
    { GL4 gl = (GL4) drawable.getGL(); 
     rendering_program = createShaderProgram(); 
     setupVertices(); 
     cameraX = 0.0f; cameraY = 0.0f; cameraZ = 8.0f; 
     cubeLocX = 0.0f; cubeLocY = -2.0f; cubeLocZ = 0.0f; 
    } 

    private void setupVertices() 
    { GL4 gl = (GL4) GLContext.getCurrentGL(); 
     float[] vertex_positions = 
     { -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 
      1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 
      1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 
      1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 
      1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 
      -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 
      -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 
      -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 
      -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 
      1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, 
      -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 
      1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f 
     }; 

     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(vertex_positions); 
     gl.glBufferData(GL_ARRAY_BUFFER, vertBuf.limit()*4, vertBuf, GL_STATIC_DRAW); 
    } 

    private Matrix3D perspective(float fovy, float aspect, float n, float f) 
    { float q = 1.0f/((float) Math.tan(Math.toRadians(0.5f * fovy))); 
     float A = q/aspect; 
     float B = (n + f)/(n - f); 
     float C = (2.0f * n * f)/(n - f); 
     Matrix3D r = new Matrix3D(); 
     r.setElementAt(0,0,A); 
     r.setElementAt(1,1,q); 
     r.setElementAt(2,2,B); 
     r.setElementAt(3,2,-1.0f); 
     r.setElementAt(2,3,C); 
     r.setElementAt(3,3,0.0f); 
     return r; 
    } 

    public static void main(String[] args) { new Code(); } 
    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {} 
    public void dispose(GLAutoDrawable drawable) {} 

    private int createShaderProgram() 
    { GL4 gl = (GL4) GLContext.getCurrentGL(); 

     String vshaderSource[] = readShaderSource("resources/vert.shader"); 
     String fshaderSource[] = readShaderSource("resources/frag.shader"); 

     int vShader = gl.glCreateShader(GL_VERTEX_SHADER); 
     int fShader = gl.glCreateShader(GL_FRAGMENT_SHADER); 

     gl.glShaderSource(vShader, vshaderSource.length, vshaderSource, null, 0); 
     gl.glShaderSource(fShader, fshaderSource.length, fshaderSource, null, 0); 

     gl.glCompileShader(vShader); 
     gl.glCompileShader(fShader); 

     int vfprogram = gl.glCreateProgram(); 
     gl.glAttachShader(vfprogram, vShader); 
     gl.glAttachShader(vfprogram, fShader); 
     gl.glLinkProgram(vfprogram); 
     return vfprogram; 
    } 

     public String[] readShaderSource(String path) { 
      //reads in shader programs 
      ArrayList<String> builder = new ArrayList(); 

      try (InputStream in = new FileInputStream(path); 
        BufferedReader reader = new BufferedReader(new InputStreamReader(in))) { 

       String line; 
       while ((line = reader.readLine()) != null) { 
        builder.add(line + "\n"); 
       } 
      } catch (IOException ex) { 
       throw new RuntimeException("Failed to load a shader file!" 
         + System.lineSeparator() + ex.getMessage()); 
      } 


      return builder.toArray(new String[1]); 
    } 
} 

不是逐字添加我自己的文件加載器。

着色器:

#version 430 

out vec4 color; 

uniform mat4 mv_matrix; 
uniform mat4 proj_matrix; 

void main(void) 
{ color = vec4(1.0, 0.0, 0.0, 1.0); 
} 

#version 430 

layout (location=0) in vec3 position; 

uniform mat4 mv_matrix; 
uniform mat4 proj_matrix; 

void main(void) 
{ 
    gl_Position = proj_matrix * mv_matrix * vec4(position,1.0); 
} 

回答

0

我說:

GLProfile glp = GLProfile.getMaxProgrammableCore(true); 
GLCapabilities caps = new GLCapabilities(glp); 
myCanvas = new GLCanvas(caps); 

的書籍代碼和它的工作。