Original Post
So i was going to try textures awhile ago and i got them to work perfectly. I was thinking i could make a cool arcade-style game or something with my newfound knowledge. Here my troubles began... My problem is: When i use textures everything works like it should, but when i try to use other things aswell as textures in my application things get weird. For example: If i want to draw a polygon with some random color, i do it and it's ok. Now i want to draw a triangle containing a texture. In my display callback i first draw the polygon and then the textured triangle. For some unknown reason the rendering of the texture object seems to clear the whole screen of other stuff, or sometimes just parts of it. If i draw the polygon after the texture object the polygon will not show at all. Since this is my first time using textures i dont know what could be wrong. I have tried commenting/uncommenting certain parts and narrowed the problem down to the texture thingie. Everything works just fine until i put textures in there. Here is some example code illustrating my problem: (how do i make a code block on this forum? :P) I got the setup_textures and load_rgb_img functions from Nehes OpenGL guide. While im at it i might aswell ask how i use .bith mp images with GLUT, because using .raw is getting tedious in that i must use Photoshop to tamper with an image to get it to work. Thanks in advance. [Edited by - cresty on February 17, 2007 1:46:50 PM]
#include "header.h"
bool load_rgb_image(const char* file_name, int w, int h, RGBIMG* refimg)
{
GLuint sz;
FILE* file;
long fsize;
GLubyte* p;
refimg->w = (GLuint) w;
refimg->h = (GLuint) h;
sz = (((3*refimg->w+3)>>2)<<2)*refimg->h;
refimg->data = new GLubyte [sz];
if (refimg->data == NULL) return false;
file = fopen(file_name , "rb");
if (!file) return false;
fseek(file, 0L, SEEK_END);
fsize = ftell(file);
if (fsize != (long)sz)
{
fclose(file);
return false;
}
fseek(file, 0L, SEEK_SET);
p = refimg->data;
while (fsize > 0)
{
fread(p, 1, 1, file);
p++;
fsize--;
}
fclose(file);
return true;
};
bool setup_textures(int i)
{
RGBIMG img;
glGenTextures(TEXTURES_NUM, g_texid);
if(i == 1)
{
if (!load_rgb_image("img/normalcut_256x256.raw", 256, 256, &img))
return false;
}
if(i == 2)
{
if (!load_rgb_image("img/eatcut_256x256.raw", 256, 256, &img))
return false;
}
glBindTexture(GL_TEXTURE_2D, g_texid[CUBE_TEX]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 3, img.w, img.h, 0, GL_RGB, GL_UNSIGNED_BYTE, img.data);
delete img.data;
return true;
};
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(800, 800);
glutInitWindowPosition(100, 100);
glutCreateWindow("Game");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(idle);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
};
void idle()
{
};
void keyboard(unsigned char key, int w, int h)
{
switch(key)
{
case 'w':
pcpos = UP;
break;
case 'a':
pcpos = LEFT;
break;
case 's':
pcpos = DOWN;
break;
case 'd':
pcpos = RIGHT;
break;
}
glutPostRedisplay();
};
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
draw(); //this model will not show at all if i use textures.
switch(pcpos)
{
case UP:
glTranslatef(0, 0.4, 0);
tex_eat();
break;
case LEFT:
glTranslatef(-0.4, 0, 0);
tex_eat();
break;
case DOWN:
glTranslatef(0, -0.4, 0);
tex_eat();
break;
case RIGHT:
glTranslatef(0.4, 0, 0);
tex_eat();
break;
default: tex_normal();
}
glutSwapBuffers();
};
void reshape(int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
};
bool init()
{
glShadeModel(GL_SMOOTH);
glClearColor(0, 0, 0, 0);
glClearDepth(1);
glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//glEnable(GL_BLEND);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable(GL_LINE_SMOOTH);
glEnable(GL_CULL_FACE);
glEnable(GL_TEXTURE_2D);
return true;
};
bool tex_normal()
{
//somewhere here the problem lies
if (!setup_textures(1)) return false;
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glBindTexture(GL_TEXTURE_2D, g_texid[CUBE_TEX]);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 1.0); glVertex2f(-0.08, -0.08);
glTexCoord2f(1.0, 1.0); glVertex2f( 0.08, -0.08);
glTexCoord2f(1.0, 0.0); glVertex2f( 0.08, 0.08);
glTexCoord2f(0.0, 0.0); glVertex2f(-0.08, 0.08);
glEnd();
return true;
};
bool tex_eat()
{
//somewhere here the problem lies
if (!setup_textures(2)) return false;
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glBindTexture(GL_TEXTURE_2D, g_texid[CUBE_TEX]);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 1.0); glVertex2f(-0.08, -0.08);
glTexCoord2f(1.0, 1.0); glVertex2f( 0.08, -0.08);
glTexCoord2f(1.0, 0.0); glVertex2f( 0.08, 0.08);
glTexCoord2f(0.0, 0.0); glVertex2f(-0.08, 0.08);
glEnd();
return true;
};
bool draw()
{
glBegin(GL_QUADS);
glColor3f(0.5, 0.5, 0.5);
glVertex2f(0.4, -0.4);
glVertex2f(0.4, 0.4);
glVertex2f(0.2, 0.2);
glVertex2f(0.2, -0.2);
glEnd();
return true;
};