So i started to learn lwjgl recently, and quickly realized that I need to improve in OpenGL to continue. I followed tutorial here and tried to implement same code using java and lwjgl (code in tutorial is in C++). I successfully managed to replicate OpenGL calls from tutorial using lwjgl api, but when I start my program i see only black screen and nothing else =( No triangle, nothing. No errors in console ether.
My Window.java class where i perform OpenGL rendering (Never mind Spring annotations, i'm just using IoC container to manage objects, method run()
is called after Spring context creation):
import com.gitlab.cvazer.dnd.map.desktop.ashlesy.Orchestrator;
import com.gitlab.cvazer.dnd.map.desktop.render.Camera;
import com.gitlab.cvazer.dnd.map.desktop.util.Shaders;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.lwjgl.Version;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.opengl.GL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.Objects;
import static org.lwjgl.glfw.Callbacks.glfwFreeCallbacks;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL30.*;
import static org.lwjgl.system.MemoryUtil.NULL;
@Slf4j
@Component
public class Window {
private @Autowired Shaders shaders;
private @Getter long window;
//START HERE!
public void run() {
new Thread(() -> {
log.info("Hello LWJGL " + Version.getVersion() + "!");
init();
try {
loop();
} catch (IOException e) {
e.printStackTrace();
}
glfwFreeCallbacks(window);
glfwDestroyWindow(window);
glfwTerminate();
Objects.requireNonNull(glfwSetErrorCallback(null)).free();
}).start();
}
private void init() {
GLFWErrorCallback.createPrint(System.err).set();
if ( !glfwInit() ) throw new IllegalStateException("Unable to initialize GLFW");
glfwDefaultWindowHints();
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
glfwWindowHint(GLFW_SAMPLES, 8);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
window = glfwCreateWindow(800, 600, "Hello World!", NULL, NULL);
if ( window == NULL ) throw new RuntimeException("Failed to create the GLFW window");
callbacks();
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
glfwShowWindow(window);
}
private void loop() throws IOException {
GL.createCapabilities();
int vertexArray = glGenVertexArrays();
glBindVertexArray(vertexArray);
int vertexBuffer = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
float[] data = {-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f};
glBufferData(GL_ARRAY_BUFFER, data, GL_STATIC_DRAW);
int program = shaders.loadShaders("vertex.glsl", "fragment.glsl");
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
long lastTime = System.currentTimeMillis();
while ( !glfwWindowShouldClose(window) ) {
long delta = System.currentTimeMillis() - lastTime;
lastTime = System.currentTimeMillis();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(program);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glVertexAttribPointer(0,3,GL_FLOAT, false, 0, vertexBuffer);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
glfwSwapBuffers(window); // swap the color buffers
glfwPollEvents();
}
}
}
Shaders.java class:
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import static org.lwjgl.opengl.GL30.*;
@Slf4j
@Service
public class Shaders {
public int loadShaders(String vertexFilePath, String fragmentFilePath) throws IOException {
int vertexShader = glCreateShader(GL_VERTEX_SHADER);
int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
String vertexCode = Files.lines(Paths.get(vertexFilePath))
.collect(Collectors.joining("\n"));
String fragmentCode = Files.lines(Paths.get(fragmentFilePath))
.collect(Collectors.joining("\n"));
compileShader(vertexShader, vertexCode);
compileShader(fragmentShader, fragmentCode);
int program = glCreateProgram();
glAttachShader(program, vertexShader);
glAttachShader(program, fragmentShader);
glLinkProgram(program);
glDetachShader(program, vertexShader);
glDetachShader(program, fragmentShader);
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
return program;
}
private void compileShader(int shader, String code){
glShaderSource(shader, code);
glCompileShader(shader);
String slog = glGetShaderInfoLog(shader);
if (slog.contentEquals("")) return;
log.info(slog);
}
}
vertex.glsl file:
#version 330 core
layout(location = 0) in vec3 vertexPosition_modelspace;
void main(){
gl_Position.xyz = vertexPosition_modelspace;
gl_Position.w = 1.0;
}
fragment.glsl file:
#version 330 core
out vec3 color;
void main(){
color = vec3(1,0,0);
}
I followed part 1 (coding OpenGL calls) and 2 (coding GLSL shaders, loading them) of this tutorial, but adding shaders didn't fixed my problem
I don't think that googling further can give me answers since almost all tutorials online on topic of lwjgl use OpenGL 1 for rendering.
How can i make this work?
The issue is the line
glVertexAttribPointer(0,3,GL_FLOAT, false, 0, vertexBuffer);
If a named buffer object is bound then the last parameter of glVertexAttribPointer
is treated as a byte offset into the buffer object's data store.
When you use glVertexAttribPointer
then you don't have to specify the vertex buffer by a parameter. The function associates the vertex attribute to the buffer object, which is currently bound to the target GL_ARRAY_BUFFER
.
It has to be:
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
See also Java Code Examples for org.lwjgl.opengl.GL20.glVertexAttribPointer()