What am i doing wrong?

Started by scottyaim, December 11, 2016, 00:14:08

Previous topic - Next topic

scottyaim

Hi, i started out with lwjgl for a couple of days ago and i am trying my best to just render a triangle but iÃ,´m out of luck.
So could anybody point out why my code isn't drawing?

package com.jage.app;

import org.lwjgl.*;
import org.lwjgl.glfw.*;
import org.lwjgl.opengl.*;

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

import java.awt.Desktop.Action;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;

import javax.swing.JOptionPane;

public class App {
	
	private GLFWErrorCallback _ErrorCallback = GLFWErrorCallback.createPrint(System.err);
	private long _Window;
	
	int VAO;
	int VBO;
	
	private String[] _VertexSource = {
			"#version 330 core\n",
			"layout (location = 0) in vec3 position;\n",
			"void main()\n",
			"{\n",
			"    gl_Position = vec4(position.x, position.y, position.z, 1.0);\n",
			"}\n"
	};
	
	private String[] _FragSource = {
			"#version 330 core\n",
			"out vec4 color;\n",
			"void main()\n",
			"{\n", 
			"	color = vec4(0.0f, 1.0f, 0.0f, 1.0f);\n",
			"}\n"
	};
	
	int _ShaderProgram;

	public void Init() {
		
		InitGLFW();
		InitGL();
		
		while(!glfwWindowShouldClose(_Window)) {
			
			glfwPollEvents();
			
			glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
			glClear(GL_COLOR_BUFFER_BIT);
			
			GL20.glUseProgram(_ShaderProgram);
			GL30.glBindVertexArray(VAO);
			glDrawArrays(GL_TRIANGLES, 0, 3);
			GL30.glBindVertexArray(0);
			
			glfwSwapBuffers(_Window);
		}
		
		GL20.glDeleteProgram(_ShaderProgram);
		GL30.glDeleteVertexArrays(VAO);
		GL15.glDeleteBuffers(VBO);
		
		glfwTerminate();
	}
	
	private void InitGLFW()
	{
		glfwSetErrorCallback(_ErrorCallback);
		
		if(!glfwInit()){
			throw new IllegalStateException("Unable To Start GLFW");
		}
		
		_Window = glfwCreateWindow(800, 600, "JAGE", NULL, NULL);
		if(_Window == NULL){
			glfwTerminate();
			
			throw new RuntimeException("Window Creation Failed");
		}
		
		GLFWKeyCallback callback = new GLFWKeyCallback() {
			
			@Override
			public void invoke(long window, int key, int arg2, int action, int arg4) {
				// TODO Auto-generated method stub
				if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
					glfwSetWindowShouldClose(window, true);
				}
			}
		};
		
		glfwSetKeyCallback(_Window, callback);
		
		glfwMakeContextCurrent(_Window);
	}
	
	private void InitGL()
	{
		int success = 0;
		GL.createCapabilities();
		
		int vertexShader = GL20.glCreateShader(GL20.GL_VERTEX_SHADER);
		GL20.glShaderSource(vertexShader, _VertexSource);
		GL20.glCompileShader(vertexShader);
		
		success = GL20.glGetShaderi(vertexShader, GL20.GL_COMPILE_STATUS);
		
		if(success == GL_FALSE)
		{
			JOptionPane.showMessageDialog(null, "Vertex Shader Compile Error");
			String log = GL20.glGetShaderInfoLog(vertexShader);
			System.out.print(log);
		}
		
		int fragShader = GL20.glCreateShader(GL20.GL_FRAGMENT_SHADER);
		GL20.glShaderSource(fragShader, _FragSource);
		GL20.glCompileShader(fragShader);
		
		success = GL20.glGetShaderi(fragShader, GL20.GL_COMPILE_STATUS);
		
		if(success == GL_FALSE)
		{
			JOptionPane.showMessageDialog(null, "Frag Shader Compile Error");
			String log = GL20.glGetShaderInfoLog(fragShader);
			System.out.print(log);
		}
		
		_ShaderProgram = GL20.glCreateProgram();
		GL20.glAttachShader(_ShaderProgram, vertexShader);
		GL20.glAttachShader(_ShaderProgram, fragShader);
		
		GL20.glLinkProgram(_ShaderProgram);
		
		success = GL20.glGetProgrami(_ShaderProgram, GL20.GL_LINK_STATUS);
		
		if(success == GL_FALSE)
		{
			JOptionPane.showMessageDialog(null, "Program Link Error");
			String log = GL20.glGetProgramInfoLog(_ShaderProgram);
			System.out.print(log);
		}
		
		GL20.glDeleteShader(vertexShader);
		GL20.glDeleteShader(fragShader);
		
		VAO = GL30.glGenVertexArrays();
		GL30.glBindVertexArray(VAO);
		
		FloatBuffer vertexBuffer;
		vertexBuffer = BufferUtils.createFloatBuffer(9);
		
		float vertices[] = {
			    -0.5f, -0.5f, 0.0f,
			     0.5f, -0.5f, 0.0f,
			     0.0f,  0.5f, 0.0f
		};  
		
		vertexBuffer.put(vertices);
		
		vertexBuffer.flip();
		
		VBO = GL15.glGenBuffers();
		GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, VBO);
		GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertexBuffer, GL15.GL_STATIC_DRAW);
		
		GL20.glVertexAttribPointer(0, 3, GL_FLOAT, false, 3 * Float.SIZE, 0);
		GL20.glEnableVertexAttribArray(0);
		
		GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
		GL30.glBindVertexArray(0);
	}
	
	public static void main(String[] args)
	{
		new App().Init();
	}
	
}

CoDi

Quote from: scottyaim on December 11, 2016, 00:14:08
GL20.glVertexAttribPointer(0, 3, GL_FLOAT, false, 3 * Float.SIZE, 0);


GL20.glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);


That's one easy to miss/misunderstand with OpenGL. The "stride" parameter is the number of bytes from the end of one vertex to the start of the next.

scottyaim

Aha, i thought stride was the number of bytes in the vertex structure as it is in directx.
Thank you for your help!

Kai

Quote from: CoDi on December 11, 2016, 00:59:59
That's one easy to miss/misunderstand with OpenGL. The "stride" parameter is the number of bytes from the end of one vertex to the start of the next.
This is incorrect, and you did misunderstand it. The stride is the number of bytes between the same vertex attribute of two consecutive vertices. The word "between" is not to be understood as "between the end of the previous and the start of the following vertex". It includes the size of the respective vertex attribute.
So you can say it is the number of bytes from the start of the vertex to the start of the consecutive vertex.
In that way a value of 0 does not make any sense because it would mean that the vertex attribute is of size 0 bytes. However the specification clearly says that in this case, the vertices are understood to be packed ( that means non-interleaved). And in fact, in this case specifying either 0 or the number of bytes of one vertex is equivalent.

The actual problem is that the OP used Float.SIZE, which is not the number of bytes of one float, but the number of bits ( = 32). The OP should have used 3 * Float.BYTES. Or, since in this case it is equivalent, 0.

As for the comparison with Direct3D, yes, both stride values mean the same thing in OpenGL and Direct3D' SetStreamSource. However, the special value 0 differs in meaning between OpenGL and Direct3D, in that D3D also uses it as the instance divisor.

scottyaim

Quote from: Kai on December 11, 2016, 11:48:34
Quote from: CoDi on December 11, 2016, 00:59:59
That's one easy to miss/misunderstand with OpenGL. The "stride" parameter is the number of bytes from the end of one vertex to the start of the next.
This is incorrect, and you did misunderstand it. The stride is the number of bytes between the same vertex attribute of two consecutive vertices. The word "between" is not to be understood as "between the end of the previous and the start of the following vertex". It includes the size of the respective vertex attribute.
So you can say it is the number of bytes from the start of the vertex to the start of the consecutive vertex.
In that way a value of 0 does not make any sense because it would mean that the vertex attribute is of size 0 bytes. However the specification clearly says that in this case, the vertices are understood to be packed ( that means non-interleaved). And in fact, in this case specifying either 0 or the number of bytes of one vertex is equivalent.

The actual problem is that the OP used Float.SIZE, which is not the number of bytes of one float, but the number of bits ( = 32). The OP should have used 3 * Float.BYTES. Or, since in this case it is equivalent, 0.

As for the comparison with Direct3D, yes, both stride values mean the same thing in OpenGL and Direct3D' SetStreamSource. However, the special value 0 differs in meaning between OpenGL and Direct3D, in that D3D also uses it as the instance divisor.

Ok, now i understand.
Thanks!  :)

CoDi

Quote from: Kai on December 11, 2016, 11:48:34
Quote from: CoDi on December 11, 2016, 00:59:59
That's one easy to miss/misunderstand with OpenGL. The "stride" parameter is the number of bytes from the end of one vertex to the start of the next.
This is incorrect, and you did misunderstand it. The stride is the number of bytes between the same vertex attribute of two consecutive vertices. The word "between" is not to be understood as "between the end of the previous and the start of the following vertex". It includes the size of the respective vertex attribute.
So you can say it is the number of bytes from the start of the vertex to the start of the consecutive vertex.

Now you can see how reading GL documentation continues to bemuse me. Every. Freaking. Time. Thanks for clarifying.