因此,我一直在看一些C ++的OpenGL教程,并且试图将代码移植到Java中,但我得到的只是一个黑屏。我真的在这里找不到错误,我想我已经对LWJGL进行了相应的修改。着色器实际上是从教程中复制的,因此它们应该可以使用。我也尝试过使用glfwSetErrorCallback()
,但它没有给我任何东西。
主要:
public static void main(String[] args) {
long window;
float[] positions = new float[]{
0.5f, -0.5f,
0.0f, 0.5f,
0.5f, -0.5f
};
if(!glfwInit()){
System.err.println("GLFW Failed to initialize");
System.exit(1);
}
window = glfwCreateWindow(800,600,"Game",0,0);
glfwMakeContextCurrent(window);
GL.createCapabilities();
int id = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, id);
glBufferData(GL_ARRAY_BUFFER, createBuffer(positions), GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, false, 0, 0);
int shader = createShader(readFile("vertexShader.shader"), readFile("fragmentShader.shader"));
glUseProgram(shader);
while(!glfwWindowShouldClose(window)){
glfwPollEvents();
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 3);
glfwSwapBuffers(window);
}
glfwTerminate();
}
createBuffer:
public static FloatBuffer createBuffer(float[] data) {
FloatBuffer buffer = BufferUtils.createFloatBuffer(data.length);
buffer.put(data);
buffer.flip();
return buffer;
}
createShader:
public static int createShader(String vertexShader, String fragmentShader){
int program = glCreateProgram();
int vs = compileShader(vertexShader, GL_VERTEX_SHADER);
int fs = compileShader(fragmentShader, GL_FRAGMENT_SHADER);
glAttachShader(program, vs);
glAttachShader(program, fs);
glLinkProgram(program);
glValidateProgram(program);
glDeleteShader(vs);
glDeleteShader(fs);
return program;
}
compileShader:
public static int compileShader(String source, int type){
int id = glCreateShader(type);
glShaderSource(id, source);
glCompileShader(id);
if(glGetShaderi(id, GL_COMPILE_STATUS) == GL_FALSE){
System.out.println(glGetShaderInfoLog(id));
glDeleteShader(id);
System.exit(1);
}
return id;
}
答案 0 :(得分:0)
您要绘制的几何不是三角形。第一和第三顶点是相同的。