Hello all,
I recently started learning OpenGL, and trying to use the LWJGL library. I started off running some source code I found
on this website. From there, I learned more about the basics, concerning creating a window and context, and input handling. I delegated this code to a few separate classes for convenience (the example rendering code still worked fine after this).
At this point I wanted to start using the more modern OpenGL approach, so I found an excellent tutorial
here. I followed this tutorial up until the point where the white triangle is rendered, and this is where the problems start.
The white triangle isn't being renderered, but...
Here's my code. I doubt the problem lies in my separate classes, but I'll post them anyway if necessary. Also, small curiosity, my graphics card is supposed to be able to handle OpenGL 4.4, but the version printed is OpenGL 4.0... Why is that?
package zeno.test.lwjgl;
import java.awt.DisplayMode;
import java.awt.GraphicsEnvironment;
import java.nio.FloatBuffer;
import javafx.scene.input.KeyCode;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL30;
import zeno.util.gl.GLCore;
import zeno.util.gl.functions.GLVersion;
import zeno.util.gl.window.Window;
import zeno.util.gl.window.events.KeyHandler;
public class LWJGLEngine implements KeyHandler
{
@SuppressWarnings("unused")
public static void main(String[] args)
{
new LWJGLEngine();
}
private boolean isRunning;
private DisplayMode mode;
private Window window;
private String vshade =
"#version 150" + "\n"
+ "in vec2 position;" + "\n"
+ "void main()" + "\n"
+ "{" + "\n"
+ "gl_Position = vec4(position, 0.0, 1.0);" + "\n"
+ "}";
private String fshade =
"#version 150" + "\n"
+ "out vec4 outColor;" + "\n"
+ "void main()" + "\n"
+ "{" + "\n"
+ "outColor = vec4(1.0, 1.0, 1.0, 1.0);" + "\n"
+ "}";
public LWJGLEngine()
{
try
{
GLCore.initialize();
mode = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayMode();
window = GLCore.createWindow(mode, "OpenGL Test", true);
window.setKeyboardHandler(this);
System.out.println(GLVersion.getDescription());
System.out.println();
int vao = GL30.glGenVertexArrays();
GL30.glBindVertexArray(vao);
float[] vertices = new float[]
{
0.0f, 0.5f,
0.5f, - 0.5f,
- 0.5f, - 0.5f
};
FloatBuffer vbuffer = BufferUtils.createFloatBuffer(6);
vbuffer.put(vertices);
int vbo = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vbuffer, GL15.GL_STATIC_DRAW);
int vshader = GL20.glCreateShader(GL20.GL_VERTEX_SHADER);
GL20.glShaderSource(vshader, vshade);
GL20.glCompileShader(vshader);
int vstatus = GL20.glGetShaderi(vshader, GL20.GL_COMPILE_STATUS);
if(vstatus != GL11.GL_TRUE)
{
System.out.println("Vertex shader did not compile.");
System.out.println(GL20.glGetShaderInfoLog(vshader));
System.out.println();
}
int fshader = GL20.glCreateShader(GL20.GL_FRAGMENT_SHADER);
GL20.glShaderSource(fshader, fshade);
GL20.glCompileShader(fshader);
int fstatus = GL20.glGetShaderi(fshader, GL20.GL_COMPILE_STATUS);
if(fstatus != GL11.GL_TRUE)
{
System.out.println("Fragment shader did not compile.");
System.out.println(GL20.glGetShaderInfoLog(fshader));
System.out.println();
}
int program = GL20.glCreateProgram();
GL20.glAttachShader(program, vshader);
GL20.glAttachShader(program, fshader);
GL30.glBindFragDataLocation(program, 0, "outColor");
GL20.glLinkProgram(program);
GL20.glUseProgram(program);
int attr = GL20.glGetAttribLocation(program, "position");
GL20.glEnableVertexAttribArray(attr);
GL20.glVertexAttribPointer(attr, 2, GL11.GL_FLOAT, false, 0, 0);
int error = GL11.glGetError();
if(error != GL11.GL_NO_ERROR)
{
System.out.println("Error during init: " + error);
}
isRunning = true;
while(isRunning)
{
executeLoop();
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
window.destroy();
}
}
void executeLoop()
{
GL11.glClearColor(0f, 0f, 1f, 1f);
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, 3);
int error = GL11.glGetError();
if(error != GL11.GL_NO_ERROR)
{
System.out.println("Error during loop: " + error);
}
window.swapBuffer();
window.executeEvents();
}
@Override
public void onRelease(KeyCode keycode)
{
// NOT APPLICABLE
}
@Override
public void onPress(KeyCode keycode)
{
if(keycode == KeyCode.ESCAPE)
{
isRunning = false;
}
}
@Override
public void onType(char keychar)
{
// NOT APPLICABLE
}
}