2017-02-27 2 views
0

화면에 렌더링하는 데 문제가 있습니다.셰이더가있는 버텍스 버퍼 객체/버텍스 배열 객체

private void initVBO() { 
    vao = GL30.glGenVertexArrays(); 
    GL30.glBindVertexArray(vao); 

    float[] vertices = { 
      -0.5f, 0.5f, 0f, 
      0.5f, 0.5f, 0f, 
      0.5f, -0.5f, 0f, 
      -0.5f, -0.5f, 0f 
    }; 
    FloatBuffer vertBuffer = BufferUtils.createFloatBuffer(vertices.length); 
    vertBuffer.put(vertices); 
    vertBuffer.flip(); 

    this.vboID = GL15.glGenBuffers(); 
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, this.vboID); 
    GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertBuffer, GL15.GL_STATIC_DRAW); 
    positionAttribute = GL20.glGetAttribLocation(getMaterial().getShader() 
      .getProgram(), "position"); 
    GL20.glEnableVertexAttribArray(positionAttribute); 
    GL20.glVertexAttribPointer(positionAttribute, 3, GL11.GL_FLOAT, false, 0, 
      0); 

    Color c = getMaterial().getColor(); 
    float[] colors = { 
     c.red, c.green, c.blue, c.alpha, 
     c.red, c.green, c.blue, c.alpha, 
     c.red, c.green, c.blue, c.alpha, 
    }; 
    FloatBuffer colorBuffer = BufferUtils.createFloatBuffer(colors.length); 
    colorBuffer.put(colors); 
    colorBuffer.flip(); 

    this.vbocID = GL15.glGenBuffers(); 
    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, this.vbocID); 
    GL15.glBufferData(GL15.GL_ARRAY_BUFFER, colorBuffer, GL15.GL_STATIC_DRAW); 
    colorAttribute = GL20.glGetAttribLocation(getMaterial().getShader().getProgram 
      (), "color"); 
    GL20.glEnableVertexAttribArray(colorAttribute); 
    GL20.glVertexAttribPointer(colorAttribute, 4, GL11.GL_FLOAT, false, 0, 0); 

    byte[] indices = { 
      0, 1, 2, 
      2, 3, 0 
    }; 
    ByteBuffer indicesBuffer = BufferUtils.createByteBuffer(indices.length); 
    indicesBuffer.put(indices); 
    indicesBuffer.flip(); 

    this.vboiID = GL15.glGenBuffers(); 
    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, this.vboiID); 
    GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, 
      GL15.GL_STATIC_DRAW); 

    GL30.glBindVertexArray(0); 

    GL20.glDisableVertexAttribArray(positionAttribute); 
    GL20.glDisableVertexAttribArray(colorAttribute); 

    GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); 
    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0); 
} 

public void render() { 
    GL20.glUseProgram(getMaterial().getShader().getProgram()); 

    GL30.glBindVertexArray(this.vao); 
    GL20.glEnableVertexAttribArray(positionAttribute); 
    GL20.glEnableVertexAttribArray(colorAttribute); 

    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, this.vboiID); 

    GL11.glDrawElements(GL11.GL_TRIANGLES, 6, GL11.GL_BYTE, 0); 

    GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0); 
    GL20.glDisableVertexAttribArray(colorAttribute); 
    GL20.glDisableVertexAttribArray(positionAttribute); 
    GL30.glBindVertexArray(0); 
    GL20.glUseProgram(0); 
} 

쉐이더가 제대로 컴파일하고, 임 위치와 색 보정의 속성을 바인딩. 그냥 glOrtho를 사용하고 있으며, 즉각적인 모드를 통한 그리기가 잘 작동하기 때문에 올바르게 설정되었습니다. 문제가 인덱스를 만들거나 glVertexAttribPointer 내의 문제 일 수 있는지 확실하지 않습니다. 나는 화면에 그려지는 사각형을 얻으려고하고있다.

업데이트 : 버텍스 쉐이더 :

#version 150 

in vec3 position; 
in vec4 color; 

out vec4 passColor; 

void main(void) { 
    gl_Position = vec4(position, 1.0); 
    passColor = color; 
} 

조각 쉐이더 :

#version 150 

in vec4 passColor; 

out vec4 outColor; 

void main(void) { 
    outColor = vec4(1.0, 1.0, 1.0, 1.0); 
} 

나는 또한 초기화를 업데이트하고 위의 코드를 렌더링. 많은 문제가 여기에있다

private void load(File vertex, File fragment) { 
     this.vertexID = FileUtility.loadShader(vertex, GL20.GL_VERTEX_SHADER); 
     this.fragmentID = FileUtility.loadShader(fragment, GL20.GL_FRAGMENT_SHADER); 

     programID = GL20.glCreateProgram(); 
     GL20.glAttachShader(programID, vertexID); 
     GL20.glAttachShader(programID, fragmentID); 

     GL20.glBindAttribLocation(programID, 0, "position"); 
     GL20.glBindAttribLocation(programID, 1, "color"); 

     GL30.glBindFragDataLocation(programID, 0, "outColor"); 

     GL20.glLinkProgram(programID); 
     GL20.glValidateProgram(programID); 

     int status = GL20.glGetProgrami(programID, GL20.GL_LINK_STATUS); 
     if (status != GL11.GL_TRUE) { 
      Log.print(LogLevel.Error, GL20.glGetProgramInfoLog(programID)); 
     } 
    } 
+0

에 표시되는 뭔가를해야한다? 'glOrtho'는'#version 110'과'ftransform' 또는'gl_ProjectionMatrix' 등을 사용하지 않는 한 이것을하지 않습니다. –

+0

'GL11.glViewport (0, 0, Configuration.getInstance()) .getWindowWidth(), Configuration.getInstance(). getWindowHeight()); GL11.glMatrixMode (GL11.GL_PROJECTION); GL11.glLoadIdentity(); i GL11.glOrtho (-1, 1, -1, -1, 1, -1); GL11.glMatrixMode (GL11.GL_MODELVIEW); ' 쉐이더 내에 투영 매트릭스를 설정하지 않습니다. 나는 꼭지점의 색깔 만 설정하고있다. – Greymouth

+0

확장되지 않은'#version 120' 쉐이더에서'in' /'out'을 정확히 사용하려면 어떻게 계획합니까? – genpfault

답변

0

, 주요 하나가 당신의 VBO 실제로 바인드되어 있지되고 있음 : 또한 여기에 쉐이더 로딩 코드입니다.

VAO를 만들고 바인드 한 다음 즉시 바인딩 해제합니다. 따라서 glBindVertexArray(0)으로 전화를 걸거나 전화 번호 initVBO의 맨 아래로 내리십시오. 또한 VAO 바인딩을 해제 할 때까지는 VBO를 바인드 해제하지 마십시오. 그렇지 않으면 VAO가 VBO에 바인딩되지 않습니다.

셰이더를 게시하지 않았지만 적어도 GLSL 1.30에 액세스 할 수있는 VAO를 사용하고 있습니다. 셰이더가 포함 된 glOrtho은 간략하게 만 지원되었으며 GLSL 1.20로 인해 사용이 중단되고 핵심 프로필에서 제거되었습니다. 가능한 빨리 고정 함수 행렬 스택에서 벗어나는 것이 좋습니다. LWJGL은 자체 행렬 클래스를 가지고 있으므로 스위치가 정말 간단해야합니다.

셰이더에서 다른 문제가 없다면, 첫 번째 것은 당신의 투영 행렬이 버텍스 쉐이더로 설정지고 어떻게 화면

+0

'GL30.glBindVertexArray (0);을 업데이트했습니다. GL20.glDisableVertexAttribArray (positionAttribute); GL20.glDisableVertexAttribArray (colorAttribute); GL15.glBindBuffer (GL15.GL_ARRAY_BUFFER, 0); GL15.glBindBuffer (GL15.GL_ELEMENT_ARRAY_BUFFER, 0); '은 그래서 순서로 바닥에 모든 이들의 이동,하지만 여전히 그리기 아무것도하지. – Greymouth

+0

질문을 쉐이더로 업데이트 할 수 있습니까?또한 백 페이스 컬링이 비활성화되어 있지 않으면 비활성화하십시오. 얼굴이 컬링되지 않았는지 확인하십시오. –

+0

코드가 업데이트되고 추가되었습니다. – Greymouth

관련 문제