Cube rotation about its edge rather than axis - wpf

I am quite new to using Visual Studio and WPF. I have always used Arduino and Processing together but I am now trying to replace Processing with WPF, since it looks more professional. The problem i have in WPF is that i have drawn a cube and i need to rotate it about 2 of its edges rather than the x- and y-axes.
I think that a possible solution would be that when i use RotateTransform3D.Rotation and AxisAngleRotation3D, i can enter sine and cosine values of angles of rotation in the Axis="" part of the code but i don't know how to get the values of rotation from previous rotations. Can anyone help?

Related

How to replace DrawClosedCurve / FillClosedCurve when moving from WinForms (GDI+) to WPF?

I've got an array of points (X,Y) which constitute a convex hull (a simple, irregularly shaped contour). Rather than rendering a polygon with straight edges, I want to render them with an approximate "smoothly curved" contour that passes through all of these points.
In WinForms/GDI+, this could be accomplished with the Graphics.DrawClosedCurve or Graphics.FillClosedCurve methods. There does not appear to be an equivalent in WPF.
I've looked into drawing using things like Path and BezierSegment, but I'm not sure if (and how) these can be used to generate a continuous closed curve, given a set of points. It appears that to do this, I'd perhaps have to generate a set of control points based on my contour as an intermediate step?
I have tried using the GDI+ methods to render onto a System.Drawing.Bitmap and then displaying that in the WPF application. This works, but the performance (particularly the conversion from System.Drawing.Bitmap to BitmapSource) is poor and not sufficient for the application. This is why I'm looking for a pure WPF solution.
Has anyone been able to draw a closed curve based off a set of points in WPF?
Unfortunately, there is no single-method equivalent to DrawClosedCurve in WPF, even though it's been requested. So you are left with at least two options:
Host a native window within your WPF window and perform all your drawing on it.
Implement your own cardinal spline drawing. You are on the right track with Bezier segments. However, there are existing implementations of it you can look at out there.

Using Matrix Multiplication to Rotate a cube in opengl

I have some code that draws a cube. I've managed to rotate the cube using glRotatef but I need to be able to rotate the cube using only matrix multiplication. I know what matrices to use but I'm not sure how to apply them to the vertices of the cube. Is there a way to pass a vector to glVertex3f? Or a way to pass an array of vertices?
I agree completely with Drew's answer. However, I figured I'd point you in the direction of a nicely-written tutorial series that shows these concepts in action:
http://en.wikibooks.org/wiki/OpenGL_Programming
Start with the first 3 tutorials to learn shader basics, and then the 4th and 5th tutorials dive into rotation with triangles and cubes. Also, the examples use FreeGLUT, which is a framework that allows you to easily interact with your code using key presses, mouse movements, mouse presses, and more.
The OpenGL functions you're using are for immediate-mode rendering, and the vertex transformations are done within OpenGL. If you want to manually transform the vertices outside of OpenGL, you'll have to either implement matrix * vector multiplication, or use a library that does this for you.
As a side note, if you're just interested in rotation, you may find Rodrigues' rotation formula easier to implement than general matrix transformation.

How to add point on Cubric Bezier Curve?

In photoshop we can create bezier curve with pen tool,when we editing the completed curve,we can add point on the curve, in blend the same.
Now I have create a bezier path with wpf,how can I add point on it ,the special problem is I can't compute the control point of the new point.
Please help me, thank you.
what photoshop does is not "adding a control point", because that would raise the curve's order and change the curvature; instead, it splits the cubic curve into two cubic curves C1 and C2, with the endpoint of C1 and the startpoint of C2 are the same coordinate.
If WPF doesn't have curve splitting built into the API, then you might need to implement curve splitting yourself. It's pretty straight forward; for the DIY explanation, see http://pomax.github.io/bezierinfo/#splitting
The only challenge is to find the "t" value for the coordinate where you clicked, the simplest solution for that being to just generate the curve from t=0:1 in 1/100 or 1/1000 steps, and recording the x/y coordinate at each t value. That gives you a lookup table for instant lookup when you need to split the curve.

OpenGL total beginner and 2D animation project?

I have installed GLUT and Visual Studio 2010 and found some tutorials on OpenGL basics (www.opengl-tutorial.org) and 2D graphics programming. I have advanced knowledge in C but no expirience with graphics programming...
For project (astronomy - time scales) , i must create one object in center of window and make other 5 objects (circles,dots...) to rotate around centered object with respect to some equations (i can implement them and solve). Equations is for calculating coordinates of that 5 objects and all of equations have parameter t (as time). For creating animation i will vary parameter t from 0 to 2pi with some step and get coordinates in different moments. If task was to print new coordinates of objects it would be easy to me but problem is how to make animation of graphics. Can i use some functions of OpenGL for rotation/translation ? How to make an object to move to desired location with coordinates determined by equation? Or i can redraw object in new coordinates every millisecond? First thing i thought was to draw all objects, calculate new coordinates, clear screen and draw all objects in new coordinates and repeat that infinitely..(it would be primitive but will work?)
Here is screen shot of that objects - http://i.snag.gy/ht7tG.jpg . My question is how to make animation by calculating new coordinates of objects each step and moving them to new location. Can i do that with basics in OpenGL and good knowledge of C and geometry? Any ideas from what to start? Thanks
Or i can redraw object in new coordinates every millisecond? First
thing i thought was to draw all objects, calculate new coordinates,
clear screen and draw all objects in new coordinates and repeat that
infinitely..
This is indeed the way to go. I would further suggest that you don't bother with shaders and vertex buffers as is the OpenGL 3/4 way. What would be easiest is called "immediate mode", deprecated by OpenGL 3/4 but available in 1/2/3. It's easy:
glPushMatrix(); //save modelview matrix
glTranslatef(obj->x, obj->y, obj->z); //move origin to object center
glBegin(GL_TRIANGLES); //start drawing triangles
glColor3f(1.0f, 0.0f, 0.0f); //a nice red one
glVertex3f(0.0, +0.6f, 0.0f);
glVertex3f(-0.4f, 0.0f, 0.0f);
glVertex3f(+0.4f, 0.0f, 0.0f); //almost equilateral
glEnd();
glPopMatrix(); //restore modelview matrix/origin
Do look into helper libraries glu (useful for setting up the camera / the projection matrix) and glut (should make it very easy to set up a window and basic controls and drawing).
It would probably take you longer to set it up (display a rotating triangle) than to figure out how to use it. In fact, here's some code to help you get started. Your first challenge could be to set up a 2D orthogonal projection matrix that projects along the Z-axis, so you can use the 2D functions (glVertex2).
First thing i thought was to draw all objects, calculate new coordinates, clear screen and draw all objects in new coordinates and repeat that infinitely..(it would be primitive but will work?)
That's exactly how it works. With GLUT, you set a display function that gets called when GLUT thinks it's time to draw a new frame. In this function, clear the screen, draw the objects and flush it to the screen. Then just instruct GLUT to draw another frame, and you're animating!
Might want to keep track of the time inbetween frames so you can animate things smoothly, but I'm sure you can figure that part out.
OpenGL is really just a drawing library. It doesn't do animation, that's up to you to implement. Clear/draw/flush is the commonly used approach for it though.
Note: with 'flush' I mean glFlush(), although GLUT in multi-buffer mode requires glutSwapBuffers()
The red book explains the proper way to draw models that can first be translated, rotated, scaled and so on: http://www.glprogramming.com/red/chapter03.html
Basically, you load the identity, perform transforms/rotations/scales (which one you want first matters - again the book explains it), draw the model as though it was at the origin at normal scale and it'll be placed in its new position. Then you can load identity and proceed with the next one. Every frame of an animation, you glClear() and recalculate/redraw everything. (It sounds expensive, but there's usually not much you can cache between draws).

Find the orientation of an image

I'm doing pattern matching with OpenCv. I have a model and I compare targets with the function cvMatchShapes.
It works but I want to know the orientation of the target. How can I do it?
Are bounding rotated rectangle suited for case when contour orientation differs 180 degrees for example?
Another way for solving your problem is to calculate contours moments (I suppose you are ussing contours in cvMatchShapes, you can compute image moments in similar way too) OpenCV Contours Moments?, then calculating principal axes angle from formula:
atan2((float)(-2)*Ixy,Ix - Iy)/2
This angle says about rotation. More theory about this issue: http://farside.ph.utexas.edu/teaching/336k/newton/node67.html

Resources