DrawPrimitives

RaspberryDJ

Member
Joined
Jun 3, 2019
Any help on this one? There's no examples it seems.
I noticed there are commands especially meant for batching graphics called

DrawIndexedPrimitives and DrawPrimitives

There are examples how to draw solid coloured quads but how would you to draw multiple quads with graphics? :)
 

RaspberryDJ

Member
Joined
Jun 3, 2019
I want to know how to draw actually images using DrawIndexedPrimitives and DrawPrimitives
and also learn what makes each of these too special (what does index mean?)
 

Difference

New Member
Joined
Jun 21, 2017
But you don't load a png in that sourcecode? You load vector from txt tile, it's just coloured quads?
Actually it's polygons triangulated to allow concave shapes.
To draw images you need to set the texurecoords and provide a material. As I recall, there used to be a Monkey-x example, so if the achieves are alive somewhere, you could maybe find that.
 

RaspberryDJ

Member
Joined
Jun 3, 2019
Actually it's polygons triangulated to allow concave shapes.
To draw images you need to set the texurecoords and provide a material. As I recall, there used to be a Monkey-x example, so if the achieves are alive somewhere, you could maybe find that.
Sounds like that's what I want, I've searched for a few months now. I can't find any?
 

RaspberryDJ

Member
Joined
Jun 3, 2019
Okay I searching for the simplest so I think I'm looking for some code to draw any image as 10 separate quads in one command. It should be doing the same as 10 simple DrawImage instructions, let's say. That's what I could work with and begin to understand how it works.
 

RaspberryDJ

Member
Joined
Jun 3, 2019
What do you mean? Oh I explained poorly I'm so sorry.
I don't have anything. I begin on a blank space and want to teach myself the graphical commands that are called in the manual :

"DrawIndexedPrimitives" and "DrawPrimitives"

I dot not know how to use them.
 

RaspberryDJ

Member
Joined
Jun 3, 2019
AMOS2 has released maybe there's a helpful community there. Here you keep everything good for yourself apparently.
 

Difference

New Member
Joined
Jun 21, 2017
I don't quite get where you're coming from, I'm a very infrequent visitor to this site.
That said, I'd like to help if you post some code.
 

RaspberryDJ

Member
Joined
Jun 3, 2019
okay?
Well.. I just told you that I've tried to find information by myself for *MONTHS* without wanting to ask anthing. Becuase THIS HAPPENS. I thought.. maybe.. maybe I ask just once maybe I can ask somethings small which would help in big ways if I just get the right push.
 

Holzchopf

Moderator
3rd Party Module Dev
Joined
Jul 31, 2017
Location
Bern, Switzerland
It's easier to start with DrawPrimitives() - you feed it:
1. order - how many "vertices" shall be used per primitive
2. count - how many primitives shall be drawn
3. vertices - the x/y screen coordinates of your vertices provided as continuous float array, e.g. [x0,y0, x1,y1, x2,y2, x3,y,3]
4. texcoords - the s/t texture coordinates (sometimes also called u/v) of your vertices, also provided as continuous float array. Texture coordinates go from 0,0 (top left corner of your texture) to 1,1 (bottom right corner of your texture)
5. material - material, from which the texture and shader data is extracted. You can load an image file as Material (using Material.load()) or access an Image's material by the image.Material() property.

Something like this should work (untested, I don't have access to CX right now):

Cerberus X:
Local material:Material = Material.load("myfile.png", 0)

' where to draw
Local x1:Float = 32
Local x2:Float = x1 + 64
Local y1:Float = 32
Local y2:Float = y1 + 64
' provide four vertices from those coordinates as array
Local verts:Float[] = [x1,y1, x2,y1, x2,y2, x1,y2]
' piece of texture to draw
Local s1:Float = 0
Local s2:Float = 0.5
Local t1:Float = 0
Local t2:Float = 0.5
' provide texture coordinates for vertices
Local texcs:Float[] = [s1,t1, s2,t1, s2,t2, s1,t2]
' draw one quad (1 primitive consisting of 4 vertices)
mycanvas.DrawPrimitives(4, 1, verts, texcs, material)
Pretty straight forward. The twist with DrawIndexedPrimitives is that it allows you to reuse vertices. E.g: you can feed it 6 vertex coordinate pairs (in xy and st each) and tell it, with the indices, which pairs to use, like
verts:Float[] = [x1,y1, x2,y1, x3,y1, x1,y2, x2,y2, x3,y3]
idxs:Int[] = [0, 1, 4, 3]
(and order=4, count=1)
would draw a quad from x1,x1 - x2,y1 - x2,y2 - x1,y2 (you'll find these pairs at given indexes in verts[])
 

Paul59

Active Member
CX Code Contributor
Joined
Dec 13, 2018
Location
UK
What do you mean? Oh I explained poorly I'm so sorry.
I don't have anything. I begin on a blank space and want to teach myself the graphical commands that are called in the manual :

"DrawIndexedPrimitives" and "DrawPrimitives"

I dot not know how to use them.
DrawIndexedPrimitives() is used in the 'VectorMonkey' morphing example that ships with CX. I tidied up the original code but it's still not a great example and there are next to no comments.

A better, simple example like the code @Holzchopf posted would be a good inclusion to CX :D


 

RaspberryDJ

Member
Joined
Jun 3, 2019
It's easier to start with DrawPrimitives() - you feed it:
1. order - how many "vertices" shall be used per primitive
2. count - how many primitives shall be drawn
3. vertices - the x/y screen coordinates of your vertices provided as continuous float array, e.g. [x0,y0, x1,y1, x2,y2, x3,y,3]
4. texcoords - the s/t texture coordinates (sometimes also called u/v) of your vertices, also provided as continuous float array. Texture coordinates go from 0,0 (top left corner of your texture) to 1,1 (bottom right corner of your texture)
5. material - material, from which the texture and shader data is extracted. You can load an image file as Material (using Material.load()) or access an Image's material by the image.Material() property.

Something like this should work (untested, I don't have access to CX right now):

Cerberus X:
Local material:Material = Material.load("myfile.png", 0)

' where to draw
Local x1:Float = 32
Local x2:Float = x1 + 64
Local y1:Float = 32
Local y2:Float = y1 + 64
' provide four vertices from those coordinates as array
Local verts:Float[] = [x1,y1, x2,y1, x2,y2, x1,y2]
' piece of texture to draw
Local s1:Float = 0
Local s2:Float = 0.5
Local t1:Float = 0
Local t2:Float = 0.5
' provide texture coordinates for vertices
Local texcs:Float[] = [s1,t1, s2,t1, s2,t2, s1,t2]
' draw one quad (1 primitive consisting of 4 vertices)
mycanvas.DrawPrimitives(4, 1, verts, texcs, material)
Pretty straight forward. The twist with DrawIndexedPrimitives is that it allows you to reuse vertices. E.g: you can feed it 6 vertex coordinate pairs (in xy and st each) and tell it, with the indices, which pairs to use, like
verts:Float[] = [x1,y1, x2,y1, x3,y1, x1,y2, x2,y2, x3,y3]
idxs:Int[] = [0, 1, 4, 3]
(and order=4, count=1)
would draw a quad from x1,x1 - x2,y1 - x2,y2 - x1,y2 (you'll find these pairs at given indexes in verts[])
Thanks for the explanation, it was a really helpful!

But do you have to use floating point now to set coordinates you can't use pixels anymore?
I was thinking to try speed up a draw of 10.000 tiny tiles and see if they draw faster using these commands, rather than what I do now which is a simple loop of DRAWAIMAGE or DRAWRECT instructions.
 

Holzchopf

Moderator
3rd Party Module Dev
Joined
Jul 31, 2017
Location
Bern, Switzerland
I was thinking to try speed up a draw of 10.000 tiny tiles and see if they draw faster using these commands, rather than what I do now which is a simple loop of DRAWAIMAGE or DRAWRECT instructions.
Oh this will definitely speed things up. And yes, it is quite a bit of brain work to get this stuff working. Pen and paper will help you a lot (draw the vertices as points, enumerate them, draw the primitives you want them to build...). The screen coordinates are still pixel values, but given as floats. Internally, mojo2 uses floats anyway so there's no real difference to using ints on your side.
 
Top Bottom