Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You should use a math library, I recommend GLM. It has its matrix functions just like in OpenGL, and uses column major matrixes so you can calculate your owns, and apply them for objects.</p> <p>First, you should have a matrix class for your scene, which calculates your view matrix, and projection matrix. (glm::lookAt, and glm::project). They work the same as in openGL. You can send them as uniforms to the vertex shader.</p> <p>For the obejcts, you calculate your own marixes, and send them as the model matrix to the shader(s).</p> <p>In the shader or on cpu you calculate the mv matrix:</p> <pre><code>vp = proj*view. </code></pre> <p>You send your individual model matrixes to the shader and calculate the final position:</p> <pre><code>gl_Position = vp*m*vec4(vertex.xyz,1); </code></pre> <p><strong>MODEL MATRIX</strong></p> <p>with glm, you can easily calculate, transform you matrixes. You create a simple identity matrix:</p> <pre><code>glm::mat4x4(1) //identity </code></pre> <p>you can translate, rotate, scale it.</p> <pre><code>glm::scale glm::rotate glm::translate </code></pre> <p>They work like in immediate mode in opengl.</p> <p>after you have your matrix send it via the uniform.</p> <p><strong>MORE MODEL MATRIX</strong></p> <pre><code>shader-&gt;senduniform("proj", camera.projectionmatrix); shader-&gt;senduniform("view", camera.viewmatrix); glm::mat4 model(1); obj1.modelmatrix = glm::translate(model,vec3(1,2,1)); shader-&gt;senduniform("model", obj1.modelmatrix); objectloader.render(obj1); obj2.modelmatrix = glm::rotate(model,obj2.degrees,vec3(obj2.rotationaxis)); shader-&gt;senduniform("model", obj2.modelmatrix); objectloader.render(obj2); </code></pre> <p>This is just one way to do this. You can write a class for push/pop matrix calculations, automate the method above like this:</p> <pre><code>obj1.rotate(degrees,vec3(axis)); //this calculates the obj1.modelmatrix for example rotating the identity matrix. obj1.translate(vec3(x,y,z))//more transform obj1.render(); //continue with object 2 </code></pre> <p><strong>VIEW MATRIX</strong></p> <p>the view matrix almost the same as model matrix. Use this to control the global "model matrix", the camera. This transforms your screen globally, and you can have model matrixes for your objects individually.</p> <p>In my camera class I calculate this with the glm::lookAt(the same as opengl) then send it via uniform to all shaders I use.</p> <p>Then when I render something I can manipulate its model matrix, rotating or scaling it, but the view matrix is global.</p> <p>If you want a static object, you don't have to use model matrix on it, you can calculate the position with only:</p> <pre><code>gl_Position = projmatrix*viewmatrix*staticobjectvertex; </code></pre> <p><strong>GLOBAL MODEL MATRIX</strong></p> <p>You can have a global model matrix too.</p> <p>Use it like</p> <pre><code>renderer.globmodel.rotate(axis,degree); renderer.globmodel.scale(x,y,z); </code></pre> <p>Send it as uniform too, and apply it after the objects' model matrix. (I've used it to render ocean reflections to texture.)</p> <p><strong>To sum up:</strong></p> <ul> <li>create a global view(camera) matrix</li> <li>create a model matrix for each of your sceens, meshes or objects</li> <li>transform the objects' matrixes individually</li> <li>send the projection, model and view matrixes via uniforms to the shader</li> <li>calculate the final position: proj*camera*model*vertex</li> <li>move your objects, and move your camera</li> </ul> <p>I'm not saying there aren't any better way to do this, but this works for me well.</p> <p>PS: if you'd like some camera class tuts I have a pretty good one;).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload