OpenGL ES在Android NDK中绘制问题。

时间:2022-09-10 19:05:36

Due to performance issues, I have had to transfer my android opengl code from Java to C. I believe I transfered all of the OpenGL code, but I now have many errors with the section of my code that draws a bitmap as a texture to the screen.

由于性能问题,我不得不将我的android opengl代码从Java转移到c。我相信我已经将opengl代码全部转移了,但是现在我的代码中有很多错误,这段代码将位图作为纹理绘制到屏幕上。

When I run the project in an emulator, the current code does not display anything and appears to have a memory leak because it slowly takes up all the memory and forces other apps to close.

当我在模拟器中运行这个项目时,当前的代码不会显示任何东西,而且似乎有内存泄漏,因为它会慢慢占用所有内存并迫使其他应用程序关闭。

Here is the code that controls this part:

下面是控制这部分的代码:

In the header file:

在头文件:

extern unsigned char colors[1024*512*3];   // 3 bytes per pixel

In the c file:

c文件中:

void appRender(int width, int height, unsigned char colors)
{
    unsigned int textureID;
    float vertices[] = 
    {
        0.0f, 0.0f,
        512.0f, 0.0f,
        0.0f, 1024.0f,
        512.0f, 1024.0f
    };

    float texture[] =
    {
        0.0f, 0.0f,
       1.0f, 0.0f,
       0.0f, 1.0f,
       1.0f, 1.0f
    };

    unsigned char indices[] = 
    {
       0, 1, 3,
       0, 3, 2
    };

    UpdateView();


    //onDraw 
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glOrthof(0.0f, 320.0f, 430.0f, 0.0f, 1.0f, -1.0f);

    //texture stuff
    glGenTextures(1,&textureID);
    glBindTexture(GL_TEXTURE_2D, textureID);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    //Different possible texture parameters, e.g
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, 512, 1024, 0, GL_RGB ,GL_UNSIGNED_BYTE, colors);

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

    glVertexPointer(2, GL_FLOAT, 0, vertices);
    glTexCoordPointer(2, GL_FLOAT, 0, texture);

    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices);

    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}

Any help would be greatly appreciated.

非常感谢您的帮助。

1 个解决方案

#1


3  

There does not appear to be a call to glEnable(GL_TEXTURE_2D) to enable texturing in your example.

在您的示例中,似乎没有调用glEnable(GL_TEXTURE_2D)来启用纹理。

You can call glGetError() to find out if what you are doing is incorrect. This has helped me debug problems in the past.

您可以调用glge恐怖()来查明您所做的是否错误。这帮助我调试了过去的问题。

Also you appear to be creating your texture in your appRender() method. If this is called for every frame you draw then it could be the cause of your memory leak as you are repeatedly recreating the same texture.

而且,你似乎在你的学徒()方法中创造你的纹理。如果这是对你所绘制的每一个帧的调用,那么它可能是你的内存泄漏的原因,因为你不断地重新创建同样的纹理。

Typically you should only generate and define the texture once during initialization.

通常,您应该只在初始化期间生成和定义纹理。

So the following should be done once before rendering.

因此,在呈现之前应该做一次。

glGenTextures(1,&textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

//Different possible texture parameters, e.g
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, 512, 1024, 0, GL_RGB ,GL_UNSIGNED_BYTE, colors);

Then when drawing you can do

当你画画的时候。

glEnable(GL_TEXTURE_2D)
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glBindTexture(GL_TEXTURE_2D, textureID);

glVertexPointer(2, GL_FLOAT, 0, vertices);
glTexCoordPointer(2, GL_FLOAT, 0, texture);

glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices);

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisable(GL_TEXTURE_2D)

#1


3  

There does not appear to be a call to glEnable(GL_TEXTURE_2D) to enable texturing in your example.

在您的示例中,似乎没有调用glEnable(GL_TEXTURE_2D)来启用纹理。

You can call glGetError() to find out if what you are doing is incorrect. This has helped me debug problems in the past.

您可以调用glge恐怖()来查明您所做的是否错误。这帮助我调试了过去的问题。

Also you appear to be creating your texture in your appRender() method. If this is called for every frame you draw then it could be the cause of your memory leak as you are repeatedly recreating the same texture.

而且,你似乎在你的学徒()方法中创造你的纹理。如果这是对你所绘制的每一个帧的调用,那么它可能是你的内存泄漏的原因,因为你不断地重新创建同样的纹理。

Typically you should only generate and define the texture once during initialization.

通常,您应该只在初始化期间生成和定义纹理。

So the following should be done once before rendering.

因此,在呈现之前应该做一次。

glGenTextures(1,&textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

//Different possible texture parameters, e.g
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, 512, 1024, 0, GL_RGB ,GL_UNSIGNED_BYTE, colors);

Then when drawing you can do

当你画画的时候。

glEnable(GL_TEXTURE_2D)
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glBindTexture(GL_TEXTURE_2D, textureID);

glVertexPointer(2, GL_FLOAT, 0, vertices);
glTexCoordPointer(2, GL_FLOAT, 0, texture);

glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices);

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisable(GL_TEXTURE_2D)