Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <windows.h>
- #include <gl/glut.h>
- #include <cmath>
- #include <iostream>
- using namespace std;
- GLUnurbsObj* nobj;
- GLfloat ctlarray[][3] = {
- { -0.8, 0.8 },
- { -0.6, -0.7 },
- { 0.0, 0.5 },
- { 0.7, 0.2 }
- };
- GLfloat bp[][4][2] = {
- { { 0.0,0.0 },{ -0.5,-0.95 },{ 0.0,-0.95 },{ 0.5,-0.95 } } ,
- { { -0.95,-0.5 },{ -0.5,-0.5 },{ 0.0,-0.5 },{ 0.5,-0.5 } },
- { { 0.95,0.0 },{ -0.5,0.0 },{ 0.0,0.0 },{ 0.5,0.0 } },
- { { -0.95,0.5 },{ -0.5,0.5 },{ 0.0,0.5 },{ 0.0,0.0 } }
- };
- int fact(int n) {
- int val = 1;
- for (int i = 2; i < n + 1; i++) {
- val = val * i;
- }
- return val;
- }
- int C(int n, int k) {
- return fact(n) / (fact(k) * fact(n - k));
- }
- float BezePoly(int n, int i, float t) {
- int c = C(n, i);
- return c * pow(t, i) * pow(1 - t, n - i);
- }
- float BezePoly3d(int n, int m, int i, int j, float u, float w) {
- int c1 = C(n, i);
- int c2 = C(m, j);
- return c1 * pow(u, i) * pow(1 - u, n - i) * c2 * pow(w, j) * pow(1 - w, m - j);
- }
- void Display()
- {
- glClear(GL_COLOR_BUFFER_BIT);
- glPointSize(3.0);
- glColor3f(0.0, 1.0, 0.0);
- //int n = 3;
- /*glBegin(GL_POINTS);
- for (float t = 0.0; t < 1.0; t = t + 0.01) {
- GLfloat x = 0.0;
- GLfloat y = 0.0;
- for (int i = 0; i <= n; i++) {
- x = x + BezePoly(n, i, t) * ctlarray[i][0];
- y = y + BezePoly(n, i, t) * ctlarray[i][1];
- }
- cout << x << y << endl;
- glVertex3f(x, y, 0.5);
- }
- glEnd();
- glFlush();*/
- int n = 3;
- int m = 3;
- glBegin(GL_POINTS);
- for (int i = 0; i <= n; i++) {
- for (int j = 0; j <= n; j++) {
- glVertex3f(bp[i][j][0], bp[i][j][1], 0);
- }
- }
- for (float u = 0; u <= 1; u += 0.01) {
- for (float w = 0; w <= 1; w += 0.01) {
- GLfloat x = 0.0;
- GLfloat y = 0.0;
- for (int i = 0; i <= n; i++) {
- for (int j = 0; j <= m; j++) {
- x += bp[i][j][0] * BezePoly3d(n, m, i, j, u, w);
- y += bp[i][j][1] * BezePoly3d(n, m, i, j, u, w);
- }
- }
- glVertex3f(x, y, 0);
- }
- }
- glEnd();
- glFlush();
- }
- void main()
- {
- glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE);
- glutInitWindowSize(960, 960);
- glutInitWindowPosition(100, 100);
- glutCreateWindow(" ");
- glutDisplayFunc(Display);
- glutMainLoop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement