1:简单实体构建(可运行代码VS)
// 提示:在合适的地方修改或添加代码
#include <GL/glut.h>
#include<stdio.h>
#include<iostream>
using namespace std;
// 评测代码所用头文件-结束
// 请在此添加你的代码
/********** Begin ********/
GLfloat points1[4][3] = { {-1,0,1},{1,0,1},{0,0,-0.7},{0,1.7,0} };
GLfloat Colors1[4][3] = { {0,1,0},{1,0,0},{1,1,0},{0,0,1} };
int vertice1[4][3] = { {0,1,2},{1,2,3},{0,2,3},{0,1,3} };
/********** End **********/
void InitGL(GLvoid)
{
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glEnable(GL_COLOR_MATERIAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}
void Create() //创建三棱锥
{
glBegin(GL_TRIANGLES);
for (int i = 0; i < 4; i++)
{
glColor3fv(Colors1[i]);
for (int j = 0; j < 3; j++)
{
int VtxId = vertice1[i][j];
glVertex3fv(points1[VtxId]);
}
}
glEnd();
}
void display(void)
{ //glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
// 请在此添加你的代码
/********** Begin ********/
glPushMatrix();
glTranslatef(0, 0.2f, -3.0f);
glRotatef(95, 1, 0, 0);
/********** End **********/
Create(); //三棱锥
glPopMatrix();
glutSwapBuffers();
glFlush();
}
void reshape(int width, int height)
{
if (height == 0)
height = 1;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitWindowPosition(100, 100);
glutInitWindowSize(400, 400);
glutCreateWindow("几何变换示例");
InitGL();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
2:机器人
// 提示:在合适的地方修改或添加代码
#include <GL/glut.h>
#include<stdio.h>
#include<iostream>
using namespace std;
GLint winWidth = 500, winHeight = 500; //设置初始化窗口大小
/*观察坐标系参数设置*/
GLfloat x0 = 2.0, yy = 2.0, z0 = 5.0; //设置观察坐标系原点
GLfloat xref = 0.0, yref = 0.0, zref = 0.0; //设置观察坐标系参考点(视点)
GLfloat Vx = 0.0, Vy = 1.0, Vz = 0.0; //设置观察坐标系向上向量(y轴)
/*观察体参数设置 */
GLfloat xwMin = -1.0, ywMin = -1.0, xwMax = 1.0, ywMax = 1.0;//设置裁剪窗口坐标范围
GLfloat dnear = 1.5, dfar = 20.0; //设置远、近裁剪面深度范围
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
/*观察变换*/
gluLookAt(x0, yy, z0, xref, yref, zref, Vx, Vy, Vz); //指定三维观察参数
// gluLookAt(2, 2, 5, 0, 0, 0, 0, 1, 0);
// 请在此添加你的代码
/********** Begin ********/
glClearColor(0, 0, 0, 0);
//glutSolidCube(0.5);
glPushMatrix();
glColor3f(1, 1, 1);
glTranslatef(0, 1.4f, 0);
glScalef(0.5, 0.7, 0.5);
glutSolidCube(0.5);
glPopMatrix();
glPushMatrix();
glColor3f(1, 0.5, 0.2);
glTranslatef(0, 1.9f, 0);
glScalef(1.5, 1.5, 0.5);
glutSolidCube(0.5);
glPopMatrix();
glPushMatrix();
glColor3f(1, 0,0);
glTranslatef(0, 0.25f, 0);
glScalef(4, 4, 0.5);
glutSolidCube(0.5);
glPopMatrix();
glPushMatrix();
glColor3f(1, 1, 0);
glTranslatef(-1.25f, 0.5f, 0);
glScalef(1, 3, 0.5);
glutSolidCube(0.5);
glPopMatrix();
glPushMatrix();
glColor3f(1, 1, 0);
glTranslatef(1.25f, 0.5f, 0);
glScalef(1, 3, 0.5);
glutSolidCube(0.5);
glPopMatrix();
glPushMatrix();
glColor3f(0.5, 0.5, 1.5);
glTranslatef(-0.5f, -1.5f, 0);
glScalef(1, 3, 0.5);
glutSolidCube(0.5);
glPopMatrix();
glPushMatrix();
glColor3f(0.5, 0.5, 1.5);
glTranslatef(0.5f, -1.5f, 0);
glScalef(1, 3, 0.5);
glutSolidCube(0.5);
glPopMatrix();
glPushMatrix();
glColor3f(1, 0.5, 0.2);
glTranslatef(-1.25f, -0.5f, 0);
glScalef(1, 1.5, 1);
glutSolidSphere(0.25,50,50);
glPopMatrix();
glPushMatrix();
glColor3f(1, 0.5, 0.2);
glTranslatef(1.25f, -0.5f, 0);
glScalef(1, 1.5, 1);
GLUquadricObj* sphere; //定义二次曲面对象
sphere = gluNewQuadric(); //生成二次曲面对象
gluSphere(sphere,0.25, 50, 50);
glPopMatrix();
/********** End **********/
glFlush();
}
void reshape(GLint newWidth, GLint newHeight)
{
/*视口变换*/
glViewport(0, 0, newWidth, newHeight); //定义视口大小
/*投影变换*/
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
/*透视投影,设置透视观察体*/
glFrustum(xwMin, xwMax, ywMin, ywMax, dnear, dfar);
/*模型变换*/
glMatrixMode(GL_MODELVIEW);
winWidth = newWidth;
winHeight = newHeight;
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitWindowPosition(100, 100);
glutInitWindowSize(500, 500); //设置初始化窗口大小
glutCreateWindow("三维观察");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
3:B样条线的绘制
#include<GL/glut.h>
struct Point
{
int x, y;
};
Point bz[11], bspt[11]; //bz为Bezier曲线,bspt为B样条曲线
int nInput;
//Bezier曲线控制点
Point pt[4] = { { 50, 100}, { 140,300},{250, 320}, {290, 120} };
//B样条曲线控制点
/**********请在此处添加你的代码*******/
/************** Begin ************/
Point pt2[4] = { {450,100},{540,300},{650,320},{690,120} };
/*************** end **************/
void CalcBZPoints() //Bezier曲线算法
{
float a0, a1, a2, a3, b0, b1, b2, b3;
a0 = pt[0].x;
a1 = -3 * pt[0].x + 3 * pt[1].x;
a2 = 3 * pt[0].x - 6 * pt[1].x + 3 * pt[2].x;
a3 = -pt[0].x + 3 * pt[1].x - 3 * pt[2].x + pt[3].x;
b0 = pt[0].y;
b1 = -3 * pt[0].y + 3 * pt[1].y;
b2 = 3 * pt[0].y - 6 * pt[1].y + 3 * pt[2].y;
b3 = -pt[0].y + 3 * pt[1].y - 3 * pt[2].y + pt[3].y;
float t = 0;
float dt = 0.01;
for (int i = 0; t < 1.1; t += 0.1, i++)
{
bz[i].x = a0 + a1 * t + a2 * t * t + a3 * t * t * t;
bz[i].y = b0 + b1 * t + b2 * t * t + b3 * t * t * t;
}
}
void CalcBSPoints() //B样条曲线
{
/**********请在此处添加你的代码*******/
/************** Begin ************/
float a0, a1, a2, a3, b0, b1, b2, b3;
float t = 0;
float dt = 0.01;
for (int i = 0; t < 1.1; t += 0.1, i++)
{
float T = t;
a0 = (-T * T * T + 3 * T * T - 3 * T + 1) / 6.0;
a1 = (3 * T * T * T - 6 * T * T + 4) / 6.0;
a2 = (-3 * T * T * T + 3 * T * T + 3 * T + 1) / 6.0;
a3 = (T * T * T) / 6.0;
bspt[i].x = pt2[0].x * a0 + pt2[1].x * a1 + pt2[2].x * a2 + pt2[3].x * a3;
bspt[i].y = pt2[0].y * a0 + pt2[1].y * a1 + pt2[2].y * a2 + pt2[3].y * a3;
}
}
/************* end ************/
void ControlPoint()
{
glPointSize(2);
for (int i = 0; i < 4; i++)
{
glBegin(GL_POINTS);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2i(pt[i].x, pt[i].y);
glEnd();
}
}
void PolylineGL(Point* pt, int num)
{
glBegin(GL_LINE_STRIP);
for (int i = 0; i < num; i++)
{
glColor3f(0.0f, 1.0f, 0.0f);
glVertex2i(pt[i].x, pt[i].y);
}
glEnd();
}
void PolylineGL1(Point* pt, int num)
{
glBegin(GL_LINE_STRIP);
for (int i = 0; i < num; i++)
{
glColor3f(1.0f, 1.0f, 1.0f);
glVertex2i(pt[i].x, pt[i].y);
}
glEnd();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 1.0f, 1.0f);
ControlPoint(); //画4个控制点
PolylineGL1(pt, 4); //画4个点之间的线段
CalcBZPoints();
PolylineGL(bz, 11);
PolylineGL1(pt2, 4);
CalcBSPoints();
PolylineGL(bspt, 11);
glFlush();
}
void init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)w, 0.0, (GLdouble)h);
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitWindowPosition(100, 100);
glutInitWindowSize(800, 400); //设置初始化窗口大小
glutCreateWindow("三维观察");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
}