File: CameraCubeMapDecl.cpp

package info (click to toggle)
darkradiant 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,080 kB
  • sloc: cpp: 264,743; ansic: 10,659; python: 1,852; xml: 1,650; sh: 92; makefile: 21
file content (108 lines) | stat: -rw-r--r-- 3,038 bytes parent folder | download | duplicates (3)
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
#include "CameraCubeMapDecl.h"
#include "textures/CubeMapTexture.h"
#include "debugging/gl.h"
#include "itextstream.h"
#include <stdexcept>

namespace shaders
{

// Constructor
CameraCubeMapDecl::CameraCubeMapDecl(const std::string& prefix)
: _prefix(prefix)
{ }

// Public construction method
NamedBindablePtr CameraCubeMapDecl::createForPrefix(const std::string& prefix)
{
    return NamedBindablePtr(new CameraCubeMapDecl(prefix));
}

// Bind directional image
void CameraCubeMapDecl::bindDirection(const std::string& dir,
                                      GLuint glDir) const
{
    // Load the image
    ImagePtr img = GlobalImageLoader().imageFromVFS(_prefix + dir);
    if (!img)
    {
        throw std::runtime_error(
            "Camera cube map directional image not found: "
            + _prefix + dir
        );
    }

    // Bind the image to OpenGL
    glTexImage2D(
        glDir,
        0,                    //level
        GL_RGBA,              //internal format
        static_cast<GLsizei>(img->getWidth()),   //width
        static_cast<GLsizei>(img->getHeight()),  //height
        0,                    //border
        GL_RGBA,               //format
        GL_UNSIGNED_BYTE,     //type
        img->getPixels()
    );
    debug::assertNoGlErrors();
}

/* NamedBindable implementation */

std::string CameraCubeMapDecl::getIdentifier() const
{
    return "_cameraCubeMap_" + _prefix;
}

std::string CameraCubeMapDecl::getExpressionString()
{
    return _prefix;
}

TexturePtr CameraCubeMapDecl::bindTexture(const std::string& name,
                                          Role /* role */) const
{
    // Load the six images from the prefix
    try
    {
        // Allocate the GL texture
        GLuint texnum;
        glGenTextures(1, &texnum);
        glBindTexture(GL_TEXTURE_CUBE_MAP, texnum);

        // Set filtering and mipmapping
		glTexParameteri(
            GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR
        );
		glTexParameteri(
            GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR
        );
		glTexParameteri(
            GL_TEXTURE_CUBE_MAP, GL_GENERATE_MIPMAP, GL_TRUE
        );

        // Bind the images
        bindDirection("_right", GL_TEXTURE_CUBE_MAP_POSITIVE_X);
        bindDirection("_left", GL_TEXTURE_CUBE_MAP_NEGATIVE_X);
        bindDirection("_up", GL_TEXTURE_CUBE_MAP_POSITIVE_Y);
        bindDirection("_down", GL_TEXTURE_CUBE_MAP_NEGATIVE_Y);
        bindDirection("_forward", GL_TEXTURE_CUBE_MAP_POSITIVE_Z);
        bindDirection("_back", GL_TEXTURE_CUBE_MAP_NEGATIVE_Z);

        rMessage() << "[shaders] bound cubemap texture " << texnum << std::endl;

        // Unbind and create texture object
        glBindTexture(GL_TEXTURE_CUBE_MAP, 0);

        return TexturePtr(new CubeMapTexture(texnum, name));
    }
    catch (const std::runtime_error& e)
    {
        rError() << "[shaders] Unable to bind camera cubemap '"
                  << name << "': " << e.what() << std::endl;

        return TexturePtr();
    }
}

}