无法在屏幕上呈现三角形

问题描述

我一直在尝试使用 opengl 在 lwjgl 的窗口中渲染一个三角形,但到目前为止没有成功。我什至遵循了指南 (https://lwjglgamedev.gitbooks.io/3d-game-development-with-lwjgl/content/chapter04/chapter4.html),但我的屏幕上根本没有显示三角形。 这是关于渲染的代码

(Renderer.java)

package com.yellow.engine.rendering;

import org.lwjgl.system.memoryStack;

import com.yellow.engine.utils.Utils;

import static org.lwjgl.opengl.GL33.*;
import static org.lwjgl.system.memoryStack.*;

import java.nio.FloatBuffer;
public class Renderer {

private ShaderProgram shader;

private int vaoId,vboId;

float[] vertices = new float[]{
    0.0f,0.5f,0.0f,-0.5f,0.0f
};

public void init(String vertexPath,String fragmentPath) throws Exception{
    shader = new ShaderProgram();
    shader.createVertexShader(Utils.loadResource(vertexPath));
    shader.createFragmentShader(Utils.loadResource(fragmentPath));
    shader.link();

   try(MemoryStack stack = stackPush()){
       FloatBuffer verticesBuffer = stack.callocFloat(vertices.length);
       
       // Crea il VAO e bindalo
       vaoId = glGenVertexArrays();
       glBindVertexArray(vaoId);

       // Crea il VBO e bindalo
       vboId = glGenBuffers();
       glBindBuffer(GL_ARRAY_BUFFER,vboId);
       glBufferData(GL_ARRAY_BUFFER,verticesBuffer,GL_STATIC_DRAW);

       // Crea posizione 0 per la shader
       // (posizione dei vertici)
       glEnabLevertexAttribArray(0);

       // Definisci la struttura dell'array di vertici
       glVertexAttribPointer(0,3,GL_FLOAT,false,0);

       // Unbinda VBO e VAO
       glBindBuffer(GL_ARRAY_BUFFER,0);
       glBindVertexArray(0);

   }
}

public void draw(){
    shader.bind();

    glBindVertexArray(vaoId);
    glEnabLevertexAttribArray(0);

    glDrawArrays(GL_TRIANGLES,3); // Primitivo,Index primo vertice,Numero di vertici

    gldisabLevertexAttribArray(0);
    glBindVertexArray(0);

    shader.unbind();
}

public void dispose(){
    if(shader != null) { shader.dispose(); }

    gldisabLevertexAttribArray(0);

    // Elimina VBO
    glBindBuffer(GL_ARRAY_BUFFER,0);
    glDeleteBuffers(vboId);

    // Elimina VAO
    glBindVertexArray(0);
    glDeleteVertexArrays(vaoId);
}

}

(ShaderProgram.java)

package com.yellow.engine.rendering;

import static org.lwjgl.opengl.GL33.*;

import com.yellow.engine.utils.Logger;

// Questa classe è usata per rendere più facile l'unione di vertex e fragment shader.
// Per poter eseguire draw call alla GPU c'è bisogno anche di un Mesh.
public class ShaderProgram {

private final int programId;
private int vertexShaderId;
private int fragmentShaderId;

public ShaderProgram() throws Exception {
    programId = glCreateProgram();
    if (programId == 0) {
        throw new Exception("Could not create Shader");
    }
}

public void createVertexShader(String shadercode) throws Exception {
    vertexShaderId = createShader(shadercode,GL_VERTEX_SHADER);
}

public void createFragmentShader(String shadercode) throws Exception {
    fragmentShaderId = createShader(shadercode,GL_FRAGMENT_SHADER);
}

protected int createShader(String shadercode,int shaderType) throws Exception {
    int shaderId = glCreateShader(shaderType);
    if (shaderId == 0) {
        throw new Exception("Error creating shader. Type: " + shaderType);
    }

    glShaderSource(shaderId,shadercode);
    glCompileShader(shaderId);

    if (glGetShaderi(shaderId,GL_COMPILE_STATUS) == 0) {
        throw new Exception("Error compiling Shader code: " + glGetShaderInfoLog(shaderId,1024));
    }

    glAttachShader(programId,shaderId);

    return shaderId;
}

public void link() throws Exception {
    glLinkProgram(programId);
    if (glGetProgrami(programId,GL_LINK_STATUS) == 0) {
        throw new Exception("Error linking Shader code: " + glGetProgramInfoLog(programId,1024));
    }

    if (vertexShaderId != 0) {
        glDetachShader(programId,vertexShaderId);
    }
    if (fragmentShaderId != 0) {
        glDetachShader(programId,fragmentShaderId);
    }

    glValidateProgram(programId);
    if (glGetProgrami(programId,GL_VALIDATE_STATUS) == 0) {
        Logger.error("Warning validating Shader code: " + glGetProgramInfoLog(programId,1024));
    }

}

public void bind() {
    gluseProgram(programId);
}

public void unbind() {
    gluseProgram(0);
}

public void dispose() {
    unbind();
    if (programId != 0) {
        glDeleteProgram(programId);
    }
}
}

编辑 看来我找到了罪魁祸首:调用 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 在绘制三角形之前或之后,也会清除三角形本身。 这是正常行为吗?如果是这样,我应该如何阻止它清除它?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)