Reputation: 3034
If you've experienced with Actionscript before you probably know how movie clips work, they have width,height,x,y values you can set on them and it's really simple to change them.
But now, I tried using libgdx (Java) & LibCinder (C++) and there's a lot into it, rendering is now done via OpenGL, and requires shaders etc', I managed to import a 3d mesh that I created with it's texture, but now things are a bit strange when it comes to changing the location / size of the mesh..
In both of the libraries I tried to use, there's no real way to change the size or position of the mesh as I thought.. (At first, I was expecting something like mesh.width/height, mesh.scale, mesh.x/y/z)
So my question is, what is the actual approach of setting those values?
Upvotes: 0
Views: 1917
Reputation: 13532
What you need to do is called 3d transformations
.
The way it is done in OpenGL or Direct3D is usually by multiplying your meshes' vertices with a transformation matrix.
The meshes are most of the time exported from a modelling program and they are in object space. To place these meshes around your world you need to bring them into world space by multiplying them with a world matrix. This world matrix can contrain a translation, rotation and scale all in one matrix. DirecXMath library gives you methods to create those matrices easily and there are free libraries for OpenGL as well.
Once you create the world matrix you pass that to your vertex shader and multiply each incoming vertex with the matrix to get the vertex in world space.
A basic vertex shader can look like this in HLSL :
cbuffer cbPerObject : register(b0)
{
float4x4 world;
float4x4 viewProj;
};
struct VS_INPUT
{
float3 position:SV_POSITION;
};
struct VS_OUTPUT
{
float4 position:SV_POSITION;
};
VS_OUTPUT main( VS_INPUT vsin )
{
VS_OUTPUT output;
float3 position = mul( float4(vsin.position,1.0), world).xyz;
output.position = mul(float4(position,1.0), viewProj);
return output;
}
So if you give each of your mesh instances a different world matrix they can all be placed at different locations and with different scales and rotations.
The viewProj
is the view and projection matrices combined, which are calculated from your camera and projection settings like view direction, position/rotation, FOV, near/far plane etc.
Upvotes: 3
Reputation: 340
Read into Model Matrix and View Matrix. You generate these using the objects coordinates and then pass the generated matrix to the shader which will move the mesh for you.
http://www.songho.ca/opengl/gl_transform.html
Upvotes: 1