Comments
Antialiasing can be achieved by creating a multisample framebuffer, either in a framebuffer object or through the default framebuffer when the LWJGL window is created for the first time.
If you are learning LWJGL
and OpenGL
then learn how to use VBOs
, because glBegin
, glVertex
, etc. are removed from the core profile of OpenGL.
VBO Example
Here is a little example of a VBO storing Vertices and Texture Coordinates for two triangles and rendering it!
I assume that you know how to load and bind textures already.
Creating the VBO
This is the code where you create the actual Vertex and Texture Coordinate Buffer and store them onto the GPU.
int vertices = 6;
int vertex_size = 3; // X, Y, Z,
int texture_size = 2; // U, V,
FloatBuffer vertex_data = BufferUtils.createFloatBuffer(vertices * vertex_size);
vertex_data.put(new float[] { -1f, 1f, 0f, }); // Vertex
vertex_data.put(new float[] { 1f, 1f, 0f, }); // Vertex
vertex_data.put(new float[] { -1f, -1f, 0f, }); // Vertex
vertex_data.put(new float[] { 1f, -1f, 0f, }); // Vertex
vertex_data.put(new float[] { -1f, -1f, 0f, }); // Vertex
vertex_data.put(new float[] { 1f, 1f, 0f, }); // Vertex
FloatBuffer texture_data = BufferUtils.createFloatBuffer(vertices * texture_size);
texture_data.put(new float[] { 0f, 1f, }); // Texture Coordinate
texture_data.put(new float[] { 1f, 1f, }); // Texture Coordinate
texture_data.put(new float[] { 0f, 0f, }); // Texture Coordinate
texture_data.put(new float[] { 1f, 0f, }); // Texture Coordinate
texture_data.put(new float[] { 0f, 0f, }); // Texture Coordinate
texture_data.put(new float[] { 1f, 1f, }); // Texture Coordinate
vertex_data.flip();
texture_data.flip();
int vbo_vertex_handle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbo_vertex_handle);
glBufferData(GL_ARRAY_BUFFER, vertex_data, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
int vbo_texture_handle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbo_texture_handle);
glBufferData(GL_ARRAY_BUFFER, texture_data, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
Rendering the VBO
Then when you want to render the VBO, you need to do the following.
texture.bind();
glBindBuffer(GL_ARRAY_BUFFER, vbo_vertex_handle);
glVertexPointer(vertex_size, GL_FLOAT, 0, 0l);
glBindBuffer(GL_ARRAY_BUFFER, vbo_texture_handle);
glTexCoordPointer(texture_size, GL_FLOAT, 0, 0l);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glDrawArrays(GL_TRIANGLES, 0, vertices); // The vertices is of course the max vertices count, in this case 6
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
texture.unbind();
Deleting the VBO
Then when you're done with the VBO and you don't need it anymore, you can delete it by doing the following.
glDeleteBuffers(vbo_vertex_handle);
glDeleteBuffers(vbo_texture_handle);
LWJGL/OpenGL - Static Imports
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
Info
You only have to create a VBO once (It is really bad to create one for each frame), so if you used VBO to store your terrain. Then if some changes happens then only at that point you want to update the VBO, else just keep it as it is. If you use the VBO for terrain and the terrain is really huge, then split the terrain into different chunks of terrain with it's own VBO.
Extra
If you have an Wavefront OBJ Model, and you want to render it multiple times, the best way to do that, would be to load the whole model to one VBO. Then you would do some Instancing to render it multiple times in multiple position, etc.
Update
A little example of a VBO storing Vertices and Colors for a Triangle and rendering it!
Creating the VBO.
This is the code where you create the actual Vertex and Color Buffer and bind them to the VBO.
int vertices = 3;
int vertex_size = 3; // X, Y, Z,
int color_size = 3; // R, G, B,
FloatBuffer vertex_data = BufferUtils.createFloatBuffer(vertices * vertex_size);
vertex_data.put(new float[] { -1f, -1f, 0f, });
vertex_data.put(new float[] { 1f, -1f, 0f, });
vertex_data.put(new float[] { 1f, 1f, 0f, });
vertex_data.flip();
FloatBuffer color_data = BufferUtils.createFloatBuffer(vertices * color_size);
color_data.put(new float[] { 1f, 0f, 0f, });
color_data.put(new float[] { 0f, 1f, 0f, });
color_data.put(new float[] { 0f, 0f, 1f, });
color_data.flip();
int vbo_vertex_handle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbo_vertex_handle);
glBufferData(GL_ARRAY_BUFFER, vertex_data, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
int vbo_color_handle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbo_color_handle);
glBufferData(GL_ARRAY_BUFFER, color_data, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
Rendering the VBO.
This is the code you need to call, to render the VBO.
glBindBuffer(GL_ARRAY_BUFFER, vbo_vertex_handle);
glVertexPointer(vertex_size, GL_FLOAT, 0, 0l);
glBindBuffer(GL_ARRAY_BUFFER, vbo_color_handle);
glColorPointer(color_size, GL_FLOAT, 0, 0l);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glDrawArrays(GL_TRIANGLES, 0, vertices);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
The imports and the code for disposing/deleting the buffers are the same as the previous example!
Update 2 - Complete Example
Okay, so here is a complete example of a VBO containing Vertices and Colors. The example is 100% LWJGL
only!
When I run the following code this is the desired result I get.
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
import java.nio.FloatBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
public class VBOTest
{
public final static void main(String[] args)
{
int width = 1280;
int height = 720;
try
{
Display.setTitle("VBO Test");
Display.setDisplayMode(new DisplayMode(width, height));
Display.create();
}
catch (Exception ex)
{
ex.printStackTrace();
System.exit(0);
}
/*
* Initialize OpenGL States
*/
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0f, width, height, 0f, -1f, 1f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
/*
* Creating the Vertex & Color VBO
*/
final int VERTEX_SIZE = 3; // X, Y, Z,
final int COLOR_SIZE = 4; // R, G, B, A,
int vertices = 6;
int vbo_vertex_handle;
int vbo_color_handle;
FloatBuffer vertex_data = BufferUtils.createFloatBuffer(vertices * VERTEX_SIZE);
float half_width = 200f;
float half_height = 200f;
vertex_data.put(new float[] { -half_width, -half_height, 0f, });
vertex_data.put(new float[] { -half_width, half_height, 0f, });
vertex_data.put(new float[] { half_width, -half_height, 0f, });
vertex_data.put(new float[] { half_width, half_height, 0f, });
vertex_data.put(new float[] { half_width, -half_height, 0f, });
vertex_data.put(new float[] { -half_width, half_height, 0f, });
vertex_data.flip();
FloatBuffer color_data = BufferUtils.createFloatBuffer(vertices * COLOR_SIZE);
color_data.put(new float[] { 1f, 0f, 0f, 1f, });
color_data.put(new float[] { 1f, 0f, 1f, 1f, });
color_data.put(new float[] { 1f, 1f, 0f, 1f, });
color_data.put(new float[] { 0f, 1f, 0f, 1f, });
color_data.put(new float[] { 1f, 1f, 0f, 1f, });
color_data.put(new float[] { 1f, 0f, 1f, 1f, });
color_data.flip();
vbo_vertex_handle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbo_vertex_handle);
glBufferData(GL_ARRAY_BUFFER, vertex_data, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
vbo_color_handle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbo_color_handle);
glBufferData(GL_ARRAY_BUFFER, color_data, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
/*
* Main Rendering Loop
*/
boolean running = true;
while (running)
{
running = (!Display.isCloseRequested() && !Keyboard.isKeyDown(Keyboard.KEY_ESCAPE));
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glPushMatrix();
{
glTranslatef(width / 2f, height / 2f, 0f);
glBindBuffer(GL_ARRAY_BUFFER, vbo_vertex_handle);
glVertexPointer(VERTEX_SIZE, GL_FLOAT, 0, 0l);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, vbo_color_handle);
glColorPointer(COLOR_SIZE, GL_FLOAT, 0, 0l);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glDrawArrays(GL_TRIANGLES, 0, vertices);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
}
glPopMatrix();
glFlush();
Display.sync(60);
Display.update();
}
/*
* Dispose Elements
*/
glDeleteBuffers(vbo_vertex_handle);
glDeleteBuffers(vbo_color_handle);
Display.destroy();
System.exit(0);
}
}