2012-09-03 41 views
4

我一直在閱讀一段時間用於紋理地形的不同技術,並遇到了texture splatting。我發現了許多文章,討論如何在OpenGL中做到這一點,但大多數只是在理論上討論它,並提供很少的代碼,我可以研究。有誰知道/有一些代碼在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; 
} 
+0

添加了一些基本代碼... – LucasS

+4

紋理濺(以及幾乎所有的東西)是通過着色器現在完成的。你真的想要一個OpenGL 1.4的答案嗎? – Yno

+1

我寧願任何不涉及着色器的答案。 – LucasS

回答

2

如上面所提到的人,使用可編程管線,使用的着色器。在片段着色器中,您可以傳遞所有紋理,並根據從頂點着色器接收到的頂點數據在它們之間進行插值。

快速搜索給了我this result。我相信那是你需要的。也請看this post。並且this文件很好地解釋了這項技術。