I've been trying to render a triangle on my screen for the past day but I can't figure out what I'm doing incorrectly. The only thing that works is displaying a window and rendering to it using clear color. Does anyone know what I'm doing incorrectly? All I'm expecting is to see a black triangle rendered on screen like I've done once in the past but currently I'm not able to do that.
Here's the code:
Main:
public class Main {
public static void main(String[] args){
System.out.println("Hello");
// Create Window
Window.createWindow();
long window = Window.getWindow();
// Create Renderer
Renderer renderer = new Renderer();
// Data
float[] positions = {
0.5f, 0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f
};
// Create Model
Model triangle = Loader.dataToModel(positions);
// Render on window
while(!GLFW.glfwWindowShouldClose(window)){
GLFW.glfwPollEvents();
renderer.clear();
renderer.render(triangle);
GLFW.glfwSwapBuffers(window); // !@#!@#!@# remember to add!!! !@#!@# Order important prob
}
System.out.println("TERMINATED");
Window.terminateWindow();
}
}
Window:
public class Window {
private static long window;
private static Callback debugCallback;
public static void createWindow(){
// Creates a window
// Init
if(!GLFW.glfwInit()){
throw new IllegalStateException("ERROR: could not GLFW.glfwInit()");
}
// Hints
// Hint opengl version
GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MAJOR,3);
GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MINOR,2);
// Hint compatibility (profile, forward compat)
GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_PROFILE,GLFW.GLFW_OPENGL_CORE_PROFILE);
GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_FORWARD_COMPAT,GLFW.GLFW_TRUE);
// Hint to hide initially
GLFW.glfwWindowHint(GLFW.GLFW_VISIBLE,GLFW.GLFW_FALSE);
// Hint debug
GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_DEBUG_CONTEXT, GLFW.GLFW_TRUE);
// Actually create window
window = GLFW.glfwCreateWindow(500, 500, "thinMatrix4", 0, 0);
if(window == 0){
throw new IllegalStateException("ERROR: during GLFW.glfwCreateWindow");
}
// Context and Capabilities
GLFW.glfwMakeContextCurrent(window); // create context for window
GL.createCapabilities(); // find all the opengl stuff to give capabilities
// Debugging enable
GL11.glEnable(org.lwjgl.opengl.KHRDebug.GL_DEBUG_OUTPUT_SYNCHRONOUS);
debugCallback = GLUtil.setupDebugMessageCallback();
// Position window
GLFWVidMode vidMode = GLFW.glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor());
GLFW.glfwSetWindowPos(window,vidMode.width()/2,vidMode.height()/2);
// Window show
GLFW.glfwShowWindow(window);
}
public static void terminateWindow(){
// Termination
GLFW.glfwTerminate();
}
// GETTERS
public static long getWindow() {
return window;
}
}
Renderer:
public class Renderer {
public void render(Model model){
// Enable to allow model rendering
GL30.glBindVertexArray(model.getVAO());
GL20.glEnableVertexAttribArray(0);
// Render
GL11.glDrawArrays(GL11.GL_TRIANGLES,model.getVertexCount(),0);
// Clean up
GL20.glDisableVertexAttribArray(0);
GL30.glBindVertexArray(0);
}
public void clear(){
GL11.glClearColor(0f,0.8f,.6f,1f);
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
}
}
Model:
public class Model {
private int vertexCount;
private int vao;
public Model(int vao, int count){
this.vao = vao;
this.vertexCount = count;
}
// GETTERS
public int getVertexCount(){
return this.vertexCount;
}
public int getVAO(){ return this.vao; }
}
public class Loader {
private static List<Integer> vaos = new ArrayList<Integer>();
private static List<Integer> vbos = new ArrayList<Integer>();
public static Model dataToModel(float[] data){
// Create VAO
int vao = GL30.glGenVertexArrays();
GL30.glBindVertexArray(vao);
vaos.add(vao);
// Create Buffer (VBO) for actual data
int vbo = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER,vbo);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER,toFloatBuffer(data), GL15.GL_STATIC_DRAW); //!@#!@#!@# What happens if data is not a float buffer
// vertex attribute pointer
GL30.glVertexAttribPointer(0,3, GL11.GL_FLOAT,false,0,0);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER,0);
vbos.add(vbo);
// Create model
int count = data.length/3;
Model model = new Model(vao, count);
return model;
}
private static FloatBuffer toFloatBuffer(float[] data){
FloatBuffer buffer = BufferUtils.createFloatBuffer(data.length);
buffer.put(data);
buffer.flip();
return buffer;
}
}
You used glDrawArrays
incorrectly. The 2nd argument is the index of the 1st vertex to be rendered and the 3rd argument is the number of vertices:
GL11.glDrawArrays(GL11.GL_TRIANGLES,model.getVertexCount(),0);
GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, model.getVertexCount());