如何使用点原语将纹理映射到由参数方程渲染的球体

时间:2018-01-22 11:05:04

标签: opengl

是否可以将纹理映射到由参数方程生成并使用GL_POINTS原语渲染的Sphere?如果有可能,如何完成并在我的代码中,我从Web上复制代码图像加载代码并按照指示加载它。我没有遵循它们的一件事是指定纹理坐标的顶点,我不知道如何使用Sphere和GL_POINTS原语渲染它时如何准确指定它。

我正在使用旧的OpenGL2进行项目并进行太阳系模拟

以下是repository to the code,它是公开的

这就是我生成球体

的方法
health constant.Weight.UPDATE_TIME

这是我的loadTexture方法

// void Sphere::render () method, inside src/sphere.cpp - line 28

void Sphere::render () {

    unsigned int angle = 0, angle2 = 0;
    const double degree_to_rad = 3.14 / 180.0;
    double value = .0, value2 = .0;

    if ( this -> texture_file_name != "" ) {

        glEnable ( GL_TEXTURE_2D );
        glBindTexture( GL_TEXTURE_2D, this -> texture );

    }

    glBegin ( GL_POINTS );

        for ( ; angle < 360; ++ angle ) {

            value = angle * degree_to_rad;

            for ( ; angle2 < 180; ++ angle2 ) {

                value2 = angle2 * degree_to_rad;

                /*/////////////////////////////////////////////
                // do I need to do sth in here like glTexCoord2d ( ... ) ?
                ////////////////////////////////////////////*/

                glVertex3d (

                    this -> calculateX ( value, value2 ),
                    this -> calculateY ( value, value2 ),
                    this -> calculateZ ( value )

                );

            }

            angle2 = 0;

        }

    glEnd ();

    if ( this -> texture_file_name != "" ) {

        glDisable ( GL_TEXTURE_2D );

    }

};

// void Sphere::draw () method, src/sphere.cpp - line 75

void Sphere::draw () {

    glPushMatrix ();

        glTranslated (

            this -> coordinate [ 0 ],
            this -> coordinate [ 1 ],
            this -> coordinate [ 2 ]

        );

        glRotated (

            this -> angle_degree,
            this -> rotation [ 0 ],
            this -> rotation [ 1 ],
            this -> rotation [ 2 ]

        );

        this -> render ();

    glPopMatrix ();

};

double Sphere::calculateX ( const double theta_degree_angle, const double phi_degree_angle ) {

    return this -> radius * sin ( theta_degree_angle ) * cos ( phi_degree_angle );

};

double Sphere::calculateY ( const double theta_degree_angle, const double phi_degree_angle ) {

    return this -> radius * sin ( theta_degree_angle ) * sin ( phi_degree_angle );

};

double Sphere::calculateZ ( const double theta_degree_angle ) {

    return this -> radius * cos ( theta_degree_angle );

};

这是我的main.cpp

void Object::loadTexture () {

    int & w = this -> texture_width, & h = this -> texture_height;
    unsigned char * data = new unsigned char [ w * h * 3 ];
    FILE * file;

    try {

        file = fopen ( this -> texture_file_name.c_str () , "rb" );

        if ( !file ) return;

        fread ( data, w * h * 3, 1, file );
        fclose ( file );

    } catch ( std::exception & error ) {

        std::cout << "Loading Texture Error: " << error.what () << std::endl;

    }

    glGenTextures ( 1, & this -> texture );
    glBindTexture ( GL_TEXTURE_2D, this -> texture );

    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, data );

    glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );

    glTexParameterf ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
    glTexParameterf ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );

    delete [] data;

};

void Object::setTexture ( const char * file_name, const int width, const int height ) {

    this -> texture_file_name = file_name;
    this -> texture_width = width;
    this -> texture_height = height;

    this -> loadTexture ();

};

仅供参考:该项目表示它是2D,但实际上我只是为了澄清它而在3D中进行。

0 个答案:

没有答案