Files |  Tutorials |  Articles |  Links |  Home |  Team |  Forum |  Wiki |  Impressum

Aktuelle Zeit: Mi Jan 20, 2021 04:35

Foren-Übersicht » English » English Programming Forum
Unbeantwortete Themen | Aktive Themen



Ein neues Thema erstellen Auf das Thema antworten  [ 8 Beiträge ] 
Autor Nachricht
 Betreff des Beitrags: Opengl Colorkey
BeitragVerfasst: Di Jun 15, 2010 15:45 
Offline
DGL Member

Registriert: Di Mär 23, 2010 13:40
Beiträge: 14
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;


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags: Re: Opengl Colorkey
BeitragVerfasst: Di Jun 15, 2010 16:13 
Offline
DGL Member

Registriert: So Okt 21, 2007 14:16
Beiträge: 123
Programmiersprache: Delphi
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.


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags: Re: Opengl Colorkey
BeitragVerfasst: Di Jun 15, 2010 16:34 
Offline
DGL Member

Registriert: Di Mär 23, 2010 13:40
Beiträge: 14
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.


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags: Re: Opengl Colorkey
BeitragVerfasst: Di Jun 15, 2010 16:45 
Offline
DGL Member

Registriert: So Okt 21, 2007 14:16
Beiträge: 123
Programmiersprache: Delphi
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.


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags: Re: Opengl Colorkey
BeitragVerfasst: Di Jun 15, 2010 17:16 
Offline
DGL Member

Registriert: Di Mär 23, 2010 13:40
Beiträge: 14
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


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags: Re: Opengl Colorkey
BeitragVerfasst: Di Jun 15, 2010 17:51 
Offline
DGL Member

Registriert: So Okt 21, 2007 14:16
Beiträge: 123
Programmiersprache: Delphi
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.


Zuletzt geändert von Joni am Do Jun 17, 2010 13:32, insgesamt 1-mal geändert.

Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags: Re: Opengl Colorkey
BeitragVerfasst: Di Jun 15, 2010 18:03 
Offline
DGL Member

Registriert: Di Mär 23, 2010 13:40
Beiträge: 14
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 ;-)


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags: Re: Opengl Colorkey
BeitragVerfasst: Mi Jun 16, 2010 09:47 
Offline
Guitar Hero
Benutzeravatar

Registriert: Do Sep 25, 2003 15:56
Beiträge: 7772
Wohnort: Sachsen - ERZ / C
Programmiersprache: Java (, Pascal)
If this is an option, you can defenitly progress faster with fewer coding with this approach. :mrgreen:

_________________
Blog: kevin-fleischer.de und fbaingermany.com


Nach oben
 Profil  
Mit Zitat antworten  
Beiträge der letzten Zeit anzeigen:  Sortiere nach  
Ein neues Thema erstellen Auf das Thema antworten  [ 8 Beiträge ] 
Foren-Übersicht » English » English Programming Forum


Wer ist online?

Mitglieder in diesem Forum: 0 Mitglieder und 1 Gast


Du darfst keine neuen Themen in diesem Forum erstellen.
Du darfst keine Antworten zu Themen in diesem Forum erstellen.
Du darfst deine Beiträge in diesem Forum nicht ändern.
Du darfst deine Beiträge in diesem Forum nicht löschen.
Du darfst keine Dateianhänge in diesem Forum erstellen.

Suche nach:
Gehe zu:  
  Powered by phpBB® Forum Software © phpBB Group
Deutsche Übersetzung durch phpBB.de
[ Time : 0.037s | 15 Queries | GZIP : On ]