So, i've been watching some OpenGL's tutorial in C++ and i've tried to port the code into Java but all i get is a black screen. I really can't find the error here and i think i've modified things accordingly to LWJGL. The shaders are actually copied from the tutorial so they should work. I've also tried with the glfwSetErrorCallback()
but it doesn't give me anything.
Main:
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;
}
The geometry you are drawing is not a triangle. The first and third vertex are the same.