Shader has strange input values - directx

my current issue is the following: I try to create a SpriteBatch with integrated Primtive-Rendering (Sprite and PrimtiveBatch in one).
Currently Visual Studio 11's Debugger shows that there is a line rendered. The IA-Stage is correct, so the VertexShader-Stage is. But i don´t get any output.
I run over it with the debugger and found out, that the shader has abnormal huge values.
I´m passing two vertices: 20,20 (Position) and 80, 80 (Position) both colored black.
The ConstantBuffer has three matrices: World (for manual object transformation), View and Projection.
In code, they´re all correct:
M11 = 0.00215749722, and so on.
In the shader, it looks like this:
Position = x = 200000.000000000, y = 200000.000000000, z = 0.000000000, w = 0.000000000
(#1 vertex's position)
Projection[0] = x = 22.000000000, y = 0.000000000, z = 0.000000000, w = 0.000000000
Projection[1] x = 0.000000000, y = -45.000000000, z = 0.000000000, w = 0.000000000
Projection[2] x = 0.000000000, y = 0.000000000, z = 10000.000000000, w = 0.00000 0000
Projection[3] x = 0.000000000, y = 0.000000000, z = 0.000000000, w = 10000.000000000
And finally the output result (position) is 0 for XYZW.
Before it was 413,-918 or something like that. I don´t know why, but now the result is always 0...
This is very strange, and i´m totaly stuck :-/
Thanks
R

Related

How to get correct position of West and East in 3D/AR view?

I was following this tutorial and when I tried to use it for AR compass, I got wrong positions for East and West (I get West on left side (270°)).
Problematic part:
fun Place.getPositionVector(azimuth: Float, latLng: LatLng): Vector3 {
val placeLatLng = this.geometry.location.latLng
val heading = latLng.sphericalHeading(placeLatLng)
val r = -2f
val x = r * sin(azimuth + heading).toFloat()
val y = 1f
val z = r * cos(azimuth + heading).toFloat()
return Vector3(x, y, z)
}
I see that: (-1) * sin(270) = 1 makes wrong result but I don't know how to fix it.
I return negative -x value => Vector3(-x, y, z), seems like it fixes my issue.

Creating custom geometry in Scenekit

I cannot get my custom geometry to show up. When I debug it, all the variables seem to be holding the correct data, but nothing renders. I just now set it to try and render a single square composed of two triangles for a very simple test, but still no luck. I feel like I'm building the SCNGeometry wrong. After doing all my logic/loops/magic, I have the following:
var verts = SCNGeometrySource(vertices: meshVertices)
var norms = SCNGeometrySource(normals: normals)
var element = SCNGeometryElement(indices: triangleIndices, primitiveType: .triangles)
I've tried both of the following to create the SCNGeometry. Neither render anything, though showStatistics does indicate there are two triangles. Strangely when I touch the screen to try and move the camera, the triangle total jumps to 2.74K...
let newGeo = SCNGeometry(sources: [verts, norms], elements: [element])
-
var sources = [SCNGeometrySource]()
sources.append(verts)
sources.append(norms)
let newGeo = SCNGeometrySource(sources: sources, elements: [element])
I'm really not sure what to try next unless I've completely misunderstood something.
Just to confirm, this is the data I have in my variables:
meshVertices = ([SCNVector3])
[0]: x = (Float) 0, y = (Float) 0, z = (Float) 0
[1]: x = (Float) 1, y = (Float) 0, z = (Float) 0
[2]: x = (Float) 0, y = (Float) 1, z = (Float) 0
[3]: x = (Float) 1, y = (Float) 1, z = (Float) 0
normals = ([SCNVector3])
[0]: x = (Float) 0, y = (Float) 0, z = (Float) 1
[1]: x = (Float) 0, y = (Float) 0, z = (Float) 1
[2]: x = (Float) 0, y = (Float) 0, z = (Float) 1
[3]: x = (Float) 0, y = (Float) 0, z = (Float) 1
triangleIndices = ([Int])
[0]: 3
[1]: 2
[2]: 0
[3]: 3
[4]: 0
[5]: 1
I have no problem getting SCNPlane, SCNBox, or other stuff to render, so I don't think the problem is there. The problem seems to be in how I'm creating the geometry. I'm pretty sure my vertices, normals, and indices are correct, so I must be doing something wrong when trying to create the final SCNGeometry object composed of SCNGeometrySource and SCNGeometryElement.
Though it may be a little bit too late. Hope this answer can help others, too.
When specifying indices, please do not use Int type. It seems that when using SceneKit (or maybe its is something even deeper like Metal/OpenGL? I am not sure), the Int type does not take into count. Please use UInt32 or UInt8 as indices here.

Intrinsic parameters OpenCV explanation

I measured intrinsic camera parameters with the OpenCv algorithm. The imager has an resolution of (752,480)
The values are :
Focal Length X 1021.477666;
Focal Length Y 1021.146888
Principal Point X 376.2966754
Principal Point Y 253.7513058
K1 -0.263658008
K2 -0.206264024
P1 -0.001102641
P2 -0.000980475
Error 0.122991565
Now I want to calculate the distorsion of one pixel.
The coordinates of the "old" pixel are
(x = 680,y = 390).
I´m using the following formula to calculate the coordinates of the corrected pixel
x_{corrected} = x( 1 + k_1 r^2 + k_2 r^4)
y_{corrected} = y( 1 + k_1 r^2 + k_2 r^4)
For r I´m using the distance
r = sqrt((x_shifted^2 - x_principalPoint^2)+(y_shifted^2 - y_principalPoint^2))
The calculation I did :
x_corrected = 680 * ( 1 + (-0.26365 * 333^2) + (-0.2062 + 333^2 ) )
= 55524658.09
This value is incorrect I think.
Does anyone have an idea what I´m doing wrong?
You need to express your undistorted points in normalised coordinates. So you need to convert it first:
x_norm = (x-px)/px = 0.8071
and
y_norm = (y-py)/py = 0.5369
Then calculate the radius:
r = sqrt(x_norm^2+y_norm^2)
With these values you get your corrrected normalizes coordinates :
x_corr_norm = x_norm*(1+k1*r^2+k2*r^4) = 0.4601
y_corr_norm = y_norm*(1+k1*r^2+k2*r^4) = 0.3061
and you have to convert them back again:
x_corr = (x_corr_norm+1)*px = 549.4409
y_corr = (y_corr_norm+1)*py = 331.4280
For a more accurate result, you should consider the non-radial distortion too:

Formula for CGPointApplyAffineTransform function in iOS

I am trying to affine rotate matrix in Android based on iOS Code
iOS has two functions CGAffineTransformMakeRotation and CGPointApplyAffineTransform used for calculation
Step 1: CGAffineTransformMakeRotation();
Input:
2.2860321998596191
Result:
a = -0.65579550461444569,
b = 0.75493857771840255,
c = -0.75493857771840255,
d = -0.65579550461444569,
tx = 0, ty = 0
Formula:
double A = Math.cos(RadiansRotated);
double B = -Math.sin(RadiansRotated);
double C = Math.sin(RadiansRotated);
double D = Math.cos(RadiansRotated);
I am able to calculate a,b,c,d for step 1 using above formula
Step 2: CGPointApplyAffineTransform()
Input :
x = 612.55191924649432,
y = -391.95960729287646
And Matrix return from Step 1
Result:
x = -105.80336653205421,
y = 719.48442314773808
Does anyone know the formula used in ApplyAffineTransform?
I need help for Step 2
I have tried with Android's Matrix class - Not Working
I have also tried with Java's AffineTransform - Not working
The math behind the CGAffineTransform functions is described in “The Math Behind the Matrices” in the Quartz 2D Programming Guide.
The formulae for transforming a point using an affine transform are given as:
x' = ax + cy + tx
y' = bx + dy + ty
Incidentally, in your step 1, you have reversed the signs of b and c, which has the effect of reversing the direction of rotation.

Drawing a circle with an evenly-distributed set-amount of points

I was wondering how you would go about this assuming you were working with a 2D coordinate frame in pixels. I created some examples of what I mean:
Red dot represents the origin point
Grey circle shows the radius but wouldn't actually be drawn
Green dots have a set amount and get evenly distributed along the
circle
With 3 dots:
http://prntscr.com/5vbj86
With 8 dots:
http://prntscr.com/5vbobd
Spektre answered my question but in C++, here it is in lua for anyone interested:
local x,y
local n = 10
local r = 100.0
local x0 = 250.0
local y0 = 250.0
local da = 2.0 * math.pi/n
local a = 0.0
for i = 0, n - 1 do
x = x0 + r * math.cos(a)
y = y0 + r * math.sin(a)
-- draw here using x,y
a = a + da
end
on circle very easy
for evenly distributed points the angle is increasing with the same step
so for N points the step is da=2.0*M_PI/N;
The code in C++ is like this:
int i,n=10;
double x,y,a,da;
double r=100.0,x0=250.0,y0=250.0; // circle definition
da=2.0*M_PI/double(n);
for (a=0.0,i=0;i<n;i++,a+=da)
{
x=x0+r*cos(a);
y=y0+r*sin(a);
// here draw or do something with (x,y) point
}

Resources