Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (108)
games submitted by our members
Games in WIP (536)
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  
  [Solved] Struggling with blending  (Read 324 times)
0 Members and 1 Guest are viewing this topic.
Offline songangel

Junior Member


Medals: 1



« Posted 2014-01-08 23:48:04 »

Hi Guys,

I am making a 2D minecraft sort of game and all has gone well so far, but I'm stumped on something and am unable to find an answer online.  

I first draw night sky image and then a day sky image on top of the night image.  I then have a Sun class that returns a light level as a float which I then use to control the opacity of the night image so that it fades to night time which works fine.

The problem is that the rest of my world, ie the trees, grass, player etc still looks the same even at night.  Blending is enabled in my Game class and below is what I tried but I just get a black screen.  I have tried multiple blending modes to try to achieve a sort of multiply effect but nothing is working.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
// Draw the overlay after everything else is drawn
     
      glBlendFunc(GL_DST_COLOR, GL_ZERO);
      glBegin(GL_QUADS);
      glColor3f(1.0f * light, 1.0f * light, 1.0f * light);
      glVertex2f(0,0);
      glVertex2f(Game.getWidth(),0);
      glVertex2f(Game.getWidth(),Game.getHeight());
      glVertex2f(0,Game.getHeight());
      glEnd();
      glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);


I should mention that my default blend function is glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) and I would be grateful for some direction.  I have tried reading about blend modes and I understand the concept but I can't seem to get what I am looking for.  I simply want to slowly darken what is already on the screen.
Offline Danny02
« Reply #1 - Posted 2014-01-09 00:25:02 »

This nice tool helps a lot with understanding/experimenting the different blend modes:
http://www.andersriggelsen.dk/glblendfunc.php
Offline songangel

Junior Member


Medals: 1



« Reply #2 - Posted 2014-01-09 00:33:46 »

Danny it's an interesting tool but I cannot duplicate what I need with it either.  Any other suggestions?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Gef
« Reply #3 - Posted 2014-01-09 00:52:55 »

Have you tried using the alpha channel of the glColor function instead of modulate the rgb channels ?
With something like this :
1  
2  
3  
4  
   glEnable(GL_BLEND);
   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   glColor4f(0.0f, 0.0f, 0.0f, 1.0f-light);
   ...

Which should draw the black rectangle with alpha of (1.0f-light), so day gives alpha of 0.0 and night 1.0.

Offline songangel

Junior Member


Medals: 1



« Reply #4 - Posted 2014-01-09 02:12:21 »

Gef it works sort of.  For some reason I can only see the effect when I hover the mouse over a block.  In my player code I draw a selector which is just a sprite with a transparent center and black frame to show the selected block.

This one has no effect:
https://www.dropbox.com/s/k57atnvphs8kkll/1.png

This one works as expected as my mouse is over a selectable block:

https://www.dropbox.com/s/d6hlqnbgn3cglrk/2.png

I'm so close if I could just figure this part out!  If you look closely at the second image, you will notice I have a leaf block selected.  When I select any block which triggers the drawing of the selector box, the darkening effect becomes visible.

EDIT:  When I draw the player after drawing the overlay, the problem disappears but the player does not darken with the rest of the scene.  Weird...  I welcome any thoughts.

My updated drawing code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
   //float light = sun.getLightLevel();
     
      float light = 0.3f;  // Just to make it night for debugging
     
      nightSky.draw(camera.getX() + Game.getWidth() / 2, camera.getY() + Game.getHeight() / 2);
      daySky.setTransparency(light);
      daySky.draw(camera.getX() + Game.getWidth() / 2, camera.getY() + Game.getHeight() / 2);
      tileMap.draw();
      player.draw();
     
      // Draw the overlay
     
      quad.setColor(0, 0, 0, 1.0f-light);
      quad.draw(camera.getX() + Game.getWidth() / 2, camera.getY() + Game.getHeight() / 2);
      /*glBegin(GL_QUADS);
      glColor4f(0.0f, 0.0f, 0.0f, 1.0f-light);
      glVertex2f(0,0);
      glVertex2f(Game.getWidth(),0);
      glVertex2f(Game.getWidth(),Game.getHeight());
      glVertex2f(0,Game.getHeight());
      glEnd();*/
Offline songangel

Junior Member


Medals: 1



« Reply #5 - Posted 2014-01-09 03:47:48 »

Ok this one was a noobish mistake.  I forgot to disable texturing when drawing the overlay.

Relevent code that now works as expected:

The Quad class draw method:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
   public void draw(float x, float y)
   {
      glDisable(GL_TEXTURE_2D);
      glEnableClientState(GL_VERTEX_ARRAY);
      glEnableClientState(GL_COLOR_ARRAY);
     
      glVertexPointer(2, 0, vertexData);
      glColorPointer(4, 0, colorData);
     
      glPushMatrix();
      glTranslatef(x,y,0);
      glDrawArrays(GL_QUADS, 0, 4);
      glPopMatrix();
     
      glDisableClientState(GL_VERTEX_ARRAY);
      glDisableClientState(GL_COLOR_ARRAY);
      glEnable(GL_TEXTURE_2D);
   }


Gefs code from my World class:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
   @Override
   public void draw()
   {
      float light = sun.getLightLevel();
     
      //float light = 0.3f;  // Just to make it night for debugging
     
      nightSky.draw(camera.getX() + Game.getWidth() / 2, camera.getY() + Game.getHeight() / 2);
      daySky.setTransparency(light);
      daySky.draw(camera.getX() + Game.getWidth() / 2, camera.getY() + Game.getHeight() / 2);
      tileMap.draw();
      player.draw();
     
      // Draw the overlay
     
      quad.setColor(0, 0, 0, 0.8f-light);
      quad.draw(camera.getX() + Game.getWidth() / 2, camera.getY() + Game.getHeight() / 2);

   }
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.

CogWheelz (16 views)
2014-07-30 21:08:39

Riven (22 views)
2014-07-29 18:09:19

Riven (14 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (32 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!