LeeMcQueen / GameDemo

Demo
0 stars 0 forks source link

布料的黑客帝国效果 就是附加纹理在网格上面成三角函数运动和闪动 #49

Open LeeMcQueen opened 2 years ago

LeeMcQueen commented 2 years ago

■布料fragmentShader

version 330 core

out vec4 color;

in vec3 position; in vec2 texCoord; in vec3 normal;

// Texture Sampler uniform sampler2D uniTex; uniform sampler2D emissionTex;

uniform vec3 uniLightPos; uniform vec3 uniLightColor; uniform float time;

void main() { // Ambient float ambientStrength = 0.5f; vec3 ambient = ambientStrength * uniLightColor;

// Diffuse
vec3 lightDir = normalize(uniLightPos - position);
float diff = max(dot(normal, lightDir), 0.0);
vec3 diffuse = diff * uniLightColor;

// texture() will output the color obtained by sampling the texture with configured conditions
color = texture(uniTex, texCoord);
vec3 objectColor = vec3(color.x, color.y, color.z);

vec3 emissionColor = texture(emissionTex, vec2(0.0, time * 0.001)).rgb;
emissionColor = emissionColor * (sin(time) * 0.001 + 0.1) * 0.5;

vec3 result = (ambient + diffuse) * objectColor * emissionColor;
color = vec4(result, 1.0f);

}

■Display GLuint programID; GLuint vaoID; GLuint vboIDs[3]; GLuint texID; GLuint emissionTexID;

123行 // Assign texture ID and gengeration glGenTextures(1, &texID); glBindTexture(GL_TEXTURE_2D, texID); // Set the texture wrapping parameters (for 2D tex: S, T) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); // Set texture filtering parameters (Minify, Magnify) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); / Load image and configure texture / stbi_set_flip_vertically_on_load(true); int texW, texH, colorChannels; // After loading the image, stb_image will fill them //布料模拟纹理 unsigned char *data = stbi_load("res/box.png", &texW, &texH, &colorChannels, 0); if (data) { glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texW, texH, 0, GL_RGB, GL_UNSIGNED_BYTE, data); // Automatically generate all the required mipmaps for the currently bound texture. glGenerateMipmap(GL_TEXTURE_2D); } else { cout << "Failed to load texture" << endl; } // Always free image memory stbi_image_free(data);

    // Assign texture ID and gengeration
    glGenTextures(1, &emissionTexID);
    glBindTexture(GL_TEXTURE_2D, emissionTexID);
    // Set the texture wrapping parameters (for 2D tex: S, T)

    图片好像是超出的自动复制
    这里使用自动拉伸好像会比较好一点 自动拉伸的参数直接在learnopenglcn里面去找

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    // Set texture filtering parameters (Minify, Magnify)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    /** Load image and configure texture **/
    stbi_set_flip_vertically_on_load(true);
    int texW, texH, colorChannels; // After loading the image, stb_image will fill them
    //布料模拟纹理
    unsigned char *data = stbi_load("res/????????????????.png", &texW, &texH, &colorChannels, 0);
    if (data) {
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texW, texH, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
        // Automatically generate all the required mipmaps for the currently bound texture.
        glGenerateMipmap(GL_TEXTURE_2D);
    }
    else {
        cout << "Failed to load texture" << endl;
    }
    // Always free image memory
    stbi_image_free(data);

205行

unsigned int timeLocation = glGetUniformLocation(programID, "time");
glUniform1f(timeLocation, idleStartTime);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texID);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, emissionTexID);