如何计算三角形带中的NORMAL



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

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::渲染(void){int x=0;int i=0;glPushMatrix();glRotatef(角度、xrot、yrot、zrot)//旋转对象cout<lt;"valore di nrowsè:"<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();
}

这个公式是对的?如何使用法线和规格化函数转换渲染?

如果我理解了,你想要通过脸的法线吗?

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

看看这个:

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

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

相关内容

  • 没有找到相关文章

最新更新