Basic Shaders and Vertex Buffers [Scala]

Started by vladley, July 11, 2012, 21:33:44

Previous topic - Next topic

vladley

Hello. I'm trying to learn LWJGL by translating the WebGL101 tutorial, written in javascript, to Scala using LWJGL. I am able to get the first 4 basics working, here's my good implementation of Basics #4, Timers.

http://pastebin.com/PEZjU8vZ

I might be making a bit of a leap here, but I'd like to start using shaders and vertex buffers. I've written the following, and am expecting a green triangle, but only get a black screen.

http://pastebin.com/f5ukmWxw

I don't think I'm trying to do anything too difficult, and the code I'm writing is more or less in the style of Java, even though it is Scala code, so it shouldn't be too foreign. Anyone willing to take a quick look too see if I'm missing something obvious? I do like the library so far!

vladley

Alternatively, is there a complete code example the simply does the following:

Specify 3 2d vertices. Bind them to a vertex buffer and buffer the data.
Create simple shaders that add the z component and always draw green.
While the display isn't closed, draw a triangle.

That's what I've attempted to do, but I'm not sure what is not working.

edit: it could even be written in Java.

moci

Hi there, a bunch of new tutorials have been added using OpenGL 3.2 (don't know what version you are using). One of them is a complete example of how to define vertices with color information and upload them in a VBO on the graphics card. The shaders that are used need 4 components for XYZW but it's easy enough to define them so it only needs 2.

In general your vertices setup code could look like this:
// Vertex data (interleaved): X, Y, R, G, B
float[] vertexOne = new float[] {-0.5f, -0.5f, 0f, 1f, 0f};
float[] vertexTwo = new float[] {0f, 0.5f, 0, 1f, 0f};
float[] vertexThree = new float[] {0.5f, -0.5f, 0f, 1f, 0f};

// Create a FloatBuffer
FloatBuffer verticesFloats = ...
verticesFloats.put(vertexOne);
...
verticesFloats.flip(); // Always flip the buffer before sending it to OpenGL/AL/...

// Put the bytes in a VBO
int vboID = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboID)
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, verticesFloats, GL15.GL_STATIC_DRAW);

// You will need to use attribute lists for each kind of kind of data (position and color)
GL20.glVertexAttribPointer(0, 2, GL11.GL_FLOAT, false, 0, 0);
GL20.glVertexAttribPointer(1, 3, GL11.GL_FLOAT, false, 2 * 4, 0);

// Bind the attribute lists with the variables in your shader
GL20.glBindAttribLocation(pId, 0, "in_Position");
GL20.glBindAttribLocation(pId, 1, "in_Color");


And the shader code like this:
// Vertex shader
in vec2 in_Position;
in vec3 in_Color;

out vec4 pass_Color;

void main(void) {
	gl_Position = vec4(in_Position, 0, 1);
	pass_Color = vec4(in_Color, 1);
}

// Fragment shader
in vec4 pass_Color;

out vec4 out_Color;

void main(void) {
	out_Color = pass_Color;
}


This is untested code and not complete (...) but it shows you generally how it could be done. Hope it helps.