LWJGL Forum

Programming => OpenGL => Topic started by: Darkrider on November 02, 2011, 10:19:51

Title: Blending Problems
Post by: Darkrider on November 02, 2011, 10:19:51
Hey Guys!

Even if I risk to ask a highly embarassing question: I have problems to change the transparency of my painted object. I used and modified NeHe's tutorials and even if i change the glColor4f() and the Alphafunc it doesn't change a thing... well and if it changes it is a disaster =) Can someone help me? Heres my code:


package ownStuff;




import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import org.lwjgl.opengl.*;
import org.lwjgl.input.*;
import org.lwjgl.util.glu.*;
import org.newdawn.slick.opengl.*;
import org.newdawn.slick.util.*;

/**
* @author Philipp Sorio
* This class is like the barebone of a OpenGL-Window and the start of every project
*/
public class Light {
   
   /**
    * Variable if the App is done
    */
   private boolean done = false;
   
   /**
    * Variable if the App is in fullscreen Mode
    */
   private boolean fullscreen = false;
   
   /**
    * Title of the Main Window
    */
   private final String windowTitle = "OpenGL App";
   
   /**
    * Texture
    */
   private Texture texture = null;
   
   /**
    * Is F1 been pressed?
    */
   private boolean f1p = false;
   
   /**
    * Is the Left Arrow Button pressed?
    */
   private boolean lap = false;
   
   /**
    * Is the Right Arrow Button pressed?
    */
   private boolean rap = false;
   
   /**
    * Is the Down Arrow Button pressed?
    */
   private boolean dap = false;
   
   /**
    * Is the Up Arrow Button pressed?
    */
   private boolean uap = false;
   
/**
* Is "PageUp" been pressed?
*/
private boolean pup = false;

/**
* Is "PageDown" been pressed?
*/
private boolean pdp = false;

   /**
    * Rotation of the x-Axis
    */
   private float xRot = 0.0f;
   
   /**
    * Rotation of the y-Axis
    */
   private float yRot = 0.0f;
   
   /**
    * Speed of x-Rot
    */
   private float xSpeed = 0.0f;
   /**
    * Speed of x-Rot
    */
   private float ySpeed = 0.0f;
   
/**
* Distance to the Screen
*/
private float z = -5.0f;

   /**
    * Steps of changing Speed
    */
   private float step = 0.2f;
   
   /**
    * Params for Ambient Light
    */
   private float lightAmbient[] = { 0.5f, 0.5f, 0.5f, 1.0f };
   
   /**
    * Params for Diffuse Light
    */
   private float lightDiffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };    
   
   /**
    * Pos of the Light
    */
   private float lightPosition[] = { 0.0f, 0.0f, 2.0f, 1.0f }; // Light Position ( NEW )
   
   
   
   /**
    * Which DisplayMode is used
    */
   private DisplayMode displayMode;

   /**
    * Everything starts and ends here.  Takes 1 optional command line argument.
    * If fullscreen is specified on the command line then fullscreen is used,
    * otherwise windowed mode will be used.
    * @param args command line arguments
    */
   public static void main(String args[]) {
//It starts as windowed
       boolean fullscreen = false;
//Is there a command line
       if(args.length > 0)
   //is there a command that indicates the desire for fullscreen?
           if(args[0].equalsIgnoreCase("fullscreen"))
//go fullscreen
               fullscreen = true;
           
       //Instanciate the window and..
       Light li = new Light();
//..run it
       li.run(fullscreen);
   }

   /**
    * Launch point
    * @param fullscreen boolean value, set to true to run in fullscreen mode
    */
   public void run(boolean fullscreen) {
//Retrieving the windowing mode
       this.fullscreen = fullscreen;
       try {
//initialize everything
           init();
//Main Loop
           while (!done) {
//Polling and retrieving input
               polling();
//Render (draw) my App
               render();
//Update everything on the screen
               Display.update();
           }
// Clean everything up and GTFO
           cleanup();
       }
       catch (Exception e) {
           System.out.println("run(): " + e.getMessage());
           System.exit(0);
       }
   }

   /**
    * All updating is done here.  Key and mouse polling as well as window closing and
    * custom updates, such as AI.
    */
   private void polling() {
// Exit if Escape is pressed
       if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {      
           done = true;
       }
// Exit if window is closed
       if(Display.isCloseRequested()) {                    
           done = true;
       }
// Is F1 Being Pressed?
       if(!Keyboard.isKeyDown(Keyboard.KEY_F1)) {          
           f1p = false;
       }
// Is F1 Being Pressed?
       if(Keyboard.isKeyDown(Keyboard.KEY_F1) && !f1p) {    
   // Tell Program F1 Is Being Held and..
           f1p = true;                            
   //..Toggle Fullscreen / Windowed Mode
           switchMode();                                  
       }
//Is The Left Arrow Pressed?
if(!Keyboard.isKeyDown(Keyboard.KEY_LEFT)){
lap = false;
}
//Just to be sure it only reacts once
if(Keyboard.isKeyDown(Keyboard.KEY_LEFT) && !lap){
lap = true;
//Modifying Speed
if(xSpeed - step >= -1)
xSpeed -= step;
}
//Is The Right Arrow Pressed?
if(!Keyboard.isKeyDown(Keyboard.KEY_RIGHT)){
rap = false;
}
//Just to be sure it only reacts once
if(Keyboard.isKeyDown(Keyboard.KEY_RIGHT) && !rap){
rap = true;
//Modifying Speed
if(xSpeed + step <= 1)
xSpeed += step;
}
//Is The Down Arrow Pressed?
if(!Keyboard.isKeyDown(Keyboard.KEY_DOWN)){
dap = false;
}
//Just to be sure it only reacts once
if(Keyboard.isKeyDown(Keyboard.KEY_DOWN) && !dap){
dap = true;
//Modifying Speed
if(ySpeed - step >= -1)
ySpeed -= step;
}
//Is The Up Arrow Pressed?
if(!Keyboard.isKeyDown(Keyboard.KEY_UP)){
uap = false;
}
//Just to be sure it only reacts once
if(Keyboard.isKeyDown(Keyboard.KEY_UP) && !uap){
uap = true;
//Modifying Speed
if(ySpeed + step <= 1)
ySpeed += step;
}
//Is "PageUp" Pressed?
if(!Keyboard.isKeyDown(Keyboard.KEY_PRIOR)){
pup = false;
}
//Just to be sure it only reacts once
if(Keyboard.isKeyDown(Keyboard.KEY_PRIOR) && !pup){
pup = true;
//Modifying Distance
if (z - step > -100.0f)
z -= step;
}
//Is "PageDown" Pressed?
if(!Keyboard.isKeyDown(Keyboard.KEY_NEXT)){
pdp = false;
}
//Just to be sure it only reacts once
if(Keyboard.isKeyDown(Keyboard.KEY_NEXT) && !pdp){
pdp = true;
//Modifying Distance
if (z + step < -3.0f)
z += step;
}
}

/**
* Switches between FS and Windowed Mode
*/
   private void switchMode() {
//Toggle and..
       fullscreen = !fullscreen;
       try {
//..set
           Display.setFullscreen(fullscreen);
       }
       catch(Exception e) {
           System.out.println("switchMode(): " + e.getMessage());
       }
   }

   /**
    * For rendering all objects to the screen
    * @return boolean for success or not
    */
   private boolean render() throws Exception {
// Clear The Screen And The Depth Buffer
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
// Reset The Current Modelview Matrix
       GL11.glLoadIdentity();

// Move Into The Screen 5 Units
       GL11.glTranslatef(0.0f, 0.0f, z);

//[TODO] Switched

// Rotate On The X Axis
       GL11.glRotatef(yRot, 1.0f, 0.0f, 0.0f);
// Rotate On The Y Axis
       GL11.glRotatef(xRot, 0.0f, 1.0f, 0.0f);

//END SWITCHED
//Loading Textures
this.getTexture("BMP", "Crate.bmp");
       GL11.glBegin(GL11.GL_QUADS);
       // Front Face
       GL11.glNormal3f( 0.0f, 0.0f, 1.0f);                 // Normal Pointing Towards Viewer
       GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f(-1.0f, -1.0f,  1.0f);    // Point 1 (Front)
       GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f( 1.0f, -1.0f,  1.0f);    // Point 2 (Front)
       GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f( 1.0f,  1.0f,  1.0f);    // Point 3 (Front)
       GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f(-1.0f,  1.0f,  1.0f);    // Point 4 (Front)
       // Back Face
       GL11.glNormal3f( 0.0f, 0.0f,-1.0f);                 // Normal Pointing Away From Viewer
       GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f(-1.0f, -1.0f, -1.0f);    // Point 1 (Back)
       GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f(-1.0f,  1.0f, -1.0f);    // Point 2 (Back)
       GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f( 1.0f,  1.0f, -1.0f);    // Point 3 (Back)
       GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f( 1.0f, -1.0f, -1.0f);    // Point 4 (Back)
       // Top Face
       GL11.glNormal3f( 0.0f, 1.0f, 0.0f);                 // Normal Pointing Up
       GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f(-1.0f,  1.0f, -1.0f);    // Point 1 (Top)
       GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f(-1.0f,  1.0f,  1.0f);    // Point 2 (Top)
       GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f( 1.0f,  1.0f,  1.0f);    // Point 3 (Top)
       GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f( 1.0f,  1.0f, -1.0f);    // Point 4 (Top)
       // Bottom Face
       GL11.glNormal3f( 0.0f,-1.0f, 0.0f);                 // Normal Pointing Down
       GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f(-1.0f, -1.0f, -1.0f);    // Point 1 (Bottom)
       GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f( 1.0f, -1.0f, -1.0f);    // Point 2 (Bottom)
       GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f( 1.0f, -1.0f,  1.0f);    // Point 3 (Bottom)
       GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f(-1.0f, -1.0f,  1.0f);    // Point 4 (Bottom)
       // Right face
       GL11.glNormal3f( 1.0f, 0.0f, 0.0f);                 // Normal Pointing Right
       GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f( 1.0f, -1.0f, -1.0f);    // Point 1 (Right)
       GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f( 1.0f,  1.0f, -1.0f);    // Point 2 (Right)
       GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f( 1.0f,  1.0f,  1.0f);    // Point 3 (Right)
       GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f( 1.0f, -1.0f,  1.0f);    // Point 4 (Right)
       // Left Face
       GL11.glNormal3f(-1.0f, 0.0f, 0.0f);                 // Normal Pointing Left
       GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f(-1.0f, -1.0f, -1.0f);    // Point 1 (Left)
       GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f(-1.0f, -1.0f,  1.0f);    // Point 2 (Left)
       GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f(-1.0f,  1.0f,  1.0f);    // Point 3 (Left)
       GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f(-1.0f,  1.0f, -1.0f);    // Point 4 (Left)
// Done Drawing Quads
       GL11.glEnd();  
 



// X Axis Rotation
       xRot += xSpeed;
// Y Axis Rotation
       yRot += ySpeed;

       return true;
   }

   /**
    * Create a window depending on whether fullscreen is selected
    * @throws Exception Throws the Window.create() exception up the stack.
    */
   private void createWindow() throws Exception {
//Setting Fullscreen or not
       Display.setFullscreen(fullscreen);
//Which Res and Depth is available?
       DisplayMode d[] = Display.getAvailableDisplayModes();
//Searching my wanted Res and Depth
       for (int i = 0; i < d.length; i++) {
           if (d[i].getWidth() == 1024
               && d[i].getHeight() == 768
               && d[i].getBitsPerPixel() == 32) {
               displayMode = d[i];
               break;
           }
       }
//Setting DM and..
       Display.setDisplayMode(displayMode);
//..title. Finally..
       Display.setTitle(windowTitle);
//..create that damn window
       Display.create();
   }

   /**
    * Do all initilization code here.  Including Keyboard and OpenGL
    * @throws Exception Passes any exceptions up to the main loop to be handled
    */
   private void init() throws Exception {
//Create the window and..
       createWindow();
//..initialize OpenGL
       initGL();
   }

   /**
    * initialise OpenGL
    *
    */
   private void initGL() {
// Enable Texture Mapping
       GL11.glEnable(GL11.GL_TEXTURE_2D);
// Enable Smooth Shading
       GL11.glShadeModel(GL11.GL_SMOOTH);
// Black Background
       GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
// Depth Buffer Setup
       GL11.glClearDepth(1.0);

// Turn Depth Test OFF
        GL11.glDisable(GL11.GL_DEPTH_TEST);
// The Type Of Depth Testing To Do
       GL11.glDepthFunc(GL11.GL_LEQUAL);

// Select The Projection Matrix
       GL11.glMatrixMode(GL11.GL_PROJECTION);
// Reset The Projection Matrix
       GL11.glLoadIdentity();

       // Calculate The Aspect Ratio Of The Window
       GLU.gluPerspective(
//Angle of the FOV (Field of View)
45.0f,
//Aspect Ratio (W/H) (Attention! It can be a DIV0-Operation)
(float) displayMode.getWidth() / (float) displayMode.getHeight(),
//Distance of the viewer to the nearest Point possible
0.1f,
//Distance of the viewer to the farest Point possible
100.0f);
// Select The Modelview Matrix
       GL11.glMatrixMode(GL11.GL_MODELVIEW);

       // Really Nice Perspective Calculations
       GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
//Enable Lighting
GL11.glEnable(GL11.GL_LIGHTING);
ByteBuffer temp = ByteBuffer.allocateDirect(16);
temp.order(ByteOrder.nativeOrder());
GL11.glLight(GL11.GL_LIGHT1, GL11.GL_AMBIENT, (FloatBuffer)temp.asFloatBuffer().put(lightAmbient).flip());              // Setup The Ambient Light
GL11.glLight(GL11.GL_LIGHT1, GL11.GL_DIFFUSE, (FloatBuffer)temp.asFloatBuffer().put(lightDiffuse).flip());              // Setup The Diffuse Light
GL11.glLight(GL11.GL_LIGHT1, GL11.GL_POSITION,(FloatBuffer)temp.asFloatBuffer().put(lightPosition).flip());  
// Enable Light One
GL11.glEnable(GL11.GL_LIGHT1);                          
// Turn Blending On
           
GL11.glEnable(GL11.GL_BLEND);
GL11.glColor4f(1.0f,1.0f,1.0f,0.2f);
   GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE);
   }

   /**
    * Cleanup all the resources.
    *
    */
   private void cleanup() {
//Imma firin` ma LAZOR!
       Display.destroy();
   }
   
   /**
    * Initialize the Texture
    */
   private void getTexture(String format, String path) throws Exception {
texture = TextureLoader.getTexture(format, ResourceLoader.getResourceAsStream(path), true);
// Binding the Textures
texture.bind();
   }

}



ThX,

Phil
Title: Re: Blending Problems
Post by: Fool Running on November 02, 2011, 12:47:28
In order to blend a texture with the object color, you need to enable texture color modulation:
GL11.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_TEXTURE_ENV_MODE, GL11.GL_MODULATE);

Hope that helps.  ;D
Title: Re: Blending Problems
Post by: Darkrider on November 02, 2011, 14:00:00
Nope, sry =( still the same -.- already 2 days that im trying to fix it!
Title: Re: Blending Problems
Post by: CodeBunny on November 02, 2011, 18:21:23
http://www.opengl.org/resources/faq/technical/lights.htm (http://www.opengl.org/resources/faq/technical/lights.htm)

Never worked with 3D lighting, but maybe this is it? Check out:

18.010 What should I know about lighting in general?
QuoteLighting does not work with the current color as set by glColor*(). It works with material colors. Set the material colors with glMaterial*().
Title: Re: Blending Problems
Post by: Darkrider on November 02, 2011, 18:42:07
I'm sorry but i need Blending, not lighting =) lightin works! thanks anyway..
Title: Re: Blending Problems
Post by: CodeBunny on November 02, 2011, 19:08:38
I took a brief look at your code and saw lighting functionality. You seem to be attempting to use glColor with lighting.

That article I pointed you to says that glColor is ignored when lighting is active.
Title: Re: Blending Problems
Post by: Darkrider on November 02, 2011, 19:23:27
yeah, sure but in (nearly) every tutorial to blending, the color4f() is used to controle the transparency..
Title: Re: Blending Problems
Post by: CodeBunny on November 02, 2011, 19:30:33
Well, of course. It's a friggin easy way to manage the main color.

But you have to deal with various optimizations with lighting, so I'd buff up on glMaterial if I were you.
Title: Re: Blending Problems
Post by: Darkrider on November 02, 2011, 19:34:55
glMaterial or glColorMaterial?
Title: Re: Blending Problems
Post by: Darkrider on November 02, 2011, 19:37:42
I looked a lil' bit these 2 instructions up and they just control the type of light... but my lighting (diffuse AND ambient) is already working fine..
Title: Re: Blending Problems
Post by: abcdef on November 02, 2011, 19:39:20
DarkRider

CodeBunny is right, when you use textures material properties supercede the color properties. To tell open gl to use the color properties instead you need to use

glEnable(GL_COLOR_MATERIAL)

You will probably need to enable it when you do your color stuff and disable it when you define your texture

Edit : its the lighting that initialises the material properties to be used but you generally use that with textures, sorry for the confusion there

Title: Re: Blending Problems
Post by: Darkrider on November 02, 2011, 19:44:27
Holy mother of god, so i got to play with colors, disable it, bind the texture and then reenable it or what? =) sry, im standing on the line =)
Title: Re: Blending Problems
Post by: CodeBunny on November 02, 2011, 22:08:17
What?

If what you're saying means
QuoteSo I have to play enable and set GL_COLOR, disable it, bind the texture and then re-enable GL_COLOR?
then, no, that's not what you have to do. You are going to have to start using something that is completely separate from glColor. You should look up some tutorials for lighting and materials.
Title: Re: Blending Problems
Post by: Darkrider on November 03, 2011, 10:38:10
okay, i understand... thanks =)