glDrawArrays什么都没画

时间:2017-11-07 11:18:57

标签: java opengl lwjgl glfw

我正在尝试学习OpenGL。 Fist我完成了一些WebGL教程,了解着色器如何工作以及如何将它们与OpenGL代码一起使用。

我只想向前迈出一步,用LWJGL实现一些图形。

在代码中,我像在webgl中那样调用glDrawArrays函数但是没有任何内容。

这是我的顶点着色器:

void main(){
   gl_Position = vec4(0.0, 0.0, 1.0, 0.0);
   gl_PointSize = 50.0;
}

片段着色器:

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

这是我用来渲染点的java代码:

package com.harvester.game;

import java.io.IOException;
import java.nio.IntBuffer;
import com.harvester.game.loaders.ShaderLoader;
import static org.lwjgl.glfw.GLFW.glfwMakeContextCurrent;
import static org.lwjgl.opengl.GL.*;
import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryStack.*;

public class GamePlayState implements GameState {
    private StateManager stateManager;
    long window;
    int program;

    public GamePlayState(StateManager stateManager, long window) {
        this.stateManager = stateManager;
        this.window = window;
    }

    @Override
    public void init() {
        // Make the OpenGL context current
        glfwMakeContextCurrent(window);
        createCapabilities();
        initShaders();

        System.out.println("OpenGL version " + glGetString(GL_VERSION));
    }

    @Override
    public void load() {
        // TODO Auto-generated method stub
        System.out.println("load");
        glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    }

    @Override
    public void update() {
        // TODO Auto-generated method stub
        // System.out.println("update");
    }

    @Override
    public void render(float delta) {
        // glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        // System.out.println(60/(delta/1000));

        glDrawArrays(GL_POINTS, 0, 2);
        // System.out.println(glGetError());

        // glfwSwapBuffers(window); // swap the color buffers
    }

    @Override
    public void destroy() {
        // TODO Auto-generated method stub

    }

    private void initShaders() {
        // TODO Auto-generated method stub
        // System.out.println("init");
        String fragmentShaderCode = "";
        String vertexShaderCode = "";
        try {
            fragmentShaderCode = ShaderLoader.LoadFragmentShaderSource();
            vertexShaderCode = ShaderLoader.LoadVertexShaderSource();
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(-1);
        }

        int program = glCreateProgram();
        int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
        int vertextShader = glCreateShader(GL_VERTEX_SHADER);

        glShaderSource(vertextShader, vertexShaderCode);
        glCompileShader(vertextShader);

        IntBuffer statsB = stackPush().mallocInt(1);
        glGetShaderiv(vertextShader, GL_COMPILE_STATUS, statsB);

        if (statsB.get(0) == GL_FALSE) {
            System.err.println("Failed to compile vertex shader");
            System.exit(-2);
        }

        glShaderSource(fragmentShader, fragmentShaderCode);
        glCompileShader(fragmentShader);

        glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, statsB);

        if (statsB.get(0) == GL_FALSE) {
            System.err.println("Failed to compile fragment shader");
            System.exit(-3);
        }

        glAttachShader(program, vertextShader);
        glAttachShader(program, fragmentShader);

        glLinkProgram(program);

        glGetProgramiv(program, GL_LINK_STATUS, statsB);

        if (statsB.get(0) == GL_FALSE) {
            System.err.println("Failed to link Program");
            System.exit(-4);
        }


        glValidateProgram(program);
        glGetProgramiv(program, GL_VALIDATE_STATUS, statsB);

        System.out.println(glGetProgramInfoLog(program));

        glUseProgram(program);

        if (statsB.get(0) == GL_FALSE) {
            System.err.println("Failed to validate program");
            System.exit(-3);
        }

        this.program = program;
    }
}

我做错了什么?我只是想在屏幕上画一个微笑点。

(调试选项设置为true但日志显示没有错误。)

0 个答案:

没有答案