简体   繁体   English

Android NDK中的OpenGL ES绘图问题

[英]OpenGL ES drawing issues in Android NDK

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. 任何帮助将不胜感激。

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. 您可以调用glGetError()来确定您所做的操作是否不正确。 This has helped me debug problems in the past. 这过去帮助我调试了问题。

Also you appear to be creating your texture in your appRender() method. 同样,您似乎也在appRender()方法中创建纹理。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM