2

我在理解我的 OpenGL 应用程序和我的 GLSL 文件(.ver​​t 和 .frag)之间的连接时遇到了一些困难。我知道如何创建顶点或片段着色器,并且网络上有很多示例,但我很难在我的应用程序中实际使用我的着色器,特别是将(绑定?)纹理链接到我的着色器。我的问题是,我缺少什么代码来使用我上传到我的应用程序和着色器的纹理?

我当前的应用程序创建一个窗口,加载 4 个纹理(岩石、草、石头和一个混合贴图,因为我想用它来进行纹理喷溅),然后在主循环中绘制一个四边形。我的整个应用程序代码如下:

#include <windows.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <SFML/Graphics.hpp>
#include <glew.h>
#include <gl/gl.h>
#include <gl/glu.h>

#include "textfile.h"
#include "textfile.cpp"

using namespace std;

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 );
    }
};

///Shader
GLuint p, f, v;
void setShader() {

    char *vs,*fs;

    v = glCreateShader(GL_VERTEX_SHADER);
    f = glCreateShader(GL_FRAGMENT_SHADER);

    vs = textFileRead("shader.vert");
    fs = textFileRead("shader.frag");

    const char * vv = vs;
    const char * ff = fs;

    glShaderSource(v, 1, &vv,NULL);
    glShaderSource(f, 1, &ff,NULL);

    free(vs);free(fs);

    glCompileShader(v);
    glCompileShader(f);

    p = glCreateProgram();

    glAttachShader(p,v);
    glAttachShader(p,f);

    glLinkProgram(p);
    glUseProgram(p);
}

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);

    ///Terrain Textures
    sf::Image tex0;
    tex0.loadFromFile("mixmap.png");
    sf::Image tex1;
    tex1.loadFromFile("grass.png");
    sf::Image tex2;
    tex2.loadFromFile("rock.png");
    sf::Image tex3;
    tex3.loadFromFile("stone.png");

    GLuint mixmap;
    glGenTextures(1, &mixmap);
    glBindTexture(GL_TEXTURE_2D, mixmap);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, tex0.getSize().x, tex0.getSize().y, 0, GL_RGBA, GL_UNSIGNED_BYTE, (const GLvoid*)tex0.getPixelsPtr() );

    GLuint grass;
    glGenTextures(1, &grass);
    glBindTexture(GL_TEXTURE_2D, grass);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, tex1.getSize().x, tex1.getSize().y, 0, GL_RGBA, GL_UNSIGNED_BYTE, (const GLvoid*)tex1.getPixelsPtr() );

    GLuint rock;
    glGenTextures(1, &rock);
    glBindTexture(GL_TEXTURE_2D, rock);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, tex2.getSize().x, tex2.getSize().y, 0, GL_RGBA, GL_UNSIGNED_BYTE, (const GLvoid*)tex2.getPixelsPtr() );

    GLuint stone;
    glGenTextures(1, &stone);
    glBindTexture(GL_TEXTURE_2D, stone);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, tex3.getSize().x, tex3.getSize().y, 0, GL_RGBA, GL_UNSIGNED_BYTE, (const GLvoid*)tex3.getPixelsPtr() );

    ///I am afrain I am missing some code here that will actually link the textures to the shader itself

    ///Shader
    glewInit();
    setShader();

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

        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);

        glBegin(GL_QUADS);
            glVertex3f(-0.5, -0.5, 0.0);

            glVertex3f(-0.5, 0.5, 0.0);

            glVertex3f(0.5, 0.5, 0.0);

            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;
}

下面也是我的顶点和片段着色器,虽然我不确定我的顶点着色器是否完整......

顶点:

void main()
{
 gl_TexCoord[0] = gl_MultiTexCoord0;
 gl_Position = ftransform();
} 

分段:

uniform sampler2D MixMap;
uniform sampler2D Grass;
uniform sampler2D Stone;
uniform sampler2D Rock;

varying vec4 texCoord;

void main(void)
{
   vec4 mixmap   = texture2D( MixMap, texCoord.xy );
   vec4 tex0    = texture2D( Grass, texCoord.xy ); 
   vec4 tex1    = texture2D( Rock,  texCoord.xy ); 
   vec4 tex2    = texture2D( Stone, texCoord.xy );

   tex0 *= mixmap.r; 
   tex1 = mix( tex0, tex1, mixmap.g ); 
   vec4 outColor = mix( tex1, tex2, mixmap.b ); 

   gl_FragColor = outColor;
}
4

2 回答 2

5

首先,您需要将每个纹理对象绑定到 OpenGL 上下文中的纹理单元。GPU 有GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS许多纹理图像单元,每个单元都可以附加纹理。

这看起来像这样:

glActiveTexture(GL_TEXTURE0 + 0);
glBindTexture(GL_TEXTURE_2D, mixmap); //mixmap now attached to sampler 0

glActiveTexture(GL_TEXTURE0 + 1);
glBindTexture(GL_TEXTURE_2D, grass); //attached to sampler 1

glActiveTexture(GL_TEXTURE0 + 2);
glBindTexture(GL_TEXTURE_2D, rock);  //attached to sampler 2

glActiveTexture(GL_TEXTURE0 + 3);
glBindTexture(GL_TEXTURE_2D, stone); //attached to sampler 3

注意GL_TEXTURE0+ x语法。这样做是为了让您不会因为GL_TEXTUREi只达到GL_TEXTURE32;而感到束缚。您可以同时拥有超过 32 个多纹理(如果您的实现支持它)。当然,我不知道你为什么想要,但你可以。

现在您有四个纹理绑定到四个纹理图像单元。下一步是将 GLSL 采样器分配给纹理图像单元。链接着色器后,您必须查询每个采样器的统一位置,然后告诉程序它应该在哪个纹理图像单元中查找实际纹理。

compileAndLinkProgram();
glUseProgram(program);

mixmapUniformLocation = glGetUniformLocation(program, "MixMap"); //get the uniform associated with "MixMap"
glUniform1i(mixmapUniformLocation, 0); //set MixMap uniform to 0 (sampler 0);


grassUniformLocation = glGetUniformLocation(program, "Grass"); //get the uniform associated with "Grass"
glUniform1i(grassUniformLocation, 1); //set Grass uniform to 1 (sampler 1);

...repeat for 4 samplers...

现在,当您在着色器中对采样器进行采样时,它将从绑定到该采样器的纹理中提取。

于 2012-09-07T22:52:01.747 回答
0

您需要同时使用glGetUniformLocationglUniform来告诉着色器程序哪些纹理附加到哪些采样器。

此处的示例: GLSL 中的多个纹理 - 只有一个有效

于 2012-09-07T22:14:43.610 回答