zoukankan      html  css  js  c++  java
  • OpenGL学习(4)——纹理(补)

    完成章节后练习

    练习

    1. Make sure only the happy face looks in the other/reverse direction by changing the fragment shader.

    #version 330 core
    out vec4 FragColor;
    in vec4 Color;
    in vec2 texCoord;
    uniform sampler2D texSampler1;
    uniform sampler2D texSampler2;
    void main()
    {
        FragColor = mix(texture(texSampler1, texCoord)*Color, texture(texSampler2, vec2(-texCoord.x, texCoord.y)), 0.2);
    }
    

    2. Experiment with the different texture wrapping methods by specifying texture coordinates in the range 0.0f to 2.0f. See if you can display 4 smiley faces on a single container image clamped at its edge.

    glBindTexture(GL_TEXTURE_2D, texture[0]);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    

    3. Try to display only the center pixels of the texture image on the rectangle in such a way that the individual pixels are getting visible by changing the texture coordinates. Try to set the texture filtering method to GL_NEAREST to see the pixels more clearly.

    float vertices[] = {0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.6f, 0.6f,
                        -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.4f, 0.6f,
                        0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.6f, 0.4f,
                        -0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.4f, 0.4f};
    
    glBindTexture(GL_TEXTURE_2D, texture[0]);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_GL_LINEAR); //纹理被缩小时的过滤方式对该题没有影响
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    
    glBindTexture(GL_TEXTURE_2D, texture[1]);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    

    4. Use a uniform variable as the mix function's third parameter to vary the amount the two textures are visible. Use the up and down arrow keys to change how much the container or the smiley face is visible.

    #version 330 core
    out vec4 FragColor;
    in vec4 Color;
    in vec2 texCoord;
    uniform sampler2D texSampler1;
    uniform sampler2D texSampler2;
    uniform float ratio;
    void main()
    {
        FragColor = mix(texture(texSampler1, texCoord), texture(texSampler2, vec2(-texCoord.x, texCoord.y)), ratio);
    }
    
    ourShader.use();
    flat fragmentRatio = 0.0f;
    glUniform1f(glGetUniformLocation(ourShader.ID, "ratio"), fragmentRatio);
    //render loop
    while(!glfwWindowShouldClose(window)){
        processInput(window);
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);
        ourShader.use();
        if(glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS){
            fragmentRatio += 0.01f;
            glUniform1f(glGetUniformLocation(ourShader.ID, "ratio"), fragmentRatio);
        }
        if(glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS){
            fragmentRatio -= 0.01f;
            glUniform1f(glGetUniformLocation(ourShader.ID, "ratio"), fragmentRatio);
        }
        glBindVertexArray(VAO);
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, texture[0]);
        glActiveTexture(GL_TEXTURE1);
        glBindTexture(GL_TEXTURE_2D, texture[1]);
        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    
  • 相关阅读:
    js把秒数转换为HH:MM:SS及时分秒格式
    java将流量KB转换为GB、MB、KB格式
    java将秒数转换为时分秒格式
    springboot前端向后端请求返回html语句
    java判断手机号三大运营商归属的工具类
    Java读取txt文件、excel文件的方法
    Linux安装JDK、Mysql和Tomcat
    原理篇—文件包含漏洞
    原理篇—上传漏洞
    原理篇—CSRF 跨站脚本请求伪造
  • 原文地址:https://www.cnblogs.com/yiqian/p/10872525.html
Copyright © 2011-2022 走看看