Cant get a simple triangle to render on screen?

Started by orange451, June 11, 2018, 23:19:05

Previous topic - Next topic

orange451

I've probably done this a thousand times over the years... But when trying to get this simple triangle to draw, I seem to be running into issues... Could someone take a look at this MCVE?

Link: https://pastebin.com/40THr6ik

or:
package test;
import static org.lwjgl.glfw.GLFW.GLFW_CONTEXT_VERSION_MAJOR;
import static org.lwjgl.glfw.GLFW.GLFW_CONTEXT_VERSION_MINOR;
import static org.lwjgl.glfw.GLFW.GLFW_FALSE;
import static org.lwjgl.glfw.GLFW.GLFW_OPENGL_CORE_PROFILE;
import static org.lwjgl.glfw.GLFW.GLFW_OPENGL_FORWARD_COMPAT;
import static org.lwjgl.glfw.GLFW.GLFW_OPENGL_PROFILE;
import static org.lwjgl.glfw.GLFW.GLFW_RESIZABLE;
import static org.lwjgl.glfw.GLFW.glfwCreateWindow;
import static org.lwjgl.glfw.GLFW.glfwInit;
import static org.lwjgl.glfw.GLFW.glfwMakeContextCurrent;
import static org.lwjgl.glfw.GLFW.glfwPollEvents;
import static org.lwjgl.glfw.GLFW.glfwSwapBuffers;
import static org.lwjgl.glfw.GLFW.glfwTerminate;
import static org.lwjgl.glfw.GLFW.glfwWindowHint;
import static org.lwjgl.glfw.GLFW.glfwWindowShouldClose;
import static org.lwjgl.opengl.GL.createCapabilities;
import static org.lwjgl.opengl.GL11.GL_FLOAT;
import static org.lwjgl.opengl.GL11.GL_TRIANGLES;
import static org.lwjgl.opengl.GL11.GL_TRUE;
import static org.lwjgl.opengl.GL11.glDrawArrays;
import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER;
import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW;
import static org.lwjgl.opengl.GL15.glBindBuffer;
import static org.lwjgl.opengl.GL15.glBufferData;
import static org.lwjgl.opengl.GL15.glGenBuffers;
import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray;
import static org.lwjgl.opengl.GL20.glVertexAttribPointer;
import static org.lwjgl.opengl.GL30.glBindVertexArray;
import static org.lwjgl.opengl.GL30.glGenVertexArrays;
import static org.lwjgl.system.MemoryStack.stackMallocFloat;
import static org.lwjgl.system.MemoryStack.stackPop;
import static org.lwjgl.system.MemoryStack.stackPush;
import static org.lwjgl.system.MemoryUtil.NULL;

import java.nio.FloatBuffer;

public class TestGLFWCore {
	public static void main(String[] args) {
		// Initialize GLFW
		glfwInit();

		// Create window
		glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
		glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
		glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
		glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
		glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
		long window = glfwCreateWindow(800, 600, "OpenGL 3.2 Core Window", NULL, NULL);
		
		// Set Graphics Context
		glfwMakeContextCurrent(window);
		createCapabilities();

		// Setup geometry
		int size = 2;
		int verts = 3;
		int bytes = 4;
		int vbo;
		int vao;
		stackPush();
		{
			// Initial vertex data
			FloatBuffer buffer = stackMallocFloat(verts * size);
			buffer.put(-0.5f).put(-0.5f); // Vert 1
			buffer.put(+0.5f).put(-0.5f); // Vert 2
			buffer.put(+0.0f).put(+0.5f); // Vert 3
			buffer.flip();

			// Generate buffers
			vbo = glGenBuffers();
			vao = glGenVertexArrays();

			// Upload Vertex Buffer
			glBindBuffer(GL_ARRAY_BUFFER, vbo);
			glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW);

			// Set attributes
			glBindVertexArray(vao);
			glEnableVertexAttribArray(0);
			glVertexAttribPointer( 0, size, GL_FLOAT, false, size*bytes, 0 );

			// Unbind
			glBindBuffer(GL_ARRAY_BUFFER, 0);
			glBindVertexArray(0);
		}
		stackPop();

		// Game loop
		while (!glfwWindowShouldClose(window)) {
			// Poll
			glfwPollEvents();

			// Bind
			glBindVertexArray(vao);

			// Draw
			glDrawArrays(GL_TRIANGLES, 0, verts);

			// Blit
			glfwSwapBuffers(window);
		}

		// Terminate
		glfwTerminate();
	}
}

orange451

I forgot that you needed a shader to render in OpenGL core mode.

heh. ;)