OpenGL NURBS表面 [英] OpenGL NURBS surface

查看:79
本文介绍了OpenGL NURBS表面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习OpenGL,我想获得一个中间略有驼峰的表面.我目前正在使用此代码,我不确定如何调整ctrl点使其成为我想要的方式.当前类似于

I'm learning OpenGL and I want to get a surface with a slight hump in the middle. I'm currently using this code and im not sure how to adjust the ctrl points to make it the way i want. Its currently like

我想要这样:

我不确定我应该使用哪些控制点,并且我对它的工作方式感到困惑.

im not entirely sure what control points i should use and i'm confused on how it works.

#include <stdlib.h>
#include <GLUT/glut.h>

GLfloat ctrlpoints[4][4][3] = {
   {{-1.5, -1.5, 4.0}, {-0.5, -1.5, 2.0}, 
    {0.5, -1.5, -1.0}, {1.5, -1.5, 2.0}}, 
   {{-1.5, -0.5, 1.0}, {-0.5, -0.5, 3.0}, 
    {0.5, -0.5, 0.0}, {1.5, -0.5, -1.0}}, 
   {{-1.5, 0.5, 4.0}, {-0.5, 0.5, 0.0}, 
    {0.5, 0.5, 3.0}, {1.5, 0.5, 4.0}}, 
   {{-1.5, 1.5, -2.0}, {-0.5, 1.5, -2.0}, 
    {0.5, 1.5, 0.0}, {1.5, 1.5, -1.0}}
};

void display(void)
{
   int i, j;

   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   glColor3f(1.0, 1.0, 1.0);
   glPushMatrix ();
   glRotatef(85.0, 1.0, 1.0, 1.0);
   for (j = 0; j <= 8; j++) {
      glBegin(GL_LINE_STRIP);
      for (i = 0; i <= 30; i++)
         glEvalCoord2f((GLfloat)i/30.0, (GLfloat)j/8.0);
      glEnd();
      glBegin(GL_LINE_STRIP);
      for (i = 0; i <= 30; i++)
         glEvalCoord2f((GLfloat)j/8.0, (GLfloat)i/30.0);
      glEnd();
   }
   glPopMatrix ();
   glFlush();
}

void init(void)
{
   glClearColor (0.0, 0.0, 0.0, 0.0);
   glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4,
           0, 1, 12, 4, &ctrlpoints[0][0][0]);
   glEnable(GL_MAP2_VERTEX_3);
   glMapGrid2f(20, 0.0, 1.0, 20, 0.0, 1.0);
   glEnable(GL_DEPTH_TEST);
   glShadeModel(GL_FLAT);
}
void reshape(int w, int h)
{
   glViewport(0, 0, (GLsizei) w, (GLsizei) 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)w/(GLfloat)h, 
               5.0*(GLfloat)w/(GLfloat)h, -5.0, 5.0, -5.0, 5.0);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
}

int main(int argc, char** argv)
{
   glutInit(&argc, argv);
   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
   glutInitWindowSize (500, 500);
   glutInitWindowPosition (100, 100);
   glutCreateWindow (argv[0]);
   init ();
   glutDisplayFunc(display);
   glutReshapeFunc(reshape);
   glutMainLoop();
   return 0;
}

推荐答案

编辑:我以为您正在尝试,但是我看到代码来自

EDIT: I thought you were experimenting, but I see the code came from the OpenGL tutorial. I glanced through it and understand your point now. It's difficult to learn the basics from there.

掌握NURBS的最佳方法是进行交互操作.然后,您将获得关于边缘定义点(在边缘上),形状定义点(每个其他形状),它们之间的切线关系以及连续性的直觉. NURBS可以由贴片组成,在边缘处缝合在一起,高度连续性受到严格控制-也就是说,您可以为汽车的主体索取G3或为廉价的游戏模型索取C1.从任何描述中都很难理解这个概念.您想通过这种方式获得它,我强烈建议您试用 Rhino Nurbs Modeller .我几年前曾使用过它,现在似乎已经废弃了,但它仍然是其中具有最佳NURBS支持的软件(Autodesk 3d Studio MAX和MAYA的性能更差).不过,这可能会花费一些时间,对于初学者来说,我建议您使用一些简单的方法.从简单贝塞尔曲线编辑器"页面中获取小程序

The best way to get a grasp with NURBS is to play with it interactively. You'd then gain the intuition about the edge-defining points (on the edges), shape defining ones (every other), the tangent-relation between them and continuity. NURBS can be made of patches, stitched together at the edges, where continuity is highly controlled - namely you can ask for G3 for car's main body or C1 for a cheap game model. It's really difficult to get the concept from any description. It you want to get it this way, I'd highly recommend the trial of Rhino Nurbs Modeller. I used it years ago and now it seems abandoned, but still it is the software with one of the best NURBS support out there (Autodesk 3d Studio MAX and MAYA have worse). It might be a little time consuming though, for starters I'd recommend playing with something simpler; take the applet from the "Simple Bezier Curve Editor" page for a spin.

要了解NURBS,最好参考有关Bezier曲线的维基百科文章.一旦掌握了点位置和最终曲线形状之间的关系,就可以轻松地将其概括为曲面.我发现此动画非常直观:

To understand NURBS it's also good to consult Wikipedia Article about Bezier Curves. Once you get a grasp of relation between point position and the final curve shape you can easily generalize it to surfaces. I find this animation highly intuitive:

您可以将示例中的曲面想象成其中的四个曲线的集合,并在上面铺上一块布.使用我之前链接的小程序,您可以操作该位置并获得有关所产生形状的即时反馈.注意t参数-它是沿曲线的坐标,范围为[0,1]. NURBS曲面有两个坐标,按照惯例分别称为uv(这对于绘制功能很重要).

You can imagine the surface from your example as a set of four of those curves, with a cloth spanned on them. Using the applet I linked earlier, you can play with the position and get instant feedback on the resulting shape. Notice the t parameter - it is a coordinate along the curve and has a range of [0, 1]. The NURBS surface have two of those coordinates, by convention called u and v (that's important for the drawing function).

因此,代码中的ctrlpoints结构包含所有点坐标.为了简化说明,这些是四个三次方贝塞尔曲线(来自动画的曲线).对于每条曲线,在3维内有四个点.如果不理会Y轴,则它们全部都位于一个网格上,其中X和Z为:-1.5,-1.0、1.0、1.5.总共可以解释32个值(X表示4x4,Z表示4x4).

So, the ctrlpoints structure from the code holds all point coordinates. Simplifying for explanation, those are four cubic Bezier curves (the ones from the animation). For each curve you have four points within 3 dimensions. If you disregard the Y-axis, then all of them lie on a grid, with X and Z being: -1.5, -1.0, 1.0, 1.5. That explains a total of 32 values (4x4 for X plus 4x4 for Z).

其余为高度,Y值.在您的情况下,它是ctrlpoints中每个点的第二个值.为了获得预期的结果,可以使所有Y值在边缘(外部)相等,在中间略微升高(内部4个).您会得到:

The rest is the height, Y-values. In your case it is the second value of every point in ctrlpoints. To get your expected result, you can make all Y values equal on the edges (outer ones) and slightly raised in the middle (4 inner). You'll get:

用于在图片上方渲染的点:

Points used to render above image:

GLfloat ctrlpoints[4][4][3] = {
 {{-1.5, 1.0, -1.5},  {-0.5, 1.0,-1.5 }, {0.5, 1.0, -1.5 },   {1.5, 1.0,-1.5}}, 
 {{-1.5, 1.0, -0.5},  {-0.5, 2.0,-0.5 }, {0.5, 2.0, -0.5 },   {1.5, 1.0,-0.5}}, 
 {{-1.5, 1.0,  0.5},  {-0.5, 2.0, 0.5 }, {0.5, 2.0,  0.5 },   {1.5, 1.0, 0.5}}, 
 {{-1.5, 1.0,  1.5},  {-0.5, 1.0, 1.5 }, {0.5, 1.0,  1.5 },   {1.5, 1.0, 1.5}}
};
//        ^                   ^                 ^                    ^
//        |                   |                 |                    |
//        |                   |                 |                    |
//        \_________ Those are most relevant - Y-coord, height ______/

带有GLUT的OpenGL中的NURBS-API演练

我看到OpenGL API隐藏了相当相关的细节. NURBS曲面是使用Evaluator绘制的,并使用Map函数定义.

NURBS in OpenGL with GLUT - API walkthrough

I see OpenGL API is hiding quite relevant details. The NURBS surface is drawn using Evaluator and is defined with the Map function.

您应该在init(void)函数中定义控制点,如下所示:

You're supposed to define control points in the init(void) function, like so:

glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4,
           0, 1, 12, 4, &ctrlpoints[0][0][0]);

有关该功能的详细说明,请参见在MSDN网站上获取glMap2f .我们正在传递控制点,它们的类型以及诸如数组步幅和顺序之类的细节.

Good explanation of the function can be found on the MSDN Site for glMap2f. We are passing control points, their type and the details such as array stride and order.

您可以使用Evaluator函数绘制它.它以两个坐标作为参数,并在3d空间中返回一个点.这些输入坐标恰好是我之前提到的动画下的uv.在我们的示例中:

You can draw it using an Evaluator function. It takes two coordinates as arguments and gives back a point in 3d space. Those input coordinates are exactly the u and v I mentioned earlier, under the animation. In our example:

      glBegin(GL_LINE_STRIP); // we'll draw a line

      // take 31 samples of a cross-section of the surface
      for (i = 0; i <= 30; i++)
         // for each sample, evaluate a 3d point
         glEvalCoord2f((GLfloat)i/30.0, (GLfloat)j/8.0);

         // notice j is constant in the loop here, but
         // is being changed by the outer loop.
         //
         // j is iterated in 9 steps, so we'll end up
         // with 9 lines
      glEnd();

我特意省略了外部循环,该循环在这里进行描述:

I deliberately omitted the outer loop, which is described here:

   // we want 9 lines
   for (j = 0; j <= 8; j++) {
      // OpenGL state machine will be used to draw lines

      glBegin(GL_LINE_STRIP);
      // inner loop for j-th line along X

      glBegin(GL_LINE_STRIP);
      // inner loop for j-th line along Z

      glEnd(); // done with the lines
   }

工作示例

#include <stdlib.h>
#include <GL/glut.h>

GLfloat ctrlpoints[4][4][3] = {
 {{-1.5, 1.0, -1.5}, {-0.5, 1.0,-1.5 }, {0.5, 1.0, -1.5 }, {1.5, 1.0,-1.5}}, 
 {{-1.5, 1.0, -0.5}, {-0.5, 2.0,-0.5 }, {0.5, 2.0, -0.5 }, {1.5, 1.0,-0.5}}, 
 {{-1.5, 1.0,  0.5}, {-0.5, 2.0, 0.5 }, {0.5, 2.0,  0.5 }, {1.5, 1.0, 0.5}}, 
 {{-1.5, 1.0,  1.5}, {-0.5, 1.0, 1.5 }, {0.5, 1.0,  1.5 }, {1.5, 1.0, 1.5}}
};

void display(void)
{
   int i, j;

   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   glColor3f(1.0, 1.0, 1.0);
   glPushMatrix();
   glRotatef(25.0, 1.0, 1.0, 1.0);
   for (j = 0; j <= 8; j++) {
      glBegin(GL_LINE_STRIP);
      for (i = 0; i <= 30; i++)
         glEvalCoord2f((GLfloat)i/30.0, (GLfloat)j/8.0);
      glEnd();
      glBegin(GL_LINE_STRIP);
      for (i = 0; i <= 30; i++)
         glEvalCoord2f((GLfloat)j/8.0, (GLfloat)i/30.0);
      glEnd();
   }
   glPopMatrix();
   glFlush();
}

void init(void)
{
   glClearColor(0.0, 0.0, 0.0, 0.0);
   glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4,
           0, 1, 12, 4, &ctrlpoints[0][0][0]);
   glEnable(GL_MAP2_VERTEX_3);
   glMapGrid2f(20, 0.0, 1.0, 20, 0.0, 1.0);
   glEnable(GL_DEPTH_TEST);
   glShadeModel(GL_FLAT);
}

void reshape(int w, int h)
{
   glViewport(0, 0, (GLsizei) w, (GLsizei) 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)w/(GLfloat)h, 
               5.0*(GLfloat)w/(GLfloat)h, -5.0, 5.0, -5.0, 5.0);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
}

int main(int argc, char** argv)
{
   glutInit(&argc, argv);
   glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
   glutInitWindowSize(500, 500);
   glutInitWindowPosition(100, 100);
   glutCreateWindow(argv[0]);
   init();
   glutDisplayFunc(display);
   glutReshapeFunc(reshape);
   glutMainLoop();
   return 0;
}

这篇关于OpenGL NURBS表面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆