1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
| #include "SkyBox.hpp"
#include "../MyWindow.hpp"
#include "../HandleError.hpp"
#include "../String_utils.hpp"
#include "../Camera.hpp"
#include <GL/glew.h>
#include <map>
#include <string>
using namespace std;
using namespace Object;
void HandleSDL::createSkyBoxTextures(string textures[])
{
GLenum cube_map_target[6] = {
GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
};
for (int i = 0; i < 6; i++)
{
SDL_Surface * picture_surface = NULL;
SDL_Surface *gl_surface = NULL;
SDL_Surface * gl_fliped_surface = NULL;
Uint32 rmask, gmask, bmask, amask;
picture_surface = IMG_Load(textures[i].c_str());
if (picture_surface == NULL)
{
HandleError::showError(textures[i] + std::string(": No such file or directory"));
continue;
}
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
rmask = 0xff000000;
gmask = 0x00ff0000;
bmask = 0x0000ff00;
amask = 0x000000ff;
#else
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;
#endif
SDL_PixelFormat format = *(picture_surface->format);
format.BitsPerPixel = 32;
format.BytesPerPixel = 4;
format.Rmask = rmask;
format.Gmask = gmask;
format.Bmask = bmask;
format.Amask = amask;
gl_surface = SDL_ConvertSurface(picture_surface, &format, SDL_SWSURFACE);
gl_fliped_surface = flipSurface(gl_surface);
gluBuild2DMipmaps(cube_map_target[i], 4, gl_fliped_surface->w,
gl_fliped_surface->h, GL_RGBA, GL_UNSIGNED_BYTE,
gl_fliped_surface->pixels);
SDL_FreeSurface(gl_fliped_surface);
SDL_FreeSurface(gl_surface);
SDL_FreeSurface(picture_surface);
}
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST); //GL_NEAREST_MIPMAP_LINEAR
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST); //GL_NEAREST_MIPMAP_LINEAR
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); //GL_CLAMP
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); //GL_CLAMP
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
}
SkyBox::SkyBox(float size) : Cube(Vector3D(0.f, 0.f, 0.f), Rotation(), Color(), size)
{
m_hasTexture = true;
m_shader = new Shader;
m_pointsNumber = 0;
glGenTextures(1, &cube_map_texture_ID);
glEnable(GL_TEXTURE_CUBE_MAP_ARB);
glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, cube_map_texture_ID);
#ifdef WIN32
std::string tex[] = {"./textures/skybox/west.jpg", "./textures/skybox/east.jpg", "./textures/skybox/ground.jpg",
"./textures/skybox/sky.jpg", "./textures/skybox/north.jpg", "./textures/skybox/south.jpg"};
#else
std::string tex[] = {"/windows/Users/imperio/Projects/My_World_SDL/My_World_SDL/My_World_SDL/textures/skybox/west.jpg",
"/windows/Users/imperio/Projects/My_World_SDL/My_World_SDL/My_World_SDL/textures/skybox/east.jpg",
"/windows/Users/imperio/Projects/My_World_SDL/My_World_SDL/My_World_SDL/textures/skybox/ground.jpg",
"/windows/Users/imperio/Projects/My_World_SDL/My_World_SDL/My_World_SDL/textures/skybox/sky.jpg",
"/windows/Users/imperio/Projects/My_World_SDL/My_World_SDL/My_World_SDL/textures/skybox/north.jpg",
"/windows/Users/imperio/Projects/My_World_SDL/My_World_SDL/My_World_SDL/textures/skybox/south.jpg"};
#endif
MyWindow::createSkyBoxTextures(tex);
glDisable(GL_TEXTURE_CUBE_MAP_ARB);
}
SkyBox::~SkyBox()
{
/*for (int i = 0; i < 6; ++i)
glDeleteTextures(1, &textures[i]);*/
glDeleteTextures(1, &cube_map_texture_ID);
}
void SkyBox::initializeGL()
{
std::string vert;
std::string frag;
vert =
"#version 330\n"
"in vec3 vertor;\n"
"out vec3 texCoord;\n"
"uniform mat4 PVM;\n"
"void main(){\n"
"gl_Position = PVM * vec4(vertor, 1.0);\n"
"texCoord = vertor;\n"
"}";
frag =
"#version 330\n"
"in vec3 texCoord;\n"
"out vec4 fragColor;\n"
"uniform samplerCube cubemap;\n"
"void main (void){\n"
"fragColor = texture(cubemap, texCoord);\n"
"};";
m_shader->setVertexSource(vert);
m_shader->setFragmentSource(frag);
if (!m_shader->load()){
HandleError::showError("Shader didn't load in Skybox");
exit(-1);
}
GLfloat tmpVertices[] = {
-1.f,-1.f,-1.f, -1.f,1.f,-1.f, -1.f,-1.f,1.f, -1.f,1.f,1.f,
1.f,-1.f,-1.f, 1.f,-1.f,1.f, 1.f,1.f,-1.f, 1.f,1.f,1.f,
-1.f,-1.f,-1.f, -1.f,-1.f,1.f, 1.f,-1.f,-1.f, 1.f,-1.f,1.f,
-1.f,1.f,-1.f, 1.f,1.f,-1.f, -1.f,1.f,1.f, 1.f,1.f,1.f,
-1.f,-1.f,-1.f, 1.f,-1.f,-1.f, -1.f,1.f,-1.f, 1.f,1.f,-1.f,
-1.f,-1.f,1.f, -1.f,1.f,1.f, 1.f,-1.f,1.f, 1.f,1.f,1.f};
int max = sizeof(tmpVertices) / sizeof(tmpVertices[0]);
for (int i(0); i < max; ++i){
m_vertices.push_back(tmpVertices[i]);
m_textures.push_back(tmpVertices[i]);
}
m_pointsNumber = m_vertices.size() / 3;
PVM = glGetUniformLocation(m_shader->getProgramID(), "PVM");
vertex = glGetAttribLocation(m_shader->getProgramID(), "vertor");
this->initVertexBufferObject();
this->initVertexArrayObject();
}
void SkyBox::initVertexBufferObject()
{
m_verticesSize = m_vertices.size() * sizeof(GLfloat);
m_texturesSize = m_textures.size() * sizeof(GLfloat);
glGenBuffers(1, &m_vboID);
//verrouillage de l'objet
glBindBuffer(GL_ARRAY_BUFFER, m_vboID);
// Allocation de la memoire video
glBufferData(GL_ARRAY_BUFFER, m_verticesSize + m_texturesSize, 0, GL_STATIC_DRAW);
// Transfert des donnees
glBufferSubData(GL_ARRAY_BUFFER, 0, m_verticesSize, &m_vertices[0]);
glBufferSubData(GL_ARRAY_BUFFER, m_verticesSize, m_texturesSize, &m_textures[0]);
glEnableVertexAttribArray(vertex);
glVertexAttribPointer(vertex, 3, GL_FLOAT, GL_FALSE, 0, 0);
// Deverrouillage de l'objet
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void SkyBox::bindVertexBufferObject()
{
// Verrouillage du VBO
glBindBuffer(GL_ARRAY_BUFFER, m_vboID);
// Acces aux vertices dans la memoire video
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glEnableVertexAttribArray(0);
//envoi des coordonnees des textures
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(m_verticesSize));
glEnableVertexAttribArray(2);
// Deverrouillage du VBO
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void SkyBox::initializeGLNoList()
{
}
void SkyBox::paintGL(float &x, float &y)
{
glUseProgram(m_shader->getProgramID());
glBindVertexArray(m_vaoID);
float dist = Camera::getDistanceView() - 400.f;
glm::mat4 RotateX = glm::rotate(glm::mat4(1.0f), x, glm::vec3(-1.0f, 0.0f, 0.0f));
glm::mat4 RotateY = glm::rotate(RotateX, y, glm::vec3(0.0f, 1.0f, 0.0f));
glm::mat4 View = glm::mat4(1.0f);
glm::mat4 Model = glm::scale(glm::mat4(1.0f), glm::vec3(dist, dist, dist));
glm::mat4 M = Camera::getProjectionMatrix() * View * Model * RotateY;
glUniformMatrix4fv(PVM, 1, GL_FALSE, glm::value_ptr(M));
glEnable(GL_TEXTURE_CUBE_MAP_ARB);
m_texture.bind();
glDrawArrays(GL_TRIANGLE_STRIP, 0, m_pointsNumber);
m_texture.unbind();
glDisable(GL_TEXTURE_CUBE_MAP_ARB);
glBindVertexArray(0);
glUseProgram(0);
} |
Partager