#include #include #include #define TEX_HEIGHT 512 #define TEX_WIDTH 512 static GLubyte image[TEX_HEIGHT][TEX_WIDTH][4]; void initTexture(void) { FILE *fp; int i, j, c; if((fp = fopen("cat.bmp", "rb")) == NULL){ fprintf(stderr, "texture file cannot open\n"); return; } //bmpファイルのヘッダ部(先頭から54byte)読み飛ばし fseek(fp, 54L, SEEK_SET); for(i = 0 ; i < TEX_HEIGHT ; i++){ for(j = 0 ; j < TEX_WIDTH ; j++){ image[i][j][2]= (GLubyte)fgetc(fp); //B image[i][j][1]= (GLubyte)fgetc(fp); //G image[i][j][0]= (GLubyte)fgetc(fp); //R image[i][j][3]= 0; } } fclose(fp); } void displayTexPolygon(void) { glEnable(GL_TEXTURE_2D); glBegin(GL_QUADS); glTexCoord2f(0.0, 0.0); glVertex3f(-10.0,-10.0, 0.0); glTexCoord2f(0.0, 1.0); glVertex3f(-10.0, 10.0, 0.0); glTexCoord2f(1.0, 1.0); glVertex3f( 10.0, 10.0, 0.0); glTexCoord2f(1.0, 0.0); glVertex3f( 10.0,-10.0, 0.0); glEnd(); glDisable(GL_TEXTURE_2D); } void display(void) { static float spin = 0.0; glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glPushMatrix(); { glTranslatef(0.0, 0.0,-20.0); glRotatef(spin, 0.0, 1.0, 0.0); glColor3f(1.0, 1.0, 1.0); displayTexPolygon(); } spin += 0.5; glPopMatrix(); glFlush(); glutSwapBuffers(); } void init(void) { glClearColor(0.0, 0.0, 0.0, 0.0); glDepthFunc(GL_LEQUAL); glEnable(GL_DEPTH_TEST); initTexture(); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TEX_WIDTH, TEX_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, image); } void reshape(int w, int h) { glViewport(0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum(-10.0, 10.0,-10.0, 10.0, 10.0, 500.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_DEPTH|GLUT_DOUBLE|GLUT_RGBA); glutInitWindowSize (342, 342); glutInitWindowPosition (300, 200); glutCreateWindow (argv[0]); init(); glutIdleFunc(display); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMainLoop(); return 0; }