First time texture: doesn't work ( .raw ).


This is the first time I trie to work with textures but for some reason nothing happens, no errors, but no results neither.

#include <windows.h>    
#include <gl/glut.h>
#include <iostream.h>
#include <stdio.h>
//#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")

int frame=0;
int time;
int timebase=0;
int fps;
char s[30];
GLuint	texture[1];

void render(void);	
void keyboard(unsigned char key, int x, int y);
void special_keys(int a_keys, int x, int y);
void renderBitmapString(float x, float y, void *font,char *string);
void reshape(int width, int height);
void idle();
bool load_texture ( char *file_name, int width, int height, int depth, GLenum colour_type, GLenum filter_type );
void init();

int main(int argc, char** argv)
	glutInit(&argc, argv);                           
	glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); 

	glutCreateWindow("DzSpace Invaders");




	return 0;

void render(void)   

	glBindTexture(GL_TEXTURE_2D, texture[1]);
		glTexCoord2f(0.0f, 1.0f);glVertex2f(304, 64);					
		glTexCoord2f(1.0f, 1.0f);glVertex2f(336, 64);					
		glTexCoord2f(1.0f, 0.0f);glVertex2f(336, 32);					
		glTexCoord2f(0.0f, 0.0f);glVertex2f(304, 32);					

  renderBitmapString(530, 465, GLUT_BITMAP_8_BY_13,"ESC for exit");

	if (time - timebase > 1000) {
		fps = frame*1000.0/(time-timebase);
	 	timebase = time;		
		frame = 0;
	sprintf(s,"FPS: %i", fps);
	renderBitmapString(530, 450, GLUT_BITMAP_8_BY_13,s);
    glutSwapBuffers ( );

void init() {
	glEnable ( GL_TEXTURE_2D );

/*	if(!load_texture ( "ship_32x32.raw", 32, 32, 3, GL_RGB, GL_LINEAR))
		cout << "Error while loading textures" << endl;

bool load_texture ( char *file_name, int width, int height, int depth,
                    GLenum colour_type, GLenum filter_type )
   GLubyte *raw_bitmap ;
   FILE *file;

   if ((file = fopen(file_name, "rb"))==NULL )
      printf ( "File Not Found : %s
", file_name );
      exit   ( 1 );

   raw_bitmap = (GLubyte *) malloc ( width * height * depth * ( sizeof(GLubyte)) );

   if (raw_bitmap == NULL)
      printf ( "Cannot allocate memory for texture
" );
      fclose ( file);
      exit   ( 1 );

   fread  ( raw_bitmap , width * height * depth, 1 , file );
   fclose ( file);

   glGenTextures(1, &texture[0]);
   glBindTexture(GL_TEXTURE_2D, texture[0]);

	//  Set Filtering type
   glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter_type );
   glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter_type );

   //  Build Mipmaps
   gluBuild2DMipmaps ( GL_TEXTURE_2D, colour_type, width, height,
                       colour_type, GL_UNSIGNED_BYTE, raw_bitmap );

   //  Free up the array
   free ( raw_bitmap );
   return true;
  • I’v cut out some parts.
  • I know my fps counter doesn’t work ( I just don’t know why… :smiley: )
  • I know this will only work if I only load one texture.

Could somebody plzz tell my why this doesn’t work ?

You should uncomment the call to load_texture :smiley:

You created texture[0] when loading the texture but you use texture[1] when rendering. :smiley:

i don’t think int fps is a good idea. you should use float type to avoid rounding errors- or give the compiler some hints:

fps = (int)(1000.*(float)frame/(float)(time-timebase));

by the way, i’m not absolutely sure if ‘>’ has a higher priority than ‘-’, but i’d replace

if (time - timebase > 1000)


if ( (time - timebase) > 1000)

the accuracy may be better if you don’t check for a certain time interval but rather for a certain number of frames:

if(frame == 1000)

if the prog draws 3 frames within a second and almost finished the 4th, you’ll get only 3 fps. that effect is inly significant at low frame rates, of course.

Oh, hehe shame on me

I’v uncommented and changed index… now i get the strangest effects

The texture itself is kind of mosaic of red green and blue… And its like the brightness is turned half… the text that used to be white is now alsmost unreadable grey and the texture itself isn’t to bright neither.

On the fps counter: I changed it like you’v said to “if(frame == 1000)” but it still says my fps are zero whole the time…

well, maybe (frame == 1000) is just a bit too high.
if you have 50 fps, it will take 20 seconds until
fps is updated. try (frame == 50) or even less- if
the number is too high, you will not see frame rate
drops which may occur only once in a second.

	if(frame == 50) {
		fps = 1.0f / (((float)time-timebase) / 1000.0f);
	 	timebase = time;		
		frame = 0;

This it at the end of my render function and it still says my fps = 0 all the time.
Why doens’t it want to work ?
Btw… no idea’s why the texture doesn’t work neither ?

What’s the purpose of this thread: texture or framerate calculation ?

Primary: texture

But one guy started about the frames so why can’t we have it both :wink:
Or isn’t that allowed for some reason ?

This is not forbidden but that allows threads not to confuse people.

And what about your textures ? :cool:

have you changed the fps type to float or is it
still int? you should really change it to float,
and you should also change “FPS : %i” to
“FPS : %2.1f” to avoid round-off errors and to
see if the fps value is exactly zero or
only something like 0.99 which would be set to
zero if you use an integer for the fps.

by the way, you have to multiply the value with
50 because 50 frames are drawn in the time interval.

hmm…and by the way: YOU were the guy who started
talking about fps :smiley:

I’v changed everything like you said but I still get fps = 0.000… . I’m getting despared ( just don’t know how to write it, i’m not english… : D)

And textures: I get a strange mix of red blue end green colours instead of my texture… its kind of hard to get so i’v uploaded a screen if you’d be so kind to look… I’v also placed my texture, the version i use is the .raw one ( because i coudln’t load a bmp file :s ). Don’t laugh at it, its my self-drawn spaceship :smiley: .
… maybe its the same reason as why my text is so dark while it’s set to pure white…

( link: )

Isn’t that your loading process that is wrong ? I’m sure it comes from their. Also ensure the enpack alignements matches what you loaded, that’s often a source of error being able to produce such kind of error.

Hope that helps go out of despair :slight_smile:

Your crazy looking texture could be due to an error when the image is being packed from system memory into texture memory. Did you change GL_PACK_ALIGNMENT (with glPixelStore*, I believe?)

Additionally, are you sure the image file has 3 channels and not 4?

I think the reason you’re getting 0 FPS displayed is because your calculation is effectively the difference of two numbers &lt&lt 1000 (basically it looks like you’re just subtracting the time it took for one frame and subtracting the time it took for the frame that was fifty frames ago) divided by 1000, which is going to be a very small fraction. You could try extending the precision of your sprintf field to see if anything shows up. But that won’t be your true FPS value, clearly.

… i’m pretty sure it has 3 channels, altough i can’t view it anymore in photoshop when i enter “32 height, 32 width and 3 channels” but neither when i tell ps it has 4 channels…

I enlarged the precision and still got 0.0000000, i tried to divide by 10.0f, still got 0.0000, still in despair :wink:

The load function than… the problem is i was seeking for one that works for 2 days after i tried to write my own for 1 day, never worked… this one gave no error while loading so i thought i finaly found a good one…
( to bad it was one for .raw, i’d liked a more suitable format more but… i take what i find :wink: )


well, change the sprintf so that frame, time and
timebase are displayed, too. just to be sure that
all values are increasing like they should.

oh, and by the way, you didn’t post your idle func,
but i hope it does call the render function?

Oh well… I think you have the right to kill me now…( that mean’s it didn’t call for the render function !! :s stupid nooby me :stuck_out_tongue: ,… and i understand why it wouldn’t work than so no need to explain :wink: ).

Now I have 79.6 frames almost whole the time… but you said I have to multiply by 50 ? So in fact i have 4000fps?

And hasn’t someone got a clue about my wierd textures ?

I changed it to this:

if( frame == 50) {
		fps = (1.0f / (((float)time-timebase) / 1000.0f)) * 50.0;

( thx alot man, i just found out about rating members… :wink: )

thank you! :stuck_out_tongue:

now finishing the fps : what you want to know is
the number of frames drawn per second, which is

number_of_frames / elapsed_time

the number of frames is 50 in this example; the
elapsed time is (time-timebase)/1000, because time
and timebase are measured in milliseconds. so it
seems that you really have 4000 fps. you have to
note that the fps you calculate is an average for
50 frames. this means that if there is a drop in
the fps every 50 frames, you may not see it.

now to your texture: the raw file has a size of
4096 bytes, which is 32x32x4, so you seem to have
an alpha channel. you could convert the raw file,
but a very simple way to solve the problem is
calling the load_texture function with depth=4
and GL_RGBA instead of GL_RGB.

good luck further on!

Hmmz man, were you born brilliant or what :wink:

Now my text is gone but I gues thats something with lightning or so, i’m gonna google for it :slight_smile: .

I’m really happy now my fps-counter works, it’s a dream coming true ( joking about the last thing of course :wink: ).
( i have an amd 64bit 3200+, 1gig pc3200 ram and an x800pro gpu… how realistic is it i have sometimes 5000fps when all the program does is drawing a white 32*32 square and texturing it ? )

My texture was upside-down but i just rotated it with photoshop because i didn’t want to look for another error.

Last thing I risk to bore you with … if I press F1 ( my fullscreen switch-button ) my texture is gone and my text is back… i mean its like it used to be withouth texturing… i tried to call the render function too when i go fullscreen but it doesn’t help… what do i have to do to get everything working when people switch modes ? ( if i start fullscreen and not windowed its the same thing but then when they go windowed… i guess i have to redo something but i dont’t know what ).

hm…do you glDisable(GL_TEXTURE_2D) before drawing
the text? generally you should disable everything
you can- light, texture- before drawing text.

concerning the fullscreen switch- i’m not too
familiar with glut, but maybe you have to rebuild
the complete opengl state (colors, lights etc.)
after switching. maybe you have to build your
textures again.