本文介绍了LWJGL3着色器,三角形未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是OpenGL和LWJGL3的新手,我正在尝试渲染一个三角形.但是,它没有显示,只有白色的空白窗口.我想这是一个很小的东西,我以某种方式忘记了,或者我只是犯了个错误.我搜索了几个小时后仍找不到它.这是代码:

I'm new to OpenGL and LWJGL3 and I'm trying to render a triangle. However, it isn't showing up, only the white, empty window. I guess it's a pretty small piece that I somehow forgot or where I just made a mistake. I don't find it after searching for hours now though. Here's the code:

MainLoop:

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL30.*;
import static org.lwjgl.glfw.GLFW.*;

public class MainLoop {

private DisplayManager displayManager;
private Shader shader;

public MainLoop() {
    displayManager = new DisplayManager();
    displayManager.createDisplay();
    shader = new Shader("src/shader.vert", "src/shader.frag");
}

private void start() {

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

    int vao = glGenVertexArrays();
    glBindVertexArray(vao);

    int positionVbo = glGenBuffers();
    glBindBuffer(GL_ARRAY_BUFFER, positionVbo);
    glBufferData(GL_ARRAY_BUFFER, BufferUtils.createFloatBuffer(positions), GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);

    while(!displayManager.isCloseRequested()) {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glBindVertexArray(vao);

        render();
        update();

        displayManager.swapBuffers();
    }

    shader.cleanUp();
    displayManager.destroyDisplay();
}

private void update() {
    glfwPollEvents();
}

private void render() {
    shader.bind();

    //rendering stuff
    glDrawArrays(GL_TRIANGLES, 0, 3);

    shader.unbind();
}

public static void main(String[] args) {
    new MainLoop().start();
}
}

着色器:

import static org.lwjgl.opengl.GL11.GL_FALSE;
import static org.lwjgl.opengl.GL20.*;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;


public class Shader {
    private int program;

    public Shader(final String vertexShaderFilePath, final String fragmentShaderFilePath) {
        int vertexShader   = createShader(loadShaderSource(vertexShaderFilePath), GL_VERTEX_SHADER);
        int fragmentShader = createShader(loadShaderSource(fragmentShaderFilePath), GL_FRAGMENT_SHADER);

        program = glCreateProgram();
        glAttachShader(vertexShader, program);
        glAttachShader(fragmentShader, program);
        glLinkProgram(program);
        glValidateProgram(program); //just during development

        glDeleteShader(vertexShader);
        glDeleteShader(fragmentShader);
    }

    private String loadShaderSource(final String shaderFilePath) {
        String shaderSourceString = "";
        try {
            shaderSourceString = new String(Files.readAllBytes(Paths.get(shaderFilePath)));
        } catch (IOException e) {
            e.printStackTrace();
        }

        return shaderSourceString;
    }

    private int createShader(final String shaderSourceString, final int type) {
        int shader = glCreateShader(type);
        glShaderSource(shader, shaderSourceString);
        glCompileShader(shader);
        checkShaderCompileError(shader);

        return shader;
    }

    private void checkShaderCompileError(int shader) {
        if(glGetShaderi(shader, GL_COMPILE_STATUS) == GL_FALSE) {
            System.err.println("Could not compile shader.");
            System.err.println(glGetShaderInfoLog(program));
            System.exit(-1);
        }
    }

    public void bind() {
        glUseProgram(program);
    }

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

    public void cleanUp() {
        glDeleteProgram(program);
    }
}

BufferUtils :

import java.nio.FloatBuffer;

public class BufferUtils {

    public static FloatBuffer createFloatBuffer(float[] array) {
        FloatBuffer buffer = org.lwjgl.BufferUtils.createFloatBuffer(array.length);
        buffer.put(array);
        buffer.flip();
        return buffer;
    }
}

shader.vert:

#version 410 core

layout (location = 0) in vec3 vertex_position;

void main(void)
{
    gl_Position = vec4(vertex_position, 1.0);
}

shader.frag:

#version 410 core

out vec4 frag_color;

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

这是DisplayManager:

import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.system.MemoryUtil.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GLContext.*;

import java.nio.ByteBuffer;

import org.lwjgl.glfw.GLFWvidmode;

public class DisplayManager {

    private static final int WIDTH  = 1280;
    private static final int HEIGHT = 800;

    private long window;
    private Keyboard keyCallback;

    public void createDisplay() {

        // initialize GLFW
        if(glfwInit() != GL_TRUE) {
            System.err.println("GLFW failed to initialize.");
            System.exit(-1);
        }

        // set window hints
        glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
        glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
        glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
        glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
        glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
        glfwWindowHint(GLFW_SAMPLES, 4); //16 for production

        // create window
        window = glfwCreateWindow(WIDTH, HEIGHT, "OpenGL", NULL, NULL);
        if(window == NULL) {
            System.err.println("GLFW window failed to create.");
            glfwTerminate();
            System.exit(-1);
        }
        ByteBuffer videoMode = glfwGetVideoMode(glfwGetPrimaryMonitor());
        glfwSetWindowPos(window, (GLFWvidmode.width(videoMode) - WIDTH) / 2, (GLFWvidmode.height(videoMode) - HEIGHT) / 2);

        // set callback mechanisms
        glfwSetKeyCallback(window, setKeyCallback(new Keyboard()));

        // set context and show window
        glfwMakeContextCurrent(window);
        createFromCurrent();
        glfwSwapInterval(1); // v-sync
        glfwShowWindow(window);

        // openGL functions
        glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
        glEnable(GL_DEPTH);
    }

    public void destroyDisplay() {
        glfwDestroyWindow(window);
        glfwTerminate();
    }

    public boolean isCloseRequested() {
        if(glfwWindowShouldClose(window) == GL_TRUE) {
            return true;
        }
        return false;
    }

    public void swapBuffers() {
        glfwSwapBuffers(window);
    }

    public Keyboard getKeyCallback() {
        return keyCallback;
    }

    public Keyboard setKeyCallback(Keyboard keyCallback) {
        this.keyCallback = keyCallback;
        return keyCallback;
    }
}

有人发现哪里出了问题吗?

Does anybody spot what's going wrong?

提前谢谢!

推荐答案

我终于发现了错误,调用glAttachShader(shader, program);的参数顺序混合了.您首先必须传递程序,然后传递着色器ID,如下所示:glAttachShader(program, shader);.谢谢您的回答!

I finally found the error, the order of parameters for the call glAttachShader(shader, program); were mixed up. You first have to pass the program and then the shader id like so: glAttachShader(program, shader);. Thank you for your answers though!

这篇关于LWJGL3着色器,三角形未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 06:43