Problem with translucent textures

Post Reply
flure
Posts: 16
Joined: Sat Feb 21, 2009 11:51 pm

Problem with translucent textures

Post by flure » Thu May 28, 2009 12:24 pm

Hello, I've searched in the forum and found nothing about my current problem : translucent textures.
Well I've some solid polys with no texture, and some others with a translucent (GL_RGB8_A5) texture. But when rendering something appears for a fraction of a second and then nothing is shown at all.
I've reduced my code to as little I could so I can show it here :

Code: Select all

#include <nds.h>
#include <malloc.h>

u8* TranslucentTex;
int TranslucentTexId;
u16 TranslucentTexPal[8];
int TranslucentTexPalAddr;

void CreateTranslucentTexture(void)
{
	int x, y;
	TranslucentTex = (u8*)malloc(32*32);
	for(y=0; y<32; y++) {
		for(x=0; x<32; x++) {
			TranslucentTex[x + y*32] = ((31-x)<<3) | (x&y);
		}
	}
	
	for(x=0; x<8; x++) {
		TranslucentTexPal[x] = RGB15(x<<2, x<<1, x<<3);
	}
	
	glEnable(GL_TEXTURE_2D);
	glGenTextures(1, &TranslucentTexId);
	glBindTexture(GL_TEXTURE_2D, TranslucentTexId);
	glTexImage2D(0, 0, GL_RGB8_A5 , TEXTURE_SIZE_32, TEXTURE_SIZE_32, 0, TEXGEN_TEXCOORD, TranslucentTex);
	TranslucentTexPalAddr = gluTexLoadPal(TranslucentTexPal, 8, GL_RGB8_A5);
	free(TranslucentTex);
}

void Init3D()
{
	videoSetMode(MODE_5_3D);
	vramSetBankA(VRAM_A_MAIN_BG_0x06000000);
	vramSetBankB(VRAM_B_TEXTURE_SLOT3);
	vramSetBankC(VRAM_C_LCD);
	vramSetBankD(VRAM_D_LCD);
	vramSetBankE(VRAM_E_TEX_PALETTE);
	
	glInit();
	glViewport(0, 0, 255, 191);
	glClearColor(0, 0, 0, 0);
	glClearDepth(GL_MAX_DEPTH);
	glClearPolyID(63);
	glEnable(GL_ANTIALIAS);
    glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(70, 256.0 / 192.0, 0.1, 100);
	glMatrixMode(GL_MODELVIEW);
    
    CreateTranslucentTexture();
}

void Draw3D(int theta)
{
	glLoadIdentity();
	glTranslatef(0, 0, -3);
	glRotatef32i(theta, 1<<12, 1<<12, 1<<12);
	
	glEnable(GL_BLEND);
	
	glPolyFmt(POLY_ID(1) | POLY_ALPHA(31) | POLY_CULL_NONE);
	glDisable(GL_TEXTURE_2D);
	glBegin(GL_QUAD);
		glColor(RGB15(31, 31, 31));
		glVertex3f(-1, 1, -1);
		glVertex3f(-1,-1, -1);
		glVertex3f( 1,-1, -1);
		glVertex3f( 1, 1, -1);
	glEnd();
	
	glPolyFmt(POLY_ID(2) | POLY_ALPHA(31) | POLY_CULL_NONE);
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, TranslucentTexId);
	glColorTable(GL_RGB8_A5, TranslucentTexPalAddr);
	glBegin(GL_QUAD);
		glTexCoord2t16(0, 0);
		glVertex3f(-1, 1, 0);
		
		glTexCoord2t16(0, 0);
		glVertex3f(-1,-1, 0);
		
		glTexCoord2t16(1, 0);
		glVertex3f( 1,-1, 0);
		
		glTexCoord2t16(1, 0);
		glVertex3f( 1, 1, 0);
	glEnd();
	
	glFlush(0);
}

int main() 
{   
	int theta = 0;
	Init3D();
	
	while(1) {
		Draw3D(theta<<4);
		theta++;
	}
	
    return 0;
}
Has anyone ever experienced something like my problem ? Or is there a mistake, or something missing in my code ?

I would greatly appreciate any help, thanks a lot.

flure
Posts: 16
Joined: Sat Feb 21, 2009 11:51 pm

Re: Problem with translucent textures

Post by flure » Tue Oct 13, 2009 6:21 pm

Nobody has an idea ?
I'm still stuck with that, forced to use 16bpp textures where paletted ones would do the job in a more efficient way :(

Discostew
Posts: 103
Joined: Sun Mar 08, 2009 7:24 pm

Re: Problem with translucent textures

Post by Discostew » Tue Oct 13, 2009 6:52 pm

I'll take a detailed look at it when I get back from school today.

Discostew
Posts: 103
Joined: Sun Mar 08, 2009 7:24 pm

Re: Problem with translucent textures

Post by Discostew » Wed Oct 14, 2009 3:32 am

Ok, I think I have it.

glEnable/glDisable : The effect these give is on a per-frame basis, meaning something will either be on for the whole frame for everything, or not. If your project uses any textures, you enable it, and leave it. If you don't want a polygon to have a texture on it, you do it via glBindTexture.

glBindTexture : You got it right for the most part, but I'm pretty sure the first argument for it is there for compatibility purposes with code straight from actual OpenGL. With libNDS, it does nothing, so setting 0 is what's done. The 2nd argument you used correctly when you wanted a texture used, but when you don't want any used on polys that follow, simply use 0.

When you used the function for texture coordinates, it looks like you assumed it took floating point values, but it doesn't. You also didn't finish up with choosing the 4 corners of it also.

Another thing, which I'm not quite sure about but works, is that I switched from VRAM_B_TEXTURE_SLOT3 to just VRAM_B_TEXTURE, which fixed the texture problem of it not showing up. It could have been some conflicting problem with VRAM_D, as SLOT3 would be considered the spot used by that.

This code following is the edited version of yours that worked for me.

Code: Select all

#include <nds.h>
#include <malloc.h>

u8* TranslucentTex;
int TranslucentTexId;
u16 TranslucentTexPal[8];
int TranslucentTexPalAddr;

void CreateTranslucentTexture(void)
{
	int x, y;
	TranslucentTex = (u8*)malloc(32*32);
	for(y=0; y<32; y++) {
		for(x=0; x<32; x++) {
			TranslucentTex[x + y*32] = ((31-x)<<3) | (x&y);
		}
	}
	
	for(x=0; x<8; x++) {
		TranslucentTexPal[x] = RGB15(x<<2, x<<1, x<<3);
	}
	
	glGenTextures(1, &TranslucentTexId);
	glBindTexture(0, TranslucentTexId);
	glTexImage2D(0, 0, GL_RGB8_A5 , TEXTURE_SIZE_32, TEXTURE_SIZE_32, 0, TEXGEN_TEXCOORD, TranslucentTex);
	TranslucentTexPalAddr = gluTexLoadPal(TranslucentTexPal, 8, GL_RGB8_A5);
	free(TranslucentTex);
}

void Init3D()
{
	videoSetMode(MODE_5_3D);
	vramSetBankA(VRAM_A_MAIN_BG_0x06000000);
	vramSetBankB(VRAM_B_TEXTURE);
	vramSetBankC(VRAM_C_LCD);
	vramSetBankD(VRAM_D_LCD);
	vramSetBankE(VRAM_E_TEX_PALETTE);
	
	glInit();
	glEnable( GL_TEXTURE_2D | GL_BLEND | GL_ANTIALIAS );
	glViewport(0, 0, 255, 191);
	glClearColor(0, 0, 0, 31);
	glClearDepth(GL_MAX_DEPTH);
	glClearPolyID(63);
	
    glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(70, 256.0 / 192.0, 0.1, 100);
	glMatrixMode(GL_MODELVIEW);
    
    CreateTranslucentTexture();
}

void Draw3D(int theta)
{
	glLoadIdentity();
	glTranslatef(0, 0, -3);
	glRotatef32i(theta, 1<<12, 1<<12, 1<<12);
	
	glPolyFmt(POLY_ID(1) | POLY_ALPHA(31) | POLY_CULL_NONE);
	glBindTexture(0, 0);
	glBegin(GL_QUAD);
		glColor(RGB15(31, 31, 31));
		glVertex3f(-1, 1, -1);
		glVertex3f(-1,-1, -1);
		glVertex3f( 1,-1, -1);
		glVertex3f( 1, 1, -1);
	glEnd();
	
	glPolyFmt(POLY_ID(2) | POLY_ALPHA(31) | POLY_CULL_NONE);
	glBindTexture(0, TranslucentTexId);
	glColorTable(GL_RGB8_A5, TranslucentTexPalAddr);
	glBegin(GL_QUAD);
		glTexCoord2t16(0, inttot16(32));
		glVertex3f(-1, 1, 0);
		
		glTexCoord2t16(inttot16(32), inttot16(32));
		glVertex3f(-1,-1, 0);
		
		glTexCoord2t16(inttot16(32), 0);
		glVertex3f( 1,-1, 0);
		
		glTexCoord2t16(0, 0);
		glVertex3f( 1, 1, 0);
	glEnd();
	
	glFlush(0);
}

int main() 
{   
	int theta = 0;
	Init3D();
	
	while(1) {
		Draw3D(theta<<4);
		theta++;
	}
	
    return 0;
}


flure
Posts: 16
Joined: Sat Feb 21, 2009 11:51 pm

Re: Problem with translucent textures

Post by flure » Wed Oct 14, 2009 4:47 pm

Oh thanks a lot !
And I feel really stupid now that you pointed out that I didn't understand the way texture coordinates work ;)

Thanks again Discotew

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 4 guests