OpenGL glTexImage2D with raw bitmap data

Post Reply
Lrios4403
Posts: 6
Joined: Fri May 05, 2017 9:38 pm

OpenGL glTexImage2D with raw bitmap data

Post by Lrios4403 » Tue Dec 24, 2019 12:17 am

As the name states, I am reading a bitmap file from the ROM directory and loading it into the ram. I followed https://solarianprogrammer.com/2018/11/ ... mp-images/ on how to read the bitmap data. I saved the bitmap data with paint.net 128x128 in dimensions, therefore the format is GL_BGRA? After the file is read it's stored in a memory address as a char* array. I followed everything under the nehe lesson 7 but my cube when rendered still comes up completely black. Am I doing something wrong? There are a few points in my code that I'm worried about and I was wondering if someone with more opengl experience could point something out. I have the entire code repos https://github.com/Lrios4403/Christmas-Apartments, however there are a few snippets of code that I think may be causing the problem:

screen.h:

Code: Select all

int screenInitalize()
{
	
	iprintf("Initializing the screen...\n");
	
	//Setup the top screen
	iprintf("	Setting main-top...\n");
	lcdMainOnTop();
	iprintf("	Setting video modes...\n");
	videoSetMode(MODE_0_3D);
	vramSetBankA(VRAM_A_TEXTURE);                        //NEW  must set up some memory for textures
	glInit();
	
	iprintf("	Initializing Opengl...\n");
	
	glEnable(GL_ANTIALIAS);				// enable textures
	glEnable(GL_TEXTURE_2D);
	glEnable(GL_ANTIALIAS);				// enable antialiasing
	
	glClearColor(32,16,8,31);
	glClearPolyID(63); 
	glClearDepth(0x7FFF);
	glViewport(0,0,255,191);
	
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(70, 256.0 / 192.0, 0.1, 100);
	
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(70, 256.0 / 192.0, 0.1, 100);
	
	//set up a directional light arguments are light number (0-3), light color, 
	//and an x,y,z vector that points in the direction of the light, the direction must be normalized
	glLight(0, RGB15(31,31,31) , 0, floattov10(-1.0), 0);
	
	//need to set up some material properties since DS does not have them set by default
	glMaterialf(GL_AMBIENT, RGB15(8,8,8));
	glMaterialf(GL_DIFFUSE, RGB15(8,8,8));
	glMaterialf(GL_SPECULAR, BIT(15) | RGB15(8,8,8));
	glMaterialf(GL_EMISSION, RGB15(16,16,16));
	
	//ds uses a table for shinyness..this generates a half-ass one
	glMaterialShinyness();
	
	// Set the current matrix to be the model matrix
	glMatrixMode(GL_MODELVIEW);

	return 0;
}
object.h:

Code: Select all

int textureLoadFromDir(char* filedir)
{
	printf("Loading texture: %s", filedir);
	int textId = 0;
	struct bmpdata bdata = {};
	
	//Load the file data
	bmpLoadFromDir(&bdata, filedir);
	
	if(glGenTextures(1, &textId) == 0)
	{
		printf("ERROR: failed to gen!\n");
		return -1;
	}
	
	glBindTexture(0, textId);
	
	if( glTexImage2D(0, 0, GL_RGBA, TEXTURE_SIZE_128 , TEXTURE_SIZE_128, 0, TEXGEN_TEXCOORD, bdata.data) == 0)
	{
		printf("ERROR: failed to load!\n");
		return -1;
	}
	
	//Resize the texure buffer
	texturesSize++;
	struct texture* p = (struct texture*)realloc(textures, texturesSize*sizeof(texture));
	delete[] textures;
	textures = p;
	
	textures[texturesSize-1].path = filedir;
	textures[texturesSize-1].id = textId;
	
	//Cleanup
	delete[] bdata.data;
	delete &bdata;
	
	return textId;
}
render:

Code: Select all

int renderMap(struct map* m)
{
	if(m == 0) return -1;
	
	glPolyFmt(POLY_ALPHA(31) | POLY_CULL_NONE);			//ds specific, several attributes can be set here	
	glMatrixMode(GL_MODELVIEW);							// Set the current matrix to be the model matrix
	glPushMatrix();										//Push our original Matrix onto the stack (save state)

	glLoadIdentity();									// Reset The Current Modelview Matrix
	glTranslatef(vCamera.x,vCamera.y,vCamera.z);		// Move Left 1.5 Units And Into The Screen 6.0

	glBegin(GL_TRIANGLES);								// Drawing Using Triangles
	for(int i = 0; i < sizeof(m->objects); i++)
	{
		glTranslatef									// Move Left 1.5 Units And Into The Screen 6.0
		(
			m->objects[i].position.x,
			m->objects[i].position.y,
			m->objects[i].position.z
		);						
		
		glRotatef(m->objects[i].rotation.x, 1.0f, 0.0f, 0.0f);
		glRotatef(m->objects[i].rotation.y, 0.0f, 1.0f, 0.0f);
		glRotatef(m->objects[i].rotation.z, 0.0f, 0.0f, 1.0f);
	 
		for(int t = 0; t < 12; t++)
		{
			struct triangle* tri = &m->objects[i].triangles[t];
			
			if(m->objects[i].texture!=0)
			{
				glBindTexture(GL_TEXTURE_2D, m->objects[i].texture);
				glTexCoord2f(tri->t[0].x, tri->t[0].y); glVertex3f(tri->p[0].x, tri->p[0].y, tri->p[0].z);
				glTexCoord2f(tri->t[1].x, tri->t[1].y); glVertex3f(tri->p[1].x, tri->p[1].y, tri->p[1].z);
				glTexCoord2f(tri->t[2].x, tri->t[2].y); glVertex3f(tri->p[2].x, tri->p[2].y, tri->p[2].z);
			}
			else
			{
				glColor3f(tri->c[0].x, tri->c[0].y, tri->c[0].z);
				glVertex3f(tri->p[0].x, tri->p[0].y, tri->p[0].z);					// Top
				glColor3f(tri->c[1].x, tri->c[1].y, tri->c[1].z);
				glVertex3f(tri->p[1].x, tri->p[1].y, tri->p[1].z);					// Bottom Left
				glColor3f(tri->c[2].x, tri->c[2].y, tri->c[2].z);
				glVertex3f(tri->p[2].x, tri->p[2].y, tri->p[2].z);					// Bottom Right
			}
		}
	}
	
	glPopMatrix(1);										// Pop our Matrix from the stack (restore state)
	
	return 0;
}

Lrios4403
Posts: 6
Joined: Fri May 05, 2017 9:38 pm

Opengl glTexImage2D with raw bitmap data

Post by Lrios4403 » Tue Dec 24, 2019 2:50 am

Hello,

I am trying to load raw bitmap data from the ROM directory into a texture. The bitmap is saved by paint.net and the bitmap reader follows https://solarianprogrammer.com/2018/11/ ... mp-images/, yet the textured cube still shows black. What is the correct way to Load a texture with raw bitmap data. This is what I have already:

Code: Select all

struct texture
{
	int id;
	char* path;
};

struct texture* textures;
long texturesSize = 0;

int textureLoadFromDir(char* filedir)
{
	printf("Loading texture: %s", filedir);
	int textId = 0;
	struct bmpdata bdata = {};
	
	//Load the file data
	bmpLoadFromDir(&bdata, filedir);
	
	if(glGenTextures(1, &textId) == 0)
	{
		printf("ERROR: failed to gen!\n");
		return -1;
	}
	
	glBindTexture(0, textId);
	
	if( glTexImage2D(0, 0, GL_RGBA, TEXTURE_SIZE_128 , TEXTURE_SIZE_128, 0, TEXGEN_TEXCOORD, bdata.data) == 0)
	{
		printf("ERROR: failed to load!\n");
		return -1;
	}
	
	//Resize the texure buffer
	texturesSize++;
	struct texture* p = (struct texture*)realloc(textures, texturesSize*sizeof(texture));
	delete[] textures;
	textures = p;
	
	textures[texturesSize-1].path = filedir;
	textures[texturesSize-1].id = textId;
	
	//Cleanup
	delete[] bdata.data;
	delete &bdata;
	
	return textId;
}
	

elhobbs
Posts: 358
Joined: Thu Jul 02, 2009 1:19 pm

Re: OpenGL glTexImage2D with raw bitmap data

Post by elhobbs » Tue Dec 24, 2019 9:11 pm

raw bitmap data is not the same as a bitmap file (.bmp file). a .bmp file contains header information about the image data and the format. raw bitmap data does not include a header - and is literally just raw data. It is unclear which format you are using - you say raw bitmap data, but referenced code for loading a bmp file.

Lrios4403
Posts: 6
Joined: Fri May 05, 2017 9:38 pm

Re: OpenGL glTexImage2D with raw bitmap data

Post by Lrios4403 » Wed Dec 25, 2019 10:59 pm

elhobbs wrote: Tue Dec 24, 2019 9:11 pm raw bitmap data is not the same as a bitmap file (.bmp file). a .bmp file contains header information about the image data and the format. raw bitmap data does not include a header - and is literally just raw data. It is unclear which format you are using - you say raw bitmap data, but referenced code for loading a bmp file.
I am trying to extract the data. I read the header to determine what data i need to load and where. The bitmap is saved in a 16 bit format. The data should theoretically be the same as a pcx file data.

Sorry for the repeated question. I submitted two thinking one was too long

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest