最終更新: mikk_ni3_92 2007年10月07日(日) 11:03:06履歴
現在地 >> メニュー >> サンプルコード::OpenGL >> 曲面描画
gluライブラリを用いて、NURBS曲面を描画せよ。
その他条件:
制御点は以下の通り、
P00=(0,0,0), P01=(0.5,0,0), P02=(1,0,0),P03=(1.5,0,0)
P10=(0,0,0.5), P11=(0.5,1,0.5), P12=(1,1,0.5),P13=(1.5,0,0.5)
P20=(0,0,1), P21=(0.5,0,1), P22=(1,0,1),P23=(1.5,0,1)
glEnable(GL_AUTO_NORMAL)を用いて、法線は自動計算する。
glEnable(GL_NORMALIZE)で法線データを正規化する。
分かりやすいように、マウスで視点が変更できる。
#include <iostream>
#include <GL/glut.h>
void display();
void reshape(int w, int h);
void myMouseFunc(int button,int state,int x,int y);
void myMouseMotion(int x,int y);
inline void SET_UP_NURBS();
using namespace std;
/*** 制御点 ***/
GLfloat ctlpoints[3][4][3]
= {
{{0,0,0},{0.5,0,0},{1,0,0},{1.5,0,0}},
{{0,0,0.5},{0.5,1,0.5},{1,1,0.5},{1.5,0,0.5}},
{{0,0,1},{0.5,0,1},{1,0,1},{1.5,0,1}},
};
/*** ノット ***/
GLfloat knotv[8] = {0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0};
GLfloat knotu[6] = {0.0, 0.0, 0.0, 1.0, 1.0, 1.0};
inline void GLUT_INIT()
{
glutInitDisplayMode(GLUT_RGBA| GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(200,200);
}
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);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
SET_UP_NURBS();
}
GLUnurbsObj *theNurb;
inline void SET_UP_NURBS()
{
theNurb = gluNewNurbsRenderer();
gluNurbsProperty(theNurb, GLU_SAMPLING_TOLERANCE, 25.0);
gluNurbsProperty(theNurb, GLU_DISPLAY_MODE, GLU_FILL);
}
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(void)
{
glClear(GL_COLOR_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);
gluBeginSurface(theNurb);
gluNurbsSurface(theNurb, 6, knotu,8, knotv,4 * 3,3,&ctlpoints[0][0][0], 3, 4,GL_MAP2_VERTEX_3);
gluEndSurface(theNurb);
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);
}
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();
}
gluライブラリを用いて、NURBS曲面を描画せよ。
その他条件:
制御点は以下の通り、
P00=(0,0,0), P01=(0.5,0,0), P02=(1,0,0),P03=(1.5,0,0)
P10=(0,0,0.5), P11=(0.5,1,0.5), P12=(1,1,0.5),P13=(1.5,0,0.5)
P20=(0,0,1), P21=(0.5,0,1), P22=(1,0,1),P23=(1.5,0,1)
glEnable(GL_AUTO_NORMAL)を用いて、法線は自動計算する。
glEnable(GL_NORMALIZE)で法線データを正規化する。
分かりやすいように、マウスで視点が変更できる。
#include <iostream>
#include <GL/glut.h>
void display();
void reshape(int w, int h);
void myMouseFunc(int button,int state,int x,int y);
void myMouseMotion(int x,int y);
inline void SET_UP_NURBS();
using namespace std;
/*** 制御点 ***/
GLfloat ctlpoints[3][4][3]
= {
{{0,0,0},{0.5,0,0},{1,0,0},{1.5,0,0}},
{{0,0,0.5},{0.5,1,0.5},{1,1,0.5},{1.5,0,0.5}},
{{0,0,1},{0.5,0,1},{1,0,1},{1.5,0,1}},
};
/*** ノット ***/
GLfloat knotv[8] = {0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0};
GLfloat knotu[6] = {0.0, 0.0, 0.0, 1.0, 1.0, 1.0};
inline void GLUT_INIT()
{
glutInitDisplayMode(GLUT_RGBA| GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(200,200);
}
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);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
SET_UP_NURBS();
}
GLUnurbsObj *theNurb;
inline void SET_UP_NURBS()
{
theNurb = gluNewNurbsRenderer();
gluNurbsProperty(theNurb, GLU_SAMPLING_TOLERANCE, 25.0);
gluNurbsProperty(theNurb, GLU_DISPLAY_MODE, GLU_FILL);
}
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(void)
{
glClear(GL_COLOR_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);
gluBeginSurface(theNurb);
gluNurbsSurface(theNurb, 6, knotu,8, knotv,4 * 3,3,&ctlpoints[0][0][0], 3, 4,GL_MAP2_VERTEX_3);
gluEndSurface(theNurb);
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);
}
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();
}