#include "glos.h"
#include <GL/gl.h>
#include <GL/glaux.h>
#include "windows.h"
void myinit(void);
void CALLBACK display(void);
void CALLBACK reshape(GLsizei w,GLsizei h);
//定义一组控制点的存储空间
GLfloat points[4][4][3];
//定义一个指向NURBS曲面对象的指针
GLUnurbsObj *theNurb;
//用来生成控制点的过程,取代被注释掉的原来直接赋值方式生成控制点
void init_surface(void)
{
int u,v;
for(u=0;u<4;u++)
{
for(v=0;v<4;v++)
{
points[u][v][0]=2.0*((GLfloat)u-1.5);
points[u][v][1]=2.0*((GLfloat)v-1.5);
if((u==1||u==2)&&(v==1||v==2))
points[u][v][2]=3.0;
else
points[u][v][2]=-3.0;
}
}
}
/*
GLfloat points[4][4][3]={
{{-1.5,-1.5,2.0},{-0.5,-1.5,4.0},
{0.5,-1.5,3.0},{1.5,-1.5,2.0}},
{{-1.5,-0.5,3.0},{-0.5,1.5,4.0},
{0.5,0.5,2.0},{1.5,-0.5,-1.0}},
{{-1.5,0.5,2.0},{-0.5,0.5,4.0},
{0.5,0.5,5.0},{1.5,-1.5,1.5}},
{{-1.5,1.5,-2.0},{-0.5,1.5,3.0},
{0.5,0.5,1.0},{1.5,1.5,-1.0}}};
*/
GLfloat color[4][3]={
{1.0,0.0,0.0},{0.0,1.0,0.0},{0.0,0.0,1.0},{1.0,1.0,1.0}};
void initlights(void)
{
GLfloat ambient[]={0.4,0.6,0.2,1.0};
GLfloat position[]={0.0,1.0,3.0,1.0};
GLfloat mat_diffuse[]={0.2,0.4,0.8,1.0};
GLfloat mat_specular[]={1.0,1.0,1.0,1.0};
GLfloat mat_shininess[]={80.0};
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0,GL_AMBIENT,ambient);
glLightfv(GL_LIGHT0,GL_POSITION,position);
glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse);
glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);
glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);
//首先初始化控制点
init_surface();
//创建一个NURBS曲面的对象
theNurb=gluNewNurbsRenderer();
//修改此曲面对象的属性
gluNurbsProperty(theNurb,GLU_SAMPLING_TOLERANCE,25.0);
gluNurbsProperty(theNurb,GLU_DISPLAY_MODE,GLU_FILL);
}
void myinit(void)
{
auxInitDisplayMode(AUX_SINGLE|AUX_RGBA);
auxInitPosition(0,0,500,500);
auxInitWindow("sample1");
glClearColor(0.0,0.0,0.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);
glMap2f(GL_MAP2_VERTEX_3,0,1,3,4,0,1,12,4,&points[0][0][0]);
glEnable(GL_MAP2_VERTEX_3);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
glMapGrid2f(20,0.0,1.0,20,0.0,1.0);
glEnable(GL_DEPTH_TEST);
initlights();
// glShadeModel(GL_FLAT);
}
void CALLBACK reshape(GLsizei w,GLsizei h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h)
glOrtho(-5.0,5.0,-5.0*(GLfloat)h/(GLfloat)w,
5.0*(GLfloat)h/(GLfloat)w,-5.0,5.0);
else
glOrtho(-5.0*(GLfloat)h/(GLfloat)w,
5.0*(GLfloat)h/(GLfloat)w,-5.0,5.0,-5.0,5.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void CALLBACK display(void)
{
//B样条曲面(NURBS)的控制向量,可以参阅图形学的相关书籍
GLfloat knots[8]={0.0,0.0,0.0,0.0,1.0,1.0,1.0,1.0};
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f(0.0,0.0,1.0);
glPushMatrix();
glRotatef(35.0,1.0,1.0,1.0);
glRotatef(-60.0,1.0,0.0,0.0);
glScalef(0.5,0.5,0.5);
//注释掉原来的曲面绘制函数,代之以新的NURBS曲面绘制函数
// glEvalMesh2(GL_FILL,0,20,0,20);
//定义曲面的数学模型,确定其形状
gluNurbsSurface(theNurb,
8,knots,
8,knots,
4*3,
3,
&points[0][0][0],
4,4,
GL_MAP2_VERTEX_3);
//结束曲面的绘制,此结构很类似于标准的glBegin()...glEnd()
gluEndSurface(theNurb);
int i,j;
glPointSize(4.0);
glBegin(GL_POINTS);
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
glColor3fv(color[i]);
glVertex3fv(&points[i][j][0]);
}
glEnd();
glPopMatrix();
glFlush();
}
void main(void)
{
myinit();
auxReshapeFunc(reshape);
auxMainLoop(display);
}
绘制NURBS曲面.rar_NURBS c++_NURBS 曲面_nurbs_nurbs曲面_图形学曲面
版权申诉
46 浏览量
2022-09-23
06:08:45
上传
评论 2
收藏 43KB RAR 举报
御道御小黑
- 粉丝: 58
- 资源: 1万+