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

問題


点を打って面を構成し、点毎に法線を設定して、
点によってつくられた面に陰影付けをせよ。

その他条件:

 マウスで視点が変更できる。

 →点と法線2

答え


#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);

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);
}

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 xAngle = 0.0, yAngle = 0.0;
void display()
{

static GLfloat light_pos[] = { 0.0, 3.0, 5.0, 1.0 };
static GLfloat red[] = { 0.8, 0.2, 0.2, 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_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 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()
{
double ny= -1;
glPointSize(1.5);
glBegin(GL_POINTS);

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

}


glEnd();
}

目次

― その他 ―

Wiki内検索

計測中...(07.10.8〜)

Save The World






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


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

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