Procedural textures

Apprentice
Posts: 13
Joined: 2009.06
Post: #1
So I'm trying my hand at procedural textures with the help of this tutorial and some of my beginner OpenGL knowledge. When I try to render my texture to the screen, no texture or box is drawn. I know my renderToTexture method will draw the line correctly. But I lack OpenGL knowledge/experience to try and figure out what the problem is. The code compiles and runs perfectly fine, but no texture.

Code:
- (GLuint)createEmptyTexture {
    GLuint textureName;
    unsigned int *data;
    
    // Allocate space for the image 96x96x4 for RGBA
    data = calloc((96 * 96) * 4, sizeof(unsigned int));
    
    // Zero it out
    bzero(data, sizeof(data));
    
    glGenTextures(1, &textureName);
    glBindTexture(GL_TEXTURE_2D, textureName);
    glTexImage2D(GL_TEXTURE_2D, 0, 4, 96, 96, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
    
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    
    free(data);
    
    return textureName;
}

- (void)renderToTexture {

    glViewport(0, 0, 96, 96);
    
    // Render here
    GLfloat vertices[] = {10.0f, 20.0f, 0.0f,
                          10.0f, 60.0f, 0.0f};
    
    glLineWidth(7.0f);
    glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
    
    glEnableClientState(GL_VERTEX_ARRAY);
    
    glVertexPointer(3, GL_FLOAT, 0, vertices);
    glDrawArrays(GL_LINES, 0, 2);
    
    glDisableClientState(GL_VERTEX_ARRAY);
    
    glBindTexture(GL_TEXTURE_2D, _name);
    
    // copy our viewport to the texture
    glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 0, 0, 96, 96, 0);
    
    glClear(GL_COLOR_BUFFER_BIT);
    
}

- (void)renderAtPoint:(CGPoint)aPoint {
    // Save the current matrix to the stack
    glPushMatrix();
    
    // Set the rotation of our image
    //glTranslatef(aPoint.x, aPoint.y, 0.0f);
    //glTranslatef(aPoint.x + (width / 2), aPoint.y + (height / 2), 0.0f);
    //glRotatef(-_rotation, 0.0f, 0.0f, 1.0f);
    //glTranslatef(-aPoint.x + (_imageWidth / 2), -aPoint.y + (_imageHeight / 2), 0.0f);
    
    // Set the color filter
    glColor4f(0, 0, 0, 1);
    
    // Enable Vertex Array rendering
    // Enable Texture Coord Array use
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    
    // Enable texturing
    glEnable(GL_TEXTURE_2D);
    
    // Make sure we aren't rebinding the texture
    
    glBindTexture(GL_TEXTURE_2D, _name);
    
    GLfloat texCoord[] = { 0.0f, 1.0f,
                           0.0f, 0.0f,
                           1.0f, 0.0f,
                           1.0f, 1.0f };
    
    GLfloat vertices[] = { 0, 0,
                           0, 96,
                           96, 96,
                           96, 0 };
    
    // Setup the vertex pointer to our coordinates
    glVertexPointer(2, GL_FLOAT, 0, vertices);
    
    // Setup the texture coords pointer to our coords
    glTexCoordPointer(2, GL_FLOAT, 0, texCoord);
    
    // Setup our blend function
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    
    // Draw the vertices
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    
    glDisable(GL_BLEND);
    glDisable(GL_TEXTURE_2D);
    glDisable(GL_VERTEX_ARRAY);
    
    // Load the matrix back from the stack
    glPopMatrix();    
}
Quote this message in a reply
Member
Posts: 227
Joined: 2008.08
Post: #2
Check that you are a able to use non-power of two textures (NPOTs), because I know that that happens for me. 96 is not a power of two, try 128 or 64.
Quote this message in a reply
Apprentice
Posts: 13
Joined: 2009.06
Post: #3
Thanks, but it didn't work. Annoyed

BTW, _name gets set to createEmptyTexture's return value.
Quote this message in a reply
Apprentice
Posts: 13
Joined: 2009.06
Post: #4
Well...after poking around a little....I found out I wasn't calling the renderAtPoint method. *hits self* I guess that's what I get for doing this at night when I'm tired. However, it's still not working. I'm getting a black box. I tried doing this but with frame buffers...but that gave me the same black box. Am I missing something?

Code:
- (void)renderAtPoint:(CGPoint)aPoint {
    // Save the current matrix to the stack
    glPushMatrix();
    
    // Set the rotation of our image
    //glTranslatef(aPoint.x, aPoint.y, 0.0f);
    //glTranslatef(aPoint.x + (width / 2), aPoint.y + (height / 2), 0.0f);
    //glRotatef(-_rotation, 0.0f, 0.0f, 1.0f);
    //glTranslatef(-aPoint.x + (_imageWidth / 2), -aPoint.y + (_imageHeight / 2), 0.0f);
    
    // Set the color filter
    glColor4f(0, 0, 0, 1);
    
    // Enable Vertex Array rendering
    // Enable Texture Coord Array use
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    
    // Enable texturing
    glEnable(GL_TEXTURE_2D);
    
    // Make sure we aren't rebinding the texture
    
    glBindTexture(GL_TEXTURE_2D, _name);
    
    GLfloat texCoord[] = { 0.0f, 1.0f,
                           0.0f, 0.0f,
                           1.0f, 0.0f,
                           1.0f, 1.0f };
    
    GLfloat vertices[] = { 0, 128,
                           128, 128,
                           0, 0,
                           128, 0 };
    
    // Setup the vertex pointer to our coordinates
    glVertexPointer(2, GL_FLOAT, 0, vertices);
    
    // Setup the texture coords pointer to our coords
    glTexCoordPointer(2, GL_FLOAT, 0, texCoord);
    
    // Setup our blend function
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    
    // Draw the vertices
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    
    glDisable(GL_BLEND);
    glDisable(GL_TEXTURE_2D);
    
    // Load the matrix back from the stack
    glPopMatrix();    
}

- (GLuint)createEmptyTexture {
    GLuint textureName;
    
    // Allocate space for the image 96x96x4 for RGBA
    //data = calloc((128 * 128) * 4, sizeof(unsigned int));
    
    // Zero it out
    //bzero(data, sizeof(data));
    
    glGenFramebuffersOES(1, &textureName);
    
    glGenTextures(1, &_data);
    glBindTexture(GL_FRAMEBUFFER_OES, _data);
    
    glTexParameteri(GL_FRAMEBUFFER_OES, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_FRAMEBUFFER_OES, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 128, 128, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
    
    glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, _data, 0);
    
    return textureName;
}

- (void)renderToTexture {
    
    glBindFramebufferOES(GL_FRAMEBUFFER_OES,_name);
    
    // Render here
    GLfloat vertices[] = {10.0f, 20.0f, 0.0f,
                          10.0f, 60.0f, 0.0f};
    
    glLineWidth(7.0f);
    glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
    
    glEnableClientState(GL_VERTEX_ARRAY);
    
    glVertexPointer(3, GL_FLOAT, 0, vertices);
    glDrawArrays(GL_LINES, 0, 2);
    
    glDisableClientState(GL_VERTEX_ARRAY);
    
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
    
    
}
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  procedural textures (modifying pixel data) using Texture2D class mrbgty 4 4,020 Mar 12, 2009 07:51 PM
Last Post: mrbgty