Setting the color/texture of something WITHOUT using Renderer?

Hi all,



Sorry if this has been asked before, I'm new to jME and it's possible my search keyword selection isn't quite up to it yet!



Now I know that I can set the colour of an object (under lighting) using MaterialState, like so:


Spatial spatial;
ColorRGBA color;
Renderer renderer;
...
spatial.setRenderState(renderer.createMaterialState().setDiffuse(color));



A slightly more complicated but vaguely equivalent chunk of code will also do a texture.

Now my question is, is there any way of setting things up such that code that needs to set the color or texture of a spatial DOESN'T need access to the renderer (or DisplaySystem or anything apart from the spatial itself)? For some strange reason the software engineer in me starts squirming at the thought of any object building thingo needing access to the renderer if it wants to be something other than the default colour (and yet doesn't need access when it's a pregenerated model complete with colours and textures).

In straight C OpenGL I'd do something like this ...


...
glEnable(GL_COLOR_MATERIAL);
...
glBegin(...);
...
glColor4f(...);
drawMySpatial();
...
glEnd(...);
...



I tried doing the following in jME which I assumed would be vaguely equivalent:


...
GL11.glEnable(GL11.GL_COLOR_MATERIAL);
...
spatial.setSolidColor(color);
...



but it doesn't seem to make any difference - the spatial in question remains the default color when lighting is enabled (and takes on the right color when lighting is disabled).

I'd be happy with a solution that involved some jiggery pokery up front in the setup phase - or even something that involved modifying the render step slightly, as long as it doesn't involve passing objects like the renderer around.

Any ideas anyone? :D

Why don't you want to use renderer?

The reason why you have to access it is because that allows jME to support multiple rendering libraries. The ***State classes in question provide render-system specific code that allows you to draw correctly for that rendering library.

You can access the current renderer at any point of time using DisplaySystem.getDisplaySystem().getRenderer(), (or, slightly shorter with the jME-context system: JmeContext.get().getRenderer())

Ah cool thanks for that! :smiley:



I didn't realise you could use the static methods in JmeContext or DisplaySystem to access the renderer … it isn't so much that I didn't want to use the renderer, it's more it seemed rather messy from an SE perspective to have to keep passing it around so I was wondering if there was a neater way. I was half looking for the rough equivalent to the OpenGL code I posted but what you suggest also works. :slight_smile:



I should have a closer look at JmeContext and DisplaySystem to see what other gems I can get from their static methods :slight_smile: