OpenGL学习之纹理使用

1. TextureAdmin.hpp
//
//  texture.hpp
//  GL_Program


#ifndef TextureAdmin_hpp
#define TextureAdmin_hpp

#include <stdio.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <SOIL.h>
#include <iostream>

using namespace std;

class TextureAdmin
{
public:
    TextureAdmin(const string imagePath);
    ~TextureAdmin();

    //绑定纹理单元
    void BindTexture();


public:
    //纹理ID
    GLuint m_pTexture;
};

#endif /* texture_hpp */
2.TextureAdmin.cpp
//
//  texture.cpp
//  GL_Program

#include "TextureAdmin.hpp"
TextureAdmin::TextureAdmin(const string imagePath)
{
    glGenTextures(1, &m_pTexture);
    glBindTexture(GL_TEXTURE_2D, m_pTexture);

    //设置纹理的采样方式
    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_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    //加载纹理图片
    int width , height;
    unsigned char *image = SOIL_load_image(imagePath.data(), &width, &height, 0, SOIL_LOAD_RGB);
    glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, width, height,0, GL_RGB, GL_UNSIGNED_BYTE, image);
    glGenerateMipmap(GL_TEXTURE_2D);
    SOIL_free_image_data(image);
    glBindTexture(GL_TEXTURE_2D, 0);
}

TextureAdmin::~TextureAdmin()
{

}

void TextureAdmin::BindTexture()
{
    glBindTexture(GL_TEXTURE_2D, m_pTexture);
}
3. main.cpp
//  main.cpp
//  GL_Program


#include <iostream>
#include <stdlib.h>
#include <math.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "ShaderAdmin.hpp"
#include <SOIL.h>
#include "TextureAdmin.hpp"

#define WIDTH 800
#define HEIGHT 600
#define TITLE "OpenGL"


void initGL();
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
void renderTriangle();
void loadTexture(const char *texturePath);

GLuint VAO,VBO,EBO;
GLuint texture;
int main(int argc, const char * argv[]) {
    // insert code here...
    initGL();
    GLFWwindow *win = glfwCreateWindow(WIDTH, HEIGHT, TITLE, nullptr,nullptr);
    if (win == nullptr)
    {
        cout << "Failed to create GLFW window!"<<endl;
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(win);
    glfwSetKeyCallback(win, key_callback);

    glewExperimental = GL_TRUE;
    if(glewInit() != GLEW_OK)
    {
        cout << "Failed to initialize GLEW" << endl;
    }

    int width,height;
    glfwGetFramebufferSize(win, &width, &height);
    glViewport(0, 0, width, height);

    ShaderAdmin shader("shader/vShader.vert","shader/fShader.frag");
    renderTriangle();

    TextureAdmin texture("image/container.jpg");
    TextureAdmin texture2("image/awesomeface.png");

    while (!glfwWindowShouldClose(win))
    {
        glfwPollEvents();
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        //激活纹理单元0
        glActiveTexture(GL_TEXTURE0);
        texture.BindTexture();
        glUniform1i(glGetUniformLocation(shader.m_program,"ourTexture"),0);
        //激活纹理单元1
        glActiveTexture(GL_TEXTURE1);
        texture2.BindTexture();
        glUniform1i(glGetUniformLocation(shader.m_program,"ourTexture2"),1);

        shader.Use();
        glBindVertexArray(VAO);

        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
        glBindVertexArray(0);

        glfwSwapBuffers(win);
    }

    glDeleteVertexArrays(1, &VAO);
    glDeleteBuffers(1, &VBO);
    glDeleteBuffers(1, &EBO);
    glfwTerminate();

    return 0;
}

void initGL()
{
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
    //Mac环境必须添加下面这行才能生效
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
}
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
{
    // 当用户按下ESC键,我们设置window窗口的WindowShouldClose属性为true
    // 关闭应用程序
    if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
        glfwSetWindowShouldClose(window, GL_TRUE);
}
void renderTriangle()
{
    GLfloat vertices[] = {
        //    ---- 位置 ----       ---- 颜色 ----     - 纹理坐标 -
        0.5f,  0.5f, 0.0f,   1.0f, 0.0f, 0.0f,   1.0f, 1.0f,   // 右上
        0.5f, -0.5f, 0.0f,   0.0f, 1.0f, 0.0f,   1.0f, 0.0f,   // 右下
        -0.5f, -0.5f, 0.0f,   0.0f, 0.0f, 1.0f,   0.0f, 0.0f,   // 左下
        -0.5f,  0.5f, 0.0f,   1.0f, 1.0f, 0.0f,   0.0f, 1.0f    // 左上
    };
    GLuint indices[] = {
        0,1,2,
        2,3,0
    };

    glGenVertexArrays(1,&VAO);
    glGenBuffers(1,&VBO);
    glGenBuffers(1,&EBO);

    glBindVertexArray(VAO);

    glBindBuffer(GL_ARRAY_BUFFER,VBO);
    glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_STATIC_DRAW);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,EBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(indices),indices,GL_STATIC_DRAW);
    //位置属性
    glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,8 * sizeof(GLfloat),(GLvoid *)0);
    glEnableVertexAttribArray(0);
    //颜色属性
    glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,8 * sizeof(GLfloat),(GLvoid *)(3*sizeof(GLfloat)));
    glEnableVertexAttribArray(1);
    //纹理属性
    glVertexAttribPointer(2,2,GL_FLOAT,GL_FALSE,8 * sizeof(GLfloat),(GLvoid *)(6*sizeof(GLfloat)));
    glEnableVertexAttribArray(2);

    glBindBuffer(GL_ARRAY_BUFFER,0);
    glBindVertexArray(0);
}
ShaderAdmin请看:
http://blog.csdn.net/qq_33335724/article/details/79527379
参考文章:

https://learnopengl-cn.readthedocs.io/zh/latest/01%20Getting%20started/06%20Textures/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值