[英]Anti-aliasing in LWJGL 3
I am trying to draw an anti-aliased square in LWJGL 3. I am only drawing 2D sprites and I'm not drawing textures. 我正在尝试在LWJGL 3中绘制一个抗锯齿的正方形。我仅绘制2D精灵,而没有绘制纹理。 This answer to a similar question reveals that anti-aliasing in LWJGL must be achieved with post-processing, but does not actually give an example or any resources on how to do this.
这个类似问题的答案表明,LWJGL中的抗锯齿必须通过后处理来实现,但实际上并没有给出示例或有关如何执行此操作的任何资源。 Below is an example of some code.
以下是一些代码示例。 This is what is rendered on screen from the code.
这就是代码在屏幕上呈现的内容。 Please tell me how to modify this code to make anti-aliasing possible.
请告诉我如何修改此代码以使抗锯齿成为可能。
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import org.lwjgl.*;
import org.lwjgl.glfw.*;
import org.lwjgl.opengl.*;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryUtil.*;
import static org.lwjgl.opengl.GL15.*;
public class LWJGLTest {
private GLFWErrorCallback errorCallback;
private GLFWKeyCallback keyCallback;
private long window;
public void run() {
try {
init();
loop();
glfwDestroyWindow(window);
keyCallback.release();
} finally {
glfwTerminate();
errorCallback.release();
}
}
int WIDTH = 300;
int HEIGHT = 300;
private void init() {
glfwSetErrorCallback(errorCallback = GLFWErrorCallback.createPrint(System.err));
if ( glfwInit() != GLFW_TRUE )
throw new IllegalStateException("Unable to initialize GLFW");
glfwDefaultWindowHints();
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
window = glfwCreateWindow(WIDTH, HEIGHT, "Hello World!", NULL, NULL);
if ( window == NULL )
throw new RuntimeException("Failed to create the GLFW window");
glfwSetKeyCallback(window, keyCallback = new GLFWKeyCallback() {
@Override
public void invoke(long window, int key, int scancode, int action, int mods) {
if ( key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE )
glfwSetWindowShouldClose(window, GLFW_TRUE);
}
});
GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
glfwSetWindowPos(
window,
(vidmode.width() - WIDTH) / 2,
(vidmode.height() - HEIGHT) / 2
);
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
glfwShowWindow(window);
}
private void loop() {
GL.createCapabilities();
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
while ( glfwWindowShouldClose(window) == GLFW_FALSE ) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
draw();
glfwSwapBuffers(window);
glfwPollEvents();
}
}
private static void draw(){
int vertices = 4;
int colorSize = 3;
int vertexSize = 3;
FloatBuffer cBuffer = BufferUtils.createFloatBuffer(vertices * colorSize);
cBuffer.put(0).put(0).put(0);
cBuffer.put(0).put(0).put(0);
cBuffer.put(0).put(0).put(0);
cBuffer.put(0).put(0).put(0);
cBuffer.flip();
FloatBuffer vBuffer = BufferUtils.createFloatBuffer(vertices * vertexSize);
vBuffer.put(0.4056f).put(0.5792f).put(0.0f);
vBuffer.put(-0.5792f).put(0.4056f).put(0.0f);
vBuffer.put(-0.4056f).put(-0.5792f).put(0.0f);
vBuffer.put(0.5792f).put(-0.4056f).put(0.0f);
vBuffer.flip();
IntBuffer ib = BufferUtils.createIntBuffer(2);
glGenBuffers(ib);
int vHandle = ib.get(0);
int cHandle = ib.get(1);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, vHandle);
glBufferData(GL_ARRAY_BUFFER, vBuffer, GL_STATIC_DRAW);
glVertexPointer(vertexSize, GL_FLOAT, vertexSize*4, 0);
glBindBuffer(GL_ARRAY_BUFFER, cHandle);
glBufferData(GL_ARRAY_BUFFER, cBuffer, GL_STATIC_DRAW);
glColorPointer(colorSize, GL_FLOAT, colorSize*4, 0);
glDrawArrays(GL_POLYGON, 0, vertices);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
ib.put(0, vHandle);
ib.put(1, cHandle);
glDeleteBuffers(ib);
}
public static void main(String[] args){
new LWJGLTest().run();
}
}
You can enable multisampling (MSAA) in GLFW which should give you the effect you want. 您可以在GLFW中启用多重采样(MSAA) ,这将为您带来所需的效果。 Look up using GL_SAMPLES on it.
使用GL_SAMPLES查找。
eg 例如
glfwWindowHint(GLFW_STENCIL_BITS, 4);
glfwWindowHint(GLFW_SAMPLES, 4);
before you create the window. 在创建窗口之前。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.