计算机图形学(五)——纹理

纹理

在res文件夹中新建了一个picture文件夹,把图片image.jpg放入picture文件夹。
main.cpp


#include <iostream>

// GLEW
#define GLEW_STATIC
#include <GL/glew.h>

// GLFW
#include <GLFW/glfw3.h>

#include "Shader.h"

#include"SOIL2/SOIL2.h"
#include"SOIL2/stb_image.h"
const GLint WIDTH = 800, HEIGHT = 600;

int main()
{
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);//for mac
    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

    GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "Learn OpenGL B18080312", nullptr, nullptr);

    // retina display
    int screenWidth, screenHeight;
    glfwGetFramebufferSize(window, &screenWidth, &screenHeight);

    if (window == nullptr) {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }

    glfwMakeContextCurrent(window);

    glewExperimental = GL_TRUE;

    if (glewInit() != GLEW_OK) {
        std::cout << "Failed to initialize GLEW" << std::endl;
        glfwTerminate();
        return -1;
    }

    Shader shader = Shader("res/shaders/code.vs", "res/shaders/code.fs");
    GLfloat vertices[] =
    {       //position                    //uv coords纹理坐标:左下(0,0),右上(1,1)
        0.5f, 0.5f, 0.0f,     1.0f,1.0f,// top right
        0.5f, -0.5f, 0.0f,    1.0f,0.0f, // bottom right
        -0.5f, -0.5f, 0.0f,   0.0f,0.0f, // bottom left
        -0.5f, 0.5f, 0.0f  ,  0.0f,1.0f// top left
    };

    unsigned int indices[] =
    {
        0, 1, 3, // first triangle
        1, 2, 3  // second triangle
    };
    GLuint VAO, VBO;
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);

    glBindVertexArray(VAO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)0);
    glEnableVertexAttribArray(0);

    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3*sizeof(GLfloat)));
    glEnableVertexAttribArray(1);

    GLuint EBO;
    glGenBuffers(1, &EBO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);

    //Texture
    //load image
    int width, height;
    unsigned char* image =SOIL_load_image("res/pictures/image.jpg", &width, &height, 0, SOIL_LOAD_RGBA);//读出图片的长和宽,0没有意义

    //设置纹理
    GLuint texture;
    glGenTextures(1, &texture);//创建
    glBindTexture(GL_TEXTURE_2D, texture);//绑定

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);//WRAP_S是U坐标
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);//WRAP_T是V坐标
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_REPEAT);//FILTER放大缩小,如何与纹理坐标相适应
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_REPEAT);

    //把图片传到显存里
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
    //第0层图片,RGB传四个
    //第二个0:0边界处理
    // GL_RGBA原图里获得RGB信息
    //GL_UNSIGNED_BYTE:原图数据类型
    SOIL_free_image_data(image);
    glGenerateMipmap(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, 0);


    while (!glfwWindowShouldClose(window)) {
        glViewport(0, 0, screenWidth, screenHeight);
        glfwPollEvents();
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);
        shader.Use();

        //开启一个标志位,不需要对象
        glActiveTexture(GL_TEXTURE0);//GL_TEXTURE0:预设了信息,直接索引即可,信息只有类型和长宽
        glBindTexture(GL_TEXTURE_2D,texture);//绑定了texture,把texture写到GL_TEXTURE0里,只需要片缘着色器对应到GL_TEXTURE0就行
        glUniform1i(glGetUniformLocation(shader.Program, "Texture"), 0);


//opengl传数据有两种方式:1.VAO    2.动态地传数据(传的数据不能太大)
        
        glBindVertexArray(VAO);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
        glBindVertexArray(0);
        glfwSwapBuffers(window);
    }
    glDeleteVertexArrays(1, &VAO);
    glDeleteBuffers(1, &VBO);
    glDeleteBuffers(1, &EBO);
    glDeleteBuffers(1, &texture);
    glfwTerminate();
    return 0;
}

code.vs

#version 330 core
layout(location = 0) in vec3 position;
layout(location = 1) in vec2 textCoords;//vec2:纹理坐标是二维的
out vec2 textcoords;

void main()
{
        gl_Position = vec4(position.x, position.y, position.z, 1.0f);
        textcoords=vec2(textCoords.x,1-textCoords.y);
};

code.fs

#version 330 core
in vec2 textcoords;
uniform sampler2D Texture;
out vec4 color;
void main()
{
     color = texture(Texture,textcoords);
   //vec4(1.0f, 0.0f, 0.0f, 1.0f);
};

结果
在这里插入图片描述

如果把code.fs改成这样,结果会变成3X3的图片

code.fs

#version 330 core
in vec2 textcoords;
uniform sampler2D Texture;
out vec4 color;
void main()
{
     color = texture(Texture,textcoords*3.0f);
   //vec4(1.0f, 0.0f, 0.0f, 1.0f);
};

再改变code.fs

code.fs

#version 330 core
in vec2 textcoords;
uniform sampler2D Texture;
out vec4 color;
void main()
{
     color = texture(Texture,textcoords*3.0f.rgb,0.2f);
   //vec4(1.0f, 0.0f, 0.0f, 1.0f);
};

光这样改还不够,main函数做出对应改变:


#include <iostream>

// GLEW
#define GLEW_STATIC
#include <GL/glew.h>

// GLFW
#include <GLFW/glfw3.h>

#include "Shader.h"

#include"SOIL2/SOIL2.h"
#include"SOIL2/stb_image.h"
const GLint WIDTH = 800, HEIGHT = 600;

int main()
{
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);//for mac
    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

    GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "Learn OpenGL B18080312", nullptr, nullptr);

    // retina display
    int screenWidth, screenHeight;
    glfwGetFramebufferSize(window, &screenWidth, &screenHeight);

    if (window == nullptr) {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }

    glfwMakeContextCurrent(window);

    glewExperimental = GL_TRUE;

    if (glewInit() != GLEW_OK) {
        std::cout << "Failed to initialize GLEW" << std::endl;
        glfwTerminate();
 
        return -1;
    }

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    Shader shader = Shader("res/shaders/code.vs", "res/shaders/code.fs");
    GLfloat vertices[] =
    {       //position                    //uv coords纹理坐标:左下(0,0),右上(1,1)
        0.5f, 0.5f, 0.0f,     1.0f,1.0f,// top right
        0.5f, -0.5f, 0.0f,    1.0f,0.0f, // bottom right
        -0.5f, -0.5f, 0.0f,   0.0f,0.0f, // bottom left
        -0.5f, 0.5f, 0.0f  ,  0.0f,1.0f// top left
    };

    unsigned int indices[] =
    {
        0, 1, 3, // first triangle
        1, 2, 3  // second triangle
    };
    GLuint VAO, VBO;
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);

    glBindVertexArray(VAO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)0);
    glEnableVertexAttribArray(0);

    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3*sizeof(GLfloat)));
    glEnableVertexAttribArray(1);

    GLuint EBO;
    glGenBuffers(1, &EBO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);

    //Texture
    //load image
    int width, height;
    unsigned char* image =SOIL_load_image("res/pictures/image.jpg", &width, &height, 0, SOIL_LOAD_RGBA);//读出图片的长和宽,0没有意义

    //设置纹理
    GLuint texture;
    glGenTextures(1, &texture);//创建
    glBindTexture(GL_TEXTURE_2D, texture);//绑定

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);//WRAP_S是U坐标
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);//WRAP_T是V坐标
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_REPEAT);//FILTER放大缩小,如何与纹理坐标相适应
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_REPEAT);

    //把图片传到显存里
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
    //第0层图片,RGB传四个
    //第二个0:0边界处理
    // GL_RGBA原图里获得RGB信息
    //GL_UNSIGNED_BYTE:原图数据类型
    SOIL_free_image_data(image);
    glGenerateMipmap(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, 0);


    while (!glfwWindowShouldClose(window)) {
        glViewport(0, 0, screenWidth, screenHeight);
        glfwPollEvents();
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);
        shader.Use();

        //开启一个标志位,不需要对象
        glActiveTexture(GL_TEXTURE0);//GL_TEXTURE0:预设了信息,直接索引即可,信息只有类型和长宽
        glBindTexture(GL_TEXTURE_2D,texture);//绑定了texture,把texture写到GL_TEXTURE0里,只需要片缘着色器对应到GL_TEXTURE0就行
        glUniform1i(glGetUniformLocation(shader.Program, "Texture"), 0);


//opengl传数据有两种方式:1.VAO    2.动态地传数据(传的数据不能太大)
        
        glBindVertexArray(VAO);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
        glBindVertexArray(0);
        glfwSwapBuffers(window);
    }
    glDeleteVertexArrays(1, &VAO);
    glDeleteBuffers(1, &VBO);
    glDeleteBuffers(1, &EBO);
    glDeleteBuffers(1, &texture);
    glfwTerminate();
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值