Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 用c++;_C++_Opengl_Opengl Es - Fatal编程技术网

C++ 用c++;

C++ 用c++;,c++,opengl,opengl-es,C++,Opengl,Opengl Es,有人能帮我在OpenGL中计算顶点法线吗? 我正在加载一个obj文件并通过计算顶点法线添加Gouraud着色,而不使用glNormal3f或glLight函数。。 我已经声明了运算符、交叉积、内积等函数。。 我知道,为了得到顶点法线,我首先需要计算曲面法线,也就是带叉积的法向量。。而且 因为我正在加载一个obj文件。。我把obj文件的三个面点放在id1,id2,id3中类似的地方 如果有人能帮我写代码或给我一个如何开始代码的指南,我将不胜感激。请 谢谢 很难画 FACE cur_face = c

有人能帮我在OpenGL中计算顶点法线吗? 我正在加载一个obj文件并通过计算顶点法线添加Gouraud着色,而不使用glNormal3f或glLight函数。。 我已经声明了运算符、交叉积、内积等函数。。 我知道,为了得到顶点法线,我首先需要计算曲面法线,也就是带叉积的法向量。。而且 因为我正在加载一个obj文件。。我把obj文件的三个面点放在id1,id2,id3中类似的地方

如果有人能帮我写代码或给我一个如何开始代码的指南,我将不胜感激。请 谢谢

很难画

FACE cur_face = cube.face[i];
        glColor3f(cube.vertex_color[cur_face.id1].x,cube.vertex_color[cur_face.id1].y,cube.vertex_color[cur_face.id1].z);
        glVertex3f(cube.vertex[cur_face.id1].x,cube.vertex[cur_face.id1].y,cube.vertex[cur_face.id1].z);
        glColor3f(cube.vertex_color[cur_face.id2].x,cube.vertex_color[cur_face.id2].y,cube.vertex_color[cur_face.id2].z);
        glVertex3f(cube.vertex[cur_face.id2].x,cube.vertex[cur_face.id2].y,cube.vertex[cur_face.id2].z);
        glColor3f(cube.vertex_color[cur_face.id3].x,cube.vertex_color[cur_face.id3].y,cube.vertex_color[cur_face.id3].z);
        glVertex3f(cube.vertex[cur_face.id3].x,cube.vertex[cur_face.id3].y,cube.vertex[cur_face.id3].z);
    }
这是计算颜色的方程式

VECTOR kd;
VECTOR ks;
kd=VECTOR(0.8, 0.8, 0.8);
ks=VECTOR(1.0, 0.0, 0.0);
double inner =  kd.InnerProduct(ks);

int i, j;
for(i=0;i<cube.vertex.size();i++)
{
    VECTOR n = cube.vertex_normal[i];
    VECTOR l = VECTOR(100,100,0) - cube.vertex[i];
    VECTOR v = VECTOR(0,0,1) - cube.vertex[i];
    float xl = n.InnerProduct(l)/n.Magnitude();
    VECTOR x = (n * (1.0/ n.Magnitude())) * xl;
    VECTOR r = x - (l-x);

    VECTOR color = kd * (n.InnerProduct(l)) + ks * pow((v.InnerProduct(r)),10);
    cube.vertex_color[i] = color;
向量kd;
向量ks;
kd=向量(0.8,0.8,0.8);
ks=向量(1.0,0.0,0.0);
双内=kd.内积(ks);
int i,j;

对于(i=0;i而言,您似乎只需要实现从
N
向量中获取平均向量的函数。这是一种方法:

struct Vector3f {
    float x, y, z;
};
typedef struct Vector3f Vector3f;

Vector3f averageVector(Vector3f *vectors, int count) {
    Vector3f toReturn;
    toReturn.x = .0f;
    toReturn.y = .0f;
    toReturn.z = .0f;

    // sum all the vectors
    for(int i=0; i<count; i++) {
        Vector3f toAdd = vectors[i];
        toReturn.x += toAdd.x;
        toReturn.y += toAdd.y;
        toReturn.z += toAdd.z;
    }
    // divide with number of vectors
    // TODO: check (count == 0)
    float scale = 1.0f/count;
    toReturn.x *= scale;
    toReturn.y *= scale;
    toReturn.z *= scale;

    return toReturn;
}

使用
{.58f、.58f、.58f}
的标准化平均值,结果几乎是一个极低分辨率的球体,而不是一个立方体。

*这个答案适用于三角形网格,也可以扩展到多边形网格

tempVertices
存储所有顶点的列表

vertexIndices
以矢量(以平面方式)存储网格的面(三角形)的详细信息

std::向量v_法线;
//将顶点法线初始化为0
对于(int i=0;i!=tempVertices.size();i++)
{
v_正常。向后推(glm::vec3(0.0f,0.0f,0.0f));
}
//对于每个面,计算法线并附加到面的相应顶点
for(无符号整数i=0;i
现在将每个
v_normal
标准化并使用。
请注意,顶点法线的数量等于网格的顶点数量。

对于曲面法线,可以使用归一化((B-A)x(C-A))对于您绘制的每个ABC组合。对于顶点法线,我不知道您要计算什么。对于边缘形状,顶点法线与曲面法线相同。但是对于平滑形状,这几乎可以是任何东西,在大多数情况下,您可以找到一个顶点的所有曲面法线,并使用曲面法线的归一化平均值als。由于在某些情况下不明确(例如,绘制迪斯科舞厅球需要曲面法线,球不需要曲面法线,但两个球都有相同的位置数据),因此无法确定使用哪一个球我写了曲面法线的代码!!根据我所学的,法线顶点是曲面的平均值normals@MaticOblakobj文件通常包含V、F和VN。它可能是曲面法线的平均值,但正如我所说的,不适用于边缘部分。例如,长方体(立方体)需要使用曲面法线,每个角顶点实际上有3条法线(每个曲面一个)。无论如何,如果您选择,您将使用曲面法线的平均值来创建顶点法线,这很好。但问题出在哪里?您到底需要什么帮助?找到向量平均值?
{
1.0f, .0f, .0f,
.0f, 1.0f, .0f,
.0f, .0f, 1.0f
}
    std::vector<glm::vec3> v_normal;

    // initialize vertex normals to 0
    for (int i = 0; i != tempVertices.size(); i++)
    {
        v_normal.push_back(glm::vec3(0.0f, 0.0f, 0.0f));
    }

    // For each face calculate normals and append to the corresponding vertices of the face
    for (unsigned int i = 0; i < vertexIndices.size(); i += 3)
    {
        //vi v(i+1) v(i+2) are the three faces of a triangle
        glm::vec3 A = tempVertices[vertexIndices[i] - 1];
        glm::vec3 B = tempVertices[vertexIndices[i + 1] - 1];
        glm::vec3 C = tempVertices[vertexIndices[i + 2] - 1];
        glm::vec3 AB = B - A;
        glm::vec3 AC = C - A;
        glm::vec3 ABxAC = glm::cross(AB, AC);
        v_normal[vertexIndices[i] - 1] += ABxAC;
        v_normal[vertexIndices[i + 1] - 1] += ABxAC;
        v_normal[vertexIndices[i + 2] - 1] += ABxAC;
    }