Thursday, September 17, 2009

How to manually draw to an OpenGL ES texture

// Create steam texture. It's just a transparent circle
texture = 0;
GLubyte *textureData = malloc(sizeof(GLubyte) * 4 * (radius * 2) * (radius * 2));
memset(textureData, 0, sizeof(GLubyte) * 4 * (radius * 2) * (radius * 2));
unsigned int index = 0;
for (int y = 0; y <>
for (int x = 0; x <>
float d = sqrt((x - radius) * (x - radius) + (y - radius) * (y - radius));
if (d <= radius) {
GLubyte alpha = 255 * (int)(radius - d) / radius;
textureData[index++] = 250;
textureData[index++] = 250;
textureData[index++] = 255;
textureData[index++] = alpha;
} else {
index += 4;
}
}
}

// allocate a texture name
glGenTextures(1, &texture);
// select our current texture
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
// build our texture mipmaps
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, radius * 2, radius * 2,
0, GL_RGBA, GL_UNSIGNED_BYTE, textureData);
// free buffer
free(textureData);

No comments: