最終更新:
mikk_ni3_92 2007年09月07日(金) 14:58:51履歴
現在地 >> メニュー >> サンプルコード::OpenGL >> ポリゴンの裏表の描画
#include <cstdio>
#include <cstdlib>
#include <GL/glut.h>
#include <GL/gl.h>
void display();
void reshape(int w, int h);
void timer(int value);
void keyboard(unsigned char key, int x, int y);
void DRAW_SQU();
using namespace std;
/*****[テクスチャ関連]*****/
GLuint texture[1];
/*****[画像用構造体]*****/
struct Image {
unsigned long sizeX;
unsigned long sizeY;
char *data;
};
typedef struct Image Image;
/****[画像データセット関数]**************/
int ImageLoad(char *filename, Image *image) {
FILE *file;
unsigned long size;
unsigned long i;
unsigned short int planes;
unsigned short int bpp;
char temp;
if *1==NULL)
{
printf("File Not Found : %s\n",filename);
return 0;
}
fseek(file, 18, SEEK_CUR);
if *2 != 1) {
printf("Error reading width from %s.\n", filename);
return 0;
}
if *3 != 1) {
printf("Error reading height from %s.\n", filename);
return 0;
}
size = image->sizeX * image->sizeY * 3;
if *4 != 1) {
printf("Error reading planes from %s.\n", filename);
return 0;
}
if (planes != 1) {
printf("Planes from %s is not 1: %u\n", filename, planes);
return 0;
}
if *5 != 1) {
printf("Error reading bpp from %s.\n", filename);
return 0;
}
if (bpp != 24) {
printf("Bpp from %s is not 24: %u\n", filename, bpp);
return 0;
}
fseek(file, 24, SEEK_CUR);
image->data = (char *) malloc(size);
if (image->data == NULL) {
printf("Error allocating memory for color-corrected image data");
return 0;
}
if *6 != 1) {
printf("Error reading image data from %s.\n", filename);
return 0;
}
for (i=0;i<size;i+=3) {
temp = image->data[i];
image->data[i] = image->data[i+2];
image->data[i+2] = temp;
}
return 1;
}
/*****[画像読み込み準備]******************/
void LoadGLTextures()
{
Image *image1;
image1 = (Image *) malloc(sizeof(Image));
if (image1 == NULL) {
printf("Error allocating space for image");
std::exit(0);
}
if (!ImageLoad("test2.bmp", image1)) { //ここで、「画像ファイル」を指定する
std::exit(1);
}
/******* テクスチャ作成 ****************/
glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, image1->sizeX,image1->sizeY, GL_RGB,GL_UNSIGNED_BYTE, image1->data);
};
inline void GLUT_INIT()
{
glutInitDisplayMode(GLUT_RGBA| GLUT_DOUBLE | GLUT_DEPTH);
}
inline void GLUT_CALL_FUNC()
{
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc(100,timer,0);
glutKeyboardFunc(keyboard);
}
inline void MY_INIT()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
LoadGLTextures();
}
int main(int argc, char **argv)
{
glutInit(&argc,argv);
GLUT_INIT();
glutCreateWindow("window name");
GLUT_CALL_FUNC();
MY_INIT();
glutMainLoop();
return 0;
}
/********[ここからコールバック]****************************************/
void display()
{
static int r = 0;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glPushMatrix();
glRotated((double)r, 0.0, 1.0, 0.0);
DRAW_SQU();
glPopMatrix();
glDisable(GL_DEPTH_TEST);
glutSwapBuffers();
r = r + 1;
if(r > 360)
{
r= 0;
}
}
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glLoadIdentity();
gluPerspective(30.0, (double)w / (double)h, 1.0, 100.0);
gluLookAt(3.0, 4.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
void timer(int value)
{
glutPostRedisplay();
glutTimerFunc(1,timer,0); //タイマー関数
}
/**********[ここから各種関数]***********************/
int state;
void DRAW_SQU()
{
switch(state)
{
case 1:
glDisable(GL_CULL_FACE);
break;
case 2:
case 3:
glEnable(GL_CULL_FACE);
if(state == 2)
{
glCullFace(GL_BACK);
}else
{
glCullFace(GL_FRONT);
}
break;
default:
break;
}
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
glTexCoord2d(0.0, 1.0);
glVertex2d(-1,1);
glTexCoord2d(0.0, 0.0);
glVertex2d(-1,-1);
glTexCoord2d(1.0, 0.0);
glVertex2d(1,-1);
glTexCoord2d(1.0, 1.0);
glVertex2d(1,1);
glEnd();
glDisable(GL_TEXTURE_2D);
}
void keyboard(unsigned char key, int x, int y)
{
switch(key)
{
case '1':
state = 1;
break;
case '2':
state = 2;
break;
case '3':
state = 3;
break;
default:
break;
}
}
#include <cstdio>
#include <cstdlib>
#include <GL/glut.h>
#include <GL/gl.h>
void display();
void reshape(int w, int h);
void timer(int value);
void keyboard(unsigned char key, int x, int y);
void DRAW_SQU();
using namespace std;
/*****[テクスチャ関連]*****/
GLuint texture[1];
/*****[画像用構造体]*****/
struct Image {
unsigned long sizeX;
unsigned long sizeY;
char *data;
};
typedef struct Image Image;
/****[画像データセット関数]**************/
int ImageLoad(char *filename, Image *image) {
FILE *file;
unsigned long size;
unsigned long i;
unsigned short int planes;
unsigned short int bpp;
char temp;
if *1==NULL)
{
printf("File Not Found : %s\n",filename);
return 0;
}
fseek(file, 18, SEEK_CUR);
if *2 != 1) {
printf("Error reading width from %s.\n", filename);
return 0;
}
if *3 != 1) {
printf("Error reading height from %s.\n", filename);
return 0;
}
size = image->sizeX * image->sizeY * 3;
if *4 != 1) {
printf("Error reading planes from %s.\n", filename);
return 0;
}
if (planes != 1) {
printf("Planes from %s is not 1: %u\n", filename, planes);
return 0;
}
if *5 != 1) {
printf("Error reading bpp from %s.\n", filename);
return 0;
}
if (bpp != 24) {
printf("Bpp from %s is not 24: %u\n", filename, bpp);
return 0;
}
fseek(file, 24, SEEK_CUR);
image->data = (char *) malloc(size);
if (image->data == NULL) {
printf("Error allocating memory for color-corrected image data");
return 0;
}
if *6 != 1) {
printf("Error reading image data from %s.\n", filename);
return 0;
}
for (i=0;i<size;i+=3) {
temp = image->data[i];
image->data[i] = image->data[i+2];
image->data[i+2] = temp;
}
return 1;
}
/*****[画像読み込み準備]******************/
void LoadGLTextures()
{
Image *image1;
image1 = (Image *) malloc(sizeof(Image));
if (image1 == NULL) {
printf("Error allocating space for image");
std::exit(0);
}
if (!ImageLoad("test2.bmp", image1)) { //ここで、「画像ファイル」を指定する
std::exit(1);
}
/******* テクスチャ作成 ****************/
glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, image1->sizeX,image1->sizeY, GL_RGB,GL_UNSIGNED_BYTE, image1->data);
};
inline void GLUT_INIT()
{
glutInitDisplayMode(GLUT_RGBA| GLUT_DOUBLE | GLUT_DEPTH);
}
inline void GLUT_CALL_FUNC()
{
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc(100,timer,0);
glutKeyboardFunc(keyboard);
}
inline void MY_INIT()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
LoadGLTextures();
}
int main(int argc, char **argv)
{
glutInit(&argc,argv);
GLUT_INIT();
glutCreateWindow("window name");
GLUT_CALL_FUNC();
MY_INIT();
glutMainLoop();
return 0;
}
/********[ここからコールバック]****************************************/
void display()
{
static int r = 0;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glPushMatrix();
glRotated((double)r, 0.0, 1.0, 0.0);
DRAW_SQU();
glPopMatrix();
glDisable(GL_DEPTH_TEST);
glutSwapBuffers();
r = r + 1;
if(r > 360)
{
r= 0;
}
}
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glLoadIdentity();
gluPerspective(30.0, (double)w / (double)h, 1.0, 100.0);
gluLookAt(3.0, 4.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
void timer(int value)
{
glutPostRedisplay();
glutTimerFunc(1,timer,0); //タイマー関数
}
/**********[ここから各種関数]***********************/
int state;
void DRAW_SQU()
{
switch(state)
{
case 1:
glDisable(GL_CULL_FACE);
break;
case 2:
case 3:
glEnable(GL_CULL_FACE);
if(state == 2)
{
glCullFace(GL_BACK);
}else
{
glCullFace(GL_FRONT);
}
break;
default:
break;
}
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
glTexCoord2d(0.0, 1.0);
glVertex2d(-1,1);
glTexCoord2d(0.0, 0.0);
glVertex2d(-1,-1);
glTexCoord2d(1.0, 0.0);
glVertex2d(1,-1);
glTexCoord2d(1.0, 1.0);
glVertex2d(1,1);
glEnd();
glDisable(GL_TEXTURE_2D);
}
void keyboard(unsigned char key, int x, int y)
{
switch(key)
{
case '1':
state = 1;
break;
case '2':
state = 2;
break;
case '3':
state = 3;
break;
default:
break;
}
}