如何在 OpenGL 的立即模式下永久绘制东西?

克里斯·马修·乔治

我在本质上创建路径跟踪器时遇到了一个小问题。

在我的项目中,我有一个对象,它通过在 while 循环中完成的更新函数不断地有机地移动。我使用立即模式并将玩家表示为一个正方形,我想让它每次更新对象都在其当前位置绘制,而且还绘制它的先前位置,因此向路径蚀刻点对象正在运行。我很确定我们可以通过正常绘制位置来做到这一点,但在 while 循环中的这个实例之后不清除所有内容,但我不知道如何做到这一点。

编辑:对于那些想要代码的人,请务必理解该代码尤其不符合问题,并且我进行了大量概括(例如粒子被称为对象),因此一般问题的要点是可以理解的:

#include "PerlinNoise.hpp"
#include "Particle.hpp"

#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <cmath>
#include <vector>

using namespace siv;

float map(float oValue, float oMin, float oMax, float nMin, float nMax)
{
  float oRange = (oMax - oMin);
  float nRange = (nMax - nMin);
  return(((oValue - oMin) * nRange)/oRange) + nMin;
}

void drawRectangle(float x, float y, float xr, float yr, float R, float G, float B)
{
  glBegin(GL_QUADS);
  glColor3f(R,G,B);
  glVertex2f(x,y);
  glVertex2f(x+xr,y);
  glVertex2f(x+xr,y+yr);
  glVertex2f(x,y+yr);
  glEnd();
}

void drawLine(float x, float y, float xr, float yr, float rotation)
{
  float radius = sqrt(xr*xr + yr*yr);
  float a0 = asin(yr/radius);
  float tangle = a0+rotation;
  //std::cout<<tangle*180/M_PI<<std::endl;
  glBegin(GL_LINES);
  glColor3f(.1,.1,.1);
  glVertex2f(x,y);
  glVertex2f(x + sin(tangle)*radius,y + cos(tangle)*radius);
  glEnd();
}


int main()
{
  float inc = 0.1;
  int scl   = 20;
  int cols,rows;

  Particle particles[100000];

  //V2D flowfield[cols*rows];

  GLFWwindow* window;
  if (!glfwInit())
    return 1;
  int width  = 800;
  int height = 800;
  window = glfwCreateWindow(width, height, "Window", NULL, NULL);
  cols = floor(width/scl);
  rows = floor(height/scl);
  V2D flowfield[cols*rows];
  float zoff = 0;
  if (!window) {
    glfwTerminate();
    return 1;
  }
  glfwMakeContextCurrent(window);
  if(glewInit()!=GLEW_OK)
    std::cout<<"Error"<<std::endl;
  glEnable(GL_DEPTH_TEST);
  glMatrixMode(GL_PROJECTION);
  glfwGetFramebufferSize(window, &width, &height);
  glOrtho(0, width*(width/height), height, 0, -2, 2);
  PerlinNoise png = PerlinNoise(1);
  while(!glfwWindowShouldClose(window)) {
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glClearColor(0.11, 0.14, 0.17, 1);
    float yoff = 0;
    for(int y = 0; y < rows; y++)
    {
      float xoff = 0;
      for(int x = 0; x < cols; x++)
      {
        double noise = map(png.noise((double)xoff, (double)yoff, (double)zoff),-1,1,0,1);
        double angle = noise * 8 *M_PI;
        //std::cout<<angle/(2*M_PI)<<std::endl;
        int index = x + y * cols;
        V2D v = V2D(cos(angle), sin(angle));
        v.normalize();
        v = V2D(v.x*5,v.y*5);
        flowfield[index] = v;
        //drawLine(x*scl, y*scl, scl, 0, atan2(v.x, v.y));
        //drawRectangle(x*scl,y*scl,scl,scl,noise,noise,noise);

        xoff += inc;
      }
      yoff += inc;
      zoff += 0.0001;
    }
    for(int i = 0; i < 100000; i++)
    {
      particles[i].follow(flowfield);
      particles[i].update();
      particles[i].show();
    }
    glfwSwapBuffers(window);
    glfwPollEvents();
  }
  glfwTerminate();
}
数据狼

当直接绘制到窗口时(无论是否双缓冲都没有区别),您不能假设其内容在绘制之间是持久的。哎呀,严格来说,在事情完成之前,内容可能会在抽奖过程中损坏;当然,在实践中这不太可能发生,鉴于现代合成图形系统,它实际上已被消除。

您的应用程序为绘制到中间帧缓冲区对象而尖叫。无论发生什么,FBO 都保证保留其内容;您还可以随时向 FBO 的后备缓冲区添加更多绘图。

官方 OpenGL wiki 在https://www.khronos.org/opengl/wiki/Framebuffer_Object描述了 FBO

也是很久以前,我写了一个简单的代码示例(使用了很多过时的遗留 OpenGL);绘图是遗留的,但 FBO 部分是在今天完成的,就像 10 年前一样:https : //github.com/datenwolf/codesamples/blob/master/samples/OpenGL/minimalfbo/minimalfbo.c(我使用渲染来实现它纹理;渲染到渲染缓冲区和缓冲区 blit 到主帧缓冲区也适用于您)。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在OpenGL中绘制视锥

来自分类Dev

如何在OpenGL中绘制平截头体

来自分类Dev

如何在OpenGL中加快在纹理上的绘制?(3.3 + / 4.1)

来自分类Dev

如何在OpenGL中绘制平截头体

来自分类Dev

如何在OpenGL / C ++中绘制多维数据集?

来自分类Dev

android OpenGl如何绘制矩形

来自分类Dev

android OpenGl如何绘制矩形

来自分类Dev

OpenGL 混合:删除正在绘制的像素(立即模式)

来自分类Dev

如何在OpenGL中的单个绘制调用上绘制多个顶点数组?

来自分类Dev

如何通过OpenGL绘制笛卡尔平面?

来自分类Dev

如何使用MVP在OpenGL中绘制椭圆

来自分类Dev

如何使用OpenGL绘制此对象

来自分类Dev

如何删除OpenGL绘制的C ++对象?

来自分类Dev

如何通过OpenGL绘制笛卡尔平面?

来自分类Dev

如何通过GLM / OpenGL绘制多个对象?

来自分类Dev

如何使用 GPU 绘制 OpenGL 像素

来自分类Dev

如何在使用opengl的Xlib窗口中绘制2D文本?

来自分类Dev

如何在现代openGL中用3D点绘制多边形?

来自分类Dev

如何在OpenGL中绘制一个立方体?

来自分类Dev

如何在OpenGL中创建网格并用线绘制它

来自分类Dev

如何在openGL c ++中的同一窗口中绘制两个对象?

来自分类Dev

如何在3D OpenGL场景上绘制SDL 2D矩形?

来自分类Dev

如何在OpenGL着色器中从纹理图块贴图绘制图块?

来自分类Dev

如何在没有glBegin的情况下在OpenGL中绘制线

来自分类Dev

如何在 OpenGL C++ 中绘制多个三角形?

来自分类Dev

如何在(旧)opengl(2.4)中按程序绘制(超/n-)立方体

来自分类Dev

如何在Windows 10(WSL)下的Ubuntu上对OpenGL进行故障排除

来自分类Dev

如何在2D模式下绘制网格?

来自分类Dev

为什么OpenGL立即模式比核心快?

Related 相关文章

热门标签

归档