[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    playground/libs/kgllib
From:       Rivo Laks <rivolaks () hot ! ee>
Date:       2009-07-08 21:08:39
Message-ID: 1247087319.544850.20022.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 993543 by rivol:

Add Shapes helper class which can create simple geometrical objects (spheres and \
cubes for now).  The sphere-creation code is taken from the earth example which now \
uses the Shapes class.

 M  +5 -95     examples/earth/earth.cpp  
 M  +2 -3      examples/earth/earth.h  
 M  +2 -0      extras/kgllib/CMakeLists.txt  
 A             extras/kgllib/shapes.cpp   [License: LGPL (v2.1+)]
 A             extras/kgllib/shapes.h   [License: LGPL (v2.1+)]


--- trunk/playground/libs/kgllib/examples/earth/earth.cpp #993542:993543
@@ -3,7 +3,7 @@
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either 
+ * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful,
@@ -11,7 +11,7 @@
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Library General Public License for more details.
  *
- * You should have received a copy of the GNU Lesser General Public 
+ * You should have received a copy of the GNU Lesser General Public
  * License along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
@@ -22,6 +22,7 @@
 #include <kgllib/mesh.h>
 #include <kgllib/camera.h>
 #include <kgllib/textrenderer.h>
+#include <kgllib/shapes.h>
 
 #include <QTimer>
 #include <QMouseEvent>
@@ -74,7 +75,8 @@
     }
 
     mDiffuseTex->setWrapMode(GL_CLAMP);
-    mMesh = createMesh(6);
+    mMesh = new Mesh();
+    Shapes::createSphere(mMesh, 6);
     mMesh->setTexture(mDiffuseTex);
     glEnable(GL_CULL_FACE);
 
@@ -106,98 +108,6 @@
     QTimer::singleShot(20, this, SLOT(update()));
 }
 
-Mesh* Earth::createMesh(int detail)
-{
-    Vector3f xp( 1,  0,  0);
-    Vector3f xn(-1,  0,  0);
-    Vector3f yp( 0,  1,  0);
-    Vector3f yn( 0, -1,  0);
-    Vector3f zp( 0,  0,  1);
-    Vector3f zn( 0,  0, -1);
-    Vector3f octahedron[] = {
-        yp, xn, zp,
-        yp, zp, xp,
-        yp, xp, zn,
-        yp, zn, xn,
-        yn, zp, xn,
-        yn, xp, zp,
-        yn, zn, xp,
-        yn, xn, zn,
-    };
-
-    int vertexcount = 8*3;
-    Vector3f* vertices = new Vector3f[vertexcount];
-    for (int i = 0; i < vertexcount; i++) {
-        vertices[i] = octahedron[i];
-    }
-    for (int d = 0; d < detail; d++) {
-        int vc = vertexcount*4;
-        Vector3f* newv = new Vector3f[vc];
-        int j = 0;
-        for (int i = 0; i < vertexcount; i += 3) {
-            // Calculate midpoints of each side
-            Vector3f a = (vertices[i+0] + vertices[i+1]) / 2;
-            Vector3f b = (vertices[i+1] + vertices[i+2]) / 2;
-            Vector3f c = (vertices[i+2] + vertices[i+0]) / 2;
-            // Normalize them so that they'd be exactly 1 unit from center
-            a.normalize();
-            b.normalize();
-            c.normalize();
-
-            // Add new vertices
-            newv[j++] = vertices[i+0];
-            newv[j++] = a;
-            newv[j++] = c;
-
-            newv[j++] = c;
-            newv[j++] = a;
-            newv[j++] = b;
-
-            newv[j++] = b;
-            newv[j++] = a;
-            newv[j++] = vertices[i+1];
-
-            newv[j++] = b;
-            newv[j++] = vertices[i+2];
-            newv[j++] = c;
-        }
-        delete[] vertices;
-        vertices = newv;
-        vertexcount = vc;
-    }
-
-    // Calculate texcoords
-    Vector2f* texcoords = new Vector2f[vertexcount];
-    for (int i = 0; i < vertexcount; i += 3) {
-//         qDebug() << "  Processing face" << i;
-        for (int j = 0; j < 3; j++) {
-            texcoords[i+j].x() = (atan2f(vertices[i+j].x(), vertices[i+j].z()) + \
                M_PI) / (2*M_PI);
-            texcoords[i+j].y() = (vertices[i+j].y() + 1) / 2;
-        }
-        for (int j = 0; j < 3; j++) {
-            if (ei_isApprox(vertices[i+j].y(), 1.0f) || \
                ei_isApprox(vertices[i+j].y(), -1.0f)) {
-                texcoords[i+j].x() = (texcoords[i+0].x() + texcoords[i+1].x() + \
                texcoords[i+2].x() - texcoords[i+j].x()) / 2;
-            }
-            if (ei_isApprox(vertices[i+j].x(), 0.0f) && vertices[i+j].z() < 0.0f) {
-                float xsum = vertices[i+0].x() + vertices[i+1].x() + \
                vertices[i+2].x();
-                if (xsum > 0.0f) {
-                    texcoords[i+j].x() = 1;
-                } else {
-                    texcoords[i+j].x() = 0;
-                }
-            }
-//             qDebug() << "    Vertex (" << vertices[i+j].x() << ", " << \
                vertices[i+j].y() << ", " << vertices[i+j].z() <<
-//                     ") get texcoords (" << texcoords[i+j].x() << ", " << \
                texcoords[i+j].y() << ")";
-        }
-    }
-    Mesh* m = new Mesh();
-    m->setVertices(vertices);
-    m->setTexcoords(texcoords);
-    m->setVertexCount(vertexcount);
-    m->setPrimitiveType(GL_TRIANGLES);
-    return m;
-}
-
 Mesh* Earth::createStarMesh(int stars)
 {
     Vector3f* vertices = new Vector3f[stars];
--- trunk/playground/libs/kgllib/examples/earth/earth.h #993542:993543
@@ -3,7 +3,7 @@
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either 
+ * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful,
@@ -11,7 +11,7 @@
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Library General Public License for more details.
  *
- * You should have received a copy of the GNU Lesser General Public 
+ * You should have received a copy of the GNU Lesser General Public
  * License along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
@@ -44,7 +44,6 @@
     virtual void mousePressEvent(QMouseEvent* e);
     virtual void mouseMoveEvent(QMouseEvent* e);
 
-    Mesh* createMesh(int detail);
     Mesh* createStarMesh(int stars);
 
 private:
--- trunk/playground/libs/kgllib/extras/kgllib/CMakeLists.txt #993542:993543
@@ -16,6 +16,7 @@
         hdrglwidgetcontrol.cpp
         modelloader.cpp
         rendertarget.cpp
+        shapes.cpp
         simpleterrain.cpp
         trackball.cpp
         widgetproxy.cpp
@@ -40,6 +41,7 @@
         hdrglwidgetcontrol.h
         modelloader.h
         rendertarget.h
+        shapes.h
         simpleterrain.h
         trackball.h
         widgetproxy.h


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic