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

Started by
3 comments, last by OutOfTheCrypt 18 years, 9 months ago
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(&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, &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; } </code> * I'v cut out some parts. * I know my fps counter doesn't work ( I just don't know why.. [Big Grin] ) * I know this will only work if I only load one texture. Could somebody plzz tell my why this doesn't work ? ( erm... where can I find, or could somebody tell me what are the code-tags on this forum ? )
Advertisement
First, in the rendering function, you're trying to bind the second texture ID, but the array only have one ID. Second, you have commented out the texture loading function call in the init function.
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.
Code tag's are in the FAQ. Look at the top of the page.

I never could get gluBuild2DMipmaps to work for me, have you tried it just using glTexImage2D?

This should work for your fps:

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


This is my fps-counter piece of code at the end of the render function but i still get fps = 0 all the time. Why doens't it want to work :s ?

glTexImage2D gives same effect ! Damm, i'd really like my texture working :D

This topic is closed to new replies.

Advertisement