立方体纹理映射源文件和头文件以及效果图
时间: 2024-02-14 15:21:15 浏览: 45
以下是一个简单的立方体纹理映射的源文件和头文件示例,供您参考:
头文件 cube_map.h:
```c++
#ifndef CUBE_MAP_H
#define CUBE_MAP_H
#include <glad/glad.h>
#include <glm/glm.hpp>
class CubeMap {
public:
CubeMap(const char* front, const char* back, const char* top, const char* bottom, const char* left, const char* right);
~CubeMap();
void bind() const;
void unbind() const;
void set_uniform_location(GLuint program, const char* uniform_name);
private:
GLuint m_texture_id;
};
#endif
```
源文件 cube_map.cpp:
```c++
#include "cube_map.h"
#include "stb_image.h"
#include <iostream>
CubeMap::CubeMap(const char* front, const char* back, const char* top, const char* bottom, const char* left, const char* right)
{
glGenTextures(1, &m_texture_id);
glBindTexture(GL_TEXTURE_CUBE_MAP, m_texture_id);
int width, height, nr_channels;
unsigned char* data;
// load front face
data = stbi_load(front, &width, &height, &nr_channels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
stbi_image_free(data);
}
else
{
std::cout << "Failed to load front face of cube map" << std::endl;
}
// load back face
data = stbi_load(back, &width, &height, &nr_channels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
stbi_image_free(data);
}
else
{
std::cout << "Failed to load back face of cube map" << std::endl;
}
// load top face
data = stbi_load(top, &width, &height, &nr_channels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
stbi_image_free(data);
}
else
{
std::cout << "Failed to load top face of cube map" << std::endl;
}
// load bottom face
data = stbi_load(bottom, &width, &height, &nr_channels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
stbi_image_free(data);
}
else
{
std::cout << "Failed to load bottom face of cube map" << std::endl;
}
// load left face
data = stbi_load(left, &width, &height, &nr_channels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
stbi_image_free(data);
}
else
{
std::cout << "Failed to load left face of cube map" << std::endl;
}
// load right face
data = stbi_load(right, &width, &height, &nr_channels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
stbi_image_free(data);
}
else
{
std::cout << "Failed to load right face of cube map" << std::endl;
}
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
}
CubeMap::~CubeMap()
{
glDeleteTextures(1, &m_texture_id);
}
void CubeMap::bind() const
{
glBindTexture(GL_TEXTURE_CUBE_MAP, m_texture_id);
}
void CubeMap::unbind() const
{
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
}
void CubeMap::set_uniform_location(GLuint program, const char* uniform_name)
{
GLint location = glGetUniformLocation(program, uniform_name);
if (location >= 0)
{
glUniform1i(location, 0);
}
}
```
使用示例:
```c++
CubeMap cube_map("front.png", "back.png", "top.png", "bottom.png", "left.png", "right.png");
// ...
glActiveTexture(GL_TEXTURE0);
cube_map.bind();
cube_map.set_uniform_location(shader_program, "cube_map");
// ...
// in the fragment shader:
uniform samplerCube cube_map;
// ...
vec3 reflect_dir = reflect(-incident_dir, normal);
color = texture(cube_map, reflect_dir);
```
效果图:
![Cube Mapping效果图](https://i.imgur.com/6z1w4ZT.png)
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)