現在地 >> メニュー >> サンプルコード::OpenGL >> 点と法線 >> 点と法線2

問題


点を打って面を作成し、法線を設定せよ。
ただし、以下の条件を満たすこと。

条件:
  • 全ての点の法線は同じ向き。
  • マウスで視点を変更できる。
  • キーボードで光源を移動させる。

 「↑」「↓」: z方向
 「→」「←」: x方向
 「u」「d」: y方向
  • 光源の位置を、点を打つことで表示せよ

答え


#include <cstdio>
#include <GL/glut.h>
#include <GL/glu.h>
#include <GL/gl.h>

void display();
void reshape(int w, int h);


void DRAW_XYZ();
void DRAW_POINT();
void myMouseFunc(int button,int state,int x,int y);
void myMouseMotion(int x,int y);
void SpecialKey(int key, int x, int y);
void keyboard(unsigned char key, int x, int y);


using namespace std;


inline void GLUT_INIT()
{
glutInitDisplayMode(GLUT_RGBA| GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(250,250);
}

inline void GLUT_CALL_FUNC()
{
glutDisplayFunc(display);
glutReshapeFunc(reshape);

glutMouseFunc(myMouseFunc);
glutMotionFunc(myMouseMotion);
glutSpecialFunc(SpecialKey);
glutKeyboardFunc(keyboard);
}

inline void MY_INIT()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);

}


int main(int argc, char **argv)
{
glutInit(&argc,argv);
GLUT_INIT();
glutCreateWindow("window name");
GLUT_CALL_FUNC();
MY_INIT();
glutMainLoop();

return 0;
}

/********[ここからコールバック]****************************************/
double lx=0;
double ly=0;
double lz=0;

double xAngle = 0.0, yAngle = 0.0;
void display()
{
static GLfloat red[] = { 0.8, 0.2, 0.2, 1.0 };
GLfloat light_pos[] = {lx,ly,lz, 1.0 };

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(3.0, 4.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

glRotated(xAngle,1,0,0);
glRotated(yAngle,0,1,0);

glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
DRAW_XYZ();

glEnable(GL_DEPTH_TEST);

glPushMatrix();
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, red);
DRAW_POINT();
glPopMatrix();


glDisable(GL_LIGHTING);
glDisable(GL_LIGHT0);
glPointSize(10);
glBegin(GL_POINTS);
glColor3d(0,1,0);//x
glVertex3d(lx,ly,lz);
glEnd();


glDisable(GL_DEPTH_TEST);
glutSwapBuffers();

}


void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(30.0, (double)w / (double)h, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
gluLookAt(3.0, 4.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}


int xStart,yStart;
bool mouseFlag = GL_FALSE;
void myMouseFunc(int button,int state,int x,int y)
{
if(button == GLUT_LEFT_BUTTON &&state == GLUT_DOWN)
{
xStart = x;
yStart = y;
mouseFlag = GL_TRUE;
}
else
{
mouseFlag = GL_FALSE;

}

}

void myMouseMotion(int x,int y)
{
int xdis,ydis;
double a = 0.5;

if(mouseFlag == GL_FALSE)return;

xdis = x - xStart;
ydis = y - yStart;

xAngle += (double)ydis * a;
yAngle += (double)xdis * a;

xStart = x;
yStart = y;
glutPostRedisplay();
}

void SpecialKey(int key, int x, int y)
{
switch (key) {
case GLUT_KEY_UP:
lz -= 0.2;
break;

case GLUT_KEY_DOWN:
lz+=0.2;
break;

case GLUT_KEY_RIGHT:
lx +=0.2;
break;

case GLUT_KEY_LEFT:
lx -=0.2;
break;

default:
break;
}

glutPostRedisplay();

}

void keyboard(unsigned char key, int x, int y)
{
switch(key)
{
case 'u':
ly +=0.2;
break;

case 'd':
ly-=0.2;
break;
}

glutPostRedisplay();
}


/**********[ここから、各種関数]***********************/

void DRAW_XYZ()
{
glDisable(GL_LIGHTING);
glDisable(GL_LIGHT0);

glLineWidth(0.5);
glBegin(GL_LINES);

glColor3d(0,1,0);//x
glVertex2d(-100,0);
glVertex2d(100, 0);

glColor3d(1,0,0);//y
glVertex2d(0,0);
glVertex2d(0,100);

glColor3d(0,0,1);//z
glVertex3d(0,0,-100);
glVertex3d(0,0, 100);
glEnd();

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
}

void DRAW_POINT()
{
glPointSize(1.5);
glBegin(GL_POINTS);

for(int y = 0;y<60;y++)
{
for(int x = 0;x<60;x++)
{
glNormal3d(0,0,1);
glVertex3d((double)x/59,(double)y/59,0);
}

}

glEnd();
}

目次

― その他 ―

Wiki内検索

計測中...(07.10.8〜)

Save The World






▲よろしければ広告のクリックもお願いします


▲ランキングに参加しました

管理人/副管理人のみ編集できます