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

List:       kde-commits
Subject:    playground/games/astrododge
From:       Rivo Laks <rivolaks () hot ! ee>
Date:       2009-07-18 14:00:30
Message-ID: 1247925630.415505.5172.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 998755 by rivol:

Big change to object rendering:
Instead of having a complete shader for each different material, we now have just a \
single shader  function for each material. This function calculates the color of the \
material's surface. There's also a single  function for every light type, calculating \
the direction and color of the light. This allows easily combining  different \
material/light types. This also concludes the bigger rewrites, so AstroDodge should \
now be somewhat usable again :-)

 M  +1 -1      data/objects/ship.index  
 A             data/shaders/light-directional.frag  
 A             data/shaders/light-directional.vert  
 A             data/shaders/light-point.frag  
 A             data/shaders/light-point.vert  
 A             data/shaders/light-spot.frag  
 A             data/shaders/light-spot.vert  
 A             data/shaders/material-asteroid.frag  
 A             data/shaders/material-asteroid.vert  
 A             data/shaders/material-object.frag  
 A             data/shaders/material-object.vert  
 A             data/shaders/material-ship.frag  
 A             data/shaders/material-ship.vert  
 A             data/shaders/render-ambient.frag  
 A             data/shaders/render-ambient.vert  
 A             data/shaders/render-light.frag  
 A             data/shaders/render-light.vert  
 M  +1 -0      src/CMakeLists.txt  
 M  +4 -0      src/datastore.cpp  
 M  +3 -0      src/datastore.h  
 M  +36 -7     src/gameview.cpp  
 M  +2 -0      src/gameview.h  
 A             src/shadermanager.cpp   [License: GPL (v3+)]
 A             src/shadermanager.h   [License: GPL (v3+)]


--- trunk/playground/games/astrododge/data/objects/ship.index #998754:998755
@@ -1,4 +1,4 @@
 [Model]
 Model=spaceship.obj
 Mass=1000000
-Program=ship
+Material=ship
--- trunk/playground/games/astrododge/src/CMakeLists.txt #998754:998755
@@ -15,6 +15,7 @@
         mainwindow.cpp
         model.cpp
         planet.cpp
+        shadermanager.cpp
         ship.cpp
         )
 kde4_add_ui_files(astrododge_SRCS lodinfowidget.ui)
--- trunk/playground/games/astrododge/src/datastore.cpp #998754:998755
@@ -18,6 +18,7 @@
 #include "datastore.h"
 
 #include "model.h"
+#include "shadermanager.h"
 
 #include <kgllib/program.h>
 #include <kgllib/texture.h>
@@ -33,6 +34,8 @@
 {
     mDataDir = datadir;
     kDebug() << "Data dir is" << mDataDir;
+
+    mShaderManager = new ShaderManager(datadir + "shaders/");
 }
 
 DataStore::~DataStore()
@@ -101,6 +104,7 @@
             return false;
         }
         model->setTexture(texture("asteroid.png"));
+        model->setMaterial("asteroid");
         mNonFinalizedModels[modelName] = model;
         mAsteroidModels.append(model);
     }
--- trunk/playground/games/astrododge/src/datastore.h #998754:998755
@@ -25,6 +25,7 @@
 #include <QVector>
 
 class Model;
+class ShaderManager;
 
 namespace KGLLib
 {
@@ -46,6 +47,7 @@
         Model* randomAsteroidModel();
         KGLLib::Texture* texture(const QString& name);
         KGLLib::Program* program(const QString& name);
+        ShaderManager* shaderManager() const  { return mShaderManager; }
 
         bool loadAsteroidModels();
         bool loadModel(const QString& name);
@@ -63,6 +65,7 @@
         QHash<QString, KGLLib::Texture*> mTextures;
         QHash<QString, KGLLib::Program*> mPrograms;
         QList<KGLLib::GeometryBuffer*> mGeometryBuffers;
+        ShaderManager* mShaderManager;
 
         QList<Model*> mAsteroidModels;
         QHash<QString, Model*> mNonFinalizedModels;
--- trunk/playground/games/astrododge/src/gameview.cpp #998754:998755
@@ -23,6 +23,7 @@
 #include "model.h"
 #include "bullet.h"
 #include "datastore.h"
+#include "shadermanager.h"
 
 #include <kgllib/camera.h>
 #include <kgllib/widgetproxy.h>
@@ -67,6 +68,8 @@
     mEarthMesh = new Mesh();
     Shapes::createSphere(mEarthMesh, 6);
     mEarthMesh->setTexture(mEarthTex);
+
+    mShaderManager = mWorld->data()->shaderManager();
 }
 
 void GameView::setupWorldView()
@@ -103,11 +106,33 @@
     glEnable(GL_CULL_FACE);
     renderSpace();
 
-    // Setup light pos
-    glLightfv(GL_LIGHT0, GL_POSITION, Vector4f(-100000.0, 60000.0, 20000.0, \
1.0).data()); +    // Render objects
     glEnable(GL_DEPTH_TEST);
+    glDepthFunc(GL_LESS);
 
+    // Ambient + depth pass
+    mShaderManager->setRenderMode("ambient");
+    mShaderManager->setLight("directional");
+    mShaderManager->setLightColor(Vector3f(0.15, 0.15, 0.15));
     renderObjects(mWorld->objects());
+
+    // Light passes
+    KGLLib::checkGLError("Before light passes state setup");
+    glEnable(GL_BLEND);
+    glBlendFunc(GL_ONE, GL_ONE);
+    glDepthMask(GL_FALSE);
+    glDepthFunc(GL_EQUAL);
+    mShaderManager->setRenderMode("light");
+
+    mShaderManager->setLight("directional");
+    mShaderManager->setLightColor(Vector3f(0.8, 0.8, 0.8));
+    Vector3f lightDir = (camera()->modelviewMatrix() * Vector4f(2.0, 1.0, 0.5, \
0.0)).start<3>().normalized(); +    mShaderManager->setLightDirection(lightDir);
+    renderObjects(mWorld->objects());
+
+    // Restore states
+    glDepthMask(GL_TRUE);
+    glDepthFunc(GL_LESS);
     glDisable(GL_CULL_FACE);
 
     glLineWidth(2.0);
@@ -171,15 +196,13 @@
     }
     int activeTextures = 0;
 
-    Program* prog = mWorld->data()->program("asteroid");
-    prog->bind();
-
     foreach (const GameObject* obj, *objects) {
         Model* model = obj->model();
         if (!model) {
             continue;
         }
 
+        // Check buffer
         if (model->buffer() != buffer) {
             if (buffer) {
                 buffer->unbind();
@@ -187,6 +210,13 @@
             model->buffer()->bind();
             buffer = model->buffer();
         }
+        // Check material
+        if (model->material() != material) {
+            material = model->material();
+            mShaderManager->setMaterial(material);
+            mShaderManager->activateShader();
+        }
+        // Check textures
         int newActiveTextures = qMin(maxTextures, model->textureCount());
         for (int i = 0; i < qMax(activeTextures, newActiveTextures); i++) {
             Texture* newTex = model->texture(i);
@@ -222,7 +252,6 @@
 
         // Un-transform
         glPopMatrix();
-
     }
 
     for (int i = 0; i < activeTextures; i++) {
@@ -233,7 +262,7 @@
         }
     }
     glActiveTexture(GL_TEXTURE0);
-    prog->unbind();
+    mShaderManager->disableShader();
     if (buffer) {
         buffer->unbind();
     }
--- trunk/playground/games/astrododge/src/gameview.h #998754:998755
@@ -23,6 +23,7 @@
 
 class GameWorld;
 class GameObject;
+class ShaderManager;
 
 namespace KGLLib
 {
@@ -57,6 +58,7 @@
         KGLLib::Mesh* mSpaceMesh;
         KGLLib::Texture* mEarthTex;
         KGLLib::Mesh* mEarthMesh;
+        ShaderManager* mShaderManager;
 };
 
 #endif // GAMEVIEW_H


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

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