【问题标题】:Creating a texture on run time using data for OpenGL使用 OpenGL 的数据在运行时创建纹理
【发布时间】:2018-02-12 10:17:58
【问题描述】:

我目前在数据库中有一些热图数据。我成功地使用相同的数据(使用一些顶点着色)在平面上绘制了一张热图。示例:

openGL 的热图图像示例

名称:Capture.jpg 浏览量:0 大小:8.5 KB 编号:2667

现在,问题是我目前正在使用类似的东西:

glBegin(GL_QUADS);
glColor4ub(point1.color.red(), point1.color.green(), point1.color.blue(), transparency);
glVertex3d(point1.xCood - 750, point1.yCood - 750, 0);
glColor4ub(point2.color.red(), point2.color.green(), point2.color.blue(), transparency);
glVertex3d(point2.xCood - 750, point2.yCood - 750, 0);
glColor4ub(point3.color.red(), point3.color.green(), point3.color.blue(), transparency);
glVertex3d(point3.xCood - 750, point3.yCood - 750, 0);
glColor4ub(point4.color.red(), point4.color.green(), point4.color.blue(), transparency);
glVertex3d(point4.xCood - 750, point4.yCood - 750, 0);
glEnd();

这[至少在我的理论中]的作用是它在现有平面上创建了另一个层。这会导致单击下面平面的代码变得无用。现在不能过多更改现有代码,因为我无权编辑它。我发现如果我在旧平面上绘制纹理(而不是给平面着色),代码仍然可以工作。 示例(纹理图块仅定义所需的重复次数,在这种情况下值为 1):

glBegin(GL_QUADS);
glTexCoord2d(0.0, textureTile);
glVertex3d(gridRect.left(), gridRect.top(), 0.0);
glTexCoord2d(0.0, 0.0);
glVertex3d(gridRect.left(), gridRect.bottom(), 0.0);
glTexCoord2d(textureTile, 0.0);
glVertex3d(gridRect.right(), gridRect.bottom(), 0.0);
glTexCoord2d(textureTile, textureTile);
glVertex3d(gridRect.right(), gridRect.top(), 0.0);
glEnd();

也就是说,我只是成功地从我制作的图像中加载了纹理。由于假设图像是在运行时计算和绘制的,所以我尝试从数据中制作图像以作为纹理加载。 我使用 Qt API 功能来实现相同的功能。我未能重新创建相同的图像。我可能会被建议一种从拥有的数据创建纹理图像的方法。

谢谢

【问题讨论】:

  • 您最可能需要的是glTexSubImage2D()。创建纹理后,您可以随时使用您拥有的新数据对其进行更新,前提是其大小不超过基础纹理的大小,并继续使用现有的纹理对象。
  • @hidefromkgb 我的问题始于我无法在运行时创建纹理这一事实。这并不意味着我不能应用它。问题基本上是数据可以改变渲染旧图像无用需要创建一个新图像。

标签: c++ qt opengl


【解决方案1】:

抱歉回复晚了(以防其他人想要答案)。

我发现答案是使用 QOpenGLFramebufferObject。

最终代码如下所示:

glViewport(0, 0, VIEW_PORT_SIZE, VIEW_PORT_SIZE);
QOpenGLFramebufferObject fbObject(VIEW_PORT_SIZE, VIEW_PORT_SIZE);
fbObject.bind();
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

//Switch to Ortho Mode
glMatrixMode(GL_PROJECTION);      // Select Projection
glPushMatrix();                   // Push The Matrix
glLoadIdentity();                 // Reset The Matrix
glOrtho(left, right, bottom, top, -100, 100);  // Select Ortho Mode 
glMatrixMode(GL_MODELVIEW);        // Select Modelview Matrix
glPushMatrix();                    // Push The Matrix
glLoadIdentity();                  // Reset The Matrix

                                   //Initialize variables
SPointData point1, point2, point3, point4;
//Paint on buffer
//.................<some painting task>................
//Switch to perspective mode
glMatrixMode(GL_PROJECTION);      // Select Projection
glPopMatrix();                    // Pop The Matrix
glMatrixMode(GL_MODELVIEW);       // Select Modelview
glPopMatrix();                    // Pop The Matrix

fbObject.release();
return fbObject.toImage();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-31
    • 1970-01-01
    • 2011-11-22
    • 1970-01-01
    • 2014-03-12
    • 1970-01-01
    • 1970-01-01
    • 2020-11-27
    相关资源
    最近更新 更多