#### Archived

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

# Need help -- Textures in a Seperate File

This topic is 5722 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, sorry to bother you, but I was wondering if I could get some help. --By the way, I love NeHe''s Site-- I am a student at tvi.edu and learning base information towards OpenGL programing in C++. The teacher taught us to seperate our Code into 3+ files. Yet NeHe''s tutorials are based in 1 file. I''ve worked on taking his Tutorial #1 and #10 and split them into seperate files. http://w3.tvi.edu/~scinutz/Field.zip What I''m trying to do is add Textures into this program and seperate the BMP''s Loading into anopther File. then call for that texture in a glNewList. Could someone help me on textures. I thought I had removed the coding correctly on a different file, but kept getting an error towards AUX and Linking Errors on the .exe file. If you could help in anyway, even if its from Tutorial #6 and placed into another file, I would be greatful. Learning the basics is always the problem. Only an fool would program and animate. I''''m just a Simple Fool who''''s learning all he can. -- SciNutz

##### Share on other sites
I think this is the 10th time I am posting this code:
It is limited only to uncompressed, 8 bpp BMP files, but it works quite well.
If you plan to use it in some project, please give me some credit

//load a bmp texture, with the specified global alphaGLuint load_bmp8_fixed_alpha(char * FileName, Uint8 a){	int i,x,y,x_padding,x_size,y_size,colors_no,r,g,b,current_pallete_entry;	Uint8 * file_mem;	Uint8 * file_mem_start;	Uint8 * texture_mem;	Uint8 * read_buffer;	Uint8 * color_pallete;	FILE *f = NULL;	int file_lenght;	GLuint texture;  	f = fopen (FileName, "rb");  	if (!f) return;  	file_mem = (Uint8 *) calloc ( 20000, sizeof(Uint8));  	file_mem_start=file_mem;  	fread (file_mem, 1, 50, f);//header only  	//now, check to see if our bmp file is indeed a bmp file, and if it is 8 bits, uncompressed  	if(*((short *) file_mem)!=19778)//BM (the identifier)  	{		free(file_mem_start);		fclose (f);		return 0;	}	file_mem+=18;	x_size=*((int *) file_mem);	file_mem+=4;	y_size=*((int *) file_mem);	file_mem+=6;	if(*((short *)file_mem)!=8)//8 bit/pixel?	  {		free(file_mem_start);		fclose (f);		return 0;	  }	file_mem+=2;	if(*((int *)file_mem)!=0)//any compression?	  {		free(file_mem_start);		fclose (f);		return 0;	  }	  file_mem+=16;	  colors_no=*((int *)file_mem);	  if(!colors_no)colors_no=256;	  file_mem+=8;//here comes the pallete	  color_pallete=file_mem+4;	  fread (file_mem, 1, colors_no*4+4, f);//header only	  file_mem+=colors_no*4;	x_padding=x_size%4;	if(x_padding)x_padding=4-x_padding;	//now, allocate the memory for the file	texture_mem = (Uint8 *) calloc ( x_size*y_size*4, sizeof(Uint8));	read_buffer = (Uint8 *) calloc ( 2000, sizeof(Uint8));	for(y=0;y		{			//fread (texture_mem+y*x_size, 1, x_size-x_padding, f);			fread (read_buffer, 1, x_size-x_padding, f);			for(x=0;x			{				current_pallete_entry=*(read_buffer+x);				b=*(color_pallete+current_pallete_entry*4);				g=*(color_pallete+current_pallete_entry*4+1);				r=*(color_pallete+current_pallete_entry*4+2);				*(texture_mem+(y*x_size+x)*4)=r;				*(texture_mem+(y*x_size+x)*4+1)=g;				*(texture_mem+(y*x_size+x)*4+2)=b;				*(texture_mem+(y*x_size+x)*4+3)=a;			}		}	free(file_mem_start);	free(read_buffer);	fclose (f);//ok, now, hopefully, the file is loaded and converted...//so, assign the texture, and such	glGenTextures(1, &texture);	glBindTexture(GL_TEXTURE_2D, texture);	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);	glTexImage2D(GL_TEXTURE_2D,		     0,		     GL_RGBA,		     x_size, y_size,		     0,		     GL_RGBA,		     GL_UNSIGNED_BYTE,		     texture_mem);	free(texture_mem);	return texture;}

##### Share on other sites

Thank you for that Code, but having // documentation would be helpful. If you can help clarify how the files are inputed, what do I do if I want to do more then 1 texture.

What is the 8bpp meaning, and are there other formats? why 8bpp?

I''m New to textures, and slightly new to OpenGL. So, Half the coding is slightly Unknown, but I''ve learned that some of the code I dont need to fully understand, just accept.

If you could also look at my Field.Zip files that I Zipped and placed on my School site to see how I''m programming currently.

Only an fool would program and animate. I''''m just a Simple Fool who''''s learning all he can. -- SciNutz

##### Share on other sites

I have read through Tutorials #6 - #8, but his code works with 1 file system. I want to break up that 1 file into multiple so that it would be easier to read and find.

With-in the Files.zip, I have broken his Tutorial #1 into 3 files. Drive (*His main file*), Render (*Which has InitGL, and DrawGLScene*), and a Functions.h file which directs the initGL and DrawGLScene functions. I also have a Field.cpp file which has glNewList and glCallList so I can Design the Field that I wish to create seperate from the rest of the code.

The Problem is, that I''ve had trouble Seperating the Texture lines from his Tutorial #6 - #8 into a seperate file. Plus, if I left it in the main file, I had got a Linking Error and something towards the AUX part.

The Code Above might help, if I could understand the Basics, but I''m still stumped towards it.

Thanks again for reading this, and hope to hear from anyone.

Only an fool would program and animate. I''''m just a Simple Fool who''''s learning all he can. -- SciNutz

##### Share on other sites
Ok I looked at the stuff you made and It is just weird that You even want to do this, I can''t see why you don''t just leave it together, The only time I seperate my files is when I make a kind of "SUBSYSTEM" that runs independently of the rest of my application. That and I run my program in my own file with the OpenGL Setup in a setup file. Must of the time you shouldn''t split the things that you are doing up like what you are doing It has no benefits as far as I can see, but If you want to split them i suggest these Splits,

Helper.h Helper.cpp - Holds things like texture stuff and other commonly used functions and operations
Main.h Main.cpp - most of your main program
Setup.h Setup.cpp - OpenGL setup (Normally this is very general and can be used over and over again)

you don''t want them to all link to each other either

you want Main to link to Helper and To Setup but they don''t link back or to each other. You can link them backwards but I makes it less usefull if you wanted to use the code again. Thats just what I would do if I were to split it into 3 parts. I also don''t recemend useing .h''s and .cpp''s that aren''t assosiated with correct names such as your functions.h although perfecally ok, for organization its better to name it like this Main.cpp and Main.h with .h having the function prototypes for Main.cpp.

I hope that kinda clears a little up for you I would say more but Im running short on time, good luck.

1. 1
Rutin
25
2. 2
3. 3
JoeJ
19
4. 4
5. 5

• 10
• 11
• 9
• 9
• 10
• ### Forum Statistics

• Total Topics
631753
• Total Posts
3002098
×