First program of the red book - it does not visualize anything

Started by ingframin, September 16, 2016, 17:26:40

Previous topic - Next topic

ingframin

Hello,
I am new to the forum.

I am trying to reproduce the first program of the red book in order to get started with lwjgl.

While in C++ I do not have any issues in rendering (see https://github.com/ingframin/Monica-s-Bubbles, it's the starting point of a puzzle bubble clone for my gf...)
I am having some problems with lwjgl and I am sure I am making some super newbie mistake.

Can you help me?

My code is the following:
package lwjglGame;
import static org.lwjgl.glfw.GLFW.*;
import org.lwjgl.glfw.*;
import static org.lwjgl.glfw.Callbacks.*;
import static org.lwjgl.opengl.GL.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL30.*;

public class Main {
	GLFWErrorCallback errorCallback = GLFWErrorCallback.createPrint(System.err);
	public String vertex_shader = "#version 330 core\n"
			+ "layout(location = 0) in vec4 vPosition;\n"
			+ "void main(){\n"
			+ "gl_Position = vPosition;\n"
			+ "}";
	public String fragment_shader = "version 330 core\n"
			+ "out vec4 fColor;\n"
			+ "void main(){\n"
			+ "fColor = vec4(0.0,0.0,1.0,1.0);"
			+ "}"; 
	int vPosition = 0;
	
	public Main(){
		if(!glfwInit()){
			System.err.println("GLFW failed to Init!");
			System.exit(1);
		}
		
		glfwSetErrorCallback(errorCallback);
		
		long win = glfwCreateWindow(800, 600, "test 1", 0, 0);
		
		glfwMakeContextCurrent(win);
		createCapabilities();
		
		int vao = glGenVertexArrays();
		glBindVertexArray(vao);
		
		float vertices[] = {-0.5f,-0.5f,0.0f,0.5f,0.5f,-0.5f};
		int vbo = glGenBuffers();
		
		glBindBuffer(GL_ARRAY_BUFFER,vbo);
		
		glBufferData(GL_ARRAY_BUFFER,vertices,GL_STATIC_DRAW);
		
		int vshader = glCreateShader(GL_VERTEX_SHADER);
		glShaderSource(vshader,vertex_shader);
		glCompileShader(vshader);
		
		int fshader = glCreateShader(GL_FRAGMENT_SHADER);
		glShaderSource(fshader,fragment_shader);
		glCompileShader(fshader);
		
		int program = glCreateProgram();
		glAttachShader(program, vshader);
		glAttachShader(program, fshader);
		glLinkProgram(program);
		glUseProgram(program);
		
		glVertexAttribPointer(vPosition, 1, GL_FLOAT, false, 0, vertices);
		glEnableVertexAttribArray(vPosition);
		
		
		while(glfwWindowShouldClose(win)!= true){
			
			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
			glBindVertexArray(vao);
			glDrawArrays(GL_TRIANGLES, 0, 3);
			glFlush();
			glfwSwapBuffers(win);
			glfwPollEvents();
		}
		glfwFreeCallbacks(win);
		glfwDestroyWindow(win);
		glfwTerminate();
	}
	
	public static void main(String[] args){
		new Main();
	}

}


I still did not insert error checking, I cannot do it now, but I will (probably tomorrow).
Thanks in advance.

Cornix

What exactly do you expect to be rendered? Looking at your shader the entire screen should be filled with a solid color.

CoDi

Quote from: ingframin on September 16, 2016, 17:26:40
I still did not insert error checking, I cannot do it now, but I will (probably tomorrow).

It's always a bad idea to take shortcuts with OpenGL. Error check everything!

public String fragment_shader = "version 330 core\n"
        + "out vec4 fColor;\n"
        + "void main(){\n"
        + "fColor = vec4(0.0,0.0,1.0,1.0);"
        + "}";


Copy'n paste typo right here. It's "#version", not "version". glUseProgram() should report this as an error, and glGetShaderInfoLog() gives more information (if you are lucky).

glVertexAttribPointer(vPosition, 1, GL_FLOAT, false, 0, vertices);


I assume you want to render a triangle, with x/y coordinates in your float[] array. This means 2 elements per component, not 1. Also, the last parameter should be 0 here, the data is already pushed with glBufferData().

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


Pro-tip: Never clear to black, always use glClearColor(1, 0, 1, 1) or some other eye-bleeding color. This way you'll see your triangles even if they are black. And believe me, some day they will turn up black.

That's all I see right now.