Commits

dbacchet  committed 298b6ae

updated manipulator example (full mouse support)

  • Participants
  • Parent commits a6b4059

Comments (0)

Files changed (13)

File sources/opengl2_samples/test7c_trackballmanipulatorcamera/GLCameraManipulator.h

         }
         else if (currMode==evZoom)
         {
-            radius += radius*(anchorPanSS.y-y)/100; // too minimalistic zoom implementation
+            radius += radius*(anchorPanSS.y+y)/100; // too minimalistic zoom implementation (+y here because in the camera manipualtor the y coord is inverted)
             if (radius<0.1) radius=0.1;
         }
     }

File sources/opengl2_samples/test7c_trackballmanipulatorcamera/GLMaterial.cpp

         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
 
+        // add anisotropic filtering
+        /// \warning NOT SAFE!!! no check if the extension is available
+        GLfloat fLargest;
+        glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &fLargest);
+        fLargest = fLargest>4.0f ? 4.0f : fLargest; // limit to 4 the anisotropic filtering
+        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, fLargest);
+
         stbi_image_free(data);
 
         // add to map and return 

File sources/opengl2_samples/test7c_trackballmanipulatorcamera/GLMeshTools.cpp

 
 namespace 
 {   
-            // GLMaterial *mat = materialsystem::getOrCreateMaterial("simple","simple");
+            // GLMaterial *mat = materialsystem::getOrCreateMaterial("material_matte","shader_matte");
         // mat->textures[GLMaterial::evTextureDiffuse] = "Concrete.jpg";
         // mat->textures[GLMaterial::evTextureNormal] = "normal1.png";
 
         aiMaterial* aiMat = scene->mMaterials[index];
 
         std::stringstream mname;
-        static int idx = 1;
-        mname << "ai_material_" << idx++;
-        GLShader *shader2 = materialsystem::getOrCreateShaderFromFiles("simple_texSSS","simple_sh_tex.vert", "simple_sh_tex.frag");
-        GLMaterial *mat = materialsystem::getOrCreateMaterial(mname.str(),"simple_texSSS");
+        // static int idx = 1;
+        mname << "ai_material_" << index;
+        GLShader *shader2 = materialsystem::getOrCreateShaderFromFiles("shader_default","shader_default.vert", "shader_default.frag");
+        GLMaterial *mat = materialsystem::getOrCreateMaterial(mname.str(),"shader_default");
 
         // fill material with data
 
 
         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");
+        GLShader *shader = materialsystem::getOrCreateShaderFromFiles("shader_matte","shader_matte.vert", "shader_matte.frag");
+        GLMaterial *mat  = materialsystem::getOrCreateMaterial("material_matte","shader_matte");
         sm.material = mat;
         
         mesh->addSubMesh(sm);
 
         GLSubMesh sm = createBoxSubMesh( size, color);
         // set material
-        GLShader *shader = materialsystem::getOrCreateShaderFromFiles("simple","simple_sh.vert", "simple_sh.frag");
+        GLShader *shader = materialsystem::getOrCreateShaderFromFiles("shader_matte","shader_matte.vert", "shader_matte.frag");
         // GLShader *shader = new GLShader();
         // shader->initFromFile("normal_sh_tex.vert", "normal_sh_tex.frag");
-        GLMaterial *mat = materialsystem::getOrCreateMaterial("simple","simple");
+        GLMaterial *mat = materialsystem::getOrCreateMaterial("material_matte","shader_matte");
         // mat->textures[GLMaterial::evTextureDiffuse] = "Concrete.jpg";
         // mat->textures[GLMaterial::evTextureNormal] = "normal1.png";
         sm.material = mat;

File sources/opengl2_samples/test7c_trackballmanipulatorcamera/SConscript

 # build test app
 gl_test  = env_local.Program('gl2_test7c_trackballmanipulatorcamera',files_test,PDB='gl2_test7c_trackballmanipulatorcamera.pdb')
 # copy needed resources
-sconsutils.installResources(env_local,gl_test,[File('simple_sh.vert'),
-                                               File('simple_sh.frag'),
+sconsutils.installResources(env_local,gl_test,[File('shader_matte.vert'),
+                                               File('shader_matte.frag'),
+                                               File('shader_default.vert'),
+                                               File('shader_default.frag'),
                                                File('#resources/models/bumblebee_stripped.dae'),
                                                File('#resources/images/uv_tex.png')])
 

File sources/opengl2_samples/test7c_trackballmanipulatorcamera/glfw_utils.cpp

     context.mouse_posx = x;
     context.mouse_posy = context.height-y;
 
-    context.dragging = (context.mouse_button==MOUSE_BUTTON_LEFT);
+    context.dragging = (context.mouse_button!=MOUSE_BUTTON_NONE);
 }
 
 
     {
         context.mouse_button = MOUSE_BUTTON_LEFT;
         if (button==GLFW_MOUSE_BUTTON_MIDDLE)
-            context.mouse_button = MOUSE_BUTTON_CENTER;
+            context.mouse_button = MOUSE_BUTTON_MIDDLE;
         if (button==GLFW_MOUSE_BUTTON_RIGHT)
             context.mouse_button = MOUSE_BUTTON_RIGHT;
     }

File sources/opengl2_samples/test7c_trackballmanipulatorcamera/glfw_utils.h

 
 #define MOUSE_BUTTON_NONE   0
 #define MOUSE_BUTTON_LEFT   1
-#define MOUSE_BUTTON_CENTER 2
+#define MOUSE_BUTTON_MIDDLE 2
 #define MOUSE_BUTTON_RIGHT  3
 
 #define KEY_BACKSPACE 0x01

File sources/opengl2_samples/test7c_trackballmanipulatorcamera/main.cpp

 
     if (ctx.mouse_button==MOUSE_BUTTON_LEFT && !ctx.dragging)
     {
+        trackball.setMode(GLCameraManipulatorf::evRotate);
+        trackball.beginDrag(nx,ny);
+    }
+    if (ctx.mouse_button==MOUSE_BUTTON_MIDDLE && !ctx.dragging)
+    {
+        trackball.setMode(GLCameraManipulatorf::evPan);
+        trackball.beginDrag(nx,ny);
+    }
+    if (ctx.mouse_button==MOUSE_BUTTON_RIGHT && !ctx.dragging)
+    {
+        trackball.setMode(GLCameraManipulatorf::evZoom);
         trackball.beginDrag(nx,ny);
     }
     if (ctx.dragging)
     applyTransform(mesh, Matrix4f::createTranslation(0.0f, 2.47f, 0.0) );
     meshes.push_back(mesh);
     // create floor
-    GLShader *shaderFloor = materialsystem::getOrCreateShaderFromFiles("simple_tex","simple_sh_tex.vert", "simple_sh_tex.frag");
-    GLMaterial *matFloor  = materialsystem::getOrCreateMaterial("diffuse","simple_tex");
+    GLShader *shaderFloor = materialsystem::getOrCreateShaderFromFiles("shader_default","shader_default.vert", "shader_default.frag");
+    GLMaterial *matFloor  = materialsystem::getOrCreateMaterial("material_floor","shader_default");
       matFloor->textures[GLMaterial::evTextureDiffuse] = "uv_tex.png";
     GLMesh *plane = meshtools::createPrefabPlane( 15.0, 2, matFloor );
     meshes.push_back(plane);
         // Check if ESC key was pressed or window was closed
         running = !glfwGetKey( GLFW_KEY_ESC ) && glfwGetWindowParam( GLFW_OPENED );
 
-        // manage autorotate
-        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)
         {

File sources/opengl2_samples/test7c_trackballmanipulatorcamera/shader_default.frag

+uniform sampler2D u_tex_diffuse;
+
+varying vec3 normal, lightDir;
+varying vec2 vtexcoord0;
+varying vec4 ecPos;
+
+void main()
+{
+    vec3 n = normalize(normal);
+    float intensity = max(dot(lightDir, n),0.0);
+    vec4 texcolor = texture2D(u_tex_diffuse,vtexcoord0);
+    
+    vec4 lightSpecular = vec4(1.0,1.0,1.0,1.0);
+    float shininess = 100.0;
+    vec3 r = normalize(-reflect(lightDir, n));
+    vec3 v = normalize(-ecPos.xyz);
+    vec4 specular = lightSpecular * pow(max(0.0, dot(r, v)), shininess);
+    
+    gl_FragColor = vec4(texcolor.xyz*intensity,1.0) + specular;
+}
+
+

File sources/opengl2_samples/test7c_trackballmanipulatorcamera/shader_default.vert

+// uniforms
+uniform mat4 u_mv_matrix;
+uniform mat4 u_prj_matrix;
+uniform vec4 u_light0_pos;
+
+// vertex attributes 
+attribute vec3 v_position;
+attribute vec3 v_normal;
+attribute vec4 v_color;
+attribute vec2 v_texcoord0;
+
+varying vec3 normal, lightDir;
+varying vec2 vtexcoord0;
+varying vec4 ecPos;
+
+void main()
+{
+    ecPos = vec4(u_mv_matrix * vec4(v_position,1.0));
+    lightDir   = normalize(vec3(u_light0_pos) - ecPos.xyz);
+    normal     = normalize(vec3(u_mv_matrix * vec4(v_normal,0.0)));
+
+    vtexcoord0 = v_texcoord0;
+    gl_Position = u_prj_matrix * u_mv_matrix * vec4(v_position,1.0);
+}

File sources/opengl2_samples/test7c_trackballmanipulatorcamera/shader_matte.frag

+varying vec3 normal, lightDir;
+varying vec4 vcolor;
+
+void main()
+{
+    vec3 n = normalize(normal);
+    float intensity = max(dot(lightDir, n),0.0);
+
+    gl_FragColor = vec4(vcolor.xyz*intensity,1.0);
+}

File sources/opengl2_samples/test7c_trackballmanipulatorcamera/shader_matte.vert

+// uniforms
+uniform mat4 u_mv_matrix;
+uniform mat4 u_prj_matrix;
+uniform vec4 u_light0_pos;
+
+// vertex attributes 
+attribute vec3 v_position;
+attribute vec3 v_normal;
+attribute vec4 v_color;
+attribute vec2 v_texcoord0;
+
+varying vec3 normal, lightDir;
+varying vec4 vcolor;
+
+void main()
+{
+    vec4 ecPos = vec4(u_mv_matrix * vec4(v_position,1.0));
+    lightDir   = normalize(vec3(u_light0_pos) - ecPos.xyz);
+    normal     = normalize(vec3(u_mv_matrix * vec4(v_normal,0.0)));
+
+    vcolor     = v_color;
+    gl_Position = u_prj_matrix * u_mv_matrix * vec4(v_position,1.0);
+}

File sources/opengl2_samples/test7c_trackballmanipulatorcamera/simple_sh.frag

-varying vec3 normal, lightDir;
-varying vec4 vcolor;
-
-void main()
-{
-    vec3 n = normalize(normal);
-    float intensity = max(dot(lightDir, n),0.0);
-
-    gl_FragColor = vec4(vcolor.xyz*intensity,1.0);
-}

File sources/opengl2_samples/test7c_trackballmanipulatorcamera/simple_sh.vert

-// uniforms
-uniform mat4 u_mv_matrix;
-uniform mat4 u_prj_matrix;
-uniform vec4 u_light0_pos;
-
-// vertex attributes 
-attribute vec3 v_position;
-attribute vec3 v_normal;
-attribute vec4 v_color;
-attribute vec2 v_texcoord0;
-
-varying vec3 normal, lightDir;
-varying vec4 vcolor;
-
-void main()
-{
-    vec4 ecPos = vec4(u_mv_matrix * vec4(v_position,1.0));
-    lightDir   = normalize(vec3(u_light0_pos) - ecPos.xyz);
-    normal     = normalize(vec3(u_mv_matrix * vec4(v_normal,0.0)));
-
-    vcolor     = v_color;
-    gl_Position = u_prj_matrix * u_mv_matrix * vec4(v_position,1.0);
-}