1

我的 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::Render(void) { int x = 0; 诠释 i = 0; glPushMatrix(); glRotatef(angle, xrot, yrot, zrot);//旋转对象 cout<<"valore di nrows è: "<getHeights(j); 浮动向量ba[3]={0.0,0.0,0.0}; 浮动向量cb[3]={0.0,0.0,0.0}; 浮动 xnormal = 0.0; 浮动正常 = 0.0; 浮动 znormal = 0.0; 浮动长度=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 函数转换渲染?

4

1 回答 1

2

如果我明白了,你要正常的靠脸吗?

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

看看这个:

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

顶点法线可以通过面之间的平均法线来计算。

于 2012-08-27T15:09:44.510 回答