Sign in to follow this  
ari556655

OpenGL writing to a texture

Recommended Posts

I'm working on a 2d game and would like write light intensity values into a texture(not a fbo) and then copy the texture onto a screen sized quad at the end of every render pass. Is it possible to write directly into a texture every frame, and if so how would I do this(I'm using opengl es 1.0 if it even matters)?

Share this post


Link to post
Share on other sites

Basically, all you need to do is get your lighting values in an array, preferably with dimensions that are a power of 2 (for compatiblity purposes) like 64x64 or 128x128, etc.

Then you'll need to free the last texture you used, create a new one, and load the lighting data into it.

Make an array out of unsigned char lighting[128][128];

and then your function for making the texture could look something like this:




unsigned int MakeLightTexture(unsigned char *lightdata)
{
unsigned int tex;

glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
gluBuild2DMipmaps(GL_TEXTURE_2D, 1, 128, 128, GL_LUMINANCE, GL_UNSIGNED_BYTE, lightdata);

return tex;
}



I am not sure if you can just re-use the texture, and move the glGenTextures call to your initialize and just gluBuild2DMipmaps or glTexImage2D if you want to avoid using the glu32 dll.. so try that, otherwise you'll have to glDeleteTextures(1, &lighttexture); at the end of every frame after you draw your lighting over the screen.

If you are going to use colored lighting (eg: RGB) then make your lighting array have [3] on the end, and change the 2nd parm in the build2dmipmaps call to 3, and GL_LUMINANCE to GL_RGB (I think ?)

Good luck!

Share this post


Link to post
Share on other sites
That seems like it would be awfully slow, no? One of the platforms I'm building for is the iphone so speed it curcial.

Is there some way to explicitly access the texel data once it has already been created?

Also I don't beieve gluBuild2DMipmaps works in openlg es 1.0.

Share this post


Link to post
Share on other sites
Okay, as a first step I tried just building a simple light map at the start of my program. Basically just writing light intensity values into a texture. And This does not seems to be working.

Here's the code I'm using. All I get on screen is a white quad(who's brightness is equal to whatever alpha value I pass to glColor4f() ). Also for the sake of testing, the blend function I'm using is GL_ONE, GL_ONE.


void build_light_map_tex( int_t* texture,
size_t width, size_t height)
{


GLfloat image_data[width][height];

float_t light_intensity;

int_t x, y;

for (x = 0; x < width; x++)
{
for (y = 0; y < height; y++)
{
light_intensity = calculate_light_intensity(x,y);

image_data[x][y] = v;
}
}

glGenTextures(1, texture);
glBindTexture(GL_TEXTURE_2D, *texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, width, height, 0, GL_LUMINANCE, GL_FLOAT, &image_data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);



Anyone have any ideas?

Share this post


Link to post
Share on other sites
Quote:
Original post by ari556655
That seems like it would be awfully slow, no? One of the platforms I'm building for is the iphone so speed it curcial.
Don't create a new texture each frame. Instead, you want to create the texture once at the beginning of the program, and then use glTexSubImage2D() to upload new data to the texture every frame.
Quote:
Okay, as a first step I tried just building a simple light map at the start of my program. Basically just writing light intensity values into a texture. And This does not seems to be working.

Here's the code I'm using. All I get on screen is a white quad(who's brightness is equal to whatever alpha value I pass to glColor4f() ). Also for the sake of testing, the blend function I'm using is GL_ONE, GL_ONE.

*** Source Snippet Removed ***

Anyone have any ideas?
Have you enabled GL_TEXTURE_2D?

Share this post


Link to post
Share on other sites
What I'm trying to do is upload my own array containing lighting and shading info into a texture, and then apply the texture to a screen. How could I use an fbo without calling glTexSubImage2D() to first upload my data to a texture?

Share this post


Link to post
Share on other sites
Quote:
Original post by ari556655
Is there any faster method than calling glTexSubImage2D() to manipulate a texture's contents?


Not from CPU side. In GL ES you are more limited, in desktop GL you can use a PBO to update texture data.

Share this post


Link to post
Share on other sites
Quote:
Original post by ari556655
What I'm trying to do is upload my own array containing lighting and shading info into a texture, and then apply the texture to a screen. How could I use an fbo without calling glTexSubImage2D() to first upload my data to a texture?
You can't - I wasn't clear on what you were trying to do at the time. However, you can perform tricks such as blending primitives in the FBO, depending on the complexity of your lighting setup.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this