首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >尝试绘制三角形时LWJGL闪烁输出

尝试绘制三角形时LWJGL闪烁输出
EN

Stack Overflow用户
提问于 2018-08-23 03:08:55
回答 1查看 103关注 0票数 1

我一直在尝试使用LWJGL进入OpenGL,但我遇到了一个我无法找到解决方案的问题。当尝试使用下面的代码绘制三角形时,窗口会正确打开,并开始闪烁一个不一定是所需的三角形的形状(有时它会短暂出现,但通常在窗口的一个象限中会有矩形)。

我犹豫的部分原因是,根据我在网上阅读的各种帖子和文档,OpenGL在最近的记忆中发生了变化,使用了功能更少、更面向对象的方法(VBO和GLSL?)和GL4在一起。我的理解正确吗?学习这个较新的LWJGL OpenGL的首选资源是什么?

提前谢谢你!

代码语言:javascript
复制
import org.lwjgl.BufferUtils;
import org.lwjgl.glfw.*;
import org.lwjgl.opengl.*;
import org.lwjgl.system.*;

import java.nio.*;

import static org.lwjgl.glfw.Callbacks.*;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryStack.*;
import static org.lwjgl.system.MemoryUtil.*;

public class Main {
  private long windowID;

  private float[] tri = {0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f};

  public static void main(String[] args) {
    new Main().run();
  }

  public void run() { // Useful for making an instance class as opposed to a static main class?
    init();
    loop();

    glfwFreeCallbacks(windowID);
    glfwDestroyWindow(windowID);

    glfwTerminate();
    glfwSetErrorCallback(null).free();
  }

  public void init() { // Initializes all LWJGL components
    GLFWErrorCallback.createPrint(System.err).set(); // Create error callback route for GL

    if (!glfwInit()) { // Init GLFW
      throw new IllegalStateException("Failed to initialize GLFW!");
    } else {
      System.out.println("GLFW successfully initialized!");
    }

    windowID = glfwCreateWindow(640, 480, "Creating Window", NULL, NULL);

    if (windowID == NULL) { // Verify window creation
      throw new IllegalStateException("Failed to create window!");
    } else {
      System.out.println("Successfully created window!");
    }

    glfwDefaultWindowHints(); // Set window Proporties
    glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
    glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);

    glfwSetKeyCallback(windowID, (window, key, scancode, action, mods) -> { // Key callback for closing the window
      if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE)
        glfwSetWindowShouldClose(window, true);
    });

    try (MemoryStack stack = stackPush()) { // Center the window
      IntBuffer pWidth = stack.mallocInt(1);
      IntBuffer pHeight = stack.mallocInt(1);

      glfwGetWindowSize(windowID, pWidth, pHeight);

      GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());

      glfwSetWindowPos( // Center the window
          windowID, 
          (vidmode.width() - pWidth.get(0)) / 2,
          (vidmode.height() - pHeight.get(0)) / 2
          );
    }

    glfwMakeContextCurrent(windowID); // Make the window current
    glfwSwapInterval(0); // Sets the min num of pushed frames before buffers are swaped (Likely prevents horizontal tearing)
    glfwShowWindow(windowID); // Unhides the window
  }

  private void loop() {
    GL.createCapabilities();
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // The color to clear the buffers with

    while(!glfwWindowShouldClose(windowID)) { // If the window is allowed to live
      glClear(GL_COLOR_BUFFER_BIT); // The OR is nessesary for some reason

      FloatBuffer vBuff = BufferUtils.createFloatBuffer(6);
      vBuff.put(tri);
      glEnableClientState(GL_VERTEX_ARRAY);
      glVertexPointer(2, GL_FLOAT, 0, vBuff);
      glDrawArrays(GL_TRIANGLES, 0, 6);
      glDisableClientState(GL_VERTEX_ARRAY);

      glfwSwapBuffers(windowID);
      glfwPollEvents();
    } 
  }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-23 03:54:21

在缓冲区被装箱并填满后,您错过了vBuff.flip()

vBuff.put(tri)从当前位置(在本例中是缓冲区的起始位置)开始,将数据传输到缓冲区。缓冲区位置按数据大小递增。因此,新的缓冲区位置在新数据的末尾。

flip()将缓冲区的限制(长度)设置为当前位置,然后将位置设置为零。

此外,没有必要在循环中连续创建和填充缓冲区,在循环之前这样做一次就足够了:

代码语言:javascript
复制
FloatBuffer vBuff = BufferUtils.createFloatBuffer(6);
vBuff.put(tri);
vBuff.flip();

while(!glfwWindowShouldClose(windowID)) {
    glClear(GL_COLOR_BUFFER_BIT);

    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(2, GL_FLOAT, 0, vBuff);
    glDrawArrays(GL_TRIANGLES, 0, 6);
    glDisableClientState(GL_VERTEX_ARRAY);

    glfwSwapBuffers(windowID);
    glfwPollEvents();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51973589

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档