Page 1 of 3 123 LastLast
Results 1 to 10 of 21

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

  1. #1
    Junior Member Newbie
    Join Date
    Feb 2005
    Posts
    11

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

    Hi,

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

    Code :
    #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(&amp;argc, argv);                           
    	glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); 
     
    	glutInitWindowPosition(100,100);
    	glutInitWindowSize(640,480);
    	glutCreateWindow("DzSpace Invaders");
     
    	init(); 
     
    	glutDisplayFunc(render);                         
    	glutReshapeFunc(reshape);                       
    	glutKeyboardFunc(keyboard);                     
    	glutSpecialFunc(special_keys);   
     
     
    	glutIdleFunc(idle);
     
    	glutMainLoop();                                 
     
    	return 0;
    }
     
     
     
    void render(void)   
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
        glLoadIdentity();									 
    	glColor3f(1.0f,1.0f,1.0f);
    	glBindTexture(GL_TEXTURE_2D, texture[1]);
    	glBegin(GL_QUADS);									
    		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);					
    	glEnd();											
     
      renderBitmapString(530, 465, GLUT_BITMAP_8_BY_13,"ESC for exit");
     
      	frame++;
    	time=glutGet(GLUT_ELAPSED_TIME);
     
    	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\n", file_name );
          exit   ( 1 );
       }
     
       raw_bitmap = (GLubyte *) malloc ( width * height * depth * ( sizeof(GLubyte)) );
     
       if (raw_bitmap == NULL)
       {
          printf ( "Cannot allocate memory for texture\n" );
          fclose ( file);
          exit   ( 1 );
       }
     
       fread  ( raw_bitmap , width * height * depth, 1 , file );
       fclose ( file);
     
       glGenTextures(1, &amp;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.. )
    * I know this will only work if I only load one texture.

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

  2. #2
    Super Moderator OpenGL Lord
    Join Date
    Dec 2003
    Location
    Grenoble - France
    Posts
    5,574

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

    You should uncomment the call to load_texture

  3. #3
    Senior Member OpenGL Pro
    Join Date
    Jul 2001
    Location
    France
    Posts
    1,662

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

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

  4. #4
    Senior Member Frequent Contributor
    Join Date
    Aug 2004
    Location
    munich, germany
    Posts
    659

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

    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)

    with

    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.

  5. #5
    Junior Member Newbie
    Join Date
    Feb 2005
    Posts
    11

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

    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...

  6. #6
    Senior Member Frequent Contributor
    Join Date
    Aug 2004
    Location
    munich, germany
    Posts
    659

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

    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.

  7. #7
    Junior Member Newbie
    Join Date
    Feb 2005
    Posts
    11

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

    Code :
    frame++;
    	time=glutGet(GLUT_ELAPSED_TIME);
     
    	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 ?

  8. #8
    Senior Member OpenGL Pro
    Join Date
    Jul 2001
    Location
    France
    Posts
    1,662

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

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

  9. #9
    Junior Member Newbie
    Join Date
    Feb 2005
    Posts
    11

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

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

  10. #10
    Senior Member OpenGL Pro
    Join Date
    Jul 2001
    Location
    France
    Posts
    1,662

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

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

    And what about your textures ?

Page 1 of 3 123 LastLast

Similar Threads

  1. Why texture filtering doesn't work here?
    By TonyForge in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 03-09-2018, 07:49 AM
  2. rendering fbo to texture, doesn't seem to work
    By samphippen in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 05-30-2012, 11:00 PM
  3. Texture Object doesn't work
    By Limas in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 11-06-2010, 05:53 PM
  4. Texture mapping - doesn't work
    By DeadHead in forum OpenGL: Basic Coding
    Replies: 12
    Last Post: 07-26-2009, 12:06 PM
  5. Texture mapping doesn't work
    By Amir Vaxman in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 10-14-2003, 11:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean