English 中文(简体)
Trouble Zhang Textures OpenGL ES 1.1
原标题:Trouble Displaying Textures OpenGL ES 1.1

我为Piter工作,我只想使用文字,不过,我似乎可以让它发挥作用。

经过一些研究,我发现,该网页,这个网站。 这两条都是很大的参考,在用两种功能装满文字后,我只得看文 displayed。

www.un.org/Depts/DGACM/index_spanish.htm 非常简单的文本显示功能 (无工作)

void drawTexture(GLuint texture, float x, float y, float w, float h)
{
   glBindTexture(GL_TEXTURE_2D,texture);

   GLfloat box[] = {x,y+h, x+w,y+h, x,y, x+w,y};
   GLfloat tex[] = {0,0, 1,0, 1,1, 0,1};

   glEnableClientState(GL_VERTEX_ARRAY);
   glEnableClientState(GL_TEXTURE_COORD_ARRAY);

   glVertexPointer(2, GL_FLOAT, 0,box);
   glTexCoordPointer(2, GL_FLOAT, 0, tex);

   glDrawArrays(GL_TRIANGLE_STRIP,0,4);

   glDisableClientState(GL_VERTEX_ARRAY);
   glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}

通常,我并不是每个框架都设置一个阵列,只能显示形象,但这只是一个例子。 当我行使这一职能时,我没有任何东西。 Bla-无形象,无所作为(当然,我以前曾使彩色阵列,后来又使色体残疾)

<>二级简单显示功能 (这只使用一个速效的班级)

void draw_rect(RectObject* robj){
    glVertexPointer(2, GL_FLOAT, 0, [robj vertices]);
    glEnableClientState(GL_VERTEX_ARRAY);
    glColorPointer(4, GL_UNSIGNED_BYTE, 0, [robj colors]);
    glEnableClientState(GL_COLOR_ARRAY);

    if ([robj texture] != -1){
        glEnable(GL_TEXTURE_COORD_ARRAY);
        glEnable(GL_TEXTURE_2D);
        glClientActiveTexture([robj texture]);

        glTexCoordPointer(2, GL_FLOAT, 0, defaultTexCoord);
        glBindTexture(GL_TEXTURE_2D, [robj texture]);
    }

    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

    glDisable(GL_TEXTURE_2D);
    glDisable(GL_TEXTURE_COORD_ARRAY);
}

而另一方面,这项职能确实改变了显示,而不是产生结果,但是它却产生了黑广场......

Setup Background

我的 in

glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_SRC_COLOR);

<>Two LONG Texture Loading Functions

struct Texture2D LoadImage(NSString* path)
{
    struct Texture2D tex;
    tex.texture = -1;

    // Id for texture 
    GLuint texture;
    // Generate textures
    glGenTextures(1, &texture);
    // Bind it
    glBindTexture(GL_TEXTURE_2D, texture);
    // Set a few parameters to handle drawing the image
    // at lower and higher sizes than original
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);

    //NSString *path = [[NSString alloc] initWithUTF8String:imagefile.c_str()];
    path = [[NSBundle mainBundle] pathForResource:path ofType:@""];
    NSData *texData = [[NSData alloc] initWithContentsOfFile:path];
    UIImage *image = [[UIImage alloc] initWithData:texData];
    if (image == nil)
        return tex;
    // Get Image size
    GLuint width = CGImageGetWidth(image.CGImage);
    GLuint height = CGImageGetHeight(image.CGImage);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    // Allocate memory for image
    void *imageData = malloc( height * width * 4 );
    CGContextRef imgcontext = CGBitmapContextCreate(
                                                imageData, width, height, 8, 4 * width, colorSpace,
                                                kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big );
    CGColorSpaceRelease( colorSpace );
    CGContextClearRect( imgcontext,
                   CGRectMake( 0, 0, width, height ) );
    CGContextTranslateCTM( imgcontext, 0, height - height );
    CGContextDrawImage( imgcontext,
                   CGRectMake( 0, 0, width, height ), image.CGImage );

    // Generate texture in opengl
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height,
             0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);
    // Release context
    CGContextRelease(imgcontext);
    // Free Stuff
    free(imageData);
    [image release];
    [texData release];

    // Create and return texture

    tex.texture=texture;
    tex.width=width;
    tex.height=height;
    return tex;
}



GLuint makeTexture(NSString* path){
    GLuint texture[1]={-1};
    glGenTextures(1, texture);
    glBindTexture(GL_TEXTURE_2D, texture[0]);

    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); 
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);



    path = [[NSBundle mainBundle] pathForResource:path ofType:@"png"];
    NSData *texData = [[NSData alloc] initWithContentsOfFile:path];
    UIImage *image = [[UIImage alloc] initWithData:texData];
    if (image == nil)
        NSLog(@"Do real error checking here");

    GLuint width = CGImageGetWidth(image.CGImage);
    GLuint height = CGImageGetHeight(image.CGImage);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    void *imageData = malloc( height * width * 4 );
    CGContextRef context = CGBitmapContextCreate( imageData, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big );
    CGColorSpaceRelease( colorSpace );
    CGContextClearRect( context, CGRectMake( 0, 0, width, height ) );
    CGContextTranslateCTM( context, 0, height - height );
    CGContextDrawImage( context, CGRectMake( 0, 0, width, height ), image.CGImage );

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);

    CGContextRelease(context);

    free(imageData);
    [image release];
    [texData release];

    return texture[0];
}

如果你能向我指明正确的方向,那将非常感激。

最佳回答

首先,<代码>draw_rect功能有误。 Don t打glClientActiveTexture,用于多文本,不需要。 用正文标打字,会给某些真正奇怪的正文单位造成约束,或很可能造成错误。

And in the drawTexture function you are actually drawing the triangles in clockwise order. Assuming you didn t flip the y-direction in the projection matrix or something similar, if you have back-face culling enabled your whole geometry will get culled away. Try calling glDisable(GL_CULL_FACE), although back-face culling should be disabled by default. Or even better, change your vertices to counter-clockwise ordering:

box[] = { x,y+h, x,y, x+w,y+h, x+w,y };

您在<代码>drawTexture功能中也有与vert相左的案文坐标,但这不应导致案文的推移,而只是看一线奇怪。 考虑到从最后一段改为反锁定,文本坐标应为:

tex[] = { 0.0f,1.0f, 0.0f,0.0f, 1.0f,1.0f, 1.0f, 0.0f };

http://www.un.org。 您的<代码>draw_rect功能也使国家变得遥远,因为你能够使垂直阵列和彩色阵列成为可能,但当你完成学业时,不会再次加以阻断。 当你现在想在无色阵列的情况下(如<代码>drawTexture)提取某种物时,肤色阵列仍然能够使用某些任意数据。 因此,请补充一下。

glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);

right after

glDisableClientState(GL_TEXTURE_COORD_ARRAY);

<代码>draw_rect。

EDIT: And you should also wrap the drawTexture function in a pair of glEnable(GL_TEXTURE_2D) and glDisable(GL_TEXTURE_2D). You enable texturing in the initialization code, which is wrong. You should set all neccessary state right before rendering, especially such highly object-dependent state like texturing. For example once you call draw_rect before drawTexture, you end up with disabled texturing, although you enabled it in the initialization code and thought it to be always enabled. Do you see that this is not a good idea?

http://www.un.org。 我刚刚发现另一个错误。 在<代码>draw_rect上 请打电话glEnableglDisable,GL_TEX_COORD_ARRAY,这是错误的。 您必须使用<代码>glEnableClientState和glDisableClient,以便/disabling vertex ranges,如您在t <>drawTexture上所做的那样。

因此,作为中途结束,你的职能实际上应当看上去:

void drawTexture(GLuint texture, float x, float y, float w, float h)
{
    glBindTexture(GL_TEXTURE_2D,texture);
    glEnable(GL_TEXTURE_2D);

    GLfloat box[] = {x,y+h, x+w,y+h, x,y, x+w,y};
    GLfloat tex[] = {0,0, 1,0, 1,1, 0,1};

    glTexCoordPointer(2, GL_FLOAT, 0, tex);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glVertexPointer(2, GL_FLOAT, 0, box);
    glEnableClientState(GL_VERTEX_ARRAY);

    glDrawArrays(GL_TRIANGLE_STRIP,0,4);

    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);

    glDisable(GL_TEXTURE_2D);
}

void draw_rect(RectObject* robj)
{
    if ([robj texture] != -1)
    {
        glBindTexture(GL_TEXTURE_2D, [robj texture]);
        glEnable(GL_TEXTURE_2D);

        glTexCoordPointer(2, GL_FLOAT, 0, defaultTexCoord);
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    }

    glColorPointer(4, GL_UNSIGNED_BYTE, 0, [robj colors]);
    glEnableClientState(GL_COLOR_ARRAY);
    glVertexPointer(2, GL_FLOAT, 0, [robj vertices]);
    glEnableClientState(GL_VERTEX_ARRAY);

    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_COLOR_ARRAY);

    if ([robj texture] != -1)
    {
        glDisableClientState(GL_TEXTURE_COORD_ARRAY);
        glDisable(GL_TEXTURE_2D);
    }
}
问题回答

If one of the textures work and the other not, could it be a problem with the texture file ? Dimensions sometimes can trick you, try to use the same file (the one working) on both textures and see if that solved. If it does it s a problem with the texture file.

幸运的是工作。

void drawTexture(GLuint texture, float x, float y, float w, float h)
{
    glBindTexture(GL_TEXTURE_2D,texture);
    glEnable(GL_TEXTURE_2D);

    GLfloat box[] = {x,y+h, x+w,y+h, x,y, x+w,y};
    GLfloat tex[] = {0,0, 1,0, 1,1, 0,1};

    glTexCoordPointer(2, GL_FLOAT, 0, tex);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glVertexPointer(2, GL_FLOAT, 0, box);
    glEnableClientState(GL_VERTEX_ARRAY);

    glDrawArrays(GL_TRIANGLE_STRIP,0,4);

    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);

    glDisable(GL_TEXTURE_2D);
}

但是,我们应当与文本正确协调有关。 修改法典

form

GLfloat box[] = {x,y+h, x+w,y+h, x,y, x+w,y};
GLfloat tex[] = {0,0, 1,0, 1,1, 0,1};

to

GLfloat box[] = {x,y+h, x+w,y+h, x,y, x+w,y};
GLfloat tex[] = { 0.0f,1.0f,  1.0f,1.0f, 0.0f,0.0f, 1.0f, 0.0f };

Thanks stackoverflow. Thanks your help. Good luck!





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签