Hi,
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);
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
", 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… )
- I know this will only work if I only load one texture.
Could somebody plzz tell my why this doesn’t work ?