GLfloat ctrlpoints[4][4][3] = {
{{-1.5, -1.5, 3.0}, {-0.5, -1.5, 3.0},
{0.5, -1.5, 0.0}, {1.5, -1.5, 3.0}},
{{-1.5, -0.5, 2.0}, {-0.5, -0.5, 3.0},
{0.5, -0.5, 2.0}, {1.5, -0.5, 0.0}},
{{-1.5, 0.5, 3.0}, {-0.5, 0.5, 2.0},
{0.5, 0.5, 4.0}, {1.5, 0.5, 2.5}},
{{-1.5, 1.5, -1.0}, {-0.5, 1.5, -1.0},
{0.5, 1.5, 2.0}, {1.5, 1.5, 0.0}}
};
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
glBegin(GL_QUADS);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f( ctrlpoints[i][j][0], ctrlpoints[i][j][1], ctrlpoints[i][j][2]);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f( ctrlpoints[i][j+1][0], ctrlpoints[i][j+1][1], ctrlpoints[i][j+1][2]);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f( ctrlpoints[i+1][j+1][0], ctrlpoints[i+1][j+1][1], ctrlpoints[i+1][j+1][2]);
glColor3f(1.0f, 1.0f, 0.0f);
glVertex3f( ctrlpoints[i+1][j][0], ctrlpoints[i+1][j][1], ctrlpoints[i+1][j][2]);
glEnd();
}
}
这是把一组网格数据用四边形画出来,但是表面不平滑,请问如何进行平滑处理..请在代码的基础上修改,谢谢!
{{-1.5, -1.5, 3.0}, {-0.5, -1.5, 3.0},
{0.5, -1.5, 0.0}, {1.5, -1.5, 3.0}},
{{-1.5, -0.5, 2.0}, {-0.5, -0.5, 3.0},
{0.5, -0.5, 2.0}, {1.5, -0.5, 0.0}},
{{-1.5, 0.5, 3.0}, {-0.5, 0.5, 2.0},
{0.5, 0.5, 4.0}, {1.5, 0.5, 2.5}},
{{-1.5, 1.5, -1.0}, {-0.5, 1.5, -1.0},
{0.5, 1.5, 2.0}, {1.5, 1.5, 0.0}}
};
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
glBegin(GL_QUADS);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f( ctrlpoints[i][j][0], ctrlpoints[i][j][1], ctrlpoints[i][j][2]);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f( ctrlpoints[i][j+1][0], ctrlpoints[i][j+1][1], ctrlpoints[i][j+1][2]);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f( ctrlpoints[i+1][j+1][0], ctrlpoints[i+1][j+1][1], ctrlpoints[i+1][j+1][2]);
glColor3f(1.0f, 1.0f, 0.0f);
glVertex3f( ctrlpoints[i+1][j][0], ctrlpoints[i+1][j][1], ctrlpoints[i+1][j][2]);
glEnd();
}
}
这是把一组网格数据用四边形画出来,但是表面不平滑,请问如何进行平滑处理..请在代码的基础上修改,谢谢!
解决方案 »
- 资源管理器如何取得一些特殊的文件信息?如语音的时长,位速等。
- 一个困扰我很久的问题,望名位大牛指教!!!
- 弹出窗口的时候,为什么不用AnimateWindow(2000,AW_HIDE|AW_CENTER|AW_SLIDE);这句就无法正常显示?
- OLE Automation的程序,为啥首次运行宿主程序出错,而只要运行过一次那个automation程序,程序就好了?
- Unicode码转换成字符
- 问一个新建类的问题
- ISharp个人信息
- 高分想送!!急!!!!!!!
- 神啊,救救我吧,我这把菜鸟快要崩溃了!!(40分让高手轻松拿走!HELP ME!)
- 急!有关用ASP调用COM组件的问题?代码如下:
- rasphone.exe -d 不支持中文命令行参数吗?
- 以下程序为何我去除了WS_CAPTION风格,依然有标题栏,如何去掉标题栏?
怎么求四边形(GL_QUADS)的法向量?
怎么求四边形(GL_QUADS)的法向量?
怎么求四边形(GL_QUADS)的法向量?
怎么求四边形(GL_QUADS)的法向量?
struct vertex{float x,float y,float z};
struct vertex a,b,c,d;按a,b,c,d顺序,四个顶点组成一个四边形,请问这个四边形的法向量怎么求?谢谢了,要给分的..
/////
///// This returns a perpendicular vector from 2 given vectors by taking the cross product.
/////
/////////////////////////////////////// CROSS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
CPoint3D WINAPI Cross(CPoint3D vVector1, CPoint3D vVector2)
{
CPoint3D vNormal; // The vector to hold the cross product // Once again, if we are given 2 vectors (directions of 2 sides of a polygon)
// then we have a plane define. The cross product finds a vector that is perpendicular
// to that plane, which means it's point straight out of the plane at a 90 degree angle.
// The X value for the vector is: (V1.y * V2.z) - (V1.z * V2.y) // Get the X value
vNormal.x = ((vVector1.y * vVector2.z) - (vVector1.z * vVector2.y));
// The Y value for the vector is: (V1.z * V2.x) - (V1.x * V2.z)
vNormal.y = ((vVector1.z * vVector2.x) - (vVector1.x * vVector2.z));
// The Z value for the vector is: (V1.x * V2.y) - (V1.y * V2.x)
vNormal.z = ((vVector1.x * vVector2.y) - (vVector1.y * vVector2.x)); return vNormal; // Return the cross product (Direction the polygon is facing - Normal)
}
/////////////////////////////////////// VECTOR \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
///// This returns a vector between 2 points
/////
/////////////////////////////////////// VECTOR \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*CPoint3D WINAPI Vector(CPoint3D vPoint1, CPoint3D vPoint2)
{
CPoint3D vVector; // Initialize our variable to zero // In order to get a vector from 2 points (a direction) we need to
// subtract the second point from the first point. vVector.x = vPoint1.x - vPoint2.x; // Get the X value of our new vector
vVector.y = vPoint1.y - vPoint2.y; // Get the Y value of our new vector
vVector.z = vPoint1.z - vPoint2.z; // Get the Z value of our new vector return vVector; // Return our new vector
}
/////////////////////////////////////// MAGNITUDE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
///// This returns the magnitude of a normal (or any other vector)
/////
/////////////////////////////////////// MAGNITUDE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*double WINAPI Magnitude(CPoint3D vNormal)
{
// This will give us the magnitude or "Norm" as some say, of our normal.
// Here is the equation: magnitude = sqrt(V.x^2 + V.y^2 + V.z^2) Where V is the vector return (double)sqrt( (vNormal.x * vNormal.x) + (vNormal.y * vNormal.y) + (vNormal.z * vNormal.z) );
}
/////////////////////////////////////// NORMALIZE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
///// This returns a normalize vector (A vector exactly of length 1)
/////
/////////////////////////////////////// NORMALIZE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*CPoint3D WINAPI Normalize(CPoint3D vNormal)
{
double magnitude = Magnitude(vNormal); // Get the magnitude of our normal // Now that we have the magnitude, we can divide our normal by that magnitude.
// That will make our normal a total length of 1. This makes it easier to work with too. vNormal.x /= magnitude; // Divide the X value of our normal by it's magnitude
vNormal.y /= magnitude; // Divide the Y value of our normal by it's magnitude
vNormal.z /= magnitude; // Divide the Z value of our normal by it's magnitude // Finally, return our normalized normal. return vNormal; // Return the new normal of length 1.
}
/////////////////////////////////////// NORMAL \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
///// This returns the normal of a polygon (The direction the polygon is facing)
/////
/////////////////////////////////////// NORMAL \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*CPoint3D WINAPI Normal(CPoint3DArray& vTriangle)
{ // Get 2 vectors from the polygon (2 sides), Remember the order!
CPoint3D vVector1 = Vector(vTriangle[1], vTriangle[0]);
CPoint3D vVector2 = Vector(vTriangle[2], vTriangle[0]); CPoint3D vNormal = Cross(vVector1, vVector2); // Take the cross product of our 2 vectors to get a perpendicular vector // Now we have a normal, but it's at a strange length, so let's make it length 1. vNormal = Normalize(vNormal); // Use our function we created to normalize the normal (Makes it a length of one) return vNormal; // Return our normal at our desired length
}CPoint3D WINAPI Normal(double * p1,double * p2,double * p3)
{ // Get 2 vectors from the polygon (2 sides), Remember the order!
CPoint3D v1,v2; // Initialize our variable to zero // In order to get a vector from 2 points (a direction) we need to
// subtract the second point from the first point. v1.x = p2[X] - p1[X]; // Get the X value of our new vector
v1.y = p2[Y] - p1[Y]; // Get the Y value of our new vector
v1.z = p2[Z] - p1[Z]; // Get the Z value of our new vector
v2.x = p3[X] - p1[X]; // Get the X value of our new vector
v2.y = p3[Y] - p1[Y]; // Get the Y value of our new vector
v2.z = p3[Z] - p1[Z]; // Get the Z value of our new vector CPoint3D vNormal = Cross(v1,v2); // Take the cross product of our 2 vectors to get a perpendicular vector // Now we have a normal, but it's at a strange length, so let's make it length 1. vNormal = Normalize(vNormal); // Use our function we created to normalize the normal (Makes it a length of one) return vNormal; // Return our normal at our desired length
}