#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <Windows.h>
#include "glut.h"
#include <gl\GL.h>
#include <gl\GLU.h>
#include <iostream>
#include <vector>
#include <array>
#include <fstream>
using namespace std;
typedef array<GLfloat, 3> Point3f;
typedef array<GLfloat, 4> Point4f;
typedef vector<Point3f> Points3fVector;
typedef vector<Point4f> Points4fVector;
GLUnurbsObj* nobj = gluNewNurbsRenderer();
inline void drawOpenBspline(GLUnurbsObj* nobj, const Points3fVector& basePts, int order) //order is order of polynom + 1
{
vector<float> knots(basePts.size() + order);
for (int i = 0, c = 0; i < knots.size(); ++i)
{
if (i >= order && knots.size() - i >= order) ++c;
knots[i] = c;
}
gluNurbsCurve(nobj, knots.size(), &knots[0], 3, const_cast<GLfloat*>(&(basePts[0][0])), order, GL_MAP1_VERTEX_3);
}
inline void drawPeriodicBspline(GLUnurbsObj* nobj, const Points3fVector& basePts, int order) //order is order of polynom + 1
{
vector<float> knots(basePts.size() + order);
for (int i = 0; i < knots.size(); ++i)
{
knots[i] = i;
}
gluNurbsCurve(nobj, knots.size(), knots.data(), 3, const_cast<GLfloat*>(&(basePts[0][0])), order, GL_MAP1_VERTEX_3);
}
inline void drawAllPoints(const Points3fVector& points)
{
glBegin(GL_POINTS);
for (const array<GLfloat, 3>& pt : points)
glVertex3f(pt[0], pt[1], pt[2]);
glEnd();
}
inline void drawOpenBspline4(GLUnurbsObj* nobj, const Points4fVector& basePts, int order) //order is order of polynom + 1
{
vector<float> knots(basePts.size() + order);
for (int i = 0, c = 0; i < knots.size(); ++i)
{
if (i >= order && knots.size() - i >= order) ++c;
knots[i] = c;
}
gluNurbsCurve(nobj, knots.size(), &knots[0], 4, const_cast<GLfloat*>(&(basePts[0][0])), order, GL_MAP1_VERTEX_4);
}
inline void drawPeriodicBspline4(GLUnurbsObj* nobj, const Points4fVector& basePts, int order) //order is order of polynom + 1
{
vector<float> knots(basePts.size() + order);
for (int i = 0; i < knots.size(); ++i)
{
knots[i] = i;
}
gluNurbsCurve(nobj, knots.size(), &knots[0], 4, const_cast<GLfloat*>(&(basePts[0][0])), order, GL_MAP1_VERTEX_4);
}
inline void drawAllPoints4(const Points4fVector& points)
{
glBegin(GL_POINTS);
for (const Point4f& pt : points)
glVertex4f(pt[0], pt[1], pt[2], pt[3]);
glEnd();
}
inline Point3f operator -(const Point3f& r, const Point3f& l)
{
return{ r[0] - l[0], r[1] - l[1], r[2] - l[2] };
}
inline Point3f operator +(const Point3f& l, const Point3f& r)
{
return{ r[0] + l[0], r[1] + l[1], r[2] + l[2] };
}
inline Point3f operator *(const Point3f& l, const Point3f::value_type& r)
{
return{ l[0] * r, l[1] * r, l[2] * r };
}
inline Point4f operator *(const Point4f& l, const Point4f::value_type& r)
{
return{ l[0] * r, l[1] * r, l[2] * r, l[3] * r};
}
inline void operator *=(Point4f& pt, const Point4f::value_type& r)
{
for (auto& x : pt)
x *= r;
}
const float PI = 2 * acosf(0);
void Display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0, 1, 0);
glBegin(GL_LINES);
glVertex2d(0, 0);
glVertex2d(0.5, 0);
glVertex2d(0.5, 0);
glVertex2d(0.5, 0.5);
glEnd();
glColor3f(0, 1, 1);
glBegin(GL_TRIANGLES);
glVertex2d(0, 0);
glVertex2d(-1, 0);
glVertex2d(0, -1);
glEnd();
glutPostRedisplay();
glutSwapBuffers();
}
void Display2()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1, 1, 0);
glTranslatef(1e-4f, 0, 0);
glutWireSphere(1, 30, 30);
glutPostRedisplay();
glutSwapBuffers();
}
void Display3()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1, 1, 0);
glPushMatrix();
glTranslatef(0.3f, 0, 0);
glutWireSphere(1, 30, 30);
glPopMatrix();
glutPostRedisplay();
glutSwapBuffers();
}
void Display4()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1, 1, 0);
glRotatef(1e-3, 1, 1, 1);
glutWireSphere(1, 30, 30);
glutPostRedisplay();
glutSwapBuffers();
}
void Display5()
{
gluNurbsProperty(nobj, GLU_SAMPLING_TOLERANCE, 25);
GLfloat ctlarray[4][3] = { { -0.9, -0.8, 0 }, { -0.2, 0.8, 0 }, { 0.2, -0.5, 0 }, { 0.9, 0.8, 0 } };
GLfloat knot[] = { 0, 0, 0, 1, 2, 2, 2 };
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1, 1, 0);
gluNurbsCurve(nobj, sizeof(knot) / sizeof(*knot), knot, 3, &ctlarray[0][0], 3, GL_MAP1_VERTEX_3);
glPointSize(4);
glBegin(GL_POINTS);
for (int i = 0; i < 4; ++i)
{
glVertex3f(ctlarray[i][0], ctlarray[i][1], ctlarray[i][2]);
}
glEnd();
glutPostRedisplay();
glutSwapBuffers();
}
void Display6()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1, 1, 0);
glPushMatrix();
glTranslatef(0.3f, 0.3f, 0);
glRotatef(30, 1, 1, 1);
glutWireCube(0.3);
glPopMatrix();
glPushMatrix();
glTranslatef(-0.3f, 0.3f, 0);
glRotatef(-30, 1, 1, 1);
glutWireCube(0.3);
glPopMatrix();
glutPostRedisplay();
glutSwapBuffers();
}
void Display7()
{
gluNurbsProperty(nobj, GLU_SAMPLING_TOLERANCE, 25);
GLfloat ctlarray[4][3] = { { -0.9, -0.8, 0 }, { -0.2, 0.8, 0 }, { 0.2, -0.5, 0 }, { 0.9, 0.8, 0 } };
GLfloat knot1[] = { 0, 0, 1, 2, 3, 3 };
GLfloat knot2[] = { 0, 0, 0, 1, 2, 2, 2 };
GLfloat knot3[] = { 0, 0, 0, 0, 1, 1, 1, 1 };
GLfloat knot11[] = { 0, 1, 2, 3, 4, 5};
GLfloat knot21[] = { 0, 1, 2, 3, 4, 5, 6 };
GLfloat knot31[] = { 0, 1, 2, 3, 4, 5, 6, 7 };
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1, 0, 0);
gluNurbsCurve(nobj, 6, knot1, 3, &ctlarray[0][0], 2, GL_MAP1_VERTEX_3);
glColor3f(0.7, 0, 0);
gluNurbsCurve(nobj, 6, knot11, 3, &ctlarray[0][0], 2, GL_MAP1_VERTEX_3);
glColor3f(0, 1, 0);
gluNurbsCurve(nobj, 7, knot2, 3, &ctlarray[0][0], 3, GL_MAP1_VERTEX_3);
glColor3f(0, 0.7, 0);
gluNurbsCurve(nobj, 7, knot21, 3, &ctlarray[0][0], 3, GL_MAP1_VERTEX_3);
glColor3f(0, 0, 1);
gluNurbsCurve(nobj, 8, knot3, 3, &ctlarray[0][0], 4, GL_MAP1_VERTEX_3);
glColor3f(0, 0, 0.7);
gluNurbsCurve(nobj, 8, knot31, 3, &ctlarray[0][0], 4, GL_MAP1_VERTEX_3);
glColor3f(1, 1, 1);
glPointSize(4);
glBegin(GL_POINTS);
for (int i = 0; i < 4; ++i)
{
glVertex3f(ctlarray[i][0], ctlarray[i][1], ctlarray[i][2]);
}
glEnd();
glutPostRedisplay();
glutSwapBuffers();
}
void Display8()
{
glClear(GL_COLOR_BUFFER_BIT);
gluNurbsProperty(nobj, GLU_SAMPLING_TOLERANCE, 25);
static float t = 0;
Points3fVector ctlarray = {
{ -0.9f, -0.8f, 0 },
{ -0.4f, 0.4f, 0 },
{ -0.2f, 0.1f, 0 },
{ 0, 0, 0 },
{ 0.2f, -0.1f, 0 },
{ 0.5f, 0.4f, 0 },
{ 0.9f, -0.8f, 0 }
};
glColor3f(0.7, 0, 0);
drawOpenBspline(nobj, ctlarray, 3);
t += 1e-4;
if (t > 4 * acosf(0)) t = 0;
ctlarray[3][0] = 0.1f * cosf(11 * t);
ctlarray[3][1] = sinf(t);
glColor3f(0, sinf(t), 0.7);
drawOpenBspline(nobj, ctlarray, 3);
glColor3f(1, 1, 1);
glPointSize(4);
drawAllPoints(ctlarray);
glutPostRedisplay();
glutSwapBuffers();
}
void Display9()
{
glClear(GL_COLOR_BUFFER_BIT);
gluNurbsProperty(nobj, GLU_SAMPLING_TOLERANCE, 25);
static float t = 0;
glColor3f(0.4, 0, 0);
drawOpenBspline(nobj, {
{ -0.9f, -0.8f, 0 },
{ -0.2f, 0.8f, 0 },
{ 0.2f, -0.5f, 0 },
{ 0.9f, 0.8f, 0 }
}, 4);
glColor3f(0.6, 0, 0.4);
drawOpenBspline(nobj, {
{ -0.9f, -0.8f, 0 },
{ -0.2f, 0.8f, 0 },
{ -0.2f, 0.8f, 0 },
{ 0.2f, -0.5f, 0 },
{ 0.9f, 0.8f, 0 }
}, 4);
glColor3f(0.8, 0.5, 0);
drawOpenBspline(nobj, {
{ -0.9f, -0.8f, 0 },
{ -0.2f, 0.8f, 0 },
{ -0.2f, 0.8f, 0 },
{ -0.2f, 0.8f, 0 },
{ 0.2f, -0.5f, 0 },
{ 0.9f, 0.8f, 0 }
}, 4);
glColor3f(1, 1, 1);
glPointSize(4);
drawAllPoints({ { -0.9f, -0.8f, 0 }, { -0.2f, 0.8f, 0 }, { 0.2f, -0.5f, 0 }, { 0.9f, 0.8f, 0 } });
glutPostRedisplay();
glutSwapBuffers();
}
void Display10()
{
glClear(GL_COLOR_BUFFER_BIT);
gluNurbsProperty(nobj, GLU_SAMPLING_TOLERANCE, 25);
glColor3f(0.8, 0.9, 1.0);
glLineWidth(6);
drawPeriodicBspline(nobj, {
{ -0.9f, -0.8f, 0 },
{ -0.2f, 0.8f, 0 },
{ 0.2f, -0.5f, 0 },
{ 0.9f, 0.8f, 0 }
}, 3);
glLineWidth(3);
glColor3f(0.8 * 0.4, 0.9 * 0.4, 1.0 * 0.4);
drawPeriodicBspline(nobj, {
{ -0.9f, -0.8f, 0 },
{ -0.9f, -0.8f, 0 },
{ -0.2f, 0.8f, 0 },
{ 0.2f, -0.5f, 0 },
{ 0.9f, 0.8f, 0 },
{ 0.9f, 0.8f, 0 }
}, 3);
glColor3f(1, 0, 0);
glLineWidth(6);
drawPeriodicBspline(nobj, {
{ -0.9f, -0.8f, 0.0f },
{ -0.2f, 0.8f, 0.0f },
{ 0.2f, -0.5f, 0.0f },
{ 0.9f, 0.8f, 0.0f }
}, 4);
glLineWidth(3);
glColor3f(0.4, 0, 0);
drawPeriodicBspline(nobj, {
{ -0.9f, -0.8f, 0 },
{ -0.9f, -0.8f, 0 },
{ -0.9f, -0.8f, 0 },
{ -0.2f, 0.8f, 0 },
{ 0.2f, -0.5f, 0 },
{ 0.9f, 0.8f, 0 },
{ 0.9f, 0.8f, 0 },
{ 0.9f, 0.8f, 0 }
}, 4);
glColor3f(1, 1, 1);
glPointSize(4);
drawAllPoints({ { -0.9f, -0.8f, 0 }, { -0.2f, 0.8f, 0 }, { 0.2f, -0.5f, 0 }, { 0.9f, 0.8f, 0 } });
glutPostRedisplay();
glutSwapBuffers();
}
void Display11()
{
glClear(GL_COLOR_BUFFER_BIT);
gluNurbsProperty(nobj, GLU_SAMPLING_TOLERANCE, 25);
glColor3f(0.8, 0.9, 1.0);
glLineWidth(6);
drawPeriodicBspline(nobj, {
{ -0.9f, -0.8f, 0 },
{ -0.2f, 0.8f, 0 },
{ 0.2f, -0.5f, 0 },
{ 0.9f, 0.8f, 0 }
}, 3);
glLineWidth(3);
glColor3f(0.8 * 0.4, 0.9 * 0.4, 1.0 * 0.4);
drawPeriodicBspline(nobj, {
Point3f({ -0.9f, -0.8f, 0 }) * 2 - Point3f({ -0.2f, 0.8f, 0 }),
Point3f({ -0.9f, -0.8f, 0 }) ,
Point3f({ -0.2f, 0.8f, 0 }),
Point3f({ 0.2f, -0.5f, 0 }),
Point3f({ 0.9f, 0.8f, 0 }) ,
Point3f({ 0.9f, 0.8f, 0 }) * 2 - Point3f({ 0.2f, -0.5f, 0 })
}, 3);
glColor3f(1, 0, 0);
glLineWidth(6);
drawPeriodicBspline(nobj, {
{ -0.9f, -0.8f, 0.0f },
{ -0.2f, 0.8f, 0.0f },
{ 0.2f, -0.5f, 0.0f },
{ 0.9f, 0.8f, 0.0f }
}, 4);
glLineWidth(3);
glColor3f(0.4, 0, 0);
drawPeriodicBspline(nobj, {
Point3f({ -0.9f, -0.8f, 0 }) * 2 - Point3f({ -0.2f, 0.8f, 0 }),
Point3f({ -0.9f, -0.8f, 0 }),
Point3f({ -0.2f, 0.8f, 0 }),
Point3f({ 0.2f, -0.5f, 0 }),
Point3f({ 0.9f, 0.8f, 0 }),
Point3f({ 0.9f, 0.8f, 0 }) * 2 - Point3f({ 0.2f, -0.5f, 0 })
}, 4);
glColor3f(1, 1, 1);
glPointSize(4);
drawAllPoints({ { -0.9f, -0.8f, 0 }, { -0.2f, 0.8f, 0 }, { 0.2f, -0.5f, 0 }, { 0.9f, 0.8f, 0 } });
glutPostRedisplay();
glutSwapBuffers();
}
void Display12()
{
static GLUquadricObj* theqw = gluNewQuadric();
static bool inited = false;
if (!inited)
{
inited = true;
glClearColor(0.1, 0.98, 0.3, 1);
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1);
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glRotatef(1, 1, 1, 1);
glColor3f(1, 0, 0);
gluSphere(theqw, 0.3, 50, 50);
glutPostRedisplay();
glutSwapBuffers();
}
void Display13()
{
static GLUquadricObj* theqw = gluNewQuadric();
static bool inited = false;
const GLfloat lightpos[] = { 13, 0, -5, 0 };
if (!inited)
{
inited = true;
glClearColor(0.1, 0.98, 0.3, 1);
glEnable(GL_DEPTH_TEST);
//glEnable(GL_COLOR_MATERIAL);
glLightfv(GL_LIGHT0, GL_POSITION, lightpos);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glRotatef(1e-1, 1e-1, 1e-1, 1e-1);
glColor3f(1, 0, 0);
glutSolidTeapot(0.3);
glutPostRedisplay();
glutSwapBuffers();
}
void Display14()
{
static GLUquadricObj* theqw = gluNewQuadric();
static bool inited = false;
const GLfloat lightpos[] = { 0, 0, 10, 0 };
if (!inited)
{
inited = true;
glClearColor(0.1, 0.98, 0.3, 1);
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glLightfv(GL_LIGHT0, GL_POSITION, lightpos);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-5, 5, -5, 5, 5, 15);
glMatrixMode(GL_MODELVIEW);
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1, 0, 0);
glPushMatrix();
glTranslatef(0, 0, -10);
glRotatef(45, 1, 1, 1);
glutSolidCube(5);
glPopMatrix();
glutPostRedisplay();
glutSwapBuffers();
}
void Display15()
{
static GLUquadricObj* theqw = gluNewQuadric();
static bool inited = false;
const GLfloat lightpos[] = { 0, 0, 10, 0 };
const GLfloat ModelAmbient[] = { 1, 1, 1, 1 };
const GLfloat white_light[] = { 1, 0, 0, 1 };
if (!inited)
{
inited = true;
glClearColor(0.0, 0.0, 0.0, 1.0);
glEnable(GL_DEPTH_TEST);
glLightfv(GL_LIGHT0, GL_AMBIENT, white_light);
glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light);
glLightfv(GL_LIGHT0, GL_SPECULAR, white_light);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ModelAmbient);
glLightfv(GL_LIGHT0, GL_POSITION, lightpos);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-5, 5, -5, 5, 5, 15);
glMatrixMode(GL_MODELVIEW);
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1, 0, 0);
glPushMatrix();
glTranslatef(0, 0, -10);
glRotatef(45, 1, 1, 1);
glutSolidTeapot(2);
glPopMatrix();
glutPostRedisplay();
glutSwapBuffers();
}
void Display16()
{
static GLUnurbsObj* nobj = gluNewNurbsRenderer();
static bool inited = false;
if (!inited){
glClearColor(1, 1, 1, 1);
gluNurbsProperty(nobj, GLU_SAMPLING_TOLERANCE, 25);
inited = true;
}
Points4fVector pts = {
{-1, -0.7f, 0, 1},
{-0.5f, 0.7f, 0, 1},
{0, -0.7f, 0, 1},
{0.5f, 0.7f, 0, 1},
{1, -0.7f, 0, 1}
};
glClear(GL_COLOR_BUFFER_BIT);
glLineWidth(1);
glPointSize(4);
glColor3f(0, 0.5, 1);
drawAllPoints4(pts);
drawOpenBspline4(nobj, pts, 2);
glColor3f(0, 1, 0.1);
glLineWidth(2);
drawOpenBspline4(nobj, pts, 3);
float k;
k = 0.25;
glColor3f(0.1, 1, 0);
pts[2][1] = -0.7 * k;
pts[2][3] = k;
drawOpenBspline4(nobj, pts, 3);
k = 5;
glColor3f(0.1, 1, 0);
pts[2][1] = -0.7 * k;
pts[2][3] = k;
drawOpenBspline4(nobj, pts, 3);
k = 0;
glColor3f(0.1, 1, 0);
pts[2][1] = -0.7 * k;
pts[2][3] = k;
drawOpenBspline4(nobj, pts, 3);
glutPostRedisplay();
glutSwapBuffers();
}
void Display17()
{
static GLUnurbsObj* nobj = gluNewNurbsRenderer();
static bool inited = false;
if (!inited){
glClearColor(1, 1, 1, 1);
gluNurbsProperty(nobj, GLU_SAMPLING_TOLERANCE, 25);
inited = true;
}
Points4fVector pts = {
{ -1, -0.7f, 0, 1 },
{ -0.5f, 0.7f, 0, 1 },
{ 0, -0.7f, 0, 1 },
{ 0.5f, 0.7f, 0, 1 },
{ 1, -0.7f, 0, 1 }
};
glClear(GL_COLOR_BUFFER_BIT);
glLineWidth(1);
glPointSize(4);
glColor3f(0, 0.5, 1);
drawAllPoints4(pts);
drawOpenBspline4(nobj, pts, 2);
glColor3f(0, 1, 0.1);
glLineWidth(2);
drawOpenBspline4(nobj, pts, 4);
float k;
k = 0.25;
glColor3f(0.1, 1, 0);
pts[2][1] = -0.7 * k;
pts[2][3] = k;
drawOpenBspline4(nobj, pts, 4);
k = 5;
glColor3f(0.1, 1, 0);
pts[2][1] = -0.7 * k;
pts[2][3] = k;
drawOpenBspline4(nobj, pts, 4);
k = 0;
glColor3f(0.1, 1, 0);
pts[2][1] = -0.7 * k;
pts[2][3] = k;
drawOpenBspline4(nobj, pts, 4);
glutPostRedisplay();
glutSwapBuffers();
}
void Display18()
{
static GLUnurbsObj* nobj = gluNewNurbsRenderer();
static float t = 0;
static bool inited = false;
if (!inited){
glClearColor(1, 1, 1, 1);
gluNurbsProperty(nobj, GLU_SAMPLING_TOLERANCE, 25);
inited = true;
}
Points4fVector pts = {
{ -1, -0.7f, 0, 1 },
{ -0.5f, 0.7f, 0, 1 },
{ 0, -0.7f, 0, 1 },
{ 0.5f, 0.7f, 0, 1 },
{ 1, -0.7f, 0, 1 }
};
t += 1e-4;
pts[2][1] *= cos(t);
pts[2][3] *= cos(t);
glClear(GL_COLOR_BUFFER_BIT);
glLineWidth(1);
glPointSize(4);
glColor3f(0, 0.5, 1);
drawAllPoints4(pts);
drawOpenBspline4(nobj, pts, 2);
glColor3f(0, 1, 0.1);
glLineWidth(2);
drawOpenBspline4(nobj, pts, 4);
glutPostRedisplay();
glutSwapBuffers();
}
void Display19()
{
static GLUnurbsObj* nobj = gluNewNurbsRenderer();
static float t = 0;
static int sign = +1;
static bool inited = false;
if (!inited){
glClearColor(1, 1, 1, 1);
gluNurbsProperty(nobj, GLU_SAMPLING_TOLERANCE, 25);
inited = true;
}
Points4fVector pts = {
{ -0.7f, -0.7f, 0, 1 },
{ 0, +0.7f, 0, 1 },
{ +0.7f, -0.7f, 0, 1 }
};
float k = t * t * t;
pts[1] *= k;
t += 1e-4f * sign;
if (t < 0)
{
t = 0; sign = +1;
}
if (t > 3)
{
t = 3; sign = -1;
}
glClear(GL_COLOR_BUFFER_BIT);
glLineWidth(1);
glPointSize(4);
glColor3f(0, 0.5, 1);
drawAllPoints4(pts);
drawOpenBspline4(nobj, pts, 2);
glColor3f(0, 1, 0.1);
glLineWidth(2);
drawOpenBspline4(nobj, pts, 3);
glutPostRedisplay();
glutSwapBuffers();
}
void Display20()
{
static GLUnurbsObj* nobj = gluNewNurbsRenderer();
static float t = 0;
static int sign = +1;
static bool inited = false;
if (!inited){
glClearColor(1, 1, 1, 1);
gluNurbsProperty(nobj, GLU_SAMPLING_TOLERANCE, 25);
inited = true;
}
const float side = 0.9f;
Points4fVector up = {
{ -side / 2, 0, 0, 1 },
{ 0, side * sqrtf(3.0f) / 2, 0, 1 },
{ +side / 2, 0, 0, 1 }
};
Points4fVector right = {
{ +side / 2, 0, 0, 1 },
{ +side, -side * sqrtf(3.0f) / 2, 0, 1 },
{ 0, -side * sqrtf(3.0f) / 2, 0, 1 },
};
Points4fVector left = {
{ -side / 2, 0, 0, 1 },
{ -side, -side * sqrtf(3.0f) / 2, 0, 1 },
{ 0, -side * sqrtf(3.0f) / 2, 0, 1 },
};
glClear(GL_COLOR_BUFFER_BIT);
glLineWidth(1);
glPointSize(4);
glColor3f(0, 0.5, 1);
drawAllPoints4(up);
drawAllPoints4(right);
drawAllPoints4(left);
drawOpenBspline4(nobj, up, 2);
drawOpenBspline4(nobj, right, 2);
drawOpenBspline4(nobj, left, 2);
glColor3f(1, 0, 1);
up[1] *= 0.5f;
right[1] *= 0.5f;
left[1] *= 0.5f;
drawOpenBspline4(nobj, up, 3);
drawOpenBspline4(nobj, right, 3);
drawOpenBspline4(nobj, left, 3);
glColor3f(0, 1, 0.1);
glLineWidth(1);
float k = t * t * t;
up[1] *= k;
right[1] *= k;
left[1] *= k;
t += 1e-4f * sign;
if (t < -1)
{
t = -1; sign = +1;
}
if (t > 3)
{
t = 3; sign = -1;
}
drawOpenBspline4(nobj, up, 3);
drawOpenBspline4(nobj, right, 3);
drawOpenBspline4(nobj, left, 3);
glutPostRedisplay();
glutSwapBuffers();
}
void Display21()
{
static GLUnurbsObj* nobj = gluNewNurbsRenderer();
static float t = 0;
static bool inited = false;
if (!inited){
glClearColor(1, 1, 1, 1);
gluNurbsProperty(nobj, GLU_SAMPLING_TOLERANCE, 25);
inited = true;
}
Points4fVector pts = {
{ -1, -0.7f, 0, 1 },
{ -0.5f, 0.7f, 0, 1 },
{ 0, -0.7f, 0, 1 },
{ 0.5f, 0.7f, 0, 1 },
{ 1, -0.7f, 0, 1 }
};
t += 1e-4;
pts[2] *= 5 * abs(cos(t) * cos(t) * cos(t));
glClear(GL_COLOR_BUFFER_BIT);
glLineWidth(1);
glPointSize(4);
glColor3f(0, 0.5, 1);
drawAllPoints4(pts);
drawOpenBspline4(nobj, pts, 2);
glLineWidth(2);
glColor3f(0, 1, 0.1);
drawPeriodicBspline4(nobj, pts, 3);
glColor3f(1, 0, 0.5f);
drawPeriodicBspline4(nobj, pts, 4);
glutPostRedisplay();
glutSwapBuffers();
}
void Display22()
{
static GLUnurbsObj* nobj = gluNewNurbsRenderer();
static float t = 0;
static bool inited = false;
if (!inited){
glClearColor(1, 1, 1, 1);
gluNurbsProperty(nobj, GLU_SAMPLING_TOLERANCE, 25);
inited = true;
}
Points4fVector pts = {
{ -1, -0.7f, 0, 1 },
{ -0.5f, 0.7f, 0, 1 },
{ 0, -0.7f, 0, 1 },
{ 0.5f, 0.7f, 0, 1 },
{ 1, -0.7f, 0, 1 }
};
t += 1e-4;
pts[2] *= 10 * abs(cos(t) * cos(t) * cos(t));
glClear(GL_COLOR_BUFFER_BIT);
glLineWidth(1);
glPointSize(4);
glColor3f(0, 0.5, 1);
drawAllPoints4(pts);
drawOpenBspline4(nobj, pts, 2);
glLineWidth(2);
glColor3f(0, 1, 0.1);
drawOpenBspline4(nobj, pts, 3);
glColor3f(1, 0, 0.5f);
drawOpenBspline4(nobj, pts, 4);
glutPostRedisplay();
glutSwapBuffers();
}
void Display23()
{
static GLfloat ctlarray[3][2][4] =
{
0.5, 0, -0.3, 1,
0.5, 0, 0.3, 1,
0, 0.866 * 0.55, -0.3 * 0.55, 1 * 0.55,
0, 0.866 * 0.55, +0.3 * 0.55, 1 * 0.55,
-0.5, 0, -0.3, 1,
-0.5, 0, 0.3, 1,
};
static GLfloat TexP[] = { 1, 0, 0, 0 };
static GLfloat TexP1[] = { 0, 1, 0, 0 };
static GLfloat TexP2[] = { 0, 0, 1, 0 };
static GLfloat TexI[] = { 255, 0, 0, 255, 0, 0, 255, 255, 0, 255, 255, 0, 0, 255, 0, 0, 255, 0, 0, 0, 255, 0, 0, 255 };
static GLfloat texpts[2][2][2] = { 0, 0, 0, 2, 2, 0, 2, 2 };
static GLUnurbsObj* theNurb = gluNewNurbsRenderer();
static GLUquadricObj* theqw = gluNewQuadric();
static bool inited = false;
if (!inited)
{
glClearColor(0.5, 0.75, 0.75, 1);
glEnable(GL_DEPTH_TEST);
gluNurbsProperty(theNurb, GLU_SAMPLING_TOLERANCE, 25);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage1D(GL_TEXTURE_1D, 0, 3, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, TexI);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
//gluQuadricTexture(theqw, true);
glEnable(GL_TEXTURE_1D);
inited = true;
}
GLfloat knot[] = { 0, 0, 0, 1, 1, 1 };
GLfloat knot1[] = { 0, 0, 1, 1 };
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glRotatef(1e-1, 1, 1, 1);
glColor3f(1, 0, 0);
glEnable(GL_TEXTURE_GEN_S);
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
glTexGenfv(GL_S, GL_OBJECT_PLANE, TexP1);
//glMap2f(GL_MAP2_TEXTURE_COORD_1, 0, 1, 2, 2, 0, 1, 4, 2, &texpts[0][0][0]);
//glEnable(GL_MAP2_TEXTURE_COORD_1);
gluSphere(theqw, 0.3, 50, 50);
//glutSolidTeapot(0.3);
gluBeginSurface(theNurb);
gluNurbsSurface(theNurb, 6, knot, 4, knot1, 2 * 4, 4, &ctlarray[0][0][0], 3, 2, GL_MAP2_VERTEX_4);
gluEndSurface(theNurb);
glutPostRedisplay();
glutSwapBuffers();
}
void Display24()
{
static GLfloat ctlarray[3][2][4] =
{
0.5, 0, -0.3, 1,
0.5, 0, 0.3, 1,
0, 0.866 * 0.55, -0.3 * 0.55, 1 * 0.55,
0, 0.866 * 0.55, +0.3 * 0.55, 1 * 0.55,
-0.5, 0, -0.3, 1,
-0.5, 0, 0.3, 1,
};
static GLfloat TexP[] = { 1, 0, 0, 0 };
static GLfloat TexP1[] = { 0, 1, 0, 0 };
static GLfloat TexP2[] = { 0, 0, 1, 0 };
static GLfloat TexI[] = { 255, 0, 0, 255, 0, 0, 2555, 255, 0, 255, 255, 0, 0, 255, 0, 0, 255, 0, 0, 0, 255, 0, 0, 255 };
static GLfloat texpts[2][2][2] = { 0, 0, 0, 2, 2, 0, 2, 2 };
static GLUnurbsObj* theNurb = gluNewNurbsRenderer();
static GLUquadricObj* theqw = gluNewQuadric();
static bool inited = false;
if (!inited)
{
glClearColor(0.5, 0.75, 0.75, 1);
glEnable(GL_DEPTH_TEST);
gluNurbsProperty(theNurb, GLU_SAMPLING_TOLERANCE, 25);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage1D(GL_TEXTURE_1D, 0, 3, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, TexI);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
gluQuadricTexture(theqw, true);
glEnable(GL_TEXTURE_1D);
inited = true;
}
GLfloat knot[] = { 0, 0, 0, 1, 1, 1 };
GLfloat knot1[] = { 0, 0, 1, 1 };
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glRotatef(1e-1, 1, 1, 1);
glColor3f(1, 0, 0);
//glEnable(GL_TEXTURE_GEN_S);
//glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
//glTexGenfv(GL_S, GL_OBJECT_PLANE, TexP1);
glMap2f(GL_MAP2_TEXTURE_COORD_1, 0, 1, 2, 2, 0, 1, 4, 2, &texpts[0][0][0]);
glEnable(GL_MAP2_TEXTURE_COORD_1);
//gluSphere(theqw, 0.3, 50, 50);
glutSolidTeapot(0.3);
gluBeginSurface(theNurb);
gluNurbsSurface(theNurb, 6, knot, 4, knot1, 2 * 4, 4, &ctlarray[0][0][0], 3, 2, GL_MAP2_VERTEX_4);
gluEndSurface(theNurb);
glutPostRedisplay();
glutSwapBuffers();
}
void file_read(const char *filename, GLubyte*& Iz_RGB)
{
BITMAPFILEHEADER FileHeader;
BITMAPINFOHEADER InfoHeader;
FILE* myFile = fopen(filename, "rb");
fread(&FileHeader, sizeof(FileHeader), 1, myFile);
fread(&InfoHeader, sizeof(InfoHeader), 1, myFile);
fseek(myFile, FileHeader.bfOffBits, SEEK_SET);
int w = InfoHeader.biWidth;
int h = InfoHeader.biHeight;
if (Iz_RGB) delete[] Iz_RGB;
Iz_RGB = new GLubyte[w * h * 3];
fread(Iz_RGB, w * h * 3, 1, myFile);
fclose(myFile);
for (int ii = 0; ii < w * h * 3; ii += 3)
swap(Iz_RGB[ii], Iz_RGB[ii + 2]);
}
void Display25()
{
static GLfloat ctlarray[3][2][4] =
{
0.5, 0, -0.3, 1,
0.5, 0, 0.3, 1,
0, 0.866 * 0.55, -0.3 * 0.55, 1 * 0.55,
0, 0.866 * 0.55, +0.3 * 0.55, 1 * 0.55,
-0.5, 0, -0.3, 1,
-0.5, 0, 0.3, 1,
};
static GLfloat texpts[2][2][2] = { 0, 0, 0, 1, 1, 0, 1, 1 };
static GLUnurbsObj* theNurb = gluNewNurbsRenderer();
static GLUquadricObj* theqw = gluNewQuadric();
static bool inited = false;
if (!inited)
{
glClearColor(0.1, 0.98, 0.3, 1);
glEnable(GL_DEPTH_TEST);
gluNurbsProperty(theNurb, GLU_SAMPLING_TOLERANCE, 25);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
GLubyte* Iz_RGB = nullptr;
file_read("D:\\1.bmp", Iz_RGB);
glTexImage2D(GL_TEXTURE_2D, 0, 3, 256, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, Iz_RGB);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
gluQuadricTexture(theqw, true);
glEnable(GL_TEXTURE_2D);
inited = true;
delete[] Iz_RGB;
}
GLfloat knot[] = { 0, 0, 0, 1, 1, 1 };
GLfloat knot1[] = { 0, 0, 1, 1 };
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glRotatef(1e-1, 1, 1, 1);
glColor3f(1, 0, 0);
glMap2f(GL_MAP2_TEXTURE_COORD_2, 0, 1, 2, 2, 0, 1, 4, 2, &texpts[0][0][0]);
glEnable(GL_MAP2_TEXTURE_COORD_2);
gluSphere(theqw, 0.3, 50, 50);
gluBeginSurface(theNurb);
gluNurbsSurface(theNurb, 6, knot, 4, knot1, 2 * 4, 4, &ctlarray[0][0][0], 3, 2, GL_MAP2_VERTEX_4);
gluEndSurface(theNurb);
glutPostRedisplay();
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(480, 480);
glutInitWindowPosition(100, 100);
glutCreateWindow("HELLO WORLD");
glutDisplayFunc(Display25);
//glutDisplayFunc(Display24);
glutMainLoop();
return 0;
}