Muestra las diferencias entre dos versiones de la página.
| Ambos lados, revisión anterior Revisión previa Próxima revisión | Revisión previa | ||
|
wiki2:oglmaths [2015/11/22 16:22] alfred [Esféricas] |
wiki2:oglmaths [2020/05/09 09:25] (actual) |
||
|---|---|---|---|
| Línea 8: | Línea 8: | ||
| * {{:wiki2:ogl:gimbal_3_axes_rotation.gif?linkonly|Gimbal with 3 axes of rotation}}. A set of three gimbals mounted together to allow three degrees of freedom: roll, pitch and yaw. When two gimbals rotate around the same axis, the system loses one degree of freedom. | * {{:wiki2:ogl:gimbal_3_axes_rotation.gif?linkonly|Gimbal with 3 axes of rotation}}. A set of three gimbals mounted together to allow three degrees of freedom: roll, pitch and yaw. When two gimbals rotate around the same axis, the system loses one degree of freedom. | ||
| - | ===== Matrices de transformación ===== | + | ===== Matrices ===== |
| + | ==== Coordenadas homogeneas ==== | ||
| + | Partiendo del concepto de coordenadas homogeneas (x, y, z, w). Donde w... | ||
| + | * ... Vale 1 si es una posición en el espaco. | ||
| + | * ... Vale 0 si es una dirección. | ||
| + | Las coordenadas homogeneas son las que nos permiten trabajar con matrices de traslación, rotación y escalado. | ||
| + | |||
| + | La w es el valor de perspectiva, se utilizará para divir X, Y, y Z entre este. The Advantages of Dividing by W You might be wondering why we don’t simply divide by z instead. After all, if we interpret z as the distance and had two coordinates, (1, 1, 1) and (1, 1, 2) , we could then divide by z to get two normalized coordinates of (1, 1) and (0.5, 0.5). While this can work, there are additional advantages to adding w as a fourth component. We can decouple the perspective effect from the actual z coordinate, so we can switch between an orthographic and a perspective projection. There’s also a benefit to preserving the z component as a depth buffer. | ||
| + | |||
| + | ==== Orden de operaciones ==== | ||
| + | |||
| + | Recuerda, es importante el orden de operaciones. Al multiplicar una matriz por un vector el primer operando siempre será el vector: | ||
| + | <code> | ||
| + | transformedVector = myMatrix * myVector; | ||
| + | </code> | ||
| + | |||
| + | El orden para acumular (multiplicar) matrices de transformación: | ||
| + | <code> | ||
| + | TransformedVector = TranslationMatrix * RotationMatrix * ScaleMatrix * OriginalVector; | ||
| + | </code> | ||
| + | ==== Tipos de matrices ==== | ||
| + | |||
| + | === Matriz identidad (identity matrix) === | ||
| + | {{:wiki2:ogl:identityexample.png?nolink|}} | ||
| + | Cualquier vector que multipliquemos por ella queda igual. | ||
| + | |||
| + | The reason this is called an identity matrix is because we can multiply this matrix with any vector and we’ll always get back the same vector, just like we get back the same number if we multiply any number by 1. | ||
| + | <code cpp> | ||
| + | glm::mat4 myIdentityMatrix = glm::mat4(1.0f); | ||
| + | </code> | ||
| + | |||
| + | === Matriz de traslación === | ||
| + | {{:wiki2:ogl:translationmatrix.png?nolink|}} \\ | ||
| + | {{:wiki2:ogl:translationexampleposition1.png?nolink|}} | ||
| + | === Matriz de escalado === | ||
| + | {{:wiki2:ogl:scalingmatrix.png?nolink|}} \\ | ||
| + | {{:wiki2:ogl:scalingexample.png?nolink|}} | ||
| + | |||
| + | === Matrices de rotación === | ||
| + | |||
| + | === Model matrix === | ||
| + | |||
| + | Es la matriz que surge de aplicar todas las transformaciones. Al multiplicarla a un punto este pasa a tener las coordenadas del mundo. | ||
| + | |||
| + | The model matrix transforms a position in a model to the position in the world. This position is affected by the position, scale and rotation of the model that is being drawn. It is generally a combination of the simple transformations you've seen before. If you are already specifying your vertices in world coordinates (common when drawing a simple test scene), then this matrix can simply be set to the identity matrix. | ||
| + | |||
| + | === View matrix === | ||
| + | |||
| + | Es la que mueve el mundo para colocarlo en la posición de la cámara (porque en ogl no es la cámara la que se mueve sino el mundo). Una vez es multiplicada a un punto, este pasa a tener las coordenadas de cámara. | ||
| + | |||
| + | So initially your camera is at the origin of the World Space. In order to move the world, you simply introduce another matrix. Let’s say you want to move your camera of 3 units to the right (+X). This is equivalent to moving your whole world (meshes included) 3 units to the LEFT ! (-X). | ||
| + | <code cpp> | ||
| + | glm::mat4 CameraMatrix = glm::lookAt( | ||
| + | cameraPosition, // the position of your camera, in world space | ||
| + | cameraTarget, // where you want to look at, in world space | ||
| + | upVector // probably glm::vec3(0,1,0), but (0,-1,0) would make you looking upside-down, which can be great too | ||
| + | ); | ||
| + | </code> | ||
| + | |||
| + | === Projection matrix === | ||
| + | |||
| + | Es la matriz que deforma los puntos para colocarlos en la proyección deseada. | ||
| + | |||
| + | <code cpp> | ||
| + | glm::mat4 projectionMatrix = glm::perspective( | ||
| + | FoV, // The horizontal Field of View, in degrees : the amount of "zoom". Think "camera lens". Usually between 90° (extra wide) and 30° (quite zoomed in) | ||
| + | 4.0f / 3.0f, // Aspect Ratio. Depends on the size of your window. Notice that 4/3 == 800/600 == 1280/960, sounds familiar ? | ||
| + | 0.1f, // Near clipping plane. Keep as big as possible, or you'll get precision issues. | ||
| + | 100.0f // Far clipping plane. Keep as little as possible. | ||
| + | 7 ); | ||
| + | </code> | ||
| + | |||
| + | === ModelViewProjection matrix === | ||
| + | {{:wiki2:ogl:mvp.png?nolink|}} | ||
| + | |||
| + | Cumulating transformations appears the ModelViewProjection matrix. | ||
| + | |||
| + | <code> | ||
| + | // C++ : compute the matrix | ||
| + | glm::mat4 MVPmatrix = projection * view * model; // Remember : inverted ! | ||
| + | // GLSL : apply it | ||
| + | transformed_vertex = MVP * in_vertex; | ||
| + | </code> | ||
| ===== Coordenadas ===== | ===== Coordenadas ===== | ||
| ==== Cartesianas ==== | ==== Cartesianas ==== | ||
| - | (x, y, z) | + | En el que las coordenadas se plasman en un plano (cartesiano) a partir de los valores X, Y. |
| + | ==== Polares ==== | ||
| + | Las coordenadas polares o sistemas polares son un sistema de coordenadas bidimensional en el cual cada punto del plano se determina por una distancia y un ángulo, ampliamente utilizados en física y trigonometría. | ||
| ==== Esféricas ==== | ==== Esféricas ==== | ||
| - | (r, phi, theta) | + | El sistema de coordenadas esféricas se basa en la misma idea que las coordenadas polares y se utiliza para determinar la posición espacial de un punto mediante una distancia y dos ángulos. |
| + | En consecuencia, un punto P queda representado por un conjunto de tres magnitudes: el radio r, el ángulo polar o colatitud φ y el azimut θ (r, phi, theta). | ||
| ===== Figuras geométricas ===== | ===== Figuras geométricas ===== | ||
| Línea 86: | Línea 171: | ||
| } | } | ||
| </code> | </code> | ||
| + | |||
| + | ===== Billboarding ===== | ||
| + | |||