美文网首页
OpenGL编程指南06 纹理

OpenGL编程指南06 纹理

作者: rasishou | 来源:发表于2019-04-25 13:59 被阅读0次

从文件中加载图像

#include<GL/gl3w.h>//与下面的glfw3.h的顺序不能交换
#include <GLFW/glfw3.h>
#include <iostream>
#include<fstream>
#include<string>
#define STB_IMAGE_IMPLEMENTATION
#include"stb_image.h"
using namespace std;
const GLchar*ReadShader(const char*filename) {
    ifstream t;
    int length;
    t.open(filename);
    t.seekg(0,ios::end);
    length = t.tellg();
    
    t.seekg(0,ios::beg);
    GLchar*buffer = new GLchar[length+1];
    memset(buffer,0x0,length+1);
    t.read(buffer,length);
    t.close();
    return const_cast<GLchar*>(buffer);
}
int main() {
    glfwInit();
    GLFWwindow*window = glfwCreateWindow(640,480,"triangles",nullptr,nullptr);
    glfwMakeContextCurrent(window);
    gl3wInit();
    //program and shader
    GLuint program = glCreateProgram();

    GLuint vert_shader = glCreateShader(GL_VERTEX_SHADER);
    const GLchar*vert_shader_source = ReadShader("triangle.vert");
    glShaderSource(vert_shader,1,&vert_shader_source,nullptr);
    delete[] vert_shader_source;
    glCompileShader(vert_shader);
    glAttachShader(program,vert_shader);

    GLuint frag_shader = glCreateShader(GL_FRAGMENT_SHADER);
    const GLchar*frag_shader_source = ReadShader("triangle.frag");
    
    glShaderSource(frag_shader, 1, &frag_shader_source, nullptr);
    delete[] frag_shader_source;
    glCompileShader(frag_shader);
    glAttachShader(program,frag_shader);

    glLinkProgram(program);
    glUseProgram(program);
    //buffer and vertex
    float vertices[] = {
        // positions          // colors           // texture coords
         0.5f,  0.5f, 0.0f,   1.0f, 0.0f, 0.0f,   1.0f, 1.0f, // top right
         0.5f, -0.5f, 0.0f,   0.0f, 1.0f, 0.0f,   1.0f, 0.0f, // bottom right
        -0.5f, -0.5f, 0.0f,   0.0f, 0.0f, 1.0f,   0.0f, 0.0f, // bottom left
        -0.5f,  0.5f, 0.0f,   1.0f, 1.0f, 0.0f,   0.0f, 1.0f  // top left 
    };
    unsigned int indices[] = {
        0, 1, 3, // first triangle
        1, 2, 3  // second triangle
    };
    unsigned int VBO, VAO, EBO;
    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);

    // position attribute
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
    glEnableVertexAttribArray(0);
    // color attribute
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
    glEnableVertexAttribArray(1);
    // texture coord attribute
    glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
    glEnableVertexAttribArray(2);


    // load and create a texture 
    // -------------------------
    unsigned int texture;
    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture); // all upcoming GL_TEXTURE_2D operations now have effect on this texture object
    // set the texture wrapping parameters
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);   // set texture wrapping to GL_REPEAT (default wrapping method)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    // set texture filtering parameters
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    // load image, create texture and generate mipmaps
    int width, height, nrChannels;
    
    unsigned char *data = stbi_load("container.png", &width, &height, &nrChannels, STBI_rgb);
    if (data)
    {
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
        glGenerateMipmap(GL_TEXTURE_2D);
    }
    else
    {
        std::cout << "Failed to load texture" << std::endl;
    }
    stbi_image_free(data);
    while (!glfwWindowShouldClose(window)) {
        static const float black[] = {0.0f,0.0f,0.0f,0.0f};
        glClearBufferfv(GL_COLOR,0,black);
        glBindTexture(GL_TEXTURE_2D,texture);
        glBindVertexArray(VAO);
        glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,0);

        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    glfwDestroyWindow(window);
    glfwTerminate();
    return 0;
}

顶点着色器:

#version 450 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
layout (location = 2) in vec2 aTexCoord;

out vec3 ourColor;
out vec2 TexCoord;

void main()
{
    gl_Position = vec4(aPos, 1.0);
    ourColor = aColor;
    TexCoord = vec2(aTexCoord.x, aTexCoord.y);
}

片段着色器:

#version 450 core
out vec4 FragColor;

in vec3 ourColor;
in vec2 TexCoord;

// texture sampler
uniform sampler2D texture1;

void main()
{
    FragColor = texture(texture1, TexCoord);
}

要加载的图片:


container.png

总结:
1.stbi_load()中的STBI_rgb要与glTexImage2D()中的GL_RGB对应

相关文章

  • OpenGL编程指南06 纹理

    从文件中加载图像 顶点着色器: 片段着色器: 要加载的图片: 总结:1.stbi_load()中的STBI_rgb...

  • OpenGL名词

    推荐两本书:OpenGL编程指南,OpenGL超级宝典 OpenGL (Open Graphics Library...

  • OpenGL-名词解析

    OpenGL 工具书:OpenGL超级宝典(蓝宝书)、OpenGL编程指南(红宝书) 图形API都有哪些 • Op...

  • OpenGL-纹理的初步应用

    Title: OpenGL-纹理的初步应用Date: 2016-08-06 01:30Modified: 2016...

  • GLKit常用API解析

    GLKTextureInfo 创建OpenGL纹理信息 name: OpenGL上下文中纹理名称 target: ...

  • OpenGL ES GLKit 􏰼􏰜常用API解析

    GLKTextureInfo创建OpenGL纹理信息 name : OpenGL上下文中纹理名称 target :...

  • OpenGL纹理内容

    纹理可以理解为一张图片,OpenGL渲染图片会将图片的像素保存在纹理缓存中。 OpenGL常用纹理函数 载入纹理 ...

  • OpenGL纹理

    纹理可以理解为一张图片,OpenGL渲染图片会将图片的像素保存在纹理缓存中。OpenGL常用纹理函数 载入纹理 纹...

  • OPenGL ES纹理翻转解决方案

    纹理翻转 在使用OpenGL函数加载纹理到图形时,经常遇到纹理上下颠倒的问题。原因是因为OpenGL要求纹理坐标原...

  • WebGL编程指南笔记三 第五章 颜色和纹理

    参考【《WebGL编程指南》读书笔记-颜色与纹理】[https://blog.csdn.net/floating_...

网友评论

      本文标题:OpenGL编程指南06 纹理

      本文链接:https://www.haomeiwen.com/subject/qmzagqtx.html