Awesomium offscreen rendered to Leadwerks texture - awesomium

I'm using the Leadwerks game engine and I'm trying to get Awesomium to render to a Leadwerks Texture but having no luck. Below is the code where I create a texture, allocate unsigned char* variable that I put the Awesomium surface into via CopyTo() then set that variable to the LEadwerks Texture but the screen stays black so clearly I'm not understanding something here. Any ideas on what I'm missing?
Texture* uiTex = Texture::Create(window->GetWidth(), window->GetHeight());
unsigned char* pixels = (unsigned char*)malloc(uiTex->GetMipmapSize(0));
// copy surface to LE texture and draw that texture to screen
BitmapSurface* surface = static_cast<BitmapSurface*>(view->surface());
surface->CopyTo(pixels, 1024, 32, true, false);
uiTex->SetPixels((char*)pixels);
context->DrawImage(uiTex, 0, 0);

I know this is old but if some unlucky bastard like myself stumble upon this in the future there might as well be a little guidance.
Begin by checking that everything has been setup correctly in Awesomium. Otherwise it will seem fine but the surface returned will simply be null.
Save the Awesomium surface to file and inspect to verify that it gets generated in the first place.

Related

ArcGIS - How to move a graphic or symbol on the map

I'm trying to create an overlay in ArcGIS that has moving graphics/symbols which are updated by coordinates received from roving devices. I'm able to display a simple symbol initially but cannot get it to move on the map. My test code is
GraphicsOverlay machineOverlay = new GraphicsOverlay();
MainMapView.GraphicsOverlays.Add(machineOverlay);
MapPointBuilder rdLocation = new MapPointBuilder(150.864119200149, -32.3478640837185, SpatialReferences.Wgs84);
SimpleMarkerSymbol sRD1234 = new SimpleMarkerSymbol()
{
Color = System.Drawing.Color.Red,
Size = 10,
Style = SimpleMarkerSymbolStyle.Circle
};
Graphic graphicWithSymbol = new Graphic(rdLocation.ToGeometry(), sRD1234);
machineOverlay.Graphics.Add(graphicWithSymbol);
// here the red circle is displayed correctly on the map
rdLocation.SetXY(150.887115, -32.357600);
rdLocation.ReplaceGeometry(rdLocation.ToGeometry());
// here I expect the red circle to move but it doesn't
Do I need to trigger an event to "re-render" or refresh the overlay, or what do I need to do to get the graphic to move on my map?
There was a similar question here and the answer was "just update the geometry" which is what I'm attempting to do, but with no success.
If there is an entirely different or better approach to moving markers on a map please suggest, I'm just getting started in the ArcGIS runtime.
Thanks
After a lot of searching I replaced one line of code and its now working
//rdLocation.ReplaceGeometry(rdLocation.ToGeometry());
graphicWithSymbol.Geometry = rdLocation.ToGeometry();
It seems I misunderstood the function of ReplaceGeometry(). Any clarification on this would be helpful.

How to apply shaders and generate images only once?

I'm trying to apply a pixelation shader to my textures and I need it to be applied only once, after that I can reuse my shader generated images as textures over and over without having to calculate every single time.
so how do I take a few images -> apply a shader and render them only once every time the game loads -> and use them as my textures?
so far I've managed to find the shader to apply:
shader_type canvas_item;
uniform int amount = 40;
void fragment()
{
vec2 grid_uv = round(UV * float(amount)) / float(amount);
vec4 text = texture(TEXTURE, grid_uv);
COLOR = text;
}
but I have no idea how to render out the images using it
Shaders reside in the GPU, and their output goes to the screen. To save the image, the CPU would have to see the GPU output, and that does not happen… Usually. And since it does not go through the CPU, the performance is good. Usually. Well, at least it is better than if the CPU was doing it all the time.
Also, are you sure you don't want to get a pixel art look by other means? Such as removing filter from the texture, changing the stretch mode and working on a small resolution, and perhaps enable pixel snap? No? Watch How to make a silky smooth camera for pixelart games in Godot. Still No? Ok...
Anyway, for what you want, you are going to need a Viewport.
Viewport setup
What you will need is to create a Viewport. Don't forget to set its size. Also may want to set render_target_v_flip to true, this flips the image vertically. If you find the output image is upside down it is because you need to toggle render_target_v_flip.
Then place as child of the Viewport what you want to render.
Rendering
Next, you can read the texture form the Viewport, convert it to an image, and save it to a png. I'm doing this on a tool script attached to the Viewport, so I'll have a workaround to trigger the code from the inspector panel. My code looks like this:
tool
extends Viewport
export var save:bool setget do_save
func do_save(new_value) -> void:
var image := get_texture().get_data()
var error := image.save_png("res://output.png")
if error != OK:
push_error("failed to save output image.")
You can, of course, export a FILE path String to ease changing it in the inspector panel. Here I'm handing common edge cases:
tool
extends Viewport
export(String, FILE) var path:String
export var save:bool setget do_save
func do_save(_new_value) -> void:
var target_path := path.strip_edges()
var folder := target_path.get_base_dir()
var file_name := target_path.get_file()
var extension := target_path.get_extension()
if file_name == "":
push_error("empty file name.")
return
if not (Directory.new()).dir_exists(folder):
push_error("output folder does not exist.")
return
if extension != "png":
target_path += "png" if target_path.ends_with(".") else ".png"
var image := get_texture().get_data()
var error := image.save_png(target_path)
if error != OK:
push_error("failed to save output image.")
return
print("image saved to ", target_path)
Another option is to use ResourceSaver:
tool
extends Viewport
export var save:bool setget do_save
func do_save(new_value) -> void:
var image := get_texture().get_data()
var error := ResourceSaver.save("res://image.res", image)
if error != OK:
push_error("failed to save output image.")
This will only work from the Godot editor, and will only work for Godot, since you get a Godot resource file. Although I find interesting the idea of using Godot to generate images. I'm going to suggest going with ResourceSaver if you want to automate generating them for Godot.
About saving resources from tool scripts
In the examples above, I'm assuming you are saving to a resource path. This is because the intention is to use the output image as a resource in Godot. Using a resource path has a couple implications:
This might not work on an exported game (since the goals is improve the workflow, this is OK).
Godot would need to re-import the resource, but will not notice it changed.
We can deal with the second point from an EditorPlugin, if that is what you are doing, you can do this to tell Godot to scan for changes:
get_editor_interface().get_resource_filesystem().scan()
And if you are not, you can cheat by creating an empty EditorPlugin. The idea is to do this:
var ep = EditorPlugin.new()
ep.get_editor_interface().get_resource_filesystem().scan()
ep.free()
By the way, you will want to cache cache the EditorPlugin instead of making a new one each time. Or better yet, cache the EditorFileSystem you get from get_resource_filesystem.
Automation
Now, I'm aware that it can be cumbersome to have to place things inside the Viewport. It might be Ok for your workflow if you don't need to do it all the time.
But what about automating it? Well, regardless of the approach, you will need a tool script that makes a hidden Viewport, takes a Node, checks if it has a shader, if it does, it moves it temporarily to the Viewport, get the rendered texture (get_texture()) sets it as the texture of the Node, removes the shader, and returns the Node to its original position in the scene. Or instead of looking for a shader in the Node, always apply a shader to whatever Node, perhaps loaded as a resource instead of hard-coded.
Note: I believe you need to let an idle frame pass between adding the Node to the Viewport and getting the texture, so the texture updates. Or was it two idle frames? Well, if one does not work, try adding another one.
About making an EditorPlugin
As you know, you can create an addon from project settings. This will create an EditorPlugin script for you. There you can either add an option to the tools menu (with add_tool_menu_item), or add it to the tool bar of the editor (with add_control_to_container). And have it act on the current selection in the edited scene (you can either use get_selection, or overwrite the edit and handles methods). You may also want to make an undo entry for that, see get_undo_redo.
Or, alternatively you can have it keep track (or look for) the Nodes it has to act upon, and then work on the build virtual method, which runs when the project is about to run. I haven't worked with the build virtual method, so I don't know if it has any quirks to gotchas to be aware of.

Fast drawing/rendering 2d image texture

I am freaking out to solve my software flickering while running/processing images.
First, I'll introduce my problem, explaining what I did, then at the end I'll leave my questions/needs:
My software process images and shows it imediatelly on screen, acting like a scanner. The image pixels is built using image photosensors, processed using OpenCV, and draw as soon as possible on screen to provide a smooth movement while the image is generated.
In practice, I first have a white-solid 1000x808 blank image, which its pixels are replaced with respective photosensored built-image and is show on the screen so that the image flows from the left to the right on the screen.
The entire process of each frame generation lasts about 10 ~ 12ms.
However, The frames does not flows as expected on screen. It flickers a lot. As I have no Idea how to measure FPS on OpenGL, I suppose my OpenGL code (which I know nothing about OpenGL at all) is causing the flickering.
I am using the following code on my GLWidget, which I did my best to write but I am quite sure that it is not OpenGL best-performance-written.
I have almost nothing on my GLWidget constructor, but I have playing around with these functions, with no significant effect:
QGLFormat glFormat;
glFormat.setSwapInterval(0);
glFormat.setDoubleBuffer(true);
glFormat.setSampleBuffers(true);
glFormat.setDirectRendering(true);
glFormat.setSamples(4);
setFormat(glFormat);
My initializeGL is shown as follows:
initializeGL()
{
glClearColor(255.0f,255.0f,255.0f,0.1f);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,this->width(), this->height(), 0.0f,0.0f,1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_TEXTURE_2D);
glGenTextures(3, &this->texture);
glBindTexture(GL_TEXTURE_2D, this->texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 3, this->width(), this->height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glDisable(GL_TEXTURE_2D);
this->original = cv::Mat(this->height(), this->width(), CV_8UC4);
memset(this->original.data, 0xff, this->original.rows * this->original.step);
this->repaint = true;
}
As you can see, at this point a blank image sized 1000x808 (screen size) is shown on initialization (because images aren't built yet)
When the images starts to being built (every 5 column) I send the image (through setImage) to the screen, then it renders with OpenGL on overriden paintGL function:
setImage(cv::Mat imageRGBA)
{
this->original = imageRGBA;
this->repaint = true;
update();
}
paintGL()
{
if (this->repaint) {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, this->texture);
glTexImage2D(GL_TEXTURE_2D, 0, 3, this->original.cols, this->original.rows, 0, GL_RGBA, GL_UNSIGNED_BYTE, this->original.data );
glBegin(GL_QUADS);
glTexCoord2f(0,1); glVertex2f(0,this->height());
glTexCoord2f(0,0); glVertex2f(0,0);
glTexCoord2f(1,0); glVertex2f(this->width(),0);
glTexCoord2f(1,1); glVertex2f(this->width(),this->height());
glEnd();
glDisable(GL_TEXTURE_2D);
this->repaint = false;
}
}
Considerations:
As you can see, I work only with 2D images sized 1000x808 (fixed), and it shouldn't require too much power for rendering. I am currently coding this to work on Intel Core i3 Haswell machine (with integrated intel HD Graphics). On top a Linux Ubuntu 14.04 x64 OS, with latest intel drivers installed.
Expectation:
I expect to render/draw the images as soon as possible, without flickering. I have about 150 1000x808 images to render in a row (with ~12ms between images build process).
I would love to have a smooth ~60Hz smooth images being rendered on the screen
Questions:
Based on my code, what I need to do due to improve performance and stop flickering?
Is it really necessary to move to QOpenGLWidget other than current QGLWidget (deprecated?)?
As I have been written an OpenGL "immediate mode" code, Should I consider to move to VBO/PBO/VAO? And which one is best suitable for me?
Do I need linux tweaks due to improve performance? If so, what kind
of?
Any kind of help is very welcome!

Why MovieClip is not visible?

I tried to use createjs to create a movieClip and add an image inside it like this:
rect2 = new Bitmap(_preloader.getResult("rect").result);
mv = new MovieClip("single", 0, false, []);
mv.addChild(rect2);
_stage.addChild(mv);
I expect to see rect2 on stage, but it dose not show up, if I added rect2 to stage it will show up, so what's I am missing here?
Is there any reason you are using MovieClip? It is mainly used to handle export from Flash Pro using Toolkit for CreateJS.
Instead, you might try using a Bitmap instance, which wraps an image, canvas, or video element.
One note for MovieClips is that you need to gotoAndStop/gotoAndPlay in order to set the initial state, it does not "default" to the first frame like Flash.

Draw 32-bits with alpha channel from resources using Direct2D

I have a legacy MCF application that displays some images (bmp 32-bits with alpha channel information) by pre-multiplying the images and using CDC::AlphaBlend method.
I would like to introduce some new graphics using Direct2D but I don't want to migrate all the images to png or other formats.
I managed to draw a bmp image from a file but I'm facing problems to get the image from resources and also the displayed image does not use the alpha channel information.
So could anybody help me out with this?
This is my code to create the bitmap:
hr = pIWICFactory->CreateDecoderFromFilename( L"D:\\image.bmp",
NULL,
GENERIC_READ,
WICDecodeMetadataCacheOnDemand,
&pDecoder);
if (SUCCEEDED(hr))
{
// Create the initial frame.
hr = pDecoder->GetFrame(0, &pSource);
}
if (SUCCEEDED(hr))
{
//create a Direct2D bitmap from the WIC bitmap.
hr = pRenderTarget->CreateBitmapFromWicBitmap(
pSource,
NULL,
ppBitmap
);
}
This is the code to draw the bitmap:
m_pRenderTarget->DrawBitmap(
m_pBitmap,
D2D1::RectF(0.0f, 0.0f, size.width, size.height)
);
You'll need to make an IStream from the resource to pass to IWICImagingFactory::CreateDecoderFromStream.
Since resources are available in memory (assuming the module that contains them is loaded), the easiest way to do that is to create an IWICStream object using IWICImagingFactory::CreateStream and initialize it using IWICStream::InitializeFromMemory.
To get the size of the resource and a pointer to the first byte, use the FindResource, LoadResource, LockResource, and SizeofResource functions.
If your bitmap's header uses BI_BITFIELDS to specify a format with alpha data, I believe WIC will respect that. I don't have any experience with Direct2D, so I can't say if you need to do anything further to make it use alpha data.
If you can't use BI_BITFIELDS (or if that doesn't work), you can write your own IWICBitmapSource implementation that wraps the frame's IWICBitmapSource. You should be able to pass most calls directly to the frame source, and supply your own GetPixelFormat method that returns the real format of your image data. Alternatively, you can create an IWICBitmap with the format you want, lock the bitmap, and copy in the pixel data from the frame source.

Resources