gpt4 book ai didi

android - opengl es,glActiveTexture时出错

转载 作者:行者123 更新时间:2023-11-29 20:11:26 24 4
gpt4 key购买 nike

我正在Android 系统中开发一个基于Qt 5.4 的应用程序。我正在生成多个 OES 纹理并在 Qt FBO TEXTURE_2D 中渲染通过着色器。每个 OES 纹理都有自己的 eglcontext,每个人都有相同的 sharedContext。 OES 纹理是一张一张使用的,我的意思是,我不想同时渲染多个,但我需要创建多个,因为我必须能够在同一时间渲染多个 future 。

这是我必须更新时使用的代码:

m_program->bind();
glActiveTexture(GL_TEXTURE0 + textureId);
glBindTexture(GL_TEXTURE_EXTERNAL_OES, textureId);
/* my code to fill shader attr and glDraw*() */
glBindTexture(GL_TEXTURE_EXTERNAL_OES, 0);
m_program->release();

问题是,出于某种原因,我只能渲染创建的第一个 OES 纹理,因为我得到了 W/Adreno-ES20(28468): <core_glActiveTexture:348>: GL_INVALID_ENUM。当尝试 glActiveTexture 第二个时。因此,目标纹理被填充为黑色。

我已经检查了所有这些并且是正确的:

  • 纹理编号
  • 调用 eglCreateContext 时的 sharedContext。
  • 渲染时的当前 eglContext,我的意思是,调用上面的时候代码。
  • 第二个 OES 纹理已填充
  • GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS 16
  • GL_MAX_TEXTURE_IMAGE_UNITS 16
  • GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS 32

我有点迷茫,我不确定这是我的代码中的限制还是错误。

有什么想法吗?

编辑:在其他设备上尝试,应用程序工作正常,所以我认为问题与设备限制有关..

最佳答案

生成纹理时,您无法保证 ID 将从零开始或递增 1。因此您可能不会期望值会是 [0, 1, 2,...] 并且不能将 Activity 纹理用作 GL_TEXTURE0 + textureID。您需要创建自己的自动增量系统,最好通过创建某种纹理对象来生成纹理、分配两个 ID 等等。

要增加内部 ID,您需要做的就是使用一个静态整数值,该值会随着您生成的每个纹理而增加。这是最基本的系统,您将在某个时间只加载一些纹理,然后假设它们都不应该被释放和重用,然后重用它们。其他系统需要包括对已释放纹理的某种跟踪,并能够重用空槽。

因此对于基础知识,您将拥有一个对象/类,例如:

static GLint currentActiveTextureID = GL_TEXTURE0;

class ActiveTextureObject {
GLuint textureID;
GLenum activeTextureID;

void generateNew() {
activeTextureID = currentActiveTextureID++;
useActive();
glGenTextures(1, &activeTextureID);
}

void useActive() {
glActiveTexture(activeTextureID);
}
void bind() {
glBindTexture(GL_TEXTURE_2D, textureID);
}
void bindAndUseActive() {
useActive();
bind();
}
};

这现在包含您在代码段中发布的所有功能。如果您愿意,也可以在构造函数中调用方法 generateNew。如您所见,该对象将纹理和 Activity 纹理绑定(bind)在一起,因此您可以简单地调用 bindAndUseActive 来完成这两项操作。

对于更复杂的系统,您可以使用一个元素数组来表示 Activity 纹理的插槽。然后您可以遍历数组以找到一个空槽。

class ActiveTexturePool {

static const GLint maximumActiveTextures = 16; // number of maximum units
GLint currentActiveTextureID[maximumActiveTextures]; // have the container

ActiveTexturePool() { // a constructor is needed to reset the data
memset(currentActiveTextureID, 0, sizeof(currentActiveTextureID)); // sets all to zero
}

class ActiveTextureObject {
public:
GLuint textureID;
GLenum activeTextureID;

void generateNew() {
useActive();
glGenTextures(1, &activeTextureID);
}

void useActive() {
glActiveTexture(activeTextureID);
}
void bind() {
glBindTexture(GL_TEXTURE_2D, textureID);
}
void bindAndUseActive() {
useActive();
bind();
}
};

ActiveTextureObject getNewTextureObject() {
ActiveTextureObject toReturn;

for(GLint i=0; i<maximumActiveTextures; i++) {
if(currentActiveTextureID[i] == 0) {
GLenum activeTexture = GL_TEXTURE0 + i;

currentActiveTextureID[i] = activeTexture;
toReturn.activeTextureID = activeTexture;

return toReturn;;
}
}
return NULL; // the pool is full, you may not create another active texture!
}

void recycleTexture(ActiveTextureObject texture) { // remove from the pool
for(GLint i=0; i<maximumActiveTextures; i++) {
if(currentActiveTextureID[i] == texture.activeTextureID) {
currentActiveTextureID[i] = 0;
texture.activeTextureID = 0;
// you may also delete the data here but that should most likely be handled by the ActiveTextureObject
break;
}
}
}
};

现在在这种情况下,您宁愿在某处创建一个池来处理纹理。您可能很容易拥有一个或多个可用于多个上下文的池。最后,最好创建一个包含主要上下文并具有纹理池的类。然后上下文对象还可以为您正在生成的每个纹理创建共享上下文,并且还负责删除、回收纹理和上下文。

关于android - opengl es,glActiveTexture时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34798281/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com