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 関数の引数を計算する方法がわかりません。次に、顔に法線を使用する必要があります。この問題で私を助けてくれますか?


はい、面ごとに法線が必要です。その後、頂点ごとに法線を実行する必要があります。面ごとに法線を作成するには、これを行います。int i = 0; glPushMatrix(); glRotatef(angle, xrot, yrot, zrot);// オブジェクトを回転 cout<<"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();
}

処方は正しいですか?法線と正規化の関数を使用してレンダリングを変換するにはどうすればよいですか?

4

1 に答える 1

2

私が理解した場合、通常の顔が必要ですか?

三角形の点があれば、各面の法線を計算できます。

これを見てください:

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

頂点による法線は、面間の平均法線を取得して計算できます。

于 2012-08-27T15:09:44.510 に答える