I am unable to get any shape representation with VBO (triangle).

Started by creator, October 26, 2016, 04:36:10

Previous topic - Next topic

creator

Hi all.

Just starting out, been at this for some time now and I am having problems with drawing shapes on my newly created window. I am using LWJGL 3 stable on Fedora Linux.

The entire code is down bellow, I am not expecting anyone to go through the whole thing, but I figured I would post it anyway just in case.

Problem:
In MasterRenderer class in method render() if I put GL_POINTS instead of GL_TRIANGLES I get a dot in the middle of the screen, if I leave GL_TRIANGLES I get nothing as nothing is shown, there are no errors, I made sure that all methods are being called in proper order or I am pretty sure of it in any case, inserted a tone of print outs and just went one method at a time to make sure that things are getting initialized and called.

Could you please help me out, I am just starting.

Thank you all for your time and effort.

Vertex.java
public class Vertex 
{
	public static final int SIZE = 3;
	
	private float x;
	private float y;
	private float z;
	
	public Vertex(float x, float y, float z)
	{
		this.x = x;
		this.y = y;
		this.z = z;
	}
	
	public float getX() {
		return x;
	}

	public void setX(float x) {
		this.x = x;
	}

	public float getY() {
		return y;
	}

	public void setY(float y) {
		this.y = y;
	}

	public float getZ() {
		return z;
	}

	public void setZ(float z) {
		this.z = z;
	}
}



Model.java
package com.base.engine;

import static org.lwjgl.opengl.GL15.*;

import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;

public class Model 
{
	private int vbo;
	private int size;
	
	public int getS()
	{
		return size;
	}
	
	public Model()
	{
		vbo = glGenBuffers();
		
		size = 0;
	}
	
	public void bufferVertices(Vertex[] vertices)
	{
		FloatBuffer buffer = BufferUtils.createFloatBuffer(vertices.length * Vertex.SIZE);
		
		for(Vertex vertex : vertices)
		{
			buffer.put(vertex.getX());
			buffer.put(vertex.getY());
			buffer.put(vertex.getZ());
		}
		
		buffer.flip();
		
		glBindBuffer(GL_ARRAY_BUFFER, vbo);
		glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW);
		glBindBuffer(GL_ARRAY_BUFFER, 0);
		
		size = vertices.length;
		
	}
	
	public int getVBO() {
		return vbo;
	}

	public int getSize() {   
		return size;
	}

}


MasterRenderer.java
package com.base.engine;

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

public class MasterRenderer 
{
	private Model model;
	
	public MasterRenderer()
	{
		init();
		
		model = new Model();
		
		Vertex[] vertices = {
				new Vertex( -1,-1,0),
				new Vertex( 1,-1,0),
				new Vertex( 0,1,0)
		};
		model.bufferVertices(vertices);
		
		
	}
	
	public void render()
	{
		prepare();
		
		glBindBuffer(GL_ARRAY_BUFFER, model.getVBO());
		glEnableVertexAttribArray(0);
		glVertexAttribPointer(0, 3, GL_FLOAT, false, Vertex.SIZE * 4, 0);
		glDrawArrays(GL_POINTS, 0, model.getSize());
		glDisableVertexAttribArray(0);
		glBindBuffer(GL_ARRAY_BUFFER, 0);	
	}
	
	public void prepare()
	{
		glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	}
	
	private void init()
	{
		glEnable(GL_DEPTH_TEST);
	}
	
	public void cleanUp()
	{
		
	}
}


MainComponent.java
package com.base.engine;

import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.glGetError;
import static org.lwjgl.glfw.Callbacks.*;

import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.opengl.GL;


public class MainComponent 
{	
	private GLFWErrorCallback errorCallback;
	
	private MasterRenderer renderer;
	private Window window;
	
	private boolean running = false;
	
	public MainComponent()
	{
		init();
		renderer = new MasterRenderer();
	}
	
	private void input()
	{
		glfwPollEvents();
		if(window.shouldClose())
			stop();
	}
	
	private void update()
	{
		
	}
	
	private void render()
	{
		renderer.render();
		window.render();
	}
	
	private void run()
	{
		long lastTime = System.nanoTime();
		long curTime = lastTime;
		
		long timer = System.currentTimeMillis();
		
		double ns = 1000000000 / 60;
		double delta = 0.0;
		
		int fps = 0;
		int ups =  0;
		
		while(running)
		{
			curTime = System.nanoTime();
			delta += (curTime - lastTime) / ns;
			lastTime = curTime;
			
			while(delta >= 1.0)
			{
				input();
				update();
				ups++;
				delta--;
			}
			render();
			fps++;
			
			if(System.currentTimeMillis() > timer + 1000)
			{
				window.setTitle("Tutorial - " + "ups: " + ups + " | fps: " + fps);
				ups = 0;
				fps = 0;
				timer += 1000;
			}
			
		}
		cleanUp();
	}
	
	public void start()
	{
		if(running)
			return;
		running = true;
		run();
	}
	
	public void stop()
	{
		if(!running)
			return;
		running = false;
	}
	
	private void init()
	{
		glfwSetErrorCallback(errorCallback = GLFWErrorCallback.createPrint(System.err));
		glfwInit();
		
		window = new Window(1280, 720, "Tutorial");
		GL.createCapabilities();
	}
	
	private void cleanUp()
	{
		window.hide();
		renderer.cleanUp();
		window.dispose();
	}
	
	public static void main(String[] args)
	{
		
		MainComponent main = new MainComponent();
		main.start();
		
	}
}


Window.java
package com.base.engine;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.system.MemoryUtil.*;

public class Window 
{
	private long window;
	
	public Window(int width, int height, String title)
	{
		glfwDefaultWindowHints();
		glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
		glfwWindowHint(GLFW_VISIBLE, GL_TRUE);
		glfwWindowHint(GLFW_DECORATED, GL_TRUE);
		glfwWindowHint(GLFW_FOCUSED, GL_TRUE);
		
		window = glfwCreateWindow(width, height, title, NULL, NULL);
		
		glfwMakeContextCurrent(window);
	}
	
	 public void dispose()
	 {
		 glfwDestroyWindow(window);
	 }
	 
	 public void hide()
	 {
		 glfwHideWindow(window);
	 }
	 
	 public void render()
	 {
		 glfwSwapBuffers(window);
	 }
	 
	 public void show()
	 {
		 glfwShowWindow(window);
	 }
	 
	 public void setTitle(String title)
	 {
		 glfwSetWindowTitle(window, title);
	 }
	 
	 public boolean shouldClose()
	 {
		 if(glfwWindowShouldClose(window))
			 return true;
		 return false;
	 }
}


abcdef

Are you sure you aren't drawing a black triangle on a black background?

I haven't used the method you are using for so long that I'm not 100% sure on what your error could be, but I would recommend to search for "Riven VBO examples" on google and you should get a java-gaming.org article (I can't access jgo to give you the link myself from where I am at the moment) with a nice working tutorial on VBO's. You just need to integrate it in to GLFW (From what I see that bit looks correct)

Cornix

There are many possibilities for your problem and you should check all of them just to make sure:
- The color of your triangle is the same as the background (what abcdef said)
- The winding order of your vertices is wrong and you cull the backface
- your matrices are screwed up and you render your triangle outside of your window
- your viewport settings are screwed up and you cull your triangle
- your VBO / VAO setting is screwed up. You need to make sure to pass the correct params to the bind / render functions
- your shaders are screwed up
- you generate glErrors but you dont poll them
- etc etc etc

If you cant find your problem I suggest you start with a simple example from the LWJGL demos which works and then you successively refactor it to match your own code while testing every now and then to make sure everything still works.

creator

The colours are not the same, the colour of the point when I just draw a point is white, I even tried changing the background.
I found that example with vbo I ll give it a shoot.

Kai

I don't see where the problem is. When I change the primitive type of the glDrawArrays() draw to GL_TRIANGLES, I see a triangle.
You don't see GL_POINTS of course, because they lie on the edges of the window (-1, ... +1) and are tiny tiny pixels.

creator

I am running exactly the same code as I pasted it. If you are getting it and I am not, could it be that the problem is elsewhere outside of the code?

advanced_set

QuoteIf you cant find your problem I suggest you start with a simple example from the LWJGL demos which works and then you successively refactor it to match your own code while testing every now and then to make sure everything still works.

Creator, I would follow this advice. OpenGL is quite bug prone because you need to follow steps in a particular order and a lot of things can go wrong, in particular with the maths. I could not move forward until I found this tutorial here, which I recommend:
http://learnopengl.com/

I strongly recommend you to defer the object modelling until you have examples running. Just go with C-like procedural code and refactor after you have things working.