4

我已经阅读了一段时间用于纹理地形的不同技术并遇到了纹理飞溅。我发现很多文章都在讨论如何在 OpenGL 中做到这一点,但大多数都只是理论上讨论它并且几乎没有提供我可以研究的代码。有谁知道/有一些代码可以在 OpenGL 中说明这一点?

澄清一下,我希望能够加载四种不同的纹理,并根据四边形/顶点的高度,逐渐将纹理从一个更改为下一个。

编辑:下面是一些快速的代码,可以帮助显示我想知道的内容

#include <windows.h>
#include <SFML/Graphics.hpp>
#include <gl/gl.h>
#include <gl/glu.h>

#define GL_CLAMP_TO_EDGE 0x812F

class Scene {
public:
    void resize( int w, int h ) {
        // OpenGL Reshape
        glViewport( 0, 0, w, h );
        glMatrixMode( GL_PROJECTION );
        glLoadIdentity();
        gluPerspective( 120.0, (GLdouble)w/(GLdouble)h, 0.5, 500.0 );
        glMatrixMode( GL_MODELVIEW );
    }
};

int main() {

    sf::RenderWindow window(sf::VideoMode(800, 600, 32), "Test");

    ///Setup the scene, materials, lighting
    Scene scene;
    scene.resize(800,600);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHTING);
    glColorMaterial(GL_FRONT_AND_BACK, GL_EMISSION);
    glEnable(GL_COLOR_MATERIAL);
    glShadeModel(GL_SMOOTH);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glEnable(GL_LIGHT0);
    float XL = .5, YL = .1, ZL = 1;
    GLfloat ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
    GLfloat diffuseLight[] = { 0.8f, 0.8f, 0.8, 1.0f };
    GLfloat specularLight[] = { 0.5f, 0.5f, 0.5f, 1.0f };
    GLfloat lightpos[] = {XL, YL, ZL, 0.};
    glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
    glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
    glLightfv(GL_LIGHT0, GL_POSITION, lightpos);

    ///Test terrain texture splatting

    ///Load the textures
    sf::Image tex1;
    tex1.loadFromFile("texture1.png");
    sf::Image tex2;
    tex2.loadFromFile("texture2.png");

    ///Set the first texture
    GLuint grass;
    glGenTextures(1, &grass);
    glBindTexture(GL_TEXTURE_2D, grass);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, tex1.getSize().x, tex1.getSize().y, 0, GL_RGBA, GL_UNSIGNED_BYTE, (const GLvoid*)tex1.getPixelsPtr() );

    ///Set the second texture
    GLuint dirt;
    glGenTextures(1, &dirt);
    glBindTexture(GL_TEXTURE_2D, dirt);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, tex2.getSize().x, tex2.getSize().y, 0, GL_RGBA, GL_UNSIGNED_BYTE, (const GLvoid*)tex2.getPixelsPtr() );

    ///Start loop
    while( window.isOpen() ) {
        sf::Event event;
        while( window.pollEvent( event ) ) {
            if( event.type == sf::Event::Closed )
                window.close();
        }

        ///Clear buffer and set camera
        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(50.0, 1.0, 1.0, 50);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        gluLookAt(1, 0, 1, 0, 0, 0, 0, 1, 0);

        ///Begin rendering quad             
        glEnable(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D, grass);
        ///I know that around here I should enable blending in order to get my two textures to mix, but I am not certain
        glBegin(GL_QUADS);

            glTexCoord2f(0, 0);
            glVertex3f(-0.5, -0.5, 0.0);

            glTexCoord2f(1, 0);
            glVertex3f(-0.5, 0.5, 0.0);

            glTexCoord2f(1, 1);
            glVertex3f(0.5, 0.5, 0.0);

            glTexCoord2f(0, 1);
            glVertex3f(0.5, -0.5, 0.0);
        glEnd();

        ///Reset env settings for SFML
        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

        window.display();
    }
    return 1;
}
4

1 回答 1

2

如上所述,使用可编程管道,使用着色器。在片段着色器中,您可以传递所有纹理并根据从顶点着色器接收到的顶点数据在它们之间进行插值。

快速搜索给了我这个结果。我相信这就是你所需要的。也看看这个帖子这篇论文很好地解释了这项技术。

于 2012-09-04T07:24:06.893 回答