Cannot render any shapes

Started by dropkick, December 14, 2015, 18:44:04

Previous topic - Next topic

dropkick

I'm new to the whole LWJGL3, and I've read through numerous different tutorials each with their own separate way to render shapes and none have worked for me. I'm sure it's probably something stupid or the fact that I've just tried so many different things in the same project. Either way, I am able to change the clear color, but not much else besides that. Here's all my relevant code.

package io.github.tylerstonge.cookiez;

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.opengl.GL30.*;
import static org.lwjgl.opengl.GL32.*;
import static org.lwjgl.system.MemoryUtil.*;

import java.nio.FloatBuffer;

import io.github.tylerstonge.cookiez.input.KeyboardHandler;
import io.github.tylerstonge.cookiez.input.MouseHandler;
import io.github.tylerstonge.cookiez.utils.ShaderHandler;
import io.github.tylerstonge.cookiez.utils.Timer;

import org.lwjgl.BufferUtils;
import org.lwjgl.glfw.GLFWCursorPosCallback;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.glfw.GLFWKeyCallback;
import org.lwjgl.glfw.GLFWVidMode;
import org.lwjgl.opengl.GL;

public class Main implements Runnable {
	
	// Constants
	public static final int WIDTH = 640;
	public static final int HEIGHT = 480;
	public static final String TITLE = "CookieZ";
	
	// Core
	public long window;
	public boolean running = false;
	public Thread thread;
	
	// Store callback to avoid garbage collection
	private GLFWKeyCallback keyCallback;
	private GLFWErrorCallback errorCallback;
	private GLFWCursorPosCallback mouseCallback;
	
	// Shaders
	int vertexShader;
	int fragmentShader;
	int shaderProgram;
	
	// Buffers
	int vbo;
	int vao;
	
	// Timing variables
	private Timer timer;
	private float targetUPS = 30f;
	
	private void init() {
		this.running = true;
		
		// Set error callback to System.err
		errorCallback = GLFWErrorCallback.createPrint(System.err);
		glfwSetErrorCallback(errorCallback);
		
		if (glfwInit() != GL_TRUE) {
			throw new IllegalStateException("Unable to initialize GLFW");
		}
		
		// Window options
		glfwDefaultWindowHints();
		glfwWindowHint(GLFW_SAMPLES, 4);
		glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
		glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
		glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
		glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
		
		window = glfwCreateWindow(WIDTH, HEIGHT, TITLE, NULL, NULL);
		
		if (window == NULL) {
			throw new RuntimeException("Failed to create the GLFW window");
		}
		
		GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
		glfwSetWindowPos(window, (vidmode.width() - WIDTH) / 2, (vidmode.height() - HEIGHT) / 2);
		glfwMakeContextCurrent(window);
		glfwSwapInterval(1);
		glfwShowWindow(window);
		
		GL.createCapabilities();
		
		// Enable input
		keyCallback = new KeyboardHandler();
		glfwSetKeyCallback(window, keyCallback);
		mouseCallback = new MouseHandler();
		glfwSetCursorPosCallback(window, mouseCallback);
		
		// Set default clearing color
		glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
		glEnable(GL_DEPTH_TEST);
		System.out.println("OpenGL: "+glGetString(GL_VERSION));
		
		timer = new Timer(1f/targetUPS);
		
		// Initialize shaders
		shaderProgram = ShaderHandler.initializeShaderProgram();
		int shaderStatus = glGetProgrami(shaderProgram, GL_LINK_STATUS);
		if (shaderStatus != GL_TRUE)
			throw new RuntimeException(glGetProgramInfoLog(shaderProgram));
		
		// Debug stuff
	    float[] vert = new float[]{
                -0.5f, -0.5f, 
                -0.5f,  0.5f, 
                 0.5f, -0.5f,
                 0.5f,  0.5f, 
                -0.5f,  0.5f, 
                 0.5f, -0.5f, 
	    };
		FloatBuffer vertices = BufferUtils.createFloatBuffer(vert.length);
		vertices.put(vert).flip();
		
		vbo = glGenBuffers();
		glBindBuffer(GL_ARRAY_BUFFER, vbo);
		glBufferData(GL_ARRAY_BUFFER, vertices, GL_STATIC_DRAW);
		glBindBuffer(GL_ARRAY_BUFFER, 0);
		
		vao = glGenVertexArrays();
		glBindVertexArray(vao);
		
		glVertexAttribPointer(0, 2, GL_FLOAT, false, 0, 0);
		glBindVertexArray(0);
	}
	
	private void update() {
		timer.updateUPS();
		// Poll for new events
		glfwPollEvents();
		
		// Test keyboard
		if (KeyboardHandler.isKeyDown(GLFW_KEY_SPACE))
			System.out.println("Space key is pressed");
	}

	private void render(float alpha) {
		timer.updateFPS();
		// Clear screen
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		
		glUseProgram(shaderProgram);
		
		glBindBuffer(GL_ARRAY_BUFFER, vbo);
		glBindAttribLocation(shaderProgram, 0, "position");
		glEnableVertexAttribArray(0);
		glVertexAttribPointer(0, 2, GL_FLOAT, false, 0, 0);
		
		glDrawArrays(GL_TRIANGLES, 0, 6);
		
		// disable location
		glDisableVertexAttribArray(0);
		
		// unbind
		glUseProgram(0);
		glfwSwapBuffers(window);
	}
	
	private void dispose() {
		// Dispose program
		glDeleteProgram(shaderProgram);
		
		// Dispose VBO
		glBindBuffer(GL_ARRAY_BUFFER, 0);
		glDeleteBuffers(vbo);
		
		// Destroy window
		glfwDestroyWindow(window);
		keyCallback.release();
		glfwTerminate();
		errorCallback.release();
	}
	
	public void run() {
		init();
		@SuppressWarnings("unused")
		float delta;
		while (running) {
			timer.update();
			delta = timer.getDelta();
			while (timer.shouldUpdate())
					update();
			render(timer.getAlpha());
			
			if (glfwWindowShouldClose(window) == GL_TRUE) {
				running = false;
			}
		}
		dispose();
	}
	
	public static void main(String[] args) {
		new Main().run();
	}
	
}


package io.github.tylerstonge.cookiez.utils;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL30.*;

import java.io.*;

public class ShaderHandler {

	public static final String BASE_DIR = "/io/github/tylerstonge/cookiez/shaders/";
	public static final String VERTEX_SHADER = "vertex.shader";
	public static final String FRAGMENT_SHADER = "fragment.shader";
	
	public static CharSequence getShader(String path) throws IOException {
		InputStream is = ShaderHandler.class.getResourceAsStream(BASE_DIR+path);
		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
		
		String shader = "";
		String line;
		while ((line = bufferedReader.readLine()) != null) {
			shader += line + "\n";
		}
		bufferedReader.close();
		return shader;
	}
	
	public static int initializeShaderProgram() {
		int vertexShader = glCreateShader(GL_VERTEX_SHADER);
		int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
		try {
			glShaderSource(vertexShader, ShaderHandler.getShader(ShaderHandler.VERTEX_SHADER));
			glCompileShader(vertexShader);
			glShaderSource(fragmentShader, ShaderHandler.getShader(ShaderHandler.FRAGMENT_SHADER));
			glCompileShader(fragmentShader);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		int vstatus = glGetShaderi(vertexShader, GL_COMPILE_STATUS);
		if (vstatus != GL_TRUE)
			throw new RuntimeException(glGetShaderInfoLog(vertexShader));
		
		int fstatus = glGetShaderi(fragmentShader, GL_COMPILE_STATUS);
		if (fstatus != GL_TRUE)
			throw new RuntimeException(glGetShaderInfoLog(fragmentShader));
		
		int shaderProgram = glCreateProgram();
		glAttachShader(shaderProgram, vertexShader);
		glAttachShader(shaderProgram, fragmentShader);
		glBindFragDataLocation(shaderProgram, 0, "fragColor");
		glLinkProgram(shaderProgram);
		
		glDeleteShader(vertexShader);
		glDeleteShader(fragmentShader);
		return shaderProgram;
	}
	
}


fragment.shader:
#version 330 core

out vec4 color;

void main()
{
	color = vec4(0.0f, 1.0f, 0.0f, 1.0f);
}


vertex.shader:
#version 330 core

in vec2 position;

void main()
{
	gl_Position = vec4(position, 0.0f, 1.0f);
}


I've been struggling with this for a few days now, any help is much appreciated!

abcdef

Some thoughts

1) z = 0 is ok, but only if you have a 2D projection matrix. By default z = 0 is not something you will see unless you also have a view matrix which moves the world forward so you can see more
2) Having x and y in the 0.5 range means you will have a tiny triangle, try making those 50 to make it easy to see

Kai

You do not have a VAO bound when invoking the draw call.
Simply remove the call of `glDisableVertexAttribArray(0)` in line 156 of your code listing.
There are however a lot of redundant OpenGL calls (all vertex specification calls in the render() method) as well as calls that have no effect (the glBindAttribLocation() in the render() method) and calls that are kindof illegal or rather pointless (calling glVertexAttribPointer() in line 127 of your listing when there is no GL_ARRAY_BUFFER bound).

Having z=0 is okay, since the OP does have a 2D projection matrix (i.e. no matrix transformation at all, which equals the identitiy matrix), so the model coordinates are essentially in clip/NDC space where all coordinates between [-1..+1] are visible.