Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (575)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Masking Alpha with two 2D textures LWJGL?  (Read 860 times)
0 Members and 1 Guest are viewing this topic.
Offline edgar287

Junior Newbie





« Posted 2013-07-29 05:02:36 »

Hello guys
I read a lot of guides but I didnt found one who work for me ... or Im just too dumb to "translate them" to my code Yawn ...

I rendering my textures in this way:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
public void render() {

      glBindTexture(GL_TEXTURE_RECTANGLE_ARB, sprite.getT());

      if (isLinealScale()) {
         glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER,
               GL_LINEAR);
         glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER,
               GL_LINEAR);
      }

      float x = getPosX() + getModPosX() - getBindX();
      float y = getPosY() + getModPosY() - getBindY();
      float w = getPosX() + getModPosX() + getCurrentWidth() - getBindX();
      float h = getPosY() + getModPosY() + getCurrentHeight() - getBindY();

      float sprX = sprite.getX();
      float sprY = sprite.getY();
      float sprW = sprX + sprite.getW();
      float sprH = sprY + sprite.getH();

      // switch (blend) {
      // case MULTIPLY:
      // glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
      // break;
      // case SCREEN:
      // glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
      // break;
      //
      // default:
      // break;
      // }

      glPushMatrix();

      glTranslatef(x + getBindX(), y + getBindY(), 0);
      glRotatef(getAngle(), 0, 0, 1);
      glTranslatef(-x - getBindX(), -y - getBindY(), 0);

      glBegin(GL_QUADS);
      glColor4f(1.0f, 1.0f, 1.0f, getOpasity());
      if (flip) {
         glTexCoord2f(sprW, sprY);
         glVertex2f(x, y);
         glTexCoord2f(sprX, sprY);
         glVertex2f(w, y);
         glTexCoord2f(sprX, sprH);
         glVertex2f(w, h);
         glTexCoord2f(sprW, sprH);
         glVertex2f(x, h);

      } else {
         glTexCoord2f(sprX, sprY);
         glVertex2f(x, y);
         glTexCoord2f(sprW, sprY);
         glVertex2f(w, y);
         glTexCoord2f(sprW, sprH);
         glVertex2f(w, h);
         glTexCoord2f(sprX, sprH);
         glVertex2f(x, h);
      }
      glColor4f(1.0f, 1.0f, 1.0f, 1f);
      glEnd();

      glPopMatrix();

      glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);
      // glBlendFunc(GL_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

      if (isLinealScale()) {
         glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER,
               GL_NEAREST);
         glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER,
               GL_NEAREST);
      }

   }

as  you can see I dont use "GL_TEXTURE_2D" when I bind a texture, instep i use "GL_TEXTURE_RECTANGLE_ARB", this might be one of the reasons I cant accomplish the masking ... but when I bind my texture id with GL_TEXTURE_2D I only get a white square :| (and Im using glEnable(GL_TEXTURE_2D) by default in my Display and even with that it doesnt work for me)

Any way ...  I create a new texture object extending my normal texture who work fine with png textures with alpha and with glBlendFunc(GL_ALPHA, GL_ONE_MINUS_SRC_ALPHA) as the default blend.
This new object just add another sprite named mask who may or not has a different size, and may or not to be in the same png.

The type of masking i whant is this one ...
having this texture (this texture may have Alpha channel):

and this mask (just white and black texture without Alpha channel??? Huh *really want this one have alpha channel too  Clueless*)

and with some combination of glBlendFunc in the texture and the mask .. get this

(without the background texture  Stare ...just transparency)

By the moment I have this code
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
public void render() {

      glDisable(GL_DEPTH_TEST);

      float x = getPosX() + getModPosX() - getBindX();
      float y = getPosY() + getModPosY() - getBindY();

      glPushMatrix();

      glTranslatef(x + getBindX(), y + getBindY(), 0);
      glRotatef(getAngle(), 0, 0, 1);
      glTranslatef(-x - getBindX(), -y - getBindY(), 0);

      // ============ First Render ==============

      // glColor4f(1.0f, 1.0f, 1.0f, 1f);
      // glColor4f(1.0f, 1.0f, 1.0f, 0f);
      // glColor4f(0f, 0f, 0f, 1f);

      // glBlendFunc(GL_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
      // GL14.glBlendEquation(GL14.GL_FUNC_ADD);
      // GL14.glBlendFuncSeparate(GL_ONE_MINUS_DST_ALPHA, GL_DST_ALPHA,
      // GL_ZERO, GL_ONE);

      // glBlendFunc(GL_ONE, GL_ZERO);

      // glBlendFunc(GL_ONE, GL_SRC_COLOR);
      // glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_COLOR);
      // glBlendFunc(GL_ONE, GL_DST_COLOR);
      // glBlendFunc(GL_ONE, GL_ONE_MINUS_DST_COLOR);
      // glBlendFunc(GL_ONE, GL_SRC_ALPHA);
      // glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
      // glBlendFunc(GL_ONE, GL_DST_ALPHA);
      // glBlendFunc(GL_ONE, GL_ONE_MINUS_DST_ALPHA);
      // glBlendFunc(GL_ONE, GL_SRC_ALPHA_SATURATE);
      // glBlendFunc(GL_ONE, GL_CONSTANT_COLOR);
      // glBlendFunc(GL_ONE, GL_ONE_MINUS_CONSTANT_COLOR);
      // glBlendFunc(GL_ONE, GL_CONSTANT_ALPHA);
      // glBlendFunc(GL_ONE, GL_ONE_MINUS_CONSTANT_ALPHA);

      // glBlendFunc(GL_ONE_MINUS_CONSTANT_COLOR, GL_ONE_MINUS_SRC_ALPHA);

      glBlendFunc(GL_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
      renderMask();

      // ============== End First Render ==============

      glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);

      // ============== Second Render ==============

      glColor4f(1.0f, 1.0f, 1.0f, getOpasity());
//      glBlendFunc(GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
       glBlendFunc(GL_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
      renderSprite();

      // ============== End Second Render ==============

      glPopMatrix();

      glColor4f(1.0f, 1.0f, 1.0f, 1f);

      glBlendFunc(GL_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

      glEnable(GL_DEPTH_TEST);

   }

   private void renderMask() {

      float x = getPosX() + getModPosX() - getBindX();
      float y = getPosY() + getModPosY() - getBindY();
      float w = getPosX() + getModPosX() + getCurrentWidth() - getBindX();
      float h = getPosY() + getModPosY() + getCurrentHeight() - getBindY();

      float maskSprX = mask.getX();
      float maskSprY = mask.getY();
      float maskSprW = maskSprX + mask.getW();
      float maskSprH = maskSprY + mask.getH();

      if (linearMask) {
         glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER,
               GL_LINEAR);
         glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER,
               GL_LINEAR);
      }

      glBindTexture(GL_TEXTURE_RECTANGLE_ARB, mask.getT());

      glBegin(GL_QUADS);
      if (isFlip()) {
         glTexCoord2f(maskSprW, maskSprY);
         glVertex2f(x, y);
         glTexCoord2f(maskSprX, maskSprY);
         glVertex2f(w, y);
         glTexCoord2f(maskSprX, maskSprH);
         glVertex2f(w, h);
         glTexCoord2f(maskSprW, maskSprH);
         glVertex2f(x, h);

      } else {
         glTexCoord2f(maskSprX, maskSprY);
         glVertex2f(x, y);
         glTexCoord2f(maskSprW, maskSprY);
         glVertex2f(w, y);
         glTexCoord2f(maskSprW, maskSprH);
         glVertex2f(w, h);
         glTexCoord2f(maskSprX, maskSprH);
         glVertex2f(x, h);
      }

      glEnd();

      if (linearMask) {
         glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER,
               GL_NEAREST);
         glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER,
               GL_NEAREST);
      }
   }

   private void renderSprite() {

      float x = getPosX() + getModPosX() - getBindX();
      float y = getPosY() + getModPosY() - getBindY();
      float w = getPosX() + getModPosX() + getCurrentWidth() - getBindX();
      float h = getPosY() + getModPosY() + getCurrentHeight() - getBindY();

      float sprX = getSprite().getX();
      float sprY = getSprite().getY();
      float sprW = sprX + getSprite().getW();
      float sprH = sprY + getSprite().getH();

      if (isLinealScale()) {
         glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER,
               GL_LINEAR);
         glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER,
               GL_LINEAR);
      }

      glBindTexture(GL_TEXTURE_RECTANGLE_ARB, getSprite().getT());

      glBegin(GL_QUADS);

      if (isFlip()) {
         glTexCoord2f(sprW, sprY);
         glVertex2f(x, y);
         glTexCoord2f(sprX, sprY);
         glVertex2f(w, y);
         glTexCoord2f(sprX, sprH);
         glVertex2f(w, h);
         glTexCoord2f(sprW, sprH);
         glVertex2f(x, h);

      } else {
         glTexCoord2f(sprX, sprY);
         glVertex2f(x, y);
         glTexCoord2f(sprW, sprY);
         glVertex2f(w, y);
         glTexCoord2f(sprW, sprH);
         glVertex2f(w, h);
         glTexCoord2f(sprX, sprH);
         glVertex2f(x, h);
      }

      glEnd();

      if (isLinealScale()) {
         glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER,
               GL_NEAREST);
         glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER,
               GL_NEAREST);
      }

   }


But to be honest with you ... I dont have any idea what i am doing Stare ...
I dont know if i have to render the texture before the mask
I dont know if what Im trying to do is even possible
I dont know witch glBlendFunc add before the mask rendering and witch one before the normal texture
I dont know if i HAVE to use GL_TEXTURE_2D in the glBindTexture method

In other words Im totally lost Stare

I really appreciate your attention and your suggestions  Smiley
 * Sorry for the ridiculous amount of comments ... I try a lot of blend functions .. concepts and guides ... *
Offline davedes
« Reply #1 - Posted 2013-07-29 13:42:32 »

Learn to walk before you run... in other words, learn OpenGL basics before you start using it for 2D effects like masking.

Texturing Tutorial

I would recommend using GL_TEXTURE_2D instead of GL_TEXTURE_RECTANGLE_ARB. The latter is a bit outdated.

Also you don't need to bind texture zero at the end of your rendering. Nor do you need to set the filtering every frame -- you just set it once for the texture you want, and it will stay set until you change it.

Quote
with glBlendFunc(GL_ALPHA, GL_ONE_MINUS_SRC_ALPHA) as the default blend.
GL_ALPHA is not a valid parameter to blendFunc. I think you mean GL_SRC_ALPHA.

The screenshots you have are from my blending tutorial, which uses shaders, and is probably the most efficient means of accomplishing that kind of a blend. It includes full source code for LWJGL or for LibGDX... So I'm not sure why you are trying to do this with blend functions instead.

There are some other masking techniques, including a working example with blend func, explained in the following (work in progress) article:
https://github.com/mattdesl/lwjgl-basics/wiki/LibGDX-Masking

I've included LibGDX code. If you haven't already considered it, I would suggest using LibGDX as your OpenGL wrapper rather than relying on deprecated GL code like glPushMatrix, glVertex, glRotate, etc.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (35 views)
2014-10-17 03:59:02

Norakomi (26 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (26 views)
2014-10-15 16:18:58

TehJavaDev (50 views)
2014-10-14 00:39:48

TehJavaDev (52 views)
2014-10-14 00:35:47

TehJavaDev (40 views)
2014-10-14 00:32:37

BurntPizza (63 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (76 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!