3

I'm running this lwjgl application:

Display.setDisplayMode(new DisplayMode(500, 500));
Display.create();

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-5, 5, -5, 5, -10, 5);
glMatrixMode(GL_MODELVIEW);

float x = 0;
while (!Display.isCloseRequested()) {
    Display.update();

    glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST);
    glColor3f(1, 0, 0);
    x += 0.01f;
    glRotatef(x, x, 3 * x, 0.5f * x);
    glBegin(GL_QUADS);
    drawCube();
    glEnd();
    glLoadIdentity();
}
Display.destroy();

Which basically draws a 1x1x1 cube in the window. Method drawCube() is this:

public void drawCube() {
    glColor3f(0.0f, 1.0f, 0.0f);          // Set The Color To Green
    glVertex3f(1.0f, 1.0f, 0f);           // Top Right Of The Quad (Top)
    glVertex3f(0f, 1.0f, 0f);             // Top Left Of The Quad (Top)
    glVertex3f(0f, 1.0f, 1.0f);           // Bottom Left Of The Quad (Top)
    glVertex3f(1.0f, 1.0f, 1.0f);         // Bottom Right Of The Quad (Top)

    glColor3f(1.0f, 0.5f, 0.0f);          // Set The Color To Orange
    glVertex3f(1.0f, 0f, 1.0f);           // Top Right Of The Quad (Bottom)
    glVertex3f(0f, 0f, 1.0f);             // Top Left Of The Quad (Bottom)
    glVertex3f(0f, 0f, 0f);               // Bottom Left Of The Quad (Bottom)
    glVertex3f(1.0f, 0f, 0f);             // Bottom Right Of The Quad (Bottom)

    glColor3f(1.0f, 0.0f, 0.0f);          // Set The Color To Red
    glVertex3f(1.0f, 1.0f, 1.0f);         // Top Right Of The Quad (Front)
    glVertex3f(0f, 1.0f, 1.0f);           // Top Left Of The Quad (Front)
    glVertex3f(0f, 0f, 1.0f);             // Bottom Left Of The Quad (Front)
    glVertex3f(1.0f, 0f, 1.0f);           // Bottom Right Of The Quad (Front)

    glColor3f(1.0f, 1.0f, 0.0f);          // Set The Color To Yellow
    glVertex3f(1.0f, 0f, 0f);             // Bottom Left Of The Quad (Back)
    glVertex3f(0f, 0f, 0f);               // Bottom Right Of The Quad (Back)
    glVertex3f(0f, 1.0f, 0f);             // Top Right Of The Quad (Back)
    glVertex3f(1.0f, 1.0f, 0f);           // Top Left Of The Quad (Back)

    glColor3f(0.0f, 0.0f, 1.0f);          // Set The Color To Blue
    glVertex3f(0f, 1.0f, 1.0f);           // Top Right Of The Quad (Left)
    glVertex3f(0f, 1.0f, 0f);             // Top Left Of The Quad (Left)
    glVertex3f(0f, 0f, 0f);               // Bottom Left Of The Quad (Left)
    glVertex3f(0f, 0f, 1.0f);             // Bottom Right Of The Quad (Left)

    glColor3f(1.0f, 0.0f, 1.0f);          // Set The Color To Violet
    glVertex3f(1.0f, 1.0f, 0f);           // Top Right Of The Quad (Right)
    glVertex3f(1.0f, 1.0f, 1.0f);         // Top Left Of The Quad (Right)
    glVertex3f(1.0f, 0f, 1.0f);           // Bottom Left Of The Quad (Right)
    glVertex3f(1.0f, 0f, 0f);             // Bottom Right Of The Quad (Right)
}

It outputs this:

Horrible picture

To me lines are pretty horrible, Does lwjgl have anti aliasing? If yes, how can i enable it?

4

2 回答 2

16

For Anti-Aliasing, you can use multisampling. To enable it, add a PixelFormat parameter to Display.create();. Here's an example:

Display.create(new PixelFormat(/*Alpha Bits*/8, /*Depth bits*/ 8, /*Stencil bits*/ 0, /*samples*/8)); I'm not sure what the first 3 parameters do - these are values I found here. I've also seen people use Display.create(new PixelFormat(8,0,0,8)).

NOTE: If you set the AA too high (or other things an incorrect value) it will throw an LWJGLException.

Hope this helps.

PS apologies for the late answer.

于 2014-01-13T20:54:24.290 回答
4

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.

Test Image

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);
    }
}
于 2013-09-10T09:03:10.663 回答