15 #include "rocketRenderInterface.h"
16 #include "cullableObject.h"
17 #include "cullHandler.h"
18 #include "geomVertexData.h"
19 #include "geomVertexArrayData.h"
20 #include "internalName.h"
21 #include "geomVertexWriter.h"
22 #include "geomTriangles.h"
23 #include "colorAttrib.h"
24 #include "colorBlendAttrib.h"
25 #include "cullBinAttrib.h"
26 #include "depthTestAttrib.h"
27 #include "depthWriteAttrib.h"
28 #include "scissorAttrib.h"
30 #include "textureAttrib.h"
31 #include "texturePool.h"
41 nassertv(context != NULL);
46 _net_state = RenderState::make(
47 CullBinAttrib::make(
"unsorted", 0),
48 DepthTestAttrib::make(RenderAttrib::M_none),
49 DepthWriteAttrib::make(DepthWriteAttrib::M_off),
50 ColorBlendAttrib::make(ColorBlendAttrib::M_add,
51 ColorBlendAttrib::O_incoming_alpha,
52 ColorBlendAttrib::O_one_minus_incoming_alpha
54 ColorAttrib::make_vertex()
56 _dimensions = context->GetDimensions();
61 _net_transform = NULL;
71 make_geom(Rocket::Core::Vertex* vertices,
72 int num_vertices,
int* indices,
int num_indices,
76 vdata->unclean_set_num_rows(num_vertices);
83 for (
int i = 0; i < num_vertices; ++i) {
84 const Rocket::Core::Vertex &vertex = vertices[i];
87 cwriter.add_data4i(vertex.colour.red, vertex.colour.green,
88 vertex.colour.blue, vertex.colour.alpha);
89 twriter.add_data2f(vertex.tex_coord.x * tex_scale[0],
90 (1.0f - vertex.tex_coord.y) * tex_scale[1]);
98 idata->unclean_set_num_rows(num_indices);
101 for (
int i = 0; i < num_indices; ++i) {
107 geom->add_primitive(triangles);
117 render_geom(const
Geom* geom, const
RenderState* state, const Rocket::Core::Vector2f& translation) {
120 if (_enable_scissor) {
121 state = state->add_attrib(ScissorAttrib::make(_scissor));
123 <<
"Rendering geom " << geom <<
" with state "
124 << *state <<
", translation (" << offset <<
"), "
125 <<
"scissor region (" << _scissor <<
")\n";
128 <<
"Rendering geom " << geom <<
" with state "
129 << *state <<
", translation (" << offset <<
")\n";
132 CPT(TransformState) internal_transform =
133 _trav->get_scene()->get_cs_world_transform()->compose(
134 _net_transform->compose(TransformState::make_pos(offset)));
139 _trav->get_cull_handler()->record_object(
object, _trav);
149 RenderGeometry(Rocket::Core::Vertex* vertices,
150 int num_vertices,
int* indices,
int num_indices,
151 Rocket::Core::TextureHandle thandle,
152 const Rocket::Core::Vector2f& translation) {
157 if (texture != (
Texture *)NULL) {
161 PT(
Geom) geom = make_geom(vertices, num_vertices, indices, num_indices,
165 if (texture != (
Texture *)NULL) {
166 state = RenderState::make(TextureAttrib::make(texture));
168 state = RenderState::make_empty();
171 render_geom(geom, state, translation);
180 Rocket::Core::CompiledGeometryHandle RocketRenderInterface::
181 CompileGeometry(Rocket::Core::Vertex* vertices,
182 int num_vertices,
int* indices,
int num_indices,
183 Rocket::Core::TextureHandle thandle) {
187 CompiledGeometry *c =
new CompiledGeometry;
190 if (texture != (
Texture *)NULL) {
192 <<
"Compiling geom " << c->_geom <<
" with texture '"
193 << texture->get_name() <<
"'\n";
198 stage->set_mode(TextureStage::M_modulate);
201 attr = DCAST(TextureAttrib, attr->add_on_stage(stage, (
Texture *)texture));
207 <<
"Compiling geom " << c->_geom <<
" without texture\n";
209 c->_state = RenderState::make_empty();
212 c->_geom = make_geom(vertices, num_vertices, indices, num_indices,
213 GeomEnums::UH_static, tex_scale);
215 return (Rocket::Core::CompiledGeometryHandle) c;
224 void RocketRenderInterface::
225 RenderCompiledGeometry(Rocket::Core::CompiledGeometryHandle geometry,
const Rocket::Core::Vector2f& translation) {
227 CompiledGeometry *c = (CompiledGeometry*) geometry;
228 render_geom(c->_geom, c->_state, translation);
237 void RocketRenderInterface::
238 ReleaseCompiledGeometry(Rocket::Core::CompiledGeometryHandle geometry) {
239 delete (CompiledGeometry*) geometry;
248 bool RocketRenderInterface::
249 LoadTexture(Rocket::Core::TextureHandle& texture_handle,
250 Rocket::Core::Vector2i& texture_dimensions,
251 const Rocket::Core::String& source) {
265 texture_dimensions.x = 0;
266 texture_dimensions.y = 0;
270 tex->set_minfilter(SamplerState::FT_nearest);
271 tex->set_magfilter(SamplerState::FT_nearest);
276 int width = tex->get_orig_file_x_size();
277 int height = tex->get_orig_file_y_size();
278 if (width == 0 && height == 0) {
281 width = tex->get_x_size();
282 height = tex->get_y_size();
284 texture_dimensions.x = width;
285 texture_dimensions.y = height;
288 texture_handle = (Rocket::Core::TextureHandle) tex.p();
299 bool RocketRenderInterface::
300 GenerateTexture(Rocket::Core::TextureHandle& texture_handle,
301 const Rocket::Core::byte* source,
302 const Rocket::Core::Vector2i& source_dimensions) {
305 tex->setup_2d_texture(source_dimensions.x, source_dimensions.y,
306 Texture::T_unsigned_byte, Texture::F_rgba);
310 tex->set_size_padded(source_dimensions.x, source_dimensions.y);
312 PTA_uchar image = tex->modify_ram_image();
315 size_t src_stride = source_dimensions.x * 4;
316 size_t dst_stride = tex->get_x_size() * 4;
317 const
unsigned char *src_ptr = source + (src_stride * source_dimensions.y);
318 unsigned char *dst_ptr = &image[0];
320 for (; src_ptr > source; dst_ptr += dst_stride) {
321 src_ptr -= src_stride;
322 for (
size_t i = 0; i < src_stride; i += 4) {
323 dst_ptr[i + 0] = src_ptr[i + 2];
324 dst_ptr[i + 1] = src_ptr[i + 1];
325 dst_ptr[i + 2] = src_ptr[i];
326 dst_ptr[i + 3] = src_ptr[i + 3];
330 tex->set_wrap_u(SamplerState::WM_clamp);
331 tex->set_wrap_v(SamplerState::WM_clamp);
332 tex->set_minfilter(SamplerState::FT_nearest);
333 tex->set_magfilter(SamplerState::FT_nearest);
336 texture_handle = (Rocket::Core::TextureHandle) tex.p();
347 void RocketRenderInterface::
348 ReleaseTexture(Rocket::Core::TextureHandle texture_handle) {
349 Texture *tex = (Texture *)texture_handle;
350 if (tex != (Texture *)NULL) {
361 void RocketRenderInterface::
362 EnableScissorRegion(
bool enable) {
363 _enable_scissor = enable;
372 void RocketRenderInterface::
373 SetScissorRegion(
int x,
int y,
int width,
int height) {
374 _scissor[0] = x / (PN_stdfloat) _dimensions.x;
375 _scissor[1] = (x + width) / (PN_stdfloat) _dimensions.x;
376 _scissor[2] = 1.0f - ((y + height) / (PN_stdfloat) _dimensions.y);
377 _scissor[3] = 1.0f - (y / (PN_stdfloat) _dimensions.y);
This object provides a high-level interface for quickly writing a sequence of numeric values from a v...
void add_data1i(int data)
Sets the write row to a particular 1-component value, and advances the write row. ...
Specifies parameters that may be passed to the loader.
Represents a texture object, which is typically a single 2-d image but may also represent a 1-d or 3-...
This is a three-component vector distance (as opposed to a three-component point, which represents a ...
A lightweight C++ object whose constructor calls acquire() and whose destructor calls release() on a ...
LVecBase2 get_tex_scale() const
Returns a scale pair that is suitable for applying to geometry via NodePath::set_tex_scale(), which will convert texture coordinates on the geometry from the range 0..1 into the appropriate range to render the video part of the texture.
Indicates the set of TextureStages and their associated Textures that should be applied to (or remove...
void render(Rocket::Core::Context *context, CullTraverser *trav)
Called by RocketNode in cull_callback.
static AutoTextureScale get_textures_power_2()
This flag returns ATS_none, ATS_up, or ATS_down and controls the scaling of textures in general...
The smallest atom of cull.
void set_auto_texture_scale(AutoTextureScale scale)
Set this flag to ATS_none, ATS_up, ATS_down, or ATS_pad to control how a texture is scaled from disk ...
The name of a file, such as a texture file or an Egg file.
This defines the actual numeric vertex data stored in a Geom, in the structure defined by a particula...
A container for geometry primitives.
This is the base class for all two-component vectors and points.
static LVector3f right(CoordinateSystem cs=CS_default)
Returns the right vector for the given coordinate system.
This represents a unique collection of RenderAttrib objects that correspond to a particular renderabl...
const TransformState * get_world_transform() const
Returns the position of the starting node relative to the camera.
Defines a series of disconnected triangles.
This is the preferred interface for loading textures from image files.
Class that provides the main render interface for libRocket integration.
Defines the properties of a named stage of the multitexture pipeline.
static LVector3f up(CoordinateSystem cs=CS_default)
Returns the up vector for the given coordinate system.
This object performs a depth-first traversal of the scene graph, with optional view-frustum culling...
This is the data for one array of a GeomVertexData structure.
static Filename from_os_specific(const string &os_specific, Type type=T_general)
This named constructor returns a Panda-style filename (that is, using forward slashes, and no drive letter) based on the supplied filename string that describes a filename in the local system conventions (for instance, on Windows, it may use backslashes or begin with a drive letter and a colon).