22 #include "base/integral_types.h"
51 static const char kVertexShaderString[] =
52 "attribute vec3 aVertex;\n"
53 "attribute vec2 aTexCoords;\n"
54 "varying vec2 vTextureCoords;\n"
57 " vTextureCoords = aTexCoords;\n"
58 " gl_Position = vec4(aVertex, 1.);\n"
61 static const char kTextureFragmentShaderString[] =
63 "#ifdef GL_FRAGMENT_PRECISION_HIGH\n"
64 "precision highp float;\n"
66 "precision mediump float;\n"
70 "uniform sampler2D uTexture;\n"
71 "varying vec2 vTextureCoords;\n"
74 " gl_FragColor = texture2D(uTexture, vTextureCoords);\n"
77 static const char kCubeMapFragmentShaderString[] =
79 "#ifdef GL_FRAGMENT_PRECISION_HIGH\n"
80 "precision highp float;\n"
82 "precision mediump float;\n"
86 "uniform int uCubeMapFace;\n"
87 "uniform samplerCube uCubeMap;\n"
88 "varying vec2 vTextureCoords;\n"
91 " /* Put coords in range (-1, 1). */\n"
92 " float s = -1. + 2. * vTextureCoords.s;\n"
93 " float t = -1. + 2. * vTextureCoords.t;\n"
95 " if (uCubeMapFace == 0) { /* Left */\n"
96 " tc = vec3(-1., t, s);\n"
97 " } else if (uCubeMapFace == 1) { /* Bottom */\n"
98 " tc = vec3(s, -1., t);\n"
99 " } else if (uCubeMapFace == 2) { /* Back */\n"
100 " tc = vec3(s, -t, -1.);\n"
101 " } else if (uCubeMapFace == 3) { /* Right */\n"
102 " tc = vec3(1., t, -s);\n"
103 " } else if (uCubeMapFace == 4) { /* Top */\n"
104 " tc = vec3(s, 1., -t);\n"
105 " } else { /* Front */\n"
106 " tc = vec3(s, t, 1.);\n"
108 " gl_FragColor = textureCube(uCubeMap, tc);\n"
122 return allocator.
Get() ?
132 reg->IncludeGlobalRegistry();
155 const bool is_cubemap = cubemap.Get();
160 new(allocator) gfx::StateTable(viewport_size[0], viewport_size[1]));
161 state_table->SetViewport(
162 math::Range2i::BuildWithSize(math::Point2i::Zero(), viewport_size));
163 node->SetStateTable(state_table);
166 gfxutils::RectangleSpec rect_spec;
167 rect_spec.allocator = allocator;
168 rect_spec.size.Set(2.f, 2.f);
171 node->AddShape(rect_shape);
175 node->SetShaderProgram(
177 "Ion image renderutils", reg, kVertexShaderString,
178 is_cubemap ? kCubeMapFragmentShaderString : kTextureFragmentShaderString,
183 node->AddUniform(reg->Create<gfx::Uniform>(
"uCubeMap", cubemap));
184 node->AddUniform(reg->Create<gfx::Uniform>(
"uCubeMapFace",
185 static_cast<int>(face)));
187 node->AddUniform(reg->Create<gfx::Uniform>(
"uTexture", texture));
205 DCHECK(renderer->GetGraphicsManager().Get());
206 const gfx::GraphicsManager& gm = *renderer->GetGraphicsManager();
214 new (st_alloc) gfx::FramebufferObject(image_size[0], image_size[1]));
215 fbo->SetColorAttachment(0U,
216 gfx::FramebufferObject::Attachment(target_format));
217 renderer->BindFramebuffer(fbo);
218 renderer->DrawScene(node);
222 math::Range2i::BuildWithSize(math::Point2i::Zero(), image_size),
233 static const gfx::ImagePtr RenderTextureOrCubeMapTextureToImage(
239 if ((texture.Get() || cubemap.Get()) &&
240 renderer.Get() && size[0] && size[1]) {
241 gfx::NodePtr node = BuildNode(texture, cubemap, face, size,
242 GetShortTermAllocator(allocator));
243 output_image = RenderToImage(renderer, node, size, allocator);
262 return RenderTextureOrCubeMapTextureToImage(
264 math::Vector2i(width, height), renderer, allocator);
269 uint32
width, uint32 height,
271 return RenderTextureOrCubeMapTextureToImage(
273 math::Vector2i(width, height), renderer, allocator);
kShortTerm is used for objects that are very transient in nature, such as scratch memory used to comp...
base::ReferentPtr< Node >::Type NodePtr
base::ReferentPtr< FramebufferObject >::Type FramebufferObjectPtr
Convenience typedef for shared pointer to a FramebufferObject.
const gfx::ImagePtr RenderTextureImage(const gfx::TexturePtr &texture, uint32 width, uint32 height, const gfx::RendererPtr &renderer, const base::AllocatorPtr &allocator)
Public functions.
Format
Supported image formats.
base::ReferentPtr< Image >::Type ImagePtr
base::ReferentPtr< Shape >::Type ShapePtr
Convenience typedef for shared pointer to a Shape.
base::ReferentPtr< CubeMapTexture >::Type CubeMapTexturePtr
Convenience typedef for shared pointer to a CubeMapTexture.
CubeFace
The names of faces of the cube map.
#define DCHECK_GT(val1, val2)
virtual const AllocatorPtr & GetAllocatorForLifetime(AllocationLifetime lifetime) const
Returns the correct Allocator to use to allocate memory with a specific lifetime. ...
base::ReferentPtr< StateTable >::Type StateTablePtr
Convenience typedef for shared pointer to a StateTable.
SharedPtr< Allocator > AllocatorPtr
Three channel RGB images.
T * Get() const
Returns a raw pointer to the instance, which may be NULL.
const Grid & image
The original monochrome image data, as doubles (0 - 1).
bits blue, packed half float data.
TexturePtr texture
The Texture to add sub-image data to.
static const ShaderProgramPtr BuildFromStrings(const std::string &id_string, const ShaderInputRegistryPtr ®istry_ptr, const std::string &vertex_shader_string, const std::string &fragment_shader_string, const base::AllocatorPtr &allocator)
Convenience function that builds and returns a new ShaderProgram instance that uses the given ShaderI...
base::ReferentPtr< ShaderInputRegistry >::Type ShaderInputRegistryPtr
Convenience typedef for shared pointer to a ShaderInputRegistry.
base::ReferentPtr< Renderer >::Type RendererPtr
Convenience typedef for shared pointer to a Renderer.
A SharedPtr is a smart shared pointer to an instance of some class that implements reference counting...
const gfx::ShapePtr BuildRectangleShape(const RectangleSpec &spec)
Builds and returns a Shape representing a rectangle in one of the principal Cartesian planes...
base::ReferentPtr< Texture >::Type TexturePtr
Convenience typedef for shared pointer to a Texture.
static const AllocatorPtr & GetDefaultAllocatorForLifetime(AllocationLifetime lifetime)
const gfx::ImagePtr RenderCubeMapTextureFaceImage(const gfx::CubeMapTexturePtr &cubemap, gfx::CubeMapTexture::CubeFace face, uint32 width, uint32 height, const gfx::RendererPtr &renderer, const base::AllocatorPtr &allocator)
This is similar to RenderTextureImage(), but instead operates on one face of a CubeMapTexture.