#include <windows.h>
#include <GL/gl.h>
#include <GL/glut.h>
GLubyte tex16[16][16][4];
GLubyte tex8[8][8][4];
GLubyte tex4[4][4][4];
GLubyte tex2[2][2][4];
GLubyte tex1[1][1][4];
GLuint texName;
void disp( void ) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D , texName);
glBegin(GL_POLYGON);
glTexCoord2f(0 , 0); glVertex3f(-10 , -20 , -2);
glTexCoord2f(0 , 20); glVertex3f(-10 , 20 , -2);
glTexCoord2f(20 , 20); glVertex3f(1000 , 20 , -1000);
glTexCoord2f(20 , 0); glVertex3f(1000 , -20 , -1000);
glEnd();
glFlush();
}
void createTex(int width , int height , int r , int g , int b , void * buf) {
int i , max = width * height * 4;
GLubyte * pixels = buf;
for(i = 0 ; i < max ; i += 4) {
pixels[i] = r;
pixels[i + 1] = g;
pixels[i + 2] = b;
pixels[i + 3] = 0xFF;
}
}
int main(int argc , char ** argv) {
glutInit(&argc , argv);
glutInitWindowSize(400 , 300);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA | GLUT_DEPTH);
glutCreateWindow("Kitty on your lap");
glutDisplayFunc(disp);
glEnable(GL_TEXTURE_2D);
glGenTextures(1 , &texName);
glBindTexture(GL_TEXTURE_2D , texName);
createTex(16 , 16 , 0xFF , 0 , 0 , tex16);
glTexImage2D(
GL_TEXTURE_2D , 0 , GL_RGBA , 16 , 16 ,0 ,
GL_RGBA , GL_UNSIGNED_BYTE , tex16
);
createTex(8 , 8 , 0x80 , 0x80 , 0 , tex8);
glTexImage2D(
GL_TEXTURE_2D , 1 , GL_RGBA , 8 , 8 ,0 ,
GL_RGBA , GL_UNSIGNED_BYTE , tex8
);
createTex(4 , 4 , 0 , 0xFF , 0 , tex4);
glTexImage2D(
GL_TEXTURE_2D , 2 , GL_RGBA , 4 , 4 , 0 ,
GL_RGBA , GL_UNSIGNED_BYTE , tex4
);
createTex(2 , 2 , 0 , 0x80 , 0x80 , tex2);
glTexImage2D(
GL_TEXTURE_2D , 3 , GL_RGBA , 2 , 2 ,0 ,
GL_RGBA , GL_UNSIGNED_BYTE , tex2
);
createTex(1 , 1 , 0 , 0 , 0xFF , tex1);
glTexImage2D(
GL_TEXTURE_2D , 4 , GL_RGBA , 1 , 1 ,0 ,
GL_RGBA , GL_UNSIGNED_BYTE , tex1
);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(160 , 0.75 , 1 , 1000);
glutMainLoop();
glDeleteTextures(1 , &texName);
return 0;
}

| 定数 | 解説 |
|---|---|
| GL_NEAREST | 処理するピクセルの中心に最も近い値を返します |
| GL_LINEAR | 処理するピクセルに最も近い 4 つのテクスチャ要素の加重平均を返します |
| GL_NEAREST_MIPMAP_NEAREST | 処理するピクセルのサイズに最適なミップマップを選択し GL_NEAREST を基準として値を返します |
| GL_LINEAR_MIPMAP_NEAREST | 処理するピクセルのサイズに最適なミップマップを選択し GL_LINEARE を基準として値を返します |
| GL_NEAREST_MIPMAP_LINEAR | 処理するピクセルのサイズに最適な 2 つのミップマップを選択し GL_NEAREST を基準としてそれぞれのミップマップから値を生成し それらの値の加重平均を返します |
| GL_LINEAR_MIPMAP_LINEAR | 処理するピクセルのサイズに最適な 2 つのミップマップを選択し GL_LINEARE を基準としてそれぞれのミップマップから値を生成し それらの値の加重平均を返します |
#include <windows.h>
#include <GL/gl.h>
#include <GL/glut.h>
GLubyte tex[4][4][4];
GLuint texName;
void disp( void ) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D , texName);
glBegin(GL_POLYGON);
glTexCoord2f(0 , 0); glVertex3f(-10 , -20 , -2);
glTexCoord2f(0 , 20); glVertex3f(-10 , 20 , -2);
glTexCoord2f(20 , 20); glVertex3f(1000 , 20 , -1000);
glTexCoord2f(20 , 0); glVertex3f(1000 , -20 , -1000);
glEnd();
glFlush();
}
void createTex(int width , int height , int r , int g , int b , void * buf) {
int i , max = width * height * 4;
GLubyte * pixels = buf;
for(i = 0 ; i < max ; i += 4) {
pixels[i] = r;
pixels[i + 1] = g;
pixels[i + 2] = b;
pixels[i + 3] = 0xFF;
}
}
int main(int argc , char ** argv) {
glutInit(&argc , argv);
glutInitWindowSize(400 , 300);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA | GLUT_DEPTH);
glutCreateWindow("Kitty on your lap");
glutDisplayFunc(disp);
glEnable(GL_TEXTURE_2D);
glGenTextures(1 , &texName);
glBindTexture(GL_TEXTURE_2D , texName);
createTex(4 , 4 , 0xFF , 0 , 0 , tex);
glTexImage2D(
GL_TEXTURE_2D , 0 , GL_RGBA , 4 , 4 , 0 ,
GL_RGBA , GL_UNSIGNED_BYTE , tex
);
createTex(2 , 2 , 0 , 0xFF , 0 , tex);
glTexImage2D(
GL_TEXTURE_2D , 1 , GL_RGBA , 2 , 2 ,0 ,
GL_RGBA , GL_UNSIGNED_BYTE , tex
);
createTex(1 , 1 , 0 , 0 , 0xFF , tex);
glTexImage2D(
GL_TEXTURE_2D , 2 , GL_RGBA , 1 , 1 ,0 ,
GL_RGBA , GL_UNSIGNED_BYTE , tex
);
glTexParameteri(
GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER ,
GL_NEAREST_MIPMAP_NEAREST
);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(160 , 0.75 , 1 , 1000);
glutMainLoop();
glDeleteTextures(1 , &texName);
return 0;
}
