OpenGL纹理
如果不用头文件,把所有东西堆在同一个cpp文件中,会出现“超出GPU内存的错误!” 1 //我们自己的着色器类 #ifndef SHADER_H
#define SHADER_H #include <GL/glew.h> //包含glew来获取所有的必须Opengl头文件 #include <string>
#include <fstream>
#include <sstream>
#include <iostream> class Shader {
public:
unsigned int ID; //程序ID //构造器读取并构建着色器
Shader(const GLchar* vertexPath, const GLchar* fragmentPath);//顶点着色器的源码路径和片段着色器的源码路径 void use(); //使用、激活程序 //uniform工具函数
void setBool(const std::string &name, bool value) const;
void setInt(const std::string &name, int value) const;
void setFloat(const std::string &name, float value) const;
}; Shader::Shader(const GLchar* vertexPath, const GLchar* fragmentPath)
{ //从文件路径中获取顶点、片段着色器 std::string vertexCode;
std::string fragmentCode;
std::ifstream vShaderFile;
std::ifstream fShaderFile; //确保fstream对象可以抛出异常
vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
fShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit); try
{
//打开文件
vShaderFile.open(vertexPath);
fShaderFile.open(fragmentPath); std::stringstream vShaderStream, fShaderStream; vShaderStream << vShaderFile.rdbuf(); //读取文件的缓冲内容到数据流中
fShaderStream << fShaderFile.rdbuf(); vShaderFile.close(); //关闭文件处理器
fShaderFile.close(); vertexCode = vShaderStream.str(); //转换数据流到string
fragmentCode = fShaderStream.str(); } catch (std::ifstream::failure e)
{
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ" << std::endl;
} const char* vShaderCode = vertexCode.c_str();
const char* fShaderCode = fragmentCode.c_str(); //2.编译着色器
unsigned int vertex, fragment;
int success;
char infoLog[]; //顶点着色器
vertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex, , &vShaderCode, NULL);
glCompileShader(vertex);
glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(vertex, , NULL, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED!\n" << std::endl;
} fragment = glCreateShader(GL_FRAGMENT_SHADER); //片段着色器
glShaderSource(fragment, , &fShaderCode, NULL);
glCompileShader(fragment);
glGetShaderiv(fragment, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(fragment, , NULL, infoLog);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATON_FAILED!\n" << std::endl;
} ID = glCreateProgram(); //着色器程序
glAttachShader(ID, vertex);
glAttachShader(ID, fragment);
glLinkProgram(ID);
glGetProgramiv(ID, GL_LINK_STATUS, &success);
if (!success)
{
glGetProgramInfoLog(ID, , NULL, infoLog);
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED!\n" << std::endl;
} glDeleteShader(vertex); //删除着色器,他们已经链接到我们的程序中了,已经不需要了
glDeleteShader(fragment); } void Shader::use()
{
glUseProgram(ID); //激活这个着色器程序
} void Shader::setBool(const std::string& name, bool value) const
{
glUniform1i(glGetUniformLocation(ID, name.c_str()), (int)value);
} void Shader::setInt(const std::string &name, int value) const
{
glUniform1i(glGetUniformLocation(ID, name.c_str()), value);
} void Shader::setFloat(const std::string &name, float value) const
{
glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
} #endif
纹理cpp文件
1 #include <iostream>
using namespace std; #define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>
//#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include "Shader.h" void framebuffers_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow* window); float mixValue = 0.2f; int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, );
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, );
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(, , "LearnOpengl", NULL, NULL);
if (window == NULL)
{
cout << "Failed to create window!\n" << endl;
glfwTerminate();
return -;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffers_size_callback); //改变窗口大小的回调函数 glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK)
{
cout << "Fail to initialize GLEW!\n" << endl;
return -;
} /*unsigned vertexShader, fragmentShader;
vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader); int success;
char infolog[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(vertexShader, 512, NULL, infolog);
cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED!\n" << infolog << endl;
} fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource = (fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(fragmentShader, 512, NULL, infolog);
cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED!\n" << infolog << endl;
} unsigned int shaderProgram;
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if (!success)
{
glGetProgramInfoLog(shaderProgram, 512, NULL, infolog);
cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED!\n" << infolog << endl;
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);*/ Shader ourShader("E:\\C++\\1.txt", "E:\\C++\\2.txt");
GLfloat vertices[] = {
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, //4.0f控制把图赋值多少16份
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
}; unsigned int indices[] = {
, , ,
, ,
}; unsigned VBO, VAO, EBO;
glGenVertexArrays(, &VAO);
glGenBuffers(, &VBO);
glGenBuffers(, &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(, , GL_FLOAT, GL_FALSE, * sizeof(float), (void*));
glEnableVertexAttribArray(); glVertexAttribPointer(, , GL_FLOAT, GL_FALSE, * sizeof(float), (void*)( * sizeof(float)));
glEnableVertexAttribArray(); glVertexAttribPointer(, , GL_FLOAT, GL_FALSE, * sizeof(float), (void*)( * sizeof(float)));
glEnableVertexAttribArray(); unsigned int texture1;
glGenTextures(, &texture1);
glBindTexture(GL_TEXTURE_2D, texture1); 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_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); int width, height, nrChannels; //stbi_set_flip_vertically_on_load(true); //倒转图片
//stbi_set_flip_vertically_on_load(true); //倒转图片 unsigned char *data = stbi_load("container.jpg", &width, &height, &nrChannels, );
if (data)
{
glTexImage2D(GL_TEXTURE_2D, , GL_RGB, width, height, , GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
cout << "Failed to load texture1!\n" << endl;
}
stbi_image_free(data); unsigned int texture2;
glGenTextures(, &texture2);
glBindTexture(GL_TEXTURE_2D, texture2); //texture1和texture2的环绕方式不同的组合效果很不一样
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
data = stbi_load("timg.jpg", &width, &height, &nrChannels, );
if (data)
{
glTexImage2D(GL_TEXTURE_2D, , GL_RGB, width, height, , GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
cout << "Fail to load texture2!\n" << endl;
}
stbi_image_free(data); ourShader.use();
glUniform1i(glGetUniformLocation(ourShader.ID, "texture1"), );
glUniform1i(glGetUniformLocation(ourShader.ID, "texture2"), ); glBindBuffer(GL_ARRAY_BUFFER, );
glBindVertexArray(); while (!glfwWindowShouldClose(window))
{
processInput(window);
glClearColor(0.2f, 0.5f, 0.7f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT); //glBindTexture(GL_TEXTURE_2D, texture); glActiveTexture(GL_TEXTURE1); //在绑定纹理之前需先激活纹理单元
glBindTexture(GL_TEXTURE_2D, texture1);
glActiveTexture(GL_TEXTURE0); //在绑定纹理之前需先激活纹理单元
glBindTexture(GL_TEXTURE_2D, texture2); ourShader.setFloat("mixValue", mixValue); //glUseProgram(shaderProgram);
glUseProgram(ourShader.ID);
glBindVertexArray(VAO);
//glDrawArrays(GL_TRIANGLES, 0, 3);
glDrawElements(GL_TRIANGLES, , GL_UNSIGNED_INT, ); glfwSwapBuffers(window);
glfwPollEvents();
} glDeleteVertexArrays(, &VAO);
glDeleteBuffers(, &VBO);
glDeleteBuffers(, &EBO); return ;
} void framebuffers_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(, , width, height);
} void processInput(GLFWwindow* window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true); //用键盘上下键控制两个纹理的可见度比例
if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS)
{
mixValue += 0.001f;
if (mixValue >= 1.0f)
mixValue = 1.0f;
}
if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS)
{
mixValue -= 0.001f;
if (mixValue <= 0.0f)
mixValue = 0.0f;
}
}
E:\\C++\\1.txt文件内容:
#version core
layout(location = ) in vec3 aPos;
layout(location = ) in vec3 aColor;
layout(location = ) in vec2 aTexture;
out vec3 ourColor;
out vec2 ourTexture;
void main()
{
gl_Position = vec4(aPos, 1.0f);
ourColor = aColor;
ourTexture = aTexture;
}
E:\\C++\\2.txt文件内容:
#version core
out vec4 FragColor;
in vec3 ourColor;
in vec2 ourTexture;
uniform float mixValue;
uniform sampler2D texture1;
uniform sampler2D texture2;
void main()
{
FragColor = mix(texture(texture1, vec2(ourTexture.x, 1.0 - ourTexture.y)), texture(texture2, ourTexture), mixValue);
}
OpenGL纹理的更多相关文章
- OpenGL: 纹理采样 texture sample
Sampler (GLSL) Sampler通常是在Fragment shader(片元着色器)内定义的,这是一个uniform类型的变量,即处理不同的片元时这个变量是一致不变的.一个sampler和 ...
- OpenGL 纹理贴图
前一节实例代码中有个贴图操作. 今天就简单说明一下纹理贴图... 为了使用纹理贴图.我们首先需要启用纹理贴图功能. 我们可以在Renderer实现的onSurfaceCreated中定义启用: // ...
- [OpenGL]纹理贴图实现 总结
实现步骤 第一步:设置所需要的OpenGL环境 设置上下文环境 删除已经存在的渲染的缓存 设置颜色缓存 设置帧缓存 清除缓存 设置窗口大小 开启功能 编译shander 使用program 获取sha ...
- openGL 纹理05
纹理(Texture) 为了能够把纹理映射(Map)到三角形上,我们需要指定三角形的每个顶点各自对应纹理的哪个部分. 这样每个顶点就会关联着一个纹理坐标(Texture Coordinate) 用来标 ...
- OpenGL纹理上下颠倒翻转的三种解决办法
http://blog.csdn.net/narutojzm1/article/details/51940817 综述 在使用OpenGL函数加载纹理到图形时,经常遇到纹理上下颠倒的问题.原因是因为O ...
- Qt5.6.0+OpenGL 纹理贴图首战告捷
重要的话写在前面~~通过今晚的实验,知道了EBO是不能随便release的~~~一直不要release就可以了,否则vao会失效 Display.h #ifndef DISPLAYWIDGET_H # ...
- RenderMonkey 练习 第一天 【opengl 纹理】
础实例: 我们首先实现一个带纹理模型的显示,大体了解RenderMonkey的操作方式. 1. 打开RenderMonkey, 右击WorkSpace的Effect WorkSpace结点,选择Add ...
- OpenGL纹理高级
矩形纹理 对于二维纹理来说,除了GL_TEXTURE_2D之外,使用GL_TEXTURE_RECTANGLE就可以使用矩形纹理. 矩形纹理几大特点: 不能Mip,只能加载glTexImage2D的le ...
- OpenGL ES学习笔记(三)——纹理
首先申明下,本文为笔者学习<OpenGL ES应用开发实践指南(Android卷)>的笔记,涉及的代码均出自原书,如有需要,请到原书指定源码地址下载. <OpenGL ES学习笔记( ...
随机推荐
- python 爬虫newspaper3k 新闻爬去方法 利用第三方库
from newspaper import Article url = '你想要爬取的网站url' news = Article(url, language='zh') news .download( ...
- 2019-03-27-day020-单继承与多继承
昨日回顾 类的加载顺序 类内部的代码什么时候执行? 除了方法里面的代码 其余的所有内容都是在执行这个文件的时候就从上到下依次执行的 不需要调用 如果有同名的方法.属性,总是写在后面的会生 class ...
- Java学习笔记24(Map集合)
Map接口: Map接口与Collection接口无继承关系. 区别:Collection中的元素是孤立的,一个一个存进去的. Map作为一个映射集合,每一个元素包含Key-value对(键-值对). ...
- maven 细节 —— scope、坐标
对于 idea 开发环境,测试代码便是在 src/test/java(该java目录会在创建时标注为测试文件夹) 目录下的 .java 代码为测试代码: 1. scope scope的分类 compi ...
- Unescaped left brace in regex is illegal here in regex; marked by <-- HERE in m/\${ <-- HERE ([^ \t=:+{}]+)}/ at xxxx/usr/bin/automake line 3939.
/********************************************************************** * Unescaped left brace in re ...
- 深度学习环境配置Ubuntu16.04+CUDA8.0+CUDNN5
深度学习从12年开始打响,配置深度学习环境软件一直是一个头疼的问题,如何安装显卡驱动,如何安装CUDA,如何安装CUDNN:Ubuntu官方一直吐槽Nvidia显卡驱动有问题,网上大神也给出了关闭li ...
- 用纯JS实现,点击一个列表时,输出对应的索引(不能用JQuery哦)
你运行一下代码会发现,无论你点击哪个列表,控制台都是输出10.这是因为var声明的变量是函数作用域的,而不是块级作用域的.也就是说,for循环10次,每次都是改变同一个i,所以它的值会从0一直加到10 ...
- HDU 1907:John(尼姆博弈变形)
John Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total Submi ...
- ifconfig 命令,改变主机名,改DNS hosts、关闭selinux firewalld netfilter 、防火墙iptables规则
ifconfig 命令用于查看网络相关的命令: 安装:yum install net-tools -y ifdown eth_name 关闭网卡 ifup eth_name 开启网卡 配 ...
- Python——dict(自定义类作key)
Python的dict要求key为不可变数据类型,通常采用str或int,但在某些应用场景下,需要采用自定义类型对象作key, 此时的自定义类需要实现两个特殊方法:__hash__.__eq__,用于 ...