DGL
https://delphigl.com/forum/

Opengl Colorkey
https://delphigl.com/forum/viewtopic.php?f=19&t=9261
Seite 1 von 1

Autor:  Vladimir [ Di Jun 15, 2010 15:45 ]
Betreff des Beitrags:  Opengl Colorkey

Hello again,

I want to implement colorkey feature in opengl for my texture, as far as i understood now, it is not supported by opengl, so the idea would be to make two for loop for w and h of the texture and check for each pixels is they match color key, if yes then make alpha 0 otherwise make alpha 255, but how to implement that?

for i := 0 to textimage.w do
begin
for j := 0 to teximage.h do
begin
// read pixel?
// set alpha?

end;
end;

Autor:  Joni [ Di Jun 15, 2010 16:13 ]
Betreff des Beitrags:  Re: Opengl Colorkey

Hi,

you can do that with a fragment shader. You set the projection mode to orthogonal and render a quad with the texture on it. Your vertex shader may look like this:
Code:
void main(void)
{
 gl_Position     = gl_ModelViewProjectionMatrix * gl_Vertex;
 gl_TexCoord[0]  = gl_MultiTexCoord0;
}


Your fragment shader now is important:
Code:
void main(void)
{
  gl_FragColor = texture2D(Texture0, vec2(gl_TexCoord[0]));
  if (colorkey condition, e.g. gl_FragColor.r > 0.5)
    gl_FragColor.a = 1.0;
  else
    gl_FragColor.a = 0.0;
}


Now you can either copy the result to the main program using glReadPixels or into a texture using glCopyTexImage2D. To get the result in a texture, it is also possible/better to use Framebuffers.

Autor:  Vladimir [ Di Jun 15, 2010 16:34 ]
Betreff des Beitrags:  Re: Opengl Colorkey

Joni hat geschrieben:
Hi,

you can do that with a fragment shader. You set the projection mode to orthogonal and render a quad with the texture on it. Your vertex shader may look like this:
Code:
void main(void)
{
 gl_Position     = gl_ModelViewProjectionMatrix * gl_Vertex;
 gl_TexCoord[0]  = gl_MultiTexCoord0;
}


Your fragment shader now is important:
Code:
void main(void)
{
  gl_FragColor = texture2D(Texture0, vec2(gl_TexCoord[0]));
  if (colorkey condition, e.g. gl_FragColor.r > 0.5)
    gl_FragColor.a = 1.0;
  else
    gl_FragColor.a = 0.0;
}


Now you can either copy the result to the main program using glReadPixels or into a texture using glCopyTexImage2D. To get the result in a texture, it is also possible/better to use Framebuffers.


thanks for fast reply, but i forgot to mention, to do it without shaders, i want to use sdl + opengl only.

Autor:  Joni [ Di Jun 15, 2010 16:45 ]
Betreff des Beitrags:  Re: Opengl Colorkey

Hi,

if that's the case, I think it's easiest to load the whole picture into the main program using glReadPixels, then create an array with only the alpha data and then write this array back into the graphics memory using glDrawPixels with format parameter =GL_ALPHA. However, this needs a lot of bandwidth. Still, if you can use framebufferobjects and need the data in a texture, I suggest you to use one as it saves you another time-intensive copying procedure.

Autor:  Vladimir [ Di Jun 15, 2010 17:16 ]
Betreff des Beitrags:  Re: Opengl Colorkey

Joni hat geschrieben:
Hi,

if that's the case, I think it's easiest to load the whole picture into the main program using glReadPixels, then create an array with only the alpha data and then write this array back into the graphics memory using glDrawPixels with format parameter =GL_ALPHA. However, this needs a lot of bandwidth. Still, if you can use framebufferobjects and need the data in a texture, I suggest you to use one as it saves you another time-intensive copying procedure.


thanks for reply again,
well, i thinked like read pixel from texture and write to other new texture - with colorkey value changed to alpha?
the problem is, i dont know how to read pixel from texture

Autor:  Joni [ Di Jun 15, 2010 17:51 ]
Betreff des Beitrags:  Re: Opengl Colorkey

As far as I know you can either draw the texture into the framebuffer from where you can read it using glReadPixels or use a framebuffer object, which is a framebuffer linked to a texture. I posted a link to a tutorial for framebufferobjects before, but if you can't read german, I suggest you to take a look at http://www.opengl.org/registry/specs/ARB/framebuffer_object.txt The only reason against FBOs is that some older graphics cards don't support this extension. In this case, you have to render the texture to the main frame buffer, read it via glReadPixels, create the alpha data, write the alpha data back to the frame buffer and copy the contents of the framebuffer back into the texture. This procedure is definitely slower than using FBOs. As far as I know there is no other method to read and write into textures, but maybe I'm wrong.

EDIT: sorry, to write into a texture it is afaik most useful to use glTexImage2D, ignore what I wrote above.

Autor:  Vladimir [ Di Jun 15, 2010 18:03 ]
Betreff des Beitrags:  Re: Opengl Colorkey

Joni hat geschrieben:
As far as I know you can either draw the texture into the framebuffer from where you can read it using glReadPixels or use a framebuffer object, which is a framebuffer linked to a texture. I posted a link to a tutorial for framebufferobjects before, but if you can't read german, I suggest you to take a look at http://www.opengl.org/registry/specs/ARB/framebuffer_object.txt The only reason against FBOs is that some older graphics cards don't support this extension. In this case, you have to render the texture to the main frame buffer, read it via glReadPixels, create the alpha data, write the alpha data back to the frame buffer and copy the contents of the framebuffer back into the texture. This procedure is definitely slower than using FBOs. As far as I know there is no other method to read and write into textures, but maybe I'm wrong.


thanks again
but the better to use image editor and change background color to alpha ;-)

Autor:  Flash [ Mi Jun 16, 2010 09:47 ]
Betreff des Beitrags:  Re: Opengl Colorkey

If this is an option, you can defenitly progress faster with fewer coding with this approach. :mrgreen:

Seite 1 von 1 Alle Zeiten sind UTC + 1 Stunde
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/