Sign in to follow this  

OpenGL Texture problem for several particles

This topic is 2538 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi.
I am trying to render a few particles. I want the particles to glow a bit. Here is the code that I am using.


#define LIGHTSIZE 64

for(i=0; i<LIGHTSIZE; i++){
for(j=0; j<LIGHTSIZE; j++){
x = (((float)i) - ((float)LIGHTSIZE) / 2) / (((float)LIGHTSIZE) / 2);
y = (((float)j) - ((float)LIGHTSIZE) / 2) / (((float)LIGHTSIZE) / 2);
temp = 1.0f - (float)(sqrt((x * x) + (y * y)));
if(temp < 0.0f)
temp=-temp;
if(temp > 1.0f)
temp=1.0f;
lightTexture[i][j]=((unsigned char)(255*temp*temp));
//lightTexture[i][j]=50;
}
}
glEnable(GL_TEXTURE_2D);
// render particles now
for(i=0;i<=lastParticle;i++){

glGenTextures(i, &tex[i]);
glBindTexture(GL_TEXTURE_2D, tex[i]);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, LIGHTSIZE, LIGHTSIZE, 0,
GL_LUMINANCE, GL_UNSIGNED_BYTE, lightTexture);


trail[i].pos[0] = INIT_X + 0.25f - 0.05f*i + xpos*UNIT_SIZE;
trail[i].pos[1] = INIT_Y + (NOOFLINES-ypos)*UNIT_SIZE;

glBegin(GL_QUADS);
glTexCoord2f(0.0f,0.0f);
glVertex2f(trail[i].pos[0]-0.20f,trail[i].pos[1]);
glTexCoord2f(1.0f,0.0f);
glVertex2f(trail[i].pos[0]-0.20f-0.75f,trail[i].pos[1]);
glTexCoord2f(1.0f,1.0f);
glVertex2f(trail[i].pos[0]-0.20f-0.75f,trail[i].pos[1]+0.50f);
glTexCoord2f(0.0f,1.0f);
glVertex2f(trail[i].pos[0]-0.20f,trail[i].pos[1]+0.50f);
glEnd();

lumdiff /=1.10f;
printf("\nLP:%d,i=%d,x=%f,y=%f",lastParticle,i,trail[i].pos[0],trail[i].pos[1]);
}
glDisable(GL_TEXTURE_2D);


Everything is working just great but I can see only one glowing particle. The positions etc are being printed correctly but I am not able to see the rest of the particles. Can someone please point out what I am doing wrong here?

Regards,
Ravi.

Share this post


Link to post
Share on other sites
glGenTextures(i, &tex[i]) will generate i number of textures starting at tex[i] and ending at tex[i+i-1]. Is that what you want?
Also, are you generating textures every frame while drawing?
You shouldn't. If you need to update the textures, use glTexSubImage2D with the existing texture id.

Is depth testing enabled?
If so, try disabling it, as it could hide later particles depending on the depth function.

Share this post


Link to post
Share on other sites
Hi Erik.
Thanks for replying!
Hmm.. I don't want multiple textures to be generated but I do want one texture to applied to all particles. This is the code that I am using right now.



glEnable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);

// render particles now
for(i=0;i<=lastParticle;i++){

glBindTexture(GL_TEXTURE_2D,tex);

trail[i].pos[0] = INIT_X + 0.25f - 0.05f*i + xpos*UNIT_SIZE;
trail[i].pos[1] = INIT_Y + (NOOFLINES-ypos)*UNIT_SIZE;

glBegin(GL_QUADS);
glTexCoord2f(0.0f,0.0f);
glVertex2f(trail[i].pos[0]-0.25f,trail[i].pos[1]);
glTexCoord2f(1.0f,0.0f);
glVertex2f(trail[i].pos[0]-0.25f-0.75f,trail[i].pos[1]);
glTexCoord2f(1.0f,1.0f);
glVertex2f(trail[i].pos[0]-0.25f-0.75f,trail[i].pos[1]+0.50f);
glTexCoord2f(0.0f,1.0f);
glVertex2f(trail[i].pos[0]-0.25f,trail[i].pos[1]+0.50f);
glEnd();

lumdiff /=1.10f;
printf("\nLP:%d,i=%d,x=%f,y=%f",lastParticle,i,trail[i].pos[0],trail[i].pos[1]);
}
glDisable(GL_TEXTURE_2D);


And I am not generating textures on every frame. That lightTexture array is being initialized in another function init(). I just put it there to show how I am generating the texture. So the flow is something like


init(); //contains lightTexture array initialization
renderParticles(); //contains the code snippet given above
display(); /*the function being recursively invoked by OpenGL. Contains a call to renderParticles.*/


Regards,
Ravi.

P.S.: I disabled depth testing as you suggested, but it still does not work.
Also I changed tex from an GLuint array to a GLuint variable.

Share this post


Link to post
Share on other sites
How's your projection matrix set up?
Did you check for gl errors?

Edit: more questions

Did you enable blending?
Which equation?

Additionally, if you just use that one texture you don't need to bind it for every particle.

Second, call glBegin/End outside the loop or use buffers for increased performance.

Share this post


Link to post
Share on other sites
Hi.
I got that to work. The textures were just overlapping and due to some black area, I was unable to see the rest of the particles. So introducing a small offset between each texture being rendered helped.
But now, I see that my approach was totally wrong. I am able to generate an image like particle smear, but that is not what I want. Let me be more clear about what I want.

I want to render a trail of particles, which can glow. The particles should be partially visible, so that the person looking at them knows that the effect he is looking at is made up of hundreds of particles, but they should be generally surrounded by a bright light like effect. Image looking at the sun. You know that there is a solid sphere in the middle, but you really can't see it! I hope I conveyed with some precision as to what I want. Any pointers as to how I can do this will be greatly appreciated!

Regards,
Ravi.

Share this post


Link to post
Share on other sites
Quote:
Original post by crk
I am able to generate an image like particle smear, but that is not what I want. I want to render a trail of particles, which can glow. The particles should be partially visible, so that the person looking at them knows that the effect he is looking at is made up of hundreds of particles, but they should be generally surrounded by a bright light like effect. Image looking at the sun. You know that there is a solid sphere in the middle, but you really can't see it! I hope I conveyed with some precision as to what I want. Any pointers as to how I can do this will be greatly appreciated!
You need blending. Since your particle texture background appears to be black, and you want a glow effect, you probably want additive blending.

Which would be:
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE);

Share this post


Link to post
Share on other sites

This topic is 2538 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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