[CGSG] SVN Commit r426 - user/lfittl

lfittl at metalab.at lfittl at metalab.at
Sun Apr 20 05:50:49 CEST 2008


Author: lfittl
Date: 2008-04-20 05:50:49 +0200 (Sun, 20 Apr 2008)
New Revision: 426

Added:
   user/lfittl/Camera.cpp
   user/lfittl/Camera.hpp
Modified:
   user/lfittl/Main.cpp
   user/lfittl/Makefile
   user/lfittl/Mesh.hpp
   user/lfittl/Model.hpp
   user/lfittl/X11Window.cpp
   user/lfittl/X11Window.hpp
Log:
Implemented simple Camera class.


Added: user/lfittl/Camera.cpp
===================================================================
--- user/lfittl/Camera.cpp	                        (rev 0)
+++ user/lfittl/Camera.cpp	2008-04-20 03:50:49 UTC (rev 426)
@@ -0,0 +1,42 @@
+/* Copyright (c) 2008 Lukas Fittl
+ *
+ * vim: ft=cpp:enc=utf-8:et:sw=4:ts=8:sts=4:
+ *
+ * Author: Lukas Fittl <lf at nessus.at>
+ */
+
+#include <Camera.hpp>
+
+#include <GL/glew.h>
+#include <GL/gl.h>
+#include <GL/glu.h>
+
+Camera::Camera()
+    : posX_(0), posZ_(0)
+{
+}
+
+void Camera::Setup(int sizeX, int sizeY)
+{
+    if (sizeY == 0) sizeY = 1;
+    
+    glViewport(0, 0, sizeX, sizeY);
+    
+    glMatrixMode(GL_PROJECTION);
+    glLoadIdentity();
+    
+    gluPerspective(45.0, static_cast<float>(sizeX) / sizeY, 0.1, 100.0);
+    
+    glMatrixMode(GL_MODELVIEW);
+    glLoadIdentity();
+}
+
+void Camera::Apply()
+{
+    glLoadIdentity();
+
+    float x = posX_ * 0.2;
+    float z = posZ_ * -0.2;
+
+    gluLookAt(x, 2.5, z + 5.0, x, 2.5, z, 0, 1.0, 0);
+}

Added: user/lfittl/Camera.hpp
===================================================================
--- user/lfittl/Camera.hpp	                        (rev 0)
+++ user/lfittl/Camera.hpp	2008-04-20 03:50:49 UTC (rev 426)
@@ -0,0 +1,34 @@
+/* Copyright (c) 2008 Lukas Fittl
+ *
+ * vim: ft=cpp:enc=utf-8:et:sw=4:ts=8:sts=4:
+ *
+ * Author: Lukas Fittl <lf at nessus.at>
+ */
+
+#ifndef CAMERA_HPP
+#define CAMERA_HPP
+
+class Camera
+{
+public:
+    Camera();
+
+    void Setup(int sizeX, int sizeY);
+    void Apply();
+
+    inline void MoveLeft();
+    inline void MoveRight();
+    inline void MoveForward();
+    inline void MoveBackward();
+
+private:
+    int posX_;
+    int posZ_;
+};
+
+void Camera::MoveLeft() { --posX_; }
+void Camera::MoveRight() { ++posX_; }
+void Camera::MoveForward() { ++posZ_; }
+void Camera::MoveBackward() { --posZ_; }
+
+#endif // CAMERA_HPP

Modified: user/lfittl/Main.cpp
===================================================================
--- user/lfittl/Main.cpp	2008-04-20 03:44:52 UTC (rev 425)
+++ user/lfittl/Main.cpp	2008-04-20 03:50:49 UTC (rev 426)
@@ -13,46 +13,36 @@
 #include <GL/gl.h>
 #include <GL/glu.h>
 
-#include <X11Window.hpp>
+#include <Camera.hpp>
 #include <Model.hpp>
+#include <X11Window.hpp>
 
 #include <boost/shared_ptr.hpp>
 
-int sizeX = 800;
-int sizeY = 600;
-
 boost::shared_ptr<Model> model;
 
-void Draw(X11Window& window)
-{
-    static float i = 0.0;
+Camera camera;
 
-    if (i > 360.0) i = 0.0;
-    else i += 0.2;
+int keyX = 0;
+int keyZ = 0;
 
+void Draw(X11Window& window)
+{
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-    glLoadIdentity();
 
-    glPushMatrix();
-    
-    glTranslatef(0.0, -1.5, -8.0);
+    camera.Apply();
 
+    glColor3f(1.0, 0.0, 0.0);
+ 
     GLfloat position[] = { 0.0, 2.0, -2.0, 1.0 };
     glLightfv(GL_LIGHT0, GL_POSITION, position);
 
-    glRotatef(i, 1.0, 0.0, 0.0);
- 
     glColor3f(1.0, 0.5, 0.5);
 
     model->Draw();
 
-    glPopMatrix();
-    
-    glTranslatef(1.5, -2.0, -50.0 + (i / 7.0));
     glRotatef(90.0, -1.0, 0.0, 0.0);
-    
-    glColor3f(1.0, 0.0, 0.0);
-    
+   
     int rows = 100;
     int columns = 100;
 
@@ -75,21 +65,6 @@
     window.SwapBuffers();
 }
 
-void UpdateOGLPerspective(X11Window& window)
-{
-    if (sizeY == 0) sizeY = 1;
-    
-    glViewport(0, 0, sizeX, sizeY);
-    
-    glMatrixMode(GL_PROJECTION);
-    glLoadIdentity();
-    
-    gluPerspective(45.0, static_cast<float>(window.GetSizeX()) / window.GetSizeY(), 0.1, 100.0);
-    
-    glMatrixMode(GL_MODELVIEW);
-    glLoadIdentity();
-}
-
 void InitGL(X11Window& window)
 {
     GLenum err = glewInit();
@@ -114,7 +89,7 @@
     glDepthFunc(GL_LEQUAL);
     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
 
-    UpdateOGLPerspective(window);
+    camera.Setup(window.GetSizeX(), window.GetSizeY());
 
     GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
@@ -124,7 +99,7 @@
 
 int main()
 {
-    X11Window window(sizeX, sizeY, false, "Test Application");
+    X11Window window(800, 600, false, "Test Application");
 
     InitGL(window);
 
@@ -149,12 +124,10 @@
                  }
                 case ConfigureNotify:
                 {
-                    if (event.xconfigure.width != sizeX || event.xconfigure.height != sizeY)
+                    if (event.xconfigure.width != window.GetSizeX() || event.xconfigure.height != window.GetSizeY())
                     {
-                        sizeX = event.xconfigure.width;
-                        sizeY = event.xconfigure.height;
-                        
-                        UpdateOGLPerspective(window);
+                        window.OnResize(event.xconfigure.width, event.xconfigure.height);
+                        camera.Setup(window.GetSizeX(), window.GetSizeY());
                     }
                     
                     break;
@@ -162,8 +135,23 @@
                 case ButtonPress:
                 case KeyPress:
                 {
-                    done = true;
+                    // FYI: To get the proper text representation of a key press:
+                    // KeySym key;
+                    // char text[255];
+                    // if (XLookupString(&event.xkey, text, 255, &key, 0) == 1) std::cout << text << std::endl;
+
+                    KeySym key = XLookupKeysym(&event.xkey, 0);
+
+                    if (key == XK_Escape) {
+                        done = true;
+                    }
                     
+                    if (key == XK_Left) camera.MoveLeft();
+                    if (key == XK_Right) camera.MoveRight();
+
+                    if (key == XK_Up) camera.MoveForward();
+                    if (key == XK_Down) camera.MoveBackward();
+                    
                     break;
                 }
                 case ClientMessage:

Modified: user/lfittl/Makefile
===================================================================
--- user/lfittl/Makefile	2008-04-20 03:44:52 UTC (rev 425)
+++ user/lfittl/Makefile	2008-04-20 03:50:49 UTC (rev 426)
@@ -1,5 +1,5 @@
-main: Main.cpp X11Window.hpp X11Window.cpp Model.hpp Model.cpp Mesh.hpp Mesh.cpp
-	g++ -o main Main.cpp X11Window.cpp Model.cpp Mesh.cpp -I . -lGL -lGLU -lXrandr -lGLEW
+main: Main.cpp X11Window.hpp X11Window.cpp Model.hpp Model.cpp Mesh.hpp Mesh.cpp Camera.hpp Camera.cpp
+	g++ -o main Main.cpp X11Window.cpp Model.cpp Mesh.cpp Camera.cpp -I . -lGL -lGLU -lXrandr -lGLEW
 
 clean:
 	rm -rf main

Modified: user/lfittl/Mesh.hpp
===================================================================
--- user/lfittl/Mesh.hpp	2008-04-20 03:44:52 UTC (rev 425)
+++ user/lfittl/Mesh.hpp	2008-04-20 03:50:49 UTC (rev 426)
@@ -15,7 +15,7 @@
     typedef std::vector<float> TriangleStrip;
 
 public:
-    Mesh(const TriangleStrip&);
+    Mesh(const TriangleStrip& strip);
     ~Mesh();
 
     void Draw();

Modified: user/lfittl/Model.hpp
===================================================================
--- user/lfittl/Model.hpp	2008-04-20 03:44:52 UTC (rev 425)
+++ user/lfittl/Model.hpp	2008-04-20 03:50:49 UTC (rev 426)
@@ -20,7 +20,7 @@
     typedef boost::shared_ptr<Mesh> MeshPtr;
 
 public:
-    Model(const std::string&);
+    Model(const std::string& filename);
     void Draw();
 
 private:

Modified: user/lfittl/X11Window.cpp
===================================================================
--- user/lfittl/X11Window.cpp	2008-04-20 03:44:52 UTC (rev 425)
+++ user/lfittl/X11Window.cpp	2008-04-20 03:50:49 UTC (rev 426)
@@ -315,3 +315,9 @@
     
     return bestVis.id;
 }
+
+void X11Window::OnResize(int sizeX, int sizeY)
+{
+    sizeX_ = sizeX;
+    sizeY_ = sizeY;
+}

Modified: user/lfittl/X11Window.hpp
===================================================================
--- user/lfittl/X11Window.hpp	2008-04-20 03:44:52 UTC (rev 425)
+++ user/lfittl/X11Window.hpp	2008-04-20 03:50:49 UTC (rev 426)
@@ -46,6 +46,8 @@
 
     void SwapBuffers();
 
+    void OnResize(int sizeX, int sizeY);
+
     inline int GetSizeX() const;
     inline int GetSizeY() const;
 



More information about the CGSG mailing list