I'm trying to figure out how to draw shapes in UrhoSharp and I'm not getting anywhere with it. I thought it would be pretty straightforward. Searching the internet is not giving me much to go on.
All I want to do is give points so that I can draw lines between the points. 2-dimensional lines. So I can then draw shapes like a rectangle or triangle. I've looked at SkiaSharp, and drawing shapes is pretty straightforward, but for some reason, this concept seems foreign to UrhoSharp, at least from what I've looked at.
I've found a couple of code fragments where people are asking questions but I have no idea what else is going on aside from the bits that they show. If someone can point me to a reference that clearly explains how to do this, or show me here, I would really appreciate it.
Here is your example:
CustomGeometry geom = node.CreateComponent<CustomGeometry>();
geom.BeginGeometry(0, PrimitiveType.LineList);
var material = new Material();
material.SetTechnique(0, CoreAssets.Techniques.NoTextureUnlitVCol, 1, 1);
geom.SetMaterial(material);
float size = 1;
//x
geom.DefineVertex(Vector3.Zero);
geom.DefineColor(Color.Red);
geom.DefineVertex(Vector3.UnitX * size);
geom.DefineColor(Color.Red);
//y
geom.DefineVertex(Vector3.Zero);
geom.DefineColor(Color.Green);
geom.DefineVertex(Vector3.UnitY * size);
geom.DefineColor(Color.Green);
//z
geom.DefineVertex(Vector3.Zero);
geom.DefineColor(Color.Blue);
geom.DefineVertex(Vector3.UnitZ * size);
geom.DefineColor(Color.Blue);
geom.Commit();
Refer here for any future examples.
Related
Been continuing work on my HBAO shader following the resources in the HBAO paper and presentation slides. The algorithm I have implemented produces a near-convincing result however I am running into an issue around objects where a dark halo is appearing and I am not able to figure out what might be causing this. The paper mentions to snap UV to nearest Texel centre which I have yet to implement (unsure how to do this) but I am not sure if this is the reason for this issue. Would appreciate any insight into where I might be going wrong with this.
My shader can be found here: https://pastebin.com/XqaPKDcp
I think the issue might persist when I begin to sample
for(int j = 0; j < NUMBER_OF_STEPS; j++){
// step forward in the sampling direction
vec2 stepForward = (Rand.z + float(j+1)) * STEP * samplingDirection;
// use the stepforward position as an offset from the current fragment position in order to move to that location
vec2 stepPosition = uvCoords + stepForward;
}
}
Blur has been turned off for the following images: issue occurs with blur on as well
Close-up of halo occuring
Result from paper
I'm having a problem that I need to make the words I took from an external file "NOT" overlap each other. I have over 50 words that have random text sizes and places when you run it but they overlap.
How can I make them "NOT" overlap each other? the result would probably look like a word cloud.
if you think my codes would help here they are
String [] words;
int index = 0;
void setup ()
{
size (500,500);
background (255);
String [] lines = loadStrings ("alice_just_text.txt");
String entireplay = join(lines, " "); //splits it by line
words = splitTokens (entireplay, ",.?!:-;:()03 "); //splits it by word
for (int i = 0; i < 50; i++) {
float x = random(width);
float y = random(height);
int index = int(random(words.length));
textSize (random(60)); //random font size
fill (0);
textAlign (CENTER);
text (words[index], x, y, width/2, height/2);
println(words[index]);
index++ ;
}
}
Stack Overflow isn't really designed for general "how do I do this" type questions. You'll have much better luck if you post a more specific "I tried X, expected Y, but got Z instead" type question. But I'll try to help in a general sense:
You need to break your problem down into smaller pieces and then take on those pieces one at a time.
For example, you can isolate your problem to making sure rectangles don't overlap, which you can break down even further. There are a number of ways to do that:
You could use a grid to lay out your rectangles. Figure out how many squares a line of text takes up, then find a place in your grid where that word will fit. You could use something like a 2D array of boolean values, for example.
Or you could generate a random location, and then check whether there's already a rectangle there. If so, pick a new random location until you find a clear spot.
In any case, you'll probably need to use collision detection (either point-rectangle or rectangle-rectangle) to determine whether your rectangles are overlapping.
Start small. Create a small example program that just shows two rectangles on the screen. Hardcode their positions at first, but make it so they turn red if they're colliding. Work your way up from there. Make it so you can add rectangles using the mouse, but only let the user add them if there is no overlap. Then add the random location choosing. If you get stuck on a specific step, then post a MCVE and we'll go from there. Good luck.
I'm working on a project where I create mini galaxies using ellipses, rotate, radians, etc. on mouseX and mouseY. However, I'd love it if each mini galaxy could rotate left or right, so that it looks like a galaxy turning slowly in space. Not sure how I'd do this, though, and would love some guidance. Do I have to create an array that holds the ellipses for each galaxy, and then somehow rotate that? Can I simply set a rotate() for each individual ellipse as it draws to the screen? Thanks for any help!
var bgimg;
function preload(){
//for (var i = 0; i < planetArray.length; i++) {
bgimg = loadImage('Assets/galaxy_background.jpg');
}
function setup(){
createCanvas(1301, 822);
background(bgimg, 100);
//background(25,25,22);
}
function draw() {
//background(0);
fill(255);
noStroke();
textSize(19);
text("Create mini-galaxies by holding your mouse in a location. Move to create another.", 20, 40);
star()
//function mousepressed(){
}
function star(){
//angle = map(mouseX, 0,width, 0,360);
//rotate(radians(angle*100));
noStroke();
//translate(width/2, height/2);
translate(mouseX,mouseY);
fill(0);
rotate(radians(frameCount%360)); //rotates output of ellipses
rotate(radians(1000*frameCount%360));
for(var i =0; i < 20; i++){
push();
noStroke();
tint(255, 127);
fill(random(230),5,random(210),random(230));
// fill(random(125),random(250),random(100));
ellipse(10*frameCount % (width/20),0,5,5);
rotate(radians(mouseX, mouseY));
//image(stars, 10*frameCount % (width/2),0,10,10)
//image((10*frameCount % (width/2),0,10,10)
//
pop();
}
}
You'll have better luck with your questions if you try to narrow them down to an MCVE instead of posting your full sketch. It's hard to answer general "how do I do this" type questions. It's much easier to answer specific "I tried X, expected Y, but got Z instead" type question. That being said, I'll try to answer in a general sense:
You're having trouble because of the fact that you're letting your drawing accumulate by only calling the background() function once instead of clearing it every frame. There's nothing wrong with this, but it does make it impossible to apply transforms and rotations to stuff you've already drawn.
Like I said in your other question, most Processing sketches do this:
Store everything you need to draw in a data structure.
You might store an array of PVectors. Or you might create a Galaxy class that contains variables and functions that allow it to draw itself, which you call from your draw() function. The data structure you use is entirely up to you.
This page and this page contain discussions on creating objects in p5.js, or you might just try a google search. Here is an example that uses a class that knows how to draw itself, and then creates an instance of that class to create a sketch.
Clear previous frames every time draw() is called.
Most sketches call the background() function every frame. That might seem annoying because then you have to redraw everything, but that's what the data structures are for.
Redraw everything you want to be drawn every frame.
Iterate over those data structures and redraw your scene. This might be as simple as iterating over an array of PVectors, or maybe you'll want to create objects that know how to draw themselves.
Like I said, this is very general, and exactly what you do depends on how you think about all of the above. There isn't a single best way to do this, so it's hard to be more specific.
I am currently working on a project called "Raytracer" in c.
I encounter a problem, the spheres are oval when they are not centered.
Here is an excerpt of my code:
int i;
int j;
t_ray vect;
i = -1;
vect.x = 100. - cam.x;
while (++i < screenx)
{
j = -1;
vect.y = ((screenx / 2.) - i - cam.y) * -1.;
while (++j < screeny)
{
vect.z = (screeny / 2.) - j - cam.z;
}
}
This is likely not a bug, but simply a reality of how perspective projections work. When the camera is directly looking at a sphere, the projection is circular, but as it moves away from the center, it distorts. For more info read this link in the POV-Ray wiki: http://wiki.povray.org/content/Knowledgebase:Misconceptions#Topic_3
In that way the vector has different length on different pixels. You should normalize the vector at the end (dividing the components by the vector length)
It's probably late now, but to give you an answer, your "problem" is in reality called "fish-eye". I encounted this problem too. there're many ways to avoid this problem. The easiest is to increase the distance between the camera and your scene. It's not the cleaner way.
You also can normalize your rays, here are some reasons :
.keep the same distance ratio for every rays
.keep the same angle difference between every ray and its neighbors
.it makes many intersection computations ways easier
If you would look at this diagram link text, I need to find angle A by only knowing the length of all sides of a right triangle.
I don't know trig and need some help.
There are actually 2 questions in your post.
How to make a sprite point at the mouse. XNA C#:
You will have to calculate the direction between the position of the sprite and the position of the mouse.
This can be done using trigonometry functions. In this case: Arctangens2
So let's use the math library:
MouseState mouseState = Mouse.GetState();
Math.Atan2((double)mouseState.Y - sprite.Y, (double)mouseState.X - sprite.X); //this will return the angle(in radians) from sprite to mouse.
In your trigonometry example you will see that those values actually are:
Math.Atan2(BC, AC);
or
Math.Atan2(Ydiff, Xdiff);
I hope this helps =D
Cheers,
TomHashNL
I found my final solution to be:
Vector2 direction = targetPosition - currentPosition;
direction.Normalize();
float rotationInRadians = (float)Math.Atan2((double)direction.Y,
(double)direction.X) + MathHelper.PiOver2;
rotationInRadians is a raw value that can be passed to the sprite batch for the correct rotation amount--no further code is needed. Also, you may notice incorrect results if you're rotating the sprite on a corner rather than the middle.