I'm new to OpenGL and trying to render multiple triangles using single VBO in using LWJGL3. I've followed demos and read some books and wrote following code, but its crashing. Without draw command it displays window with given background at leastnot drawing my shape. I use vertex and fragment shader.
It prints following log and crashesnot drawing anything. If I don't call glDrawArrays it never crashes and paints blue background:
0(6) : warning C7533: global variable gl_FragColor is deprecated after version 120
Fragment info
-------------
0(6) : warning C7533: global variable gl_FragColor is deprecated after version 120
Java[LWJGL] ResultOpenGL debug message
ID: -10737418190x20071
Source: API
Type: OTHER
Severity: NOTIFICATION
Message: Buffer detailed info: Buffer object 1 (bound to GL_ARRAY_BUFFER_ARB, usage hint is GL_STATIC_DRAW) will use VIDEO memory as the source for buffer object operations.
package mn.digz.lwjgl;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.system.MemoryUtil.NULL;
import java.io.IOException;
import java.nio.FloatBuffer;
import mn.digzastvision.assetmanagement.lib.lwjgl.util.ShaderUtil;
import org.joml.Matrix4f;
import org.lwjgl.BufferUtils;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.glfw.GLFWFramebufferSizeCallback;
import org.lwjgl.glfw.GLFWKeyCallback;
import org.lwjgl.glfw.GLFWVidMode;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GLCapabilities;
import org.lwjgl.opengl.GLUtil;
import org.lwjgl.system.Callback;
/**
*
* @author MethoD
*/
public class OpenGLGraphRender {
long window;
int width = 1024;
int height = 768;
int vao;
int program;
int viewProjMatrixUniform;
Matrix4f viewProjMatrix = new Matrix4f();
FloatBuffer matrixBuffer = BufferUtils.createFloatBuffer(16);
FloatBuffer verticesBuffer;
int vertexPositionAttribute;
int vertexColorAttribute;
int pointSize = 3 + 4; // x,y,z + r,g,b,a
int step = 4;Float.SIZE / 8; // byte per element
int stride = step * pointSize; // need?
GLCapabilities caps;
GLFWErrorCallback errCallback;
Callback debugProc;
void init() throws IOException {
glfwSetErrorCallback(errCallback = new GLFWErrorCallback() {
GLFWErrorCallback delegate = GLFWErrorCallback.createPrint(System.err);
@Override
public void invoke(int error, long description) {
if (error == GLFW_VERSION_UNAVAILABLE)
System.err.println("This demo requires OpenGL 2.0 or higher.");
delegate.invoke(error, description);
}
@Override
public void free() {
delegate.free();
}
});
if (!glfwInit())
throw new IllegalStateException("Unable to initialize GLFW");
glfwDefaultWindowHints();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
window = glfwCreateWindow(width, height, "Silhouette rendering with geometry shader", NULL, NULL);
if (window == NULL) {
throw new AssertionError("Failed to create the GLFW window");
}
GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
glfwSetWindowPos(window, (vidmode.width() - width) / 2, (vidmode.height() - height) / 2);
glfwMakeContextCurrent(window);
glfwSwapInterval(01); // enable v sync
glfwShowWindow(window);
caps = GL.createCapabilities();
/*if (!caps.GL_EXT_geometry_shader4) {
throw new AssertionError("This demo requires the EXT_geometry_shader4 extension");
}*/
debugProc = GLUtil.setupDebugMessageCallback();
glClearColor(0.55f, 0.75f, 0.95f, 1.0f);
glEnable(GL_DEPTH_TEST);
/* Create all needed GL resources */
createTriangleVao();
createRasterProgram();
initProgram();
}
void createTriangleVao() {
verticesBuffer = BufferUtils.createFloatBuffer(pointSize * 3 * 3); // point size * 3 points * 3 triangles = 21 floats per triangle
verticesBuffer.put(new float[] {0f, 0f, 0f, 1f, 1f, 0f, 1f, 2f, 0f, 0f, 1f, 1f, 0f, 1f, 1f, 1.7f, 0f, 1f, 1f, 0f, 1f});
verticesBuffer.put(new float[] {4f, 4f, 0f, 1f, 1f, 0f, 1f, 6f, 4f, 0f, 1f, 1f, 0f, 1f, 5f, 5.7f, 0f, 1f, 1f, 0f, 1f});
verticesBuffer.put(new float[] {4f, 1f, 0f, 1f, 1f, 0f, 1f, 6f, 1f, 0f, 1f, 1f, 0f, 1f, 5f, 2.7f, 0f, 1f, 1f, 0f, 1f});
int vbo = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, verticesBuffer, GL_STATIC_DRAW);
glVertexAttribPointer(vertexPositionAttribute, 3, GL_FLOAT, false, stride, 0);
glVertexAttribPointer(vertexColorAttribute, 4, GL_FLOAT, false, stride, step * 3);
}
void createRasterProgramcreateProgram() throws IOException {
int program = glCreateProgram();
int vshader = ShaderUtil.createShader("mn/digzastvision/assetmanagement/lib/lwjgl/shader/vs.glsl", GL_VERTEX_SHADER);
int fshader = ShaderUtil.createShader("mn/digzastvision/assetmanagement/lib/lwjgl/shader/fs.glsl", GL_FRAGMENT_SHADER);
glAttachShader(program, vshader);
glAttachShader(program, fshader);
glLinkProgram(program);
int linked = glGetProgrami(program, GL_LINK_STATUS);
String programLog = glGetProgramInfoLog(program);
if (programLog != null && programLog.trim().length() > 0) {
System.err.println(programLog);
}
if (linked == 0) {
throw new AssertionError("Could not link program");
}
this.program = program;
}
/**
* Initialize the shader program.
*/
void initProgram() {
glUseProgram(this.program);
viewProjMatrixUniform = glGetUniformLocation(this.program, "viewProjMatrix");
glBindAttribLocation(program, 0, "vertexPosition");
glBindAttribLocation(program, 0, "vertexColor");
vertexPositionAttribute = glGetAttribLocation(program, "vertexPosition");
glEnableVertexAttribArray(vertexPositionAttribute);
vertexColorAttribute = glGetAttribLocation(program, "vertexColor");
verticesBuffer = BufferUtils.createFloatBuffer(pointSize * 3 * 3); // point size * 3 points * 3 triangles = 21 floats per triangle
verticesBuffer.put(new float[] {0f, 0f, 0f, 1f, 1f, 0f, 1f, 2f, 0f, 0f, 1f, 1f, 0f, 1f, 1f, 1.7f, 0f, 1f, 1f, 0f, 1f});
verticesBuffer.put(new float[] {4f, 4f, 0f, 1f, 1f, 0f, 1f, 6f, 4f, 0f, 1f, 1f, 0f, 1f, 5f, 5.7f, 0f, 1f, 1f, 0f, 1f});
verticesBuffer.put(new float[] {4f, 1f, 0f, 1f, 1f, 0f, 1f, 6f, 1f, 0f, 1f, 1f, 0f, 1f, 5f, 2.7f, 0f, 1f, 1f, 0f, 1f});
verticesBuffer.flip();
int vbo = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, verticesBuffer, GL_STATIC_DRAW);
glVertexAttribPointer(vertexPositionAttribute, 3, GL_FLOAT, false, stride, 0);
glVertexAttribPointer(vertexColorAttribute, 4, GL_FLOAT, false, stride, step * 3);
glEnableVertexAttribArray(vertexPositionAttribute);
glEnableVertexAttribArray(vertexColorAttribute);
glUseProgram(0);
}
void updaterender() {
viewProjMatrix
.setPerspective((float) Math.toRadians(30), (float) width / height, 0.01f, 50.0f)
.lookAt(0.0f, 2.0f, 7.0f,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f);
}
void render() {
glUseProgram(this.program);
glUniformMatrix4fv(viewProjMatrixUniform, false, viewProjMatrix.get(matrixBuffer));
//int numItems = verticesBuffer.array().length / pointSize;
int numItems = 3;
glDrawArrays(GL_TRIANGLES, 0, numItems);
glUseProgram(0);
}
void loop() throws IOException {
GL.createCapabilities();
debugProc = GLUtil.setupDebugMessageCallback();
glClearColor(0.55f, 0.75f, 0.95f, 1.0f);
glEnable(GL_DEPTH_TEST);
/* Create all needed GL resources */
createProgram();
initProgram();
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
update();
render();
glfwSwapBuffers(window);
}
}
void run() {
try {
init();
loop();
if (debugProc != null)
debugProc.free();
errCallback.free();
glfwDestroyWindow(window);
} catch (Throwable t) {
t.printStackTrace();
} finally {
glfwTerminate();
}
}
public static void main(String[] args) {
new OpenGLGraphRender().run();
}
}

