如何计算三角形条带中的NORMAL

时间:2012-08-27 10:19:28

标签: opengl light normalize normals gl-triangle-strip

我的OPENGL项目照明有问题。 我使用ECLIPSE在c ++上编程。 我有一个由三角形条带构造的terrein。我的渲染代码如下:

HeightMap::~HeightMap(void) {
}

float HeightMap::getScaledGrayColor(double height) {
float fColor = NULL;
if (height == this->NODATA_value) {
    return 0.0f;
}
fColor = ((255.00f / max) * height) / 255.00f;

return fColor;
}
double* HeightMap::getHeights(int rowNumber) {
rowNumber = rowNumber + 6;

ifstream fin(this->filename);
string s;
long length;

fin.seekg(0, ios::beg); // go to the first line

for (int i = 0; i < rowNumber; i++) { // loop 'till the desired line
    getline(fin, s);
}

string nextToken;
getline(fin, nextToken);
StringUtils stringUtils;
vector<string> tempVec = stringUtils.split(nextToken, " ");
double* heights = new double[this->ncols];
for (int i = 0; i < tempVec.size() - 1; i++) {
    heights[i] = strtod(tempVec[i].c_str(), NULL);
}
return heights;
}


void HeightMap::Render(void)
{
int x = 0;
int i = 0;
for (int i = 0, j = 0; j < nrows; i--, j++) {
    //cout<<"valore di i è: "<<i<<endl;
    //cout<<"valore di j è: "<<j<<endl;
    double * rowHeights = this->getHeights(j);//
    for (int x = 0; x < ncols; x++) {

        float color = getScaledGrayColor(rowHeights[x]);
        double * Quote = this->getHeights(x);
        glBegin(GL_TRIANGLE_STRIP);
        glColor3f(color, color, color);

        //triangolo ABC
        //glNormal3f((x * cellsize), (i * cellsize), 0);
        glVertex3f((x * cellsize), (i * cellsize), 0); //vertice base
        //glNormal3f((x * cellsize), ((i * cellsize) + cellsize), 0);
        glVertex3f((x * cellsize), ((i * cellsize) + cellsize), 
0);//vertice base
        //glNormal3f(((x * cellsize) + (cellsize / 2)), ((i * cellsize)+ 
(cellsize / 2)), *Quote);//vertice altezza
        glVertex3f(((x * cellsize) + (cellsize / 2)), ((i * cellsize)
                + (cellsize / 2)), *Quote);//vertice altezza


        //triangolo DEF
        //glNormal3f((x * cellsize), (i * cellsize) + cellsize, 0);
        glVertex3f((x * cellsize), (i * cellsize) + cellsize, 0); 
//vertice base
        //glNormal3f((x * cellsize) + cellsize, (i * cellsize) + cellsize, 
0);//vertice base
        glVertex3f((x * cellsize) + cellsize, (i * cellsize) + cellsize, 
0);//vertice base
        //glNormal3f(((x * cellsize) + (cellsize / 2)), ((i * cellsize)+ 
(cellsize / 2)), *Quote);//vertice altezza
        glVertex3f(((x * cellsize) + (cellsize / 2)), ((i * cellsize)
                            + (cellsize / 2)), 
*Quote);//vertice altezza

        //triangolo GHI
        //glNormal3f((x * cellsize) + cellsize, (i * cellsize) + cellsize, 
0); //vertice base
        glVertex3f((x * cellsize) + cellsize, (i * cellsize) + cellsize, 
0); //vertice base
        //glNormal3f((x * cellsize) + cellsize, i * cellsize, 0);//vertice 
base
        glVertex3f((x * cellsize) + cellsize, i * cellsize, 0);//vertice 
base
        //glNormal3f(((x * cellsize) + (cellsize / 2)), ((i * cellsize)+ 
(cellsize / 2)), *Quote);//vertice altezza
        glVertex3f(((x * cellsize) + (cellsize / 2)), ((i * cellsize)
                            + (cellsize / 2)), 
*Quote);//vertice altezza

        //triangolo LMN
        //glNormal3f((x * cellsize), (i * cellsize), 0); //vertice base
        glVertex3f((x * cellsize), (i * cellsize), 0); //vertice base
        //glNormal3f((x * cellsize) + cellsize, (i * cellsize), 
0);//vertice base
        glVertex3f((x * cellsize) + cellsize, (i * cellsize), 0);//vertice 
base
        //glNormal3f(((x * cellsize) + (cellsize / 2)), ((i * cellsize)+ 
(cellsize / 2)), *Quote);//vertice altezza
        glVertex3f(((x * cellsize) + (cellsize / 2)), ((i * cellsize)
                            + (cellsize / 2)), 
*Quote);//vertice altezza
        glEnd();
        }
    //}
}

}

我尝试使用普通的顶点,但我不知道如何计算glnormal函数的参数。 然后我必须使用正常面部。 你能帮我解决这个问题吗?


是的,我需要正常的脸,然后我必须通过顶点做正常: 对于正常的面对我这样做     void HeightMap :: Render(void)     {     int x = 0;     int i = 0;     glPushMatrix();     glRotatef(angle,xrot,yrot,zrot); //旋转对象     cout&lt;&lt;“valore dinrowsè:”&lt; getHeights(j);         float vectorba [3] = {0.0,0.0,0.0};         float vectorcb [3] = {0.0,0.0,0.0};         float xnormal = 0.0;         float ynormal = 0.0;         float znormal = 0.0;         float len = 0.0;

    for (int x = 0; x < ncols; x++) {
        //if (rowHeights[x] != this->NODATA_value) {
        float color = getScaledGrayColor(rowHeights[x]);
        double * Quote = this->getHeights(x);
        glBegin(GL_TRIANGLE_STRIP);
        glColor3f(color, color, color);
        //triangolo ABC

        //inizio normal
        vectorba[0]={(x * cellsize)-(x * cellsize)};
        vectorba[1]={((i * cellsize) + cellsize)-(i * cellsize)};
        vectorcb[0]={((x * cellsize) + (cellsize / 2))-(x * cellsize)};
        vectorcb[1]={((i * cellsize) + (cellsize / 2))-((i * cellsize)     +cellsize)};
        vectorcb[2]=*Quote;
        // calculate the cross product and place the resulting vector
        // into the variable xnormal,ynormal e znormal
        xnormal = (vectorba[1] * vectorcb[2]) - (vectorba[2] *     vectorcb[1]);
        ynormal = (vectorba[2] * vectorcb[0]) - (vectorba[0] * vectorcb[2]);
        znormal = (vectorba[0] * vectorcb[1]) - (vectorba[1] * vectorcb[0]);
        //fine normal

        //inizio normalize
        // calculate the length of the vector
            len = (float)(sqrt((xnormal * xnormal) + (ynormal * ynormal) + (znormal * znormal)));

            // avoid division by 0
            if (len == 0.0f)
                len = 1.0f;

            // reduce to unit size
            xnormal /= len;
            ynormal /= len;
            znormal /= len;
            //fine normalize

        //glNormal3f((x * cellsize), (i * cellsize), 0); //normal per vertex. controllare la formula
        glVertex3f((x * cellsize), (i * cellsize), 0); //vertice base A
        //glNormal3f((x * cellsize), ((i * cellsize) + cellsize), 0);
        glVertex3f((x * cellsize), ((i * cellsize) + cellsize), 0);//vertice base B

        glNormal3f(xnormal, ynormal, znormal); //normal per face

        //glNormal3f(((x * cellsize) + (cellsize / 2)), ((i * cellsize)+ (cellsize / 2)), *Quote);//vertice altezza
        glVertex3f(((x * cellsize) + (cellsize / 2)), ((i * cellsize)
                + (cellsize / 2)), *Quote);//vertice altezza C


        //triangolo DEF

        //inizio normal
        vectorba[0]={(x * cellsize) + cellsize-(x * cellsize)};
        vectorba[1]={((i * cellsize) + cellsize)-((i * cellsize) + cellsize)};
        vectorcb[0]={((x * cellsize) + (cellsize / 2))-(x * cellsize) + cellsize};
        vectorcb[1]={((i * cellsize)+ (cellsize / 2))-((i * cellsize) + cellsize)};
        vectorcb[2]=*Quote;
        // calculate the cross product and place the resulting vector
        // into the variable xnormal,ynormal e znormal
        xnormal = (vectorba[1] * vectorcb[2]) - (vectorba[2] * vectorcb[1]);
        ynormal = (vectorba[2] * vectorcb[0]) - (vectorba[0] * vectorcb[2]);
        znormal = (vectorba[0] * vectorcb[1]) - (vectorba[1] * vectorcb[0]);
        //fine normal

        //inizio normalize
        // calculate the length of the vector
        len = (float)(sqrt((xnormal * xnormal) + (ynormal * ynormal) + (znormal * znormal)));

        // avoid division by 0
        if (len == 0.0f)
             len = 1.0f;

        // reduce to unit size
        xnormal /= len;
        ynormal /= len;
        znormal /= len;
        //fine normalize

        //glNormal3f((x * cellsize), (i * cellsize) + cellsize, 0);
        glVertex3f((x * cellsize), (i * cellsize) + cellsize, 0); //vertice base D
        //glNormal3f((x * cellsize) + cellsize, (i * cellsize) + cellsize, 0);//vertice base
        glVertex3f((x * cellsize) + cellsize, (i * cellsize) + cellsize, 0);//vertice base E

        glNormal3f(xnormal, ynormal, znormal); //normal per face

        //glNormal3f(((x * cellsize) + (cellsize / 2)), ((i * cellsize)+ (cellsize / 2)), *Quote);//vertice altezza
        glVertex3f(((x * cellsize) + (cellsize / 2)), ((i * cellsize)+ (cellsize / 2)), *Quote);//vertice altezza F

        //triangolo GHI

        //inizio normal
        vectorba[0]={(x * cellsize) + cellsize-(x * cellsize) + cellsize};
        vectorba[1]={i * cellsize-(i * cellsize) + cellsize};
        vectorcb[0]={((x * cellsize) + (cellsize / 2))-(x * cellsize) + cellsize};
        vectorcb[1]={((i * cellsize)+ (cellsize / 2))-(i * cellsize)};
        vectorcb[2]=*Quote;
        // calculate the cross product and place the resulting vector
        // into the variable xnormal,ynormal e znormal
        xnormal = (vectorba[1] * vectorcb[2]) - (vectorba[2] * vectorcb[1]);
        ynormal = (vectorba[2] * vectorcb[0]) - (vectorba[0] * vectorcb[2]);
        znormal = (vectorba[0] * vectorcb[1]) - (vectorba[1] * vectorcb[0]);
        //fine normal

        //inizio normalize
        // calculate the length of the vector
        len = (float)(sqrt((xnormal * xnormal) + (ynormal * ynormal) + (znormal * znormal)));
        // avoid division by 0
        if (len == 0.0f)
             len = 1.0f;

        // reduce to unit size
        xnormal /= len;
        ynormal /= len;
        znormal /= len;
        //fine normalize

        //glNormal3f((x * cellsize) + cellsize, (i * cellsize) + cellsize, 0); //vertice base
        glVertex3f((x * cellsize) + cellsize, (i * cellsize) + cellsize, 0); //vertice base G
        //glNormal3f((x * cellsize) + cellsize, i * cellsize, 0);//vertice base
        glVertex3f((x * cellsize) + cellsize, i * cellsize, 0);//vertice base H

        glNormal3f(xnormal, ynormal, znormal); //normal per face

        //glNormal3f(((x * cellsize) + (cellsize / 2)), ((i * cellsize)+ (cellsize / 2)), *Quote);//vertice altezza
        glVertex3f(((x * cellsize) + (cellsize / 2)), ((i * cellsize)+ (cellsize / 2)), *Quote);//vertice altezza I

        //triangolo LMN

        //inizio normal
        vectorba[0]={(x * cellsize) + cellsize-(x * cellsize)};
        vectorba[1]={(i * cellsize)-(i * cellsize)};
        vectorcb[0]={((x * cellsize) + (cellsize / 2))-(x * cellsize) + cellsize};
        vectorcb[1]={((i * cellsize)+ (cellsize / 2))-(i * cellsize)};
        vectorcb[2]=*Quote;
        // calculate the cross product and place the resulting vector
        // into the variable xnormal,ynormal e znormal
        xnormal = (vectorba[1] * vectorcb[2]) - (vectorba[2] * vectorcb[1]);
        ynormal = (vectorba[2] * vectorcb[0]) - (vectorba[0] * vectorcb[2]);
        znormal = (vectorba[0] * vectorcb[1]) - (vectorba[1] * vectorcb[0]);
        //fine normal

        //inizio normalize
        // calculate the length of the vector
        len = (float)(sqrt((xnormal * xnormal) + (ynormal * ynormal) + (znormal * znormal)));
        // avoid division by 0
        if (len == 0.0f)
             len = 1.0f;
        // reduce to unit size
        xnormal /= len;
        ynormal /= len;
        znormal /= len;
        //fine normalize

        //glNormal3f((x * cellsize), (i * cellsize), 0); //vertice base
        glVertex3f((x * cellsize), (i * cellsize), 0); //vertice base L
        //glNormal3f((x * cellsize) + cellsize, (i * cellsize), 0);//vertice base
        glVertex3f((x * cellsize) + cellsize, (i * cellsize), 0);//vertice base M

        glNormal3f(xnormal, ynormal, znormal); //normal per face

        //glNormal3f(((x * cellsize) + (cellsize / 2)), ((i * cellsize)+ (cellsize / 2)), *Quote);//vertice altezza
        glVertex3f(((x * cellsize) + (cellsize / 2)), ((i * cellsize)+     (cellsize / 2)), *Quote);//vertice altezza N
        glEnd();
        }
    //}
}
glPopMatrix();
}

配方是对的? 如何使用normal和normalize函数转换Render?

1 个答案:

答案 0 :(得分:2)

如果我明白了,你想要面对正常吗?

如果你有三角形点,你可以计算每个面的法线。

看看这个:

http://www.opengl.org/wiki/Calculating_a_Surface_Normal

可以使用面之间的平均法线计算顶点法线。