Guys, no matter how research my rectangle doesn't show up

Started by Kudze, February 18, 2016, 20:41:56

Previous topic - Next topic

Kudze

Hello Guys I have problem that I can't see my rectangle rendered here's my code

//BufferUtil class

package Kudze.Engine.Render.OpenGL.Util;

import java.nio.FloatBuffer;
import java.nio.IntBuffer;

import org.lwjgl.BufferUtils;

import Kudze.Engine.Math.Vector3;

public class BufferUtil {

	public static IntBuffer storeDataInIntBuffer(int[] data) {
		IntBuffer buffer = BufferUtils.createIntBuffer(data.length);
		buffer.put(data);
		buffer.flip();
		return buffer;
	}
	
	public static FloatBuffer storeDataInFloatBuffer(Vector3[] data) {
		FloatBuffer buffer = BufferUtils.createFloatBuffer(data.length*3);
		for(int i = 0; i < data.length; i++) {
			buffer.put(data[i].x);
			buffer.put(data[i].y);
			buffer.put(data[i].z);
		}
		System.out.println(buffer.toString());
		buffer.flip();
		return buffer;
		
	}
	
	public static FloatBuffer storeDataInFloatBuffer(float[] data) {
		FloatBuffer buffer = BufferUtils.createFloatBuffer(data.length*3);
		buffer.put(data);
		buffer.flip();
		return buffer;
	}
	
}

//My loader class
package Kudze.Engine.Render.OpenGL.Objects3D;

import Kudze.Engine.Math.Vector3;
import Kudze.Engine.Render.OpenGL.Util.BufferUtil;

import static org.lwjgl.opengl.GL30.*;
import static org.lwjgl.opengl.GL20.*;
import java.util.ArrayList;
import java.util.List;

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

public class Loader {
	
	private List<Integer> vbos = new ArrayList<Integer>();
	private List<Integer> vaos = new ArrayList<Integer>();
	
	public Mesh loadtoVAO(Vector3[] positions, int[] indices) {
		int vaoID = createVAO();
		bindIndicesBuffer(indices);
		storeDataInAttributeList(0, positions);
		unbindVAO();
		return new Mesh(vaoID, indices.length);
		
	}
	
	private int createVAO() {
		int vaoID = glGenVertexArrays(); //Kuria
		vaos.add(vaoID);
		glBindVertexArray(vaoID); //bindina
		return vaoID;
	}
	
	private void storeDataInAttributeList(int attributeNumber, Vector3[] positions) {
		int vboID = glGenBuffers(); //sukuria vbo
		vbos.add(vboID);
		glBindBuffer(GL_ARRAY_BUFFER, vboID); //pabindina vbo (tipo kad dabar Ã...¡ita naudosim)
		glBufferData(GL_ARRAY_BUFFER, BufferUtil.storeDataInFloatBuffer(positions), GL_STATIC_DRAW); // ideda data Ã,,¯ vbo
		glVertexAttribPointer(attributeNumber, 3, GL11.GL_FLOAT, false, 0, 0); //ideda vbo Ã,,¯ vao
		glBindBuffer(GL_ARRAY_BUFFER, 0); //Unbindina vbo kad nenaudojam
	}
	
	private void unbindVAO() {
		glBindVertexArray(0);
	}
	
	private void bindIndicesBuffer(int[] indices) {
		int vboID = glGenBuffers();
		vbos.add(vboID);
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboID);
		glBufferData(GL_ELEMENT_ARRAY_BUFFER, BufferUtil.storeDataInIntBuffer(indices), GL_STATIC_DRAW);
	}
	
	public void destroy() {
		for(int vao : vaos) {
			glDeleteVertexArrays(vao);
		}
		for(int vbo : vbos) {
			glDeleteBuffers(vbo);
		}
	}


//my mesh class
package Kudze.Engine.Render.OpenGL.Objects3D;

public class Mesh {
	
	private int vaoID;
	private int vertexNum;
	
	public Mesh (int vaoID, int vertexNum) {
		this.vaoID = vaoID;
		this.vertexNum = vertexNum;
	}
	
	public int getVaoID() { return vaoID; }
	public int getVertexAmmount() { return vertexNum; }

}

//shaders abstract class
package Kudze.Engine.Render.OpenGL.Shaders;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

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

public abstract class ShaderProgram {

	private int programID;
	private int vertexShaderID;
	private int fragmentShaderID;
	
	public ShaderProgram(String vertexFile, String fragmentFile) {
		vertexShaderID = loadShader(vertexFile, GL_VERTEX_SHADER);
		fragmentShaderID = loadShader(fragmentFile, GL_FRAGMENT_SHADER);
		programID = glCreateProgram();
		glAttachShader(programID, vertexShaderID);
		glAttachShader(programID, fragmentShaderID);
		bindAttributes();
		glLinkProgram(programID);
		glValidateProgram(programID);
		glDeleteShader(vertexShaderID);
		glDeleteShader(fragmentShaderID);
	}
	
	public void start() {
		glUseProgram(programID);
	}
	
	public void stop() {
		glUseProgram(0);
	}
	
	public void dispose() {
		stop();
		glDetachShader(programID, vertexShaderID);
		glDetachShader(programID, fragmentShaderID);
		glDeleteShader(vertexShaderID);
		glDeleteShader(fragmentShaderID);
		glDeleteProgram(programID);
	}
	
	protected abstract void bindAttributes();
	
	protected void bindAttribute(int attribute, String variableName) {
		glBindAttribLocation(programID, attribute, variableName);
	}
	
	private static int loadShader(String file, int type) {
		StringBuilder shaderSRC = new StringBuilder();
		try {
			BufferedReader reader = new BufferedReader(new FileReader(file));
			String line;
			while((line = reader.readLine()) != null) {
				shaderSRC.append(line).append("\n");
			}
			reader.close();
		} catch(IOException e) {
			System.err.println("Could not load shader from file: " + file);
			e.printStackTrace();
			System.exit(1);
		}
		int shaderID = glCreateShader(type);
		glShaderSource(shaderID, shaderSRC);
		glCompileShader(shaderID);
		if(glGetShaderi(shaderID, GL_COMPILE_STATUS) == GL_FALSE) {
			System.out.println(glGetShaderInfoLog(shaderID, 1024));
			System.err.println("Could not copile shader: " + file);
			System.exit(1);
		}
		return shaderID;
	}
	
}

//shaders class

package Kudze.Engine.Render.OpenGL.Shaders;

public class StaticShader extends ShaderProgram {
	
	private static final String VERTEX_FILE = "src/Kudze/Engine/Render/OpenGL/Shaders/vertexShader.txt";
	private static final String FRAGMENT_FILE = "src/Kudze/Engine/Render/OpenGL/Shaders/fragmentShader.txt";

	public StaticShader() {
		super(VERTEX_FILE, FRAGMENT_FILE);
	}

	@Override
	protected void bindAttributes() {
		super.bindAttribute(0, "position");
	}

}

//fragment shader
#version 400 core

in vec3 colour;

out vec4 out_Color;

void main(void) {

	out_Color = vec4(colour, 1.0);

}

//vertex shader
#version 400 core

in vec3 position;

out vec3 colour;

void main (void) {

	gl_Position = vec4(position, 1.0);
	colour = vec3(position.x + 0.5, 1.0, position.y + 0.5);

}

and my game class (it clears screen before draw and does display thing after draw in other class)

package Kudze.Amandas.Game;


import Kudze.Engine.Core.Engine;
import Kudze.Engine.Core.Game;
import Kudze.Engine.Math.Vector3;
import Kudze.Engine.Render.OpenGL.Objects3D.Mesh;
import Kudze.Engine.Render.OpenGL.Shaders.StaticShader;

public class SandBox implements Game {
	
	public static Engine engine;

	@Override
	public void create(Engine engine) {
		SandBox.engine = engine;
		
	}
	
	private Mesh mesh;
	private StaticShader shader;
	
	@Override
	public void start() {
		
		Vector3[] vertices = {
				new Vector3(-0.5f, 0.5f, 0f),
				new Vector3(-0.5f, -0.5f, 0f),
				new Vector3(0.5f, -0.5f, 0f),
				new Vector3(0.5f, 0.5f, 0f),
		};
		
		int[] indices = {
				0, 1, 3,
				3, 1, 2
		};
		
		mesh = engine.video().get3DLoader().loadtoVAO(vertices, indices);
		
		shader = new StaticShader();
		
	}

	@Override
	public void update(double deltaTime) {
		
	}

	@Override
	public void draw() {
                shader.start();
		engine.video().render(mesh);
		shader.start();
	}

	@Override
	public void dispose() {
		shader.dispose();
		engine.video().get3DLoader().destroy();
	}
}




	
}

Kai

One thing you are missing is `glEnableVertexAttribArray(attributeNumber)` preferably in your `storeDataInAttributeList()` method.
Generic vertex attributes will only be used when enabled.

Kudze

I changed it like this:

private void storeDataInAttributeList(int attributeNumber, Vector3[] positions) {
		int vboID = glGenBuffers(); //sukuria vbo
		vbos.add(vboID);
		glBindBuffer(GL_ARRAY_BUFFER, vboID); //pabindina vbo (tipo kad dabar Ã...¡ita naudosim)
		glBufferData(GL_ARRAY_BUFFER, BufferUtil.storeDataInFloatBuffer(positions), GL_STATIC_DRAW); // ideda data Ã,,¯ vbo
		glEnableVertexAttribArray(attributeNumber);
		glVertexAttribPointer(attributeNumber, 3, GL11.GL_FLOAT, false, 0, 0); //ideda vbo Ã,,¯ vao
		glDisableVertexAttribArray(attributeNumber);
		glBindBuffer(GL_ARRAY_BUFFER, 0); //Unbindina vbo kad nenaudojam
	}


And it still doesn't show my rect Btw i forgot to include into my post how i draw my mesh on screen so here you go:

public void render(Mesh model) {
		glBindVertexArray(model.getVaoID());
		glEnableVertexAttribArray(0);
		glDrawElements(GL_TRIANGLES, model.getVertexAmmount(), GL_UNSIGNED_INT, 0);
		glDisableVertexAttribArray(0);
		glBindVertexArray(0);
	}