Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (429)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (467)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Java Game APIs & Engines / OpenGL Development / Masking Alpha with two 2D textures LWJGL? on: 2013-07-29 07: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 ... *
2  Java Game APIs & Engines / OpenGL Development / Re: stock with concept - game stage/scene on: 2013-07-19 02:33:15
So ... another class as a stage handler .... what a clever idea, i will try to implement this and see if its results to me .. thank you Cheesy
3  Java Game APIs & Engines / OpenGL Development / stock with concept - game stage/scene on: 2013-07-18 04:12:42
Hello guys ...
I have been programming my first game in lwjgl and I get stuck thinking how to make some sort of game stage/scene management in the main class (who create the Display and AL)

I make the following Interface
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
package interfaces;

import gametools.TimerTool;

public interface Stage {

   public void setUpObjects();
   
   public void draw();

   public void update(TimerTool timer);

   public void input();

   public void unloadObjects();

}

and an Abstract class for my game stages classes...
but Im not sure how to manage the navigation between one stage to another ...

I follow some tutorials that propose making something like this in the main class
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  
public enum GameState {
      INTRO, MAIN_MENU, GAME;
   }

private void checkInput() {
      switch (state) {
      case INTRO:
         if(Keyboard.isKeyDown(Keyboard.KEY_RETURN)){
            state=State.MAIN_MENU;
         }
         if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)){
            Display.destroy();
            System.exit(0);
         }
         break;
      case GAME:
         if(Keyboard.isKeyDown(Keyboard.KEY_BACK)){
            state=State.MAIN_MENU;
         }
         break;
      case MAIN_MENU:
         if(Keyboard.isKeyDown(Keyboard.KEY_RETURN)){
            state=State.GAME;
         }
         if(Keyboard.isKeyDown(Keyboard.KEY_SPACE)){
            state=State.INTRO;
         }
         break;
      }
   }

and rendering and updating different stuff depending on the current stage ... but it is pretty basic ...
How can i manage this transition when each stage have his own class (extending my Abstract Stage class) and make each class capable to say to the main class go to n stage, unload his content and the main class load the next stage?, and if its possible want to render and update two stages at the same time (like OPTION over MAIN_MENU, and OPTIONS over GAME).

Has been thinking in static stages or something like that but nothing accomplish what I want ...  Clueless

can anyone make me some suggestions ...
thank you a lot...

by the way ... forgive me if my english is a bit primitive ... its not my native language
Pages: [1]
 

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

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (81 views)
2014-04-15 18:08:23

BurntPizza (73 views)
2014-04-15 03:46:01

UprightPath (84 views)
2014-04-14 17:39:50

UprightPath (67 views)
2014-04-14 17:35:47

Porlus (84 views)
2014-04-14 15:48:38

tom_mai78101 (107 views)
2014-04-10 04:04:31

BurntPizza (167 views)
2014-04-08 23:06:04

tom_mai78101 (263 views)
2014-04-05 13:34:39

trollwarrior1 (214 views)
2014-04-04 12:06:45

CJLetsGame (223 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!