2d drawing primitives and images: OpenGL, Cairo or Agg - performance

I'm making a game and thought about using vector shapes for the UI. I want to know what the best renderer for this. I think Agg is faster than Cairo, but Cairo can use hardware acceleration if it's available. And how about opengl? Is a good idea I use gl textures for images and lines to do rectangles, rounded rectagles and circles? Or is better I render cairo surfaces in opengl?
Other alternatives I found are Google Skia and SFML. What do you think of Skia? The SFML also draws polygons. I can make rounded shapes using polygons. I'm even thinking of using SDL or SFML to control events. In both I can create an OpenGL context and I've gotten used to their roles in control of events, which are crossplatforms.
I want a cross-platform solution. It should work in linux, mac and windows.

If you are already using OpenGL to render your game, the one thing you should not do is use a software renderer of any kind to draw the game's UI. So either use Cairo's OpenGL backend (which I understand is not particularly good) or do the rendering yourself in OpenGL. Otherwise, you'll kill your performance with all of the pixel transfers from CPU memory to the GPU.

Related

Draw bitmap using Metal

Question is quite simple. On Windows you have BitBlt() to draw to the screen. On OS X you could normally use OpenGl but how do I draw the bitmap to the screen using Apples new Metal framework? I cant find anything valuable in Apples Metal references.
I'm right now using Core Graphics for the drawing part but since my bitmap is updating all the time, I feel like I should move to Metal to reduce the overhead.
The very short answer is this: First, you need to setup a standard rendering pipeline using Metal, which is a bit of work, if you don't know anything about the rendering pipeline (note: but this can be simplified by avoiding 3D stuff by rendering a quad with two triangles, just give xy coordinates for the vertices). There is a lot of sample code from Apple that shows how to setup a standard rendering pipeline, the simplest is maybe MetalImageProcessing, so you could strip that down (even though that uses a compute shader which is overcomplicated to draw in, you'd want to substitute it for standard vertex and fragment shaders).
Second, you need to learn how vertex and fragment shaders work and how to draw stuff with them, see shadertoy for this.
Just noticed the users last comment, if you have an array of bytes that represent an image then you can just make an MTLTexture out of that and render it to the screen, see Apple's example above but change the compute shader to standard vertex and fragments shaders for faster performance.

Artificial intelligence functionality in three.js

Does Three.JS have a function or capability of AI( Artificial intelligence )? Specifically let's say a FPS game. I want enemies to look for me and try to kill me, is it possible in three.js? Do they have a functionality or a system of such?
Webgl
create buffer
bind buffer
allocate data
set up state
issue draw call
run GLSL shaders
three.js
create a 3d context using WebGL
create 3 dimensional objects
create a scene graph
create primitives like spheres, cubes, toruses
move objects around, rotate them scale them
test for intersections between rays, triangles, planes, spheres, etc.
create 'materials' (rather than shaders)
javascript
write algorithms
I want enemies to look for me and try to kill me
Yes, three.js is capable of doing this, you just have to write an algorithm using three's classes. Your enemies would be 3d objects, casting rays, intersecting with other objects, etc.
You would be building a game engine, and you could use three.js as your rendering framework within that engine. Rendering is just one part of it. Think of a 2d shooter, you could make it using a 2d context, but you could also enhance it and make it 2.5d, by working with a 3d context. Everything else can stay the same.
any webgl engine that might have it ? or is it just not a webgl thing
Unity probably has everything you can possibly think of. Unity is capable of outputting WebGL, so it could be considered a 'webgl engine'.
Bablyon.js is more engine like.
Three Js is the best and most powerfull WebGL 3d engine that has no equal on the market , and its missing out on such an ability
Three.js isn't exactly a 3d engine. Wikipedia says:
Three.js is a lightweight cross-browser JavaScript library/API used to
create and display animated 3D computer graphics on a Web browser.
Three.js uses WebGL.
so if i need to just draw a car, or a spinning logo, i don't need them to come looking for me, or try to shoot me. I just need them to stay in one place, and rotate.
For a graphics demo you don't even need this - with a few draw instructions, you could render a full screen quad with a very elaborate pixel shader. Three gives you a ton of options, especially if you consider all the featured examples.
It works both ways, while you can expand three.js anyway you want, you can strip it down for just a very specific purpose.
If you need to build an app that needs to do image processing, and feature no '3d' graphics, you could still leverage webgl with three.js.
You don't need any vector, matrix, ray , geometry classes.
If you don't have vector3, you probably cant keep planeGeometry, but you would use bufferGeometry, and manually construct a plane. No transformations need to happen, so no need for matrix classes. You'd use shaders, and textures, and perhaps something like the EffectsComposer.
I’m afraid not. Three.js is just a engine for displaying 3d content.
Using it to create games only is one possibility. However few websites raise with pre-coded stuff like AI (among other things) to attract game creators, but using them is more restrictive than writing the exact code you need
Three.js itself doesn't however https://mugen87.github.io/yuka/ is a great AI engine that can work in collaboration with three to create AI.
They do a line if sight and a shooting game logic, as well as car logic which I've been playing around with recently, a React Three Fiber example here: https://codesandbox.io/s/loving-tdd-u1fs9o

OpenCL - Draw image and zoom into it

I'm trying to draw an image directly to the screen. The is calculated by a kernel and will be stored in the GPU-memory, so it should not be copied back to the CPU-Memory. It should also be possible to dynamically zoom into the image while calculating more data at the same time.
Can anyone point me to a method that does this effectively? Everything I've found so far was incredibly slow (for example using OpenGL to draw a cube with the image as texture). What is the fastest way to achieve this? Like I said, while zooming into the image, it should be possible to calculate more data to refine the image dynamically. From what I've read, OpenGL might be the fastest way (performance-wise) but it seems incredibly slow to me to put the image on a cube and run a ton of shaders over it if it's already the way I want it.
Mathe172
Find one of the many OpenCL / OpenGL interop examples, they all show what you want. They create an OpenCL image made to interop with an OpenGL texture, and after rendering OpenCL data into the image they display it using OpenGL -- no copy back to CPU memory over the PCIe bus. The zooming into the image can be done just by scaling up the OpenGL quad that you draw the texture onto. If you are talking lots of scaling, then you will need a high-resolution texture. If lots and lots of zooming you should rethink the problem and compute just what you need for the current display (since with lots of zoom, plenty of data will be off-screen).

THREE.js + Mesh Modifiers

Coming from ActionScript + Papervision I am familiar with "AS3DMod" a library that modifies 3D meshes to bend, twist,etc. Now I am working with THREE.js and found this:"#MOD3 As3dMod 3D Modifier Library port for Javascript and Three.js, Pre3d and J3D and Copperlicht" but the code is outmoded for any remotely current versions of THREE.js (and also is written for canvas rendering, NOT webGL). The creator has not been able to update. Does anyone know of any similar 3D libs that can "bend ,twist, bloat" 3D meshes in webGL? I am not sufficiently skilled to update this myself.
Thanks for any suggestions.
Not that I know of, but depending on what modifiers you are utilize, it may not be too difficult to write a modifier yourself. Typically you can reuse the logic in js for Canvas renderer, and as for WebGLRenderer, you can implement it in the Vertex shaders.

LibGDX - Sprite to Pixmap

I am using LibGDX for a small app project, and I need to somehow take a series of sprites and place them (or their pixels rather) into a Pixmap. The basic idea is to take random sprites that are generated through various means while the app is running, and, only at specific times, merge some of them onto a single background sprite.
I believe that most of this can be done easily, but the step of getting the sprite images into the Pixmap isn't quite so obvious to me. The sprites also have various transparent and semi-transparent pixels, so simply grabbing the color at each pixel while it is all on the same screen isn't really applicable either, as it obviously shouldn't take the background colors with it.
If there is a suitable alternative to this that would accomplish what I am looking for I would also love to hear it. Any help is highly appreciated.
I think you want to render your sprites to an off-screen buffer (called an "FBO" or FrameBuffer in libgdx) (blending them as they're added), and then render that offscreen buffer to the screen as a single draw call? If so, this question should help: libgdx SpriteBatch render to texture
This requires OpenGL ES 2.0, which will eliminate support for some older devices.

Resources