[SOLVED]Rendering Semi-Transparent textures fail !

Started by Sumec99, March 23, 2014, 19:14:31

Previous topic - Next topic

Sumec99

Hi everyone,
I want to render a texture with full and semi-transparent pixels to a quad.
but it seems like everything is very dark and I can hardly see my texture ...
I've tried
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

before but that does absolutely no difference ...
Please help, thanks

Cornix

Until you show us your render code we cant help you.

A picture of the result might be helpful as well as a picture of your texture.

Sumec99

OK, i'm going to post all these three things this afternoon,
But now I haven't got the time for doing this because I  have to go to school

matanui159

Quote from: Sumec99 on March 24, 2014, 06:29:56
But now I haven't got the time for doing this because I  have to go to school
Why School...

But i think i have the same problem, i don't know until i see a picture but i have this problem where sometimes you can see through the texture and sometime you can't. It only happens in a 3D space though, not in 2D.
ALGORITHM
A word used by programmers when they do not want to explain what they did.

WEE :)

Sumec99

As I promised here is the code of my 2 classes:
First the code of the class which should draw the texture :
package the_wormhole;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.util.glu.GLU.gluPerspective;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.FloatBuffer;

import org.lwjgl.*;
import org.lwjgl.opengl.*;
import org.lwjgl.util.vector.Vector3f;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;

public class Wormhole {
	int lightEmittingEdge;
	Texture brightEdge;
	
	public Wormhole() {
		try {
			brightEdge = TextureLoader.getTexture("PNG", new FileInputStream(new File("res/bright_edge.png")));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		lightEmittingEdge = glGenLists(1);
		glNewList(lightEmittingEdge, GL_COMPILE);
		glBegin(GL_QUADS);
		glColor4f(1f, 1f, 1f, 0f);
		glTexCoord2f(0, 0);
		glNormal3f(0, 0, 1);
		glVertex3f(-1, 1, -3.620420f);
		glTexCoord2f(1, 0);
		glNormal3f(0, 0, 1);
		glVertex3f(1, 1, -3.620420f);
		glTexCoord2f(1, 1);
		glNormal3f(0, 0, 1);
		glVertex3f(1, -1, -3.620420f);
		glTexCoord2f(0, 1);
		glNormal3f(0, 0, 1);
		glVertex3f(-1, -1, -3.620420f);
		glEnd();
		glEndList();
	}

	public void draw() {
		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		brightEdge.bind();
		glCallList(lightEmittingEdge);
		glDisable(GL_BLEND);
	}

	public void cleanUp() {
		glDeleteLists(lightEmittingEdge, 1);
		brightEdge.release();
	}
}


And my main class:
package the_wormhole;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.util.glu.GLU.gluPerspective;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.FloatBuffer;

import org.lwjgl.*;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.*;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;

public class WormholeMain {
	
	private Wormhole wormhole;
	private float rotationX;
	private float translationX;
	private float translationY;
	private float translationZ;
	private float rotationY;
	private Vector3 walk;
	private FloatBuffer fb2 = BufferUtils.createFloatBuffer(4);
	
	public WormholeMain() {
		fb2.put(new float[] { -2, -2, 0, 0});
		fb2.flip();
		
		setUpDisplay();
		setUp3dWorld();
		
		walk = new Vector3(0, 0, -1);
		wormhole = new Wormhole();
		
		mainLoop();
		cleanUp();
	}
	
	private void cleanUp() {
		wormhole.cleanUp();
		Display.destroy();
	}

	private void mainLoop() {
		while(!Display.isCloseRequested()){
			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
			glClearColor(0.8f, 0.8f, 0.8f, 1);
			
			glLight(GL_LIGHT0, GL_POSITION, fb2);
			
			applyCamera();
			
			wormhole.draw();
			
			Display.update();
			Display.sync(60);
		}
	}

	private void applyCamera() {
		if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
			rotationX += 0.3f;
			glLoadIdentity();
			walk.rotate(Vector3.Y_AXIS, Math.toRadians(-0.3));
			glRotatef(rotationX, 0, 1, 0);
			glRotatef(rotationY, 1, 0, 0);
			glTranslatef(translationX, translationY, translationZ);
		}
		if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
			rotationX -= 0.3f;
			glLoadIdentity();
			walk.rotate(Vector3.Y_AXIS, Math.toRadians(0.3));
			glRotatef(rotationX, 0, 1, 0);
			glRotatef(rotationY, 1, 0, 0);
			glTranslatef(translationX, translationY, translationZ);
		}
		if (Keyboard.isKeyDown(Keyboard.KEY_UP)) {
			rotationY -= 0.3f;
			glLoadIdentity();
			walk.rotate(Vector3.X_AXIS, Math.toRadians(0.3));
			glRotatef(rotationY, 1, 0, 0);
			glRotatef(rotationX, 0, 1, 0);
			glTranslatef(translationX, translationY, translationZ);
		}
		if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
			rotationY += 0.3f;
			glLoadIdentity();
			walk.rotate(Vector3.X_AXIS, Math.toRadians(-0.3));
			glRotatef(rotationY, 1, 0, 0);
			glRotatef(rotationX, 0, 1, 0);
			glTranslatef(translationX, translationY, translationZ);
		}
		if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
			glTranslated(-0.02f * walk.x, -0.02f * walk.y, -0.02f * walk.z);
			translationX += -0.02f * walk.x;
			translationY += -0.02f * walk.y;
			translationZ += -0.02f * walk.z;
		}
		if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
			glTranslated(0.02f * walk.x, 0.02f * walk.y, 0.02f * walk.z);
			translationX += 0.02f * walk.x;
			translationY += 0.02f * walk.y;
			translationZ += 0.02f * walk.z;
		}
	}

	private void setUp3dWorld() {
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		gluPerspective(30, 800f/600f, 0.001f, 100);
		glMatrixMode(GL_MODELVIEW);
		glEnable(GL_TRIANGLE_FAN);
		glEnable(GL_DEPTH_TEST);
		glShadeModel(GL_SMOOTH);
		glEnable(GL_LIGHTING);
		glEnable(GL_LIGHT0);
		
		FloatBuffer fb1 = BufferUtils.createFloatBuffer(4);
		fb1.put(new float[] { 0.05f, 0.05f, 0.05f, 1 });
		fb1.flip();
		
		glLightModel(GL_LIGHT_MODEL_AMBIENT, fb1);
		glLight(GL_LIGHT0, GL_POSITION, fb2);
		
		glRotatef(-25f, 0, 1, 0);
		glRotatef(30f, 1, 0, 0);
		glTranslatef(-1.5f, -2.1f, -0.55f);
	}

	private void setUpDisplay() {
		try {
			Display.setDisplayMode(new DisplayMode(800, 600));
			Display.setTitle("The Wormhole");
			Display.setVSyncEnabled(true);
			Display.create();
		} catch (LWJGLException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		new WormholeMain();
	}
	
	private class Vector3 {

		public float x, y, z;

		public static final int X_AXIS = 0, Y_AXIS = 1, Z_AXIS = 2;

		public Vector3(float x, float y, float z) {
			this.z = z;
			this.y = y;
			this.x = x;
		}

		public void rotate(int axis, double angle) {
			float oldX = x, oldY = y, oldZ = z;
			switch (axis) {
			case X_AXIS:
				x = oldX;
				y = (float) (Math.cos(angle) * oldY + (-Math.sin(angle)) * oldZ);
				z = (float) (Math.sin(angle) * oldY + Math.cos(angle) * oldZ);
				break;
			case Y_AXIS:
				x = (float) (Math.cos(angle) * oldX + Math.sin(angle) * oldZ);
				y = oldY;
				z = (float) ((-Math.sin(angle)) * oldX + Math.cos(angle) * oldZ);
				break;
			case Z_AXIS:
				x = (float) (Math.cos(angle) * oldX + (-Math.sin(angle)) * oldY);
				y = (float) (Math.sin(angle) * oldX + Math.cos(angle) * oldY);
				z = oldZ;
				break;
			default:
				break;
			}
		}
	}
}


And finally my texture:
https://docs.google.com/folderview?id=0BwDCCGhLg3mEWGZqbzV2OE9sQkU&usp=docslist_api

Cornix

You do realize that you render with the color:
glColor4f(1f, 1f, 1f, 0f)
which has a 0 in the alpha component?

Maybe this is the reason for your problem.

Sumec99

Yes , I knew that, i thought a fully transparent color would let opengl show the image i changed it to red (1, 0, 0, 1) but then my rectangle is red, it still doesn't render the texture

Cornix


Sumec99

No they aren't ... suchh a stupid mistake of mine ... now it works ...
the matter is i have had them enabled and it didn't work, but now it works ..
it's kinda funny,
ok thanks for your help !!