dbacchet avatar dbacchet committed a6b4059

* implemented the camera manipulator
* added auto mipmap generation

Comments (0)

Files changed (10)

sources/opengl2_samples/test7_simplerenderer/GLMaterial.cpp

         glBindTexture(GL_TEXTURE_2D, tid);
         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 
                                     0, GL_RGBA, GL_UNSIGNED_BYTE, data); // Texture specification
-        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+        glGenerateMipmap(GL_TEXTURE_2D);  //Generate mipmaps
+        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
 
         stbi_image_free(data);
 

sources/opengl2_samples/test7_simplerenderer/GLMeshTools.cpp

     }
 
     
+    // only one submesh
+    GLSubMesh createXZPlaneSubMesh( float sideLen, unsigned int sideQuads, Vector4f color)
+    {
+        GLSubMesh sm;
+        sm.indices.clear();
+        sm.vertices.clear();
+
+        if (sideQuads<1) sideQuads = 1;
+    
+        float quadLen = 2.0*sideLen/sideQuads;
+
+        for (unsigned int u=0; u<sideQuads; u++)
+            for (unsigned int v=0; v<sideQuads; v++)
+            {
+                float offx = -sideLen + u*quadLen;
+                float offz = -sideLen + v*quadLen;
+                // quad vertices
+                Vector3f p0(offx,0.0,offz);
+                Vector3f p1(offx+quadLen,0.0,offz);
+                Vector3f p2(offx+quadLen,0.0,offz+quadLen);
+                Vector3f p3(offx,0.0,offz+quadLen);
+                // fill sm vertices, indices and normals
+                fillFaceData( sm, Vector3f( 0.0f, 1.0f, 0.0f), p0,p1,p2,p3, color);
+            }
+        
+        return sm;
+    }
+
+    
+    // create a box prefab
+    GLMesh* createPrefabPlane( float sideLen, unsigned int sideQuads, Vector4f color )
+    {
+        GLMesh *mesh = new GLMesh;
+
+        GLSubMesh sm = createXZPlaneSubMesh( sideLen, sideQuads, color);
+        // set material
+        GLShader *shader = materialsystem::getOrCreateShaderFromFiles("simple","simple_sh.vert", "simple_sh.frag");
+        GLMaterial *mat  = materialsystem::getOrCreateMaterial("simple","simple");
+        sm.material = mat;
+        
+        mesh->addSubMesh(sm);
+        return mesh;
+    }
+
+
+    // create a box prefab
+    GLMesh* createPrefabPlane( float sideLen, unsigned int sideQuads, GLMaterial *mat )
+    {
+        GLMesh *mesh = new GLMesh;
+
+        GLSubMesh sm = createXZPlaneSubMesh( sideLen, sideQuads, Vector4f(1.0,1.0,1.0,1.0));
+        // set material
+        sm.material = mat;
+        
+        mesh->addSubMesh(sm);
+        return mesh;
+    }
+
+
     // create a box prefab
     GLMesh* createPrefabBox( Vector3f size, Vector4f color )
     {

sources/opengl2_samples/test7_simplerenderer/GLMeshTools.h

     /// create a mesh reading from a file. return a NULL pointer in case of error
     GLMesh* createMeshFromFile( const std::string &filename );
     
-    /// create a box prefab
+    /// create a prefab XZ plane in the origin
+    GLMesh* createPrefabPlane( float sideLen, unsigned int sideQuads, Vector4f color );
+    GLMesh* createPrefabPlane( float sideLen, unsigned int sideQuads, GLMaterial *mat );
+    /// create a prefab box in the origin
     GLMesh* createPrefabBox( Vector3f size, Vector4f color );
     GLMesh* createPrefabBox( Vector3f size, GLMaterial *mat );
     

sources/opengl2_samples/test7_simplerenderer/GLTrackballManipulator.h

         else if (currMode==evZoom)
         {
             radius += radius*(anchorPanSS.y-y)/100; // too minimalistic zoom implementation
+            if (radius<0.1) radius=0.1;
         }
     }
     

sources/opengl2_samples/test7b_trackballmanipulator/GLMeshTools.cpp

             }
             if (aimesh->HasNormals())
             {
-                aiVector3D v = tr*aimesh->mVertices[i];
+                aiVector3D v = tr*aimesh->mNormals[i];
                 vertex.norm = Vector3f(v.x,v.y,v.z);
             }
             if (aimesh->HasTangentsAndBitangents())

sources/opengl2_samples/test7c_trackballmanipulatorcamera/GLCameraManipulator.h

+#ifndef __DBGLTrackBallManipulator__
+#define __DBGLTrackBallManipulator__
+// /////////////////////////////////////////////////////////////////////// //
+// Copyright (c) 2012, Davide Bacchet (davide.bacchet@gmail.com)           //
+// All rights reserved.                                                    //
+//                                                                         //
+// The software is released under the Creative Commons Attribution         //
+// NonCommercial (CC BY-NC) license. The full license text is available    //
+// at http://creativecommons.org/licenses/by-nc/3.0/legalcode              //
+//                                                                         //
+// You are free to distribute and adapt the work under the conditions:     //
+// * Redistributions of source code must retain the above copyright notice //
+// * You must attribute the work in the manner specified by the author     //
+// * You may not use this work for commercial purposes.                    //
+//                                                                         //
+// Commercial Usage:                                                       //
+// To use this software as part of a commercial product, you must          //
+// obtain a written agreement from the author.                             //
+// /////////////////////////////////////////////////////////////////////// //
+
+// trackball manipulator; add the pan/zoom functionality to the standard trackball
+
+
+#include "vmath.h"
+
+#include "GLTrackball.h"
+
+
+/// @class GLCameraManipulator 
+/// trackball 
+template <typename real_type=float>
+class GLCameraManipulator
+{
+public:
+
+    enum eMode {
+            evRotate,   ///< rotate mode
+            evPan,      ///< pan the manipulator
+            evZoom,     ///< zoom mode
+            evModeNum
+    };
+
+public:
+
+    /// standard constructor
+    GLCameraManipulator()
+    : trackball()
+    , currMode(evRotate)
+    , radius(10.0)
+    , center(0.0,1.0,0.0)
+    {}
+    /// destructor
+    virtual ~GLCameraManipulator()
+    {}
+    
+    /// begin the dragging
+    virtual void beginDrag(real_type x, real_type y)
+    {
+        if (currMode==evRotate)
+            trackball.beginDrag(x,y);
+        else if (currMode==evPan || currMode==evZoom)
+        {
+            anchorPan = center; // set anchor point to the current position
+            anchorPanSS = Vector3<real_type>(-x,-y,0.0);
+            incremPan = Vector3<real_type>(0,0,0);
+        }
+    }
+    
+    /// mouse drag
+    virtual void drag(real_type x, real_type y)
+    {
+        if (currMode==evRotate)
+            trackball.drag(x,y);
+        else if (currMode==evPan)
+        {
+            Vector3<real_type> delta = Vector3<real_type>(-x,-y,0.0)-anchorPanSS;
+            incremPan = delta*radius; // compensate with radius to 'normalize' the translation (effective when connected to a camera) 
+            incremPan = trackball.getRotation().inverse()*incremPan; // to use object local orientation
+            center = anchorPan+incremPan;
+        }
+        else if (currMode==evZoom)
+        {
+            radius += radius*(anchorPanSS.y-y)/100; // too minimalistic zoom implementation
+            if (radius<0.1) radius=0.1;
+        }
+    }
+    
+    /// get current center
+    Vector3<real_type> getCenter( void ) 
+    {
+        return center;
+    }
+    /// get current rotation
+    Matrix4<real_type> getRotation( void ) 
+    {
+        return trackball.getRotation().inverse();
+    }
+    /// get current transformation
+    Matrix4<real_type> getTransformation( void ) 
+    {
+        currentTransf = trackball.getRotation().inverse();
+        Vector3<real_type> offsetRad = currentTransf*Vector3<real_type>(0.0,0.0,radius); // radius offset in local orientation
+        currentTransf.setTranslation(center+offsetRad);
+        return currentTransf;
+    }
+
+    void setMode( eMode mode )
+    { 
+        currMode = mode;
+    }
+
+
+protected:
+    
+    // member variables
+    eMode currMode;               ///< manipulator current mode
+    
+    real_type           radius;      ///< trackball radius
+    Vector3<real_type>  center;      ///< trackball center (current pan value)
+    Vector3<real_type>  anchorPan;   ///< anchor point for pan
+    Vector3<real_type>  anchorPanSS; ///< anchor point for pan (screen-space)
+    Vector3<real_type>  incremPan;   ///< increment for pan
+
+    Matrix4<real_type>  currentTransf;  ///< current transformation matrix
+
+    GLTrackball<real_type> trackball;   ///< manage rotations
+        
+};
+
+
+typedef GLCameraManipulator<float>  GLCameraManipulatorf;
+typedef GLCameraManipulator<double> GLCameraManipulatord;
+
+#endif

sources/opengl2_samples/test7c_trackballmanipulatorcamera/GLMaterial.cpp

         glBindTexture(GL_TEXTURE_2D, tid);
         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 
                                     0, GL_RGBA, GL_UNSIGNED_BYTE, data); // Texture specification
-        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+        glGenerateMipmap(GL_TEXTURE_2D);  //Generate mipmaps
+        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
 
         stbi_image_free(data);
 

sources/opengl2_samples/test7c_trackballmanipulatorcamera/GLTrackball.h

         return currentRot;
     }
 
-protected:
+public:
     
     // member variables
     

sources/opengl2_samples/test7c_trackballmanipulatorcamera/GLTrackballManipulator.h

-#ifndef __DBGLTrackBallManipulator__
-#define __DBGLTrackBallManipulator__
-// /////////////////////////////////////////////////////////////////////// //
-// Copyright (c) 2012, Davide Bacchet (davide.bacchet@gmail.com)           //
-// All rights reserved.                                                    //
-//                                                                         //
-// The software is released under the Creative Commons Attribution         //
-// NonCommercial (CC BY-NC) license. The full license text is available    //
-// at http://creativecommons.org/licenses/by-nc/3.0/legalcode              //
-//                                                                         //
-// You are free to distribute and adapt the work under the conditions:     //
-// * Redistributions of source code must retain the above copyright notice //
-// * You must attribute the work in the manner specified by the author     //
-// * You may not use this work for commercial purposes.                    //
-//                                                                         //
-// Commercial Usage:                                                       //
-// To use this software as part of a commercial product, you must          //
-// obtain a written agreement from the author.                             //
-// /////////////////////////////////////////////////////////////////////// //
-
-// trackball manipulator; add the pan/zoom functionality to the standard trackball
-
-
-#include "vmath.h"
-
-#include "GLTrackball.h"
-
-
-/// @class GLTrackballManipulator 
-/// trackball 
-template <typename real_type=float>
-class GLTrackballManipulator
-{
-public:
-
-    enum eMode {
-            evRotate,   ///< rotate mode
-            evPan,      ///< pan the manipulator
-            evZoom,     ///< zoom mode
-            evModeNum
-    };
-
-public:
-
-    /// standard constructor
-    GLTrackballManipulator()
-    : trackball()
-    , currMode(evRotate)
-    , radius(1.0)
-    {}
-    /// destructor
-    virtual ~GLTrackballManipulator()
-    {}
-    
-    /// begin the dragging
-    virtual void beginDrag(real_type x, real_type y)
-    {
-        if (currMode==evRotate)
-            trackball.beginDrag(x,y);
-        else if (currMode==evPan || currMode==evZoom)
-        {
-            anchorPan = center; // set anchor point to the current position
-            anchorPanSS = Vector3<real_type>(x,y,0.0);
-            incremPan = Vector3<real_type>(0,0,0);
-        }
-    }
-    
-    /// mouse drag
-    virtual void drag(real_type x, real_type y)
-    {
-        if (currMode==evRotate)
-            trackball.drag(x,y);
-        else if (currMode==evPan)
-        {
-            Vector3<real_type> delta = Vector3<real_type>(x,y,0.0)-anchorPanSS;
-            incremPan = delta*radius; // compensate with radius to 'normalize' the translation (effective when connected to a camera) 
-            incremPan = trackball.getRotation()*incremPan; // to use object local orientation
-            center = anchorPan+incremPan;
-        }
-        else if (currMode==evZoom)
-        {
-            radius += radius*(anchorPanSS.y-y)/100; // too minimalistic zoom implementation
-        }
-    }
-    
-    /// get current center
-    Vector3<real_type> getCenter( void ) 
-    {
-        return center;
-    }
-    /// get current rotation
-    Matrix4<real_type> getRotation( void ) 
-    {
-        return trackball.getRotation();
-    }
-    /// get current transformation
-    Matrix4<real_type> getTransformation( void ) 
-    {
-        currentTransf = trackball.getRotation();
-        Vector3<real_type> offsetRad = currentTransf*Vector3<real_type>(0.0,0.0,radius); // radius offset in local orientation
-        currentTransf.setTranslation(center+offsetRad);
-        return currentTransf;
-    }
-
-    void setMode( eMode mode )
-    { 
-        currMode = mode;
-    }
-
-protected:
-    
-    // member variables
-    eMode currMode;               ///< manipulator current mode
-    
-    real_type           radius;      ///< trackball radius
-    Vector3<real_type>  center;      ///< trackball center (current pan value)
-    Vector3<real_type>  anchorPan;   ///< anchor point for pan
-    Vector3<real_type>  anchorPanSS; ///< anchor point for pan (screen-space)
-    Vector3<real_type>  incremPan;   ///< increment for pan
-
-    Matrix4<real_type>  currentTransf;  ///< current transformation matrix
-
-    GLTrackball<real_type> trackball;   ///< manage rotations
-        
-};
-
-
-typedef GLTrackballManipulator<float>  GLTrackballManipulatorf;
-typedef GLTrackballManipulator<double> GLTrackballManipulatord;
-
-#endif

sources/opengl2_samples/test7c_trackballmanipulatorcamera/main.cpp

 #include "GLTimer.h"
 #include "GLTransform.h"
 #include "GLTrackball.h"
-#include "GLTrackballManipulator.h"
+#include "GLCameraManipulator.h"
 
 #include "vertex.h"
 
 int height = 500;    
 
 GLCamera     camera;
-GLTrackballManipulatorf trackball;
+GLCameraManipulatorf trackball;
 
 // timer
 GLTimer timer;
 
 void drawGraphics( void )
 { 
-    Matrix4f centerTransf    = trackball.getRotation();    
-      centerTransf.setTranslation(trackball.getCenter());
     Matrix4f trackballTransf = trackball.getTransformation();    
+    camera.transform.setTransformationMatrix(trackballTransf);
 
     // add other render data
     for (unsigned int i=0; i<meshes.size(); i++)
         {
             const GLSubMesh &sm = meshes[i]->submeshes[j];
             renderer->addRenderData( sm.material->shader->name, sm.material->name,
-                                     sm.vertices, sm.indices,
-                                     trackballTransf );
+                                     sm.vertices, sm.indices );//,
+                                     // trackballTransf );
         }
         
     }
 std::cin >> dbidx;
 #endif //__Debug__
 
-    trackball.setMode(GLTrackballManipulatorf::evRotate);
+    trackball.setMode(GLCameraManipulatorf::evRotate);
 
     // Initialize GLFW and Glee
     if( !glfw_Init(width,height) )
         running = !glfwGetKey( GLFW_KEY_ESC ) && glfwGetWindowParam( GLFW_OPENED );
 
         // manage autorotate
-        if (glfwGetKey('R')) trackball.setMode(GLTrackballManipulatorf::evRotate);
-        if (glfwGetKey('T')) trackball.setMode(GLTrackballManipulatorf::evPan);
-        if (glfwGetKey('Z')) trackball.setMode(GLTrackballManipulatorf::evZoom);
+        if (glfwGetKey('R')) trackball.setMode(GLCameraManipulatorf::evRotate);
+        if (glfwGetKey('T')) trackball.setMode(GLCameraManipulatorf::evPan);
+        if (glfwGetKey('Z')) trackball.setMode(GLCameraManipulatorf::evZoom);
 
         // update fps
         if (frames%100==0)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.