我正在开发一个类似于Minecraft的简单游戏,但是我曾经使用过一些旧的着色器系统,并且想开始使用现代着色器,但是由于卡在渲染中而无法在屏幕上显示任何内容...

我尝试了不同的方法:


glGetError()返回0
我试图在片段着色器中返回vec4(1,0,1,1),但是它没有用,所以我认为这是顶点着色器中的一个错误...


那么,您可以帮我弄清楚我的错误在哪里吗?

以下是代码:https://pastebin.com/dWJQkFZu

这是我的renderLoop:

public void render(){
    //check for resize
    if(Display.wasResized()){
        glViewport(0, 0, Display.getWidth(), Display.getHeight());
        guiManager.recalculate();
    }

    //create perspective
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    GLU.gluPerspective(Camera.fov, (float)Display.getWidth()/(float)Display.getHeight(), 0.1f, 1000000.0f);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    cam.render();
    world.render();
    skybox.render(cam.player.position);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, Display.getWidth(), Display.getHeight(), 0, 1, -1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glEnable(GL_ALPHA_TEST);
    glAlphaFunc(GL_GREATER, 0.5f);
    if(Camera.debug){
        FontManager.getDefaultFont().drawString(0, 0, "Debug - SaintsCube v-A0.5");
        FontManager.getDefaultFont().drawString(0, FontManager.getDefaultFont().getLineHeight()+1, "x: "+Math.floor(player.position.x));
        FontManager.getDefaultFont().drawString(0, FontManager.getDefaultFont().getLineHeight()*2+2, "y: "+Math.floor(player.position.y));
        FontManager.getDefaultFont().drawString(0, FontManager.getDefaultFont().getLineHeight()*3+3, "z: "+Math.floor(player.position.z));
        FontManager.getDefaultFont().drawString(0, FontManager.getDefaultFont().getLineHeight()*4+4, "yaw: "+Math.floor(player.rotation.x*-1));
        FontManager.getDefaultFont().drawString(0, FontManager.getDefaultFont().getLineHeight()*5+5, "pitch: "+Math.floor(player.rotation.y));
    }
    guiManager.render();
    glDisable(GL_ALPHA_TEST);
}


还有我的着色器:

chunk.vs:

#version 330

in vec3 position;
in vec2 texcoord;
in vec4 color;

uniform mat4 MVP;

out vec2 pass_texcoord;
out vec4 pass_color;

void main() {
    pass_texcoord = texcoord;
    pass_color = color;

    gl_Position = MVP * vec4(position, 1.0);
}


chunk.fs:

#version 330

out vec4 fragcolor;

uniform sampler2D sampler;

in vec2 pass_texcoord;
in vec4 pass_color;

void main() {
    vec4 texturecolor = texture(sampler, pass_texcoord) * pass_color;
    if(texturecolor.a <= 0.5){
        discard;
    }
    fragcolor = texturecolor;
}


请帮我 :)

最佳答案

如果要开始使用现代着色器方式,则不能使用立即方法。一切都将在着色器中计算(您必须自己编写,没有opengl预定义的优点)。所以不要打电话

    glTranslate3f(x, y, z);


您必须创建自己的“模型矩阵”,然后将其发送到着色器,然后将其应用于模型顶点的位置。 Modern OpenGL的全部重点是最大程度地减少与之的交互。您不必让CPU和GPU并行工作(GPU要运行得更快,CPU就会成为瓶颈),而是让CPU做一些数学运算,然后一劳永逸地将其推入GPU。

虽然您的Vertex Shader已经期望使用Matrix,但由于Java代码中缺少它而没有得到它,但是您从未传递任何Matrix。

统一的Mat4 MVP;

大例子:

顶点着色器代码:

#version 330 core

in vec4 position;
in vec4 color;

uniform mat4 model;
uniform mat4 projection;
uniform mat4 view;

out vec4 vertexColor;

void main() {
    // Just pass the color down to the fragment shader
    vertexColor = color;

    // Calculate the Model, View, Projection matrix into one ( in this order, right to left )
    mat4 mvp = projection * view * model;

    // Calculate the position of the vertex
    gl_Position = mvp * vec4(position.xyz,1);
}


片段着色器代码:

#version 330 core

in vec4 vertexColor;

out vec4 fragColor;

void main() {
    fragColor = vec4(vertexColor.xyz,1);
}


我没有看到您的Shader编译代码,但是我认为它没错...(生成Shader id,将源代码放入着色器中,进行编译,生成程序并将两个着色器都附加到它上)

编译后,您必须告诉OpenGL输出颜色在哪里:

glBindFragDataLocation(program, 0, "fragColor");


加载模型时,您还需要切换到现代的Vertex Buffer Array。默认情况下,VBA可以存储16个顶点缓冲对象,其中可以包含几乎要为顶点存储的任何数据(位置,颜色,纹理坐标,给定名称,如果您愿意...)

对于此加载部分,我使用一个单个浮点数组(后面一个VBO)按排序顺序保存所有数据(有关如何存储数据的更多信息,请参见https://www.khronos.org/opengl/wiki/Vertex_Specification_Best_Practices

在此示例中,我将只为顶点提供3个浮点位置,为颜色给出3个浮点。您还需要指定一个索引缓冲区对象

float[] data = {
   // x,   y,   z,    r,  g,  b
    -1f,  1f,  0f,   1f, 0f, 0f,// Left  top   , red  , ID: 0
    -1f, -1f,  0f,   0f, 1f, 0f,// Left  bottom, blue , ID: 1
     1f, -1f,  0f,   0f, 0f, 1f,// Right bottom, green, ID: 2
     1f,  1f,  0f,   1f, 1f, 1f // Right left  , white, ID: 3
};

byte[] indices = {
    0, 1, 2,// Left bottom triangle
    2, 3, 0 // Right top triangle
};


现在,您要加载数据,并指定数据在哪里。

// You have to use LWJGL3's way of memory management which is off-heap
// more info: https://blog.lwjgl.org/memory-management-in-lwjgl-3/
try(MemoryStack stack = MemoryStack.stackPush()){

    FloatBuffer dataBuffer = stack.mallocFloat(data.length);
    dataBuffer.put(data);
    dataBuffer.flip();

    indicesCount = indices.length; // Store for later use ( needed for rendering )
    ByteBuffer indicesBuffer = stack.malloc(indicesCount);
    indicesBuffer.put(indices);
    indicesBuffer.flip();

    vaoId = glGenVertexArrays();
    glBindVertexArray(vaoId);

    vboId = glGenBuffers();
    glBindBuffer(GL_ARRAY_BUFFER, vboId);
    glBufferData(GL_ARRAY_BUFFER, dataBuffer, GL_STATIC_DRAW);

    // Vectors
    int program_in_position = glGetAttribLocation(program, "position");
    glEnableVertexAttribArray(program_in_position);
    glVertexAttribPointer(program_in_position, 3, GL_FLOAT, false, 6*(Float.SIZE / Byte.SIZE), alreadyTakenBytes);

    // Colors
    int colorAttPos= glGetAttribLocation(program, "color");
    glEnableVertexAttribArray(colorAttPos);
    glVertexAttribPointer(colorAttPos, 3, GL_FLOAT, false, 6*(Float.SIZE / Byte.SIZE), 3*(Float.SIZE / Byte.SIZE));

    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);

    // Now the Index VBO
    indexVBO = glGenBuffers();
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL_STATIC_DRAW);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}


现在进行渲染:

    glBindVertexArray(vaoId);
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVBO);
    glDrawElements(GL_TRIANGLES, indicesCount, GL_UNSIGNED_BYTE, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    glDisableVertexAttribArray(0);
    glBindVertexArray(0);


(是的确如此)
撇开玩笑,是的,那就是一堆代码,学习它,喜欢它。而且我们甚至还没有在Shader中使用任何矩阵,但这确实很简单。每次您想要使用UniformLocation将Matrix(以FloatBuffer的形式)推入着色器时

// This needs to happen only ONCE ( position stays the same )
int uniModel = glGetUniformLocation(program, "model");

// Create a Matrix
Matrix4f model = Matrix4f.translate(0, 0, -10);

// Create Float Buffer from Matrix4f
FloatBuffer fb = model.toBuffer();

// Push FloatBuffer to Shader
glUniformMatrix4fv(uniModel, false, fb);


唯一的问题是,您将需要一个具有所需所有功能的Matrix4f类,这再次是您自己的工作,没有花哨的OpenGL调用。不要气tho,那里有几个数学库(例如:https://github.com/SilverTiger/lwjgl3-tutorial/blob/master/src/silvertiger/tutorial/lwjgl/math/Matrix4f.java

09-30 15:29
查看更多