Archived

This topic is now archived and is closed to further replies.

MarauderIIC

Okay. Can't get alpha channel to work

Recommended Posts

MarauderIIC    122
Trying to add an alpha channel to a custom file format. Here''s what I have.
//generateTerrain()

	GLfloat pixelData[32][32][4];
	unsigned int r, g, b, a;

	int width = atoi(text["W"].c_str());
	int height = land.size();

	for (unsigned int h = 0;h < land.size();h++) {
		for (unsigned int w = 0;w < land.at(h).size();w++) {
	
			switch(land.at(h).at(w)) {
			case 0:
				r = 0;
				g = 0;
				b = 0;
				a = 0;
				break;
			case 1:
				r = 165;
				g = 105;
				b = 25;
				a = 255;
				break;
			case 2:
				r = 150;
				g = 150;
				b = 150;
				a = 255;
				break;
			case 6:
				r = 180;
				g = 90;
				b = 90;
				a = 255;
				break;
			case 7:
				r = 250;
				g = 130;
				b = 60;
				a = 255;
				break;
			case 8:
				r = 35;
				g = 115;
				b = 190;
				a = 255;
				break;
			case 9:
				r = 255;
				g = 0;
				b = 255;
				a = 0;
				break;
			}
			pixelData[land.size()-1-h][w][0] = (GLfloat)r/(GLfloat)255.0;
			pixelData[land.size()-1-h][w][1] = (GLfloat)g/(GLfloat)255.0;
			pixelData[land.size()-1-h][w][2] = (GLfloat)b/(GLfloat)255.0;
			pixelData[land.size()-1-h][w][3] = (GLfloat)a;

			debug("alpha for " + uitos(land.size()-1-h) + " " + uitos(w) + ": " + uitos(a));

		}
	}

	GLuint temp;
	glGenTextures(1, &temp);
	glBindTexture(GL_TEXTURE_2D, temp);
	landGen->setTexture(temp);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexImage2D(GL_TEXTURE_2D, 0, 3, width, height, 0, GL_RGBA, GL_FLOAT, pixelData);
void World::drawTerrain() {
	glLoadIdentity();

	glDisable(GL_DEPTH_TEST);						//Settings...

	glDisable(GL_LIGHTING);
	glDisable(GL_BLEND);
	glEnable(GL_TEXTURE_2D);
	//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);


	glEnable(GL_ALPHA_TEST);
	glAlphaFunc(GL_GREATER, 0.0f);

	glTranslatef(0.0, 0.0, -2.5f);
	glBindTexture(GL_TEXTURE_2D, landGen->getTexture());
	glBegin(GL_QUADS);
		glTexCoord2f(0, 0); glVertex2f(-1, -1);
		glTexCoord2f(1, 0); glVertex2f(1, -1);
		glTexCoord2f(1, 1); glVertex2f(1, 1);
		glTexCoord2f(0, 1); glVertex2f(-1, 1);
	glEnd();
	glDisable(GL_ALPHA_TEST);
}

void World::drawBackground() {

	glLoadIdentity();

	glDisable(GL_DEPTH_TEST);
	glDisable(GL_LIGHTING);
	glEnable(GL_TEXTURE_2D);
	glDisable(GL_BLEND);

	//glBlendFunc(GL_SRC_ALPHA, GL_ONE);

 
	glTranslatef(0.0f, 0.0f, -3.0f);
	glBindTexture(GL_TEXTURE_2D, textures()->getTexture(10)->getTexture());
	glBegin(GL_QUADS);
		glTexCoord2f(0, 0); glVertex2f(-1, -1);
		glTexCoord2f(1, 0); glVertex2f(1, -1);
		glTexCoord2f(1, 1); glVertex2f(1, 1);
		glTexCoord2f(0, 1); glVertex2f(-1, 1);
	glEnd();
}
And when the picture is drawn, it''s drawn in order of drawBackground() and then drawTerrain(). But none of the colors in the terrain are transparent at all.

Share this post


Link to post
Share on other sites
_DarkWIng_    602
You might wat to read glTexImage2D spec once again. Hint : there are 2 parametrs you need to change if you want alpha compared to RGB.

You should never let your fears become the boundaries of your dreams.

Share this post


Link to post
Share on other sites