使用lwjgl启用消除锯齿功能 [英] Enable anti-aliasing with lwjgl

查看:192
本文介绍了使用lwjgl启用消除锯齿功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行这个lwjgl应用程序:

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();

这基本上是在窗口中绘制1x1x1多维数据集。方法 drawCube()是这样的:

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)
}

它输出:

对我来说线条非常可怕,lwjgl有抗锯齿吗?如果是,我该如何启用?

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

推荐答案

评论



抗锯齿可以通过在第一次创建LWJGL窗口时在帧缓冲对象中或通过默认帧缓冲区创建多样本帧缓冲来实现。

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.

如果您正在学习 LWJGL OpenGL 然后学习如何使用 VBOs ,因为 glBegin glVertex 等从OpenGL的核心配置文件中删除。

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存储两个三角形的顶点和纹理坐标并渲染它的一个小例子!

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.

这是您创建实际的顶点和纹理坐标缓冲区并将它们存储到GPU上的代码。

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);



渲染VBO



然后当你想要渲染VBO,你需要做以下几点。

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();



删除VBO



然后当你完成了VBO并且您不再需要它,您可以通过执行以下操作将其删除。

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 - 静态进口



LWJGL/OpenGL - Static Imports

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;



信息



您只需要创建一次VBO(为每个帧创建一个非常糟糕),所以如果你使用VBO来存储你的地形。然后,如果发生了一些变化,那么只有在那一点你想要更新VBO,否则就保持原样。如果你将VBO用于地形并且地形真的很大,那么用自己的VBO将地形分成不同的地形块。

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.

如果您有Wavefront OBJ模型,并且想要多次渲染它,最好的方法是将整个模型加载到一个VBO。然后你会做一些Instancing来在多个位置多次渲染它等等。

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.

  • Instancing Tutorial 1
  • Instancing Tutorial 2

VBO的一个小例子,用于存储三角形的顶点和颜色并渲染它!

A little example of a VBO storing Vertices and Colors for a Triangle and rendering it!

这是您创建实际的顶点和颜色缓冲区并将它们绑定到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);



渲染VBO。



这是你需要调用的代码来渲染VBO。

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!

好的,这里有一个包含顶点和颜色的VBO的完整示例。该示例仅为100% LWJGL

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);
    }
}

这篇关于使用lwjgl启用消除锯齿功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆