Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 3390 in orxonox.OLD


Ignore:
Timestamp:
Feb 2, 2005, 1:04:00 PM (19 years ago)
Author:
bensch
Message:

orxonox/branches/nico: heightmap now compiles on all platforms

Location:
orxonox/branches/nico/src/importer
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • orxonox/branches/nico/src/importer/Makefile.am

    r3384 r3390  
    1212                  material.cc \
    1313                  vector.cc
     14
    1415heightmap_SOURCES= main.cc \
    1516                   heightMapTerrain.cc \
    16                    heightMapViewer.cc
     17                   heightMapViewer.cc \
     18                   vector.cc \
     19                   windowHandler.cc
    1720
    1821noinst_HEADERS= framework.h \
  • orxonox/branches/nico/src/importer/Makefile.in

    r3384 r3390  
    5454PROGRAMS = $(bin_PROGRAMS)
    5555am_heightmap_OBJECTS = main.$(OBJEXT) heightMapTerrain.$(OBJEXT) \
    56         heightMapViewer.$(OBJEXT)
     56        heightMapViewer.$(OBJEXT) vector.$(OBJEXT) \
     57        windowHandler.$(OBJEXT)
    5758heightmap_OBJECTS = $(am_heightmap_OBJECTS)
    5859heightmap_LDADD = $(LDADD)
     
    134135SHELL = @SHELL@
    135136STRIP = @STRIP@
     137SUB_PROJECTS_FALSE = @SUB_PROJECTS_FALSE@
     138SUB_PROJECTS_TRUE = @SUB_PROJECTS_TRUE@
    136139VERSION = @VERSION@
    137140ac_ct_CC = @ac_ct_CC@
     
    189192heightmap_SOURCES = main.cc \
    190193                   heightMapTerrain.cc \
    191                    heightMapViewer.cc
     194                   heightMapViewer.cc \
     195                   vector.cc \
     196                   windowHandler.cc
    192197
    193198noinst_HEADERS = framework.h \
     
    210215          esac; \
    211216        done; \
    212         echo ' cd $(top_srcdir) && $(AUTOMAKE) --foreign  src/importer/Makefile'; \
     217        echo ' cd $(top_srcdir) && $(AUTOMAKE) --gnu  src/importer/Makefile'; \
    213218        cd $(top_srcdir) && \
    214           $(AUTOMAKE) --foreign  src/importer/Makefile
     219          $(AUTOMAKE) --gnu  src/importer/Makefile
    215220.PRECIOUS: Makefile
    216221Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status
  • orxonox/branches/nico/src/importer/heightMapTerrain.cc

    r3384 r3390  
    2727        if (bitmap == NULL) return false;
    2828       
    29         cout << "bitmap-dimensions: " << bitmap->w << "x" << bitmap->h << endl;
    30        
     29        cout << "bitmap-dimensions:             " << bitmap->w << " x " << bitmap->h << endl;
     30        cout << "bitmap-pitch:                  " << bitmap->pitch << endl;
     31        cout << "bitmap->format->BytesPerPixel: " << (int)bitmap->format->BytesPerPixel << endl;
     32        cout << "bitmap->format->BitsPerPixel:  " << (int)bitmap->format->BitsPerPixel << endl;
     33         
    3134        if (bitmap->w % width != 0) return false;
    3235        if (bitmap->h % height != 0) return false;
     
    4851                return false;
    4952        }
     53       
     54        cout << displayListCount << " display lists generated. starting at: " << displayListStart << endl;
     55
    5056
    5157        int i,j;
     
    6975bool HeightMapTerrain::readIntoList(int left, int top, int width, int height, int levelOfDetail, GLuint displayListNr)
    7076{
    71         // y is the height value in OpenGL convention
    72         GLuint x,y,z;
    73         GLfloat col;
    74        
    7577        cout << "creating list Nr " << displayListNr << endl;
    7678        cout << "left, top, width, height: " << left << ", " << top << ", " << width << ", " << height << endl;
    7779       
    78         glNewList(displayListNr,GL_COMPILE);
    79         {
    80                 for (z=top; z<top+height; z++)
     80        // y is the height value in OpenGL convention
     81        int x,y,z;
     82        GLfloat col;
     83
     84        // used for normal calculations
     85        Vector O; // origin: the vertex where i'm standing
     86        Vector N,E,S,W,NE,SE,SW,NW; // vectors pointing to north, east, ...
     87
     88        bool eightTriangles = false;
     89       
     90       
     91        // contains a normal vector for every vertex
     92        normals = new Vector[width*height];
     93
     94        // precalculate all vertex normals for later use in display list generation
     95        // many superflous calculations i know...
     96        for (z=top; z<top+height; z++)
     97        {
     98                for (x=left; x<left+width; x++)
    8199                {
    82                         glBegin(GL_QUAD_STRIP);
     100                        O = Vector(x,getHeightValue(x,z),z);
     101
     102                        N = Vector(x,getHeightValue(x,z-1),z-1) - O;
     103                        E = Vector(x+1,getHeightValue(x+1,z),z) - O;
     104                        S = Vector(x,getHeightValue(x,z+1),z+1) - O;
     105                        W = Vector(x-1,getHeightValue(x-1,z),z) - O;
     106
     107                        if (eightTriangles == false)
     108                                // calculate the average normal vector by adding up all 4 adjacent triangle normal vectors
     109                                normals[(z-top) * height + (x-left)] = N.cross(W) + W.cross(S) + S.cross(E);
     110
     111                        else
    83112                        {
    84                                 for (x=left; x<left+width; x++)
     113                                NE = Vector(x+1,getHeightValue(x+1,z-1),z-1) - O;
     114                                SE = Vector(x+1,getHeightValue(x+1,z+1),z+1) - O;
     115                                SW = Vector(x-1,getHeightValue(x-1,z+1),z+1) - O;
     116                                NW = Vector(x-1,getHeightValue(x-1,z-1),z-1) - O;
     117                                // calculate the average normal vector by adding up all 8 adjacent triangle normal vectors
     118                                normals[(z-top) * height + (x-left)] = N.cross(NW) + NW.cross(W) + W.cross(SW) + SW.cross(S) + S.cross(SE) + SE.cross(E) + E.cross(NE) + NE.cross(N);
     119                        }
     120
     121                        // every second vertex is the edge of eightTriangles (the others have only four triangles)
     122                        eightTriangles = !eightTriangles;
     123                }
     124        }
     125       
     126        cout << "normals precalculated for list " << displayListNr << endl;
     127       
     128        glNewList(displayListStart + displayListNr,GL_COMPILE);
     129        {
     130                glBegin(GL_TRIANGLES);
     131                {
     132                        // don't go through last col and row, because they are already done by second last col&row
     133                        for (z = top; z < (top + height) - 1; z++)
     134                        {
     135                                for (x = left; x < (left + width) - 1; x++)
    85136                                {
    86                                         y = heightValue(x,z+1);
    87                                         col = y / 256.0;
    88                                         glColor3f(1.0,0.0,0.0);
    89                                         glVertex3i(x,y,z+1);
    90                                        
    91                                         y = heightValue(x,z);
    92                                         col = y / 256.0;
    93                                         glColor3f(1.0,0.0,0.0);
    94                                         glVertex3i(x,y,z);
     137                                        // draw 2 triangles per (x,z) pair
     138                                       
     139                                        if (eightTriangles)
     140                                        {
     141                                                call_glNormal_and_glVertex(x,z,top,left,height);
     142                                                call_glNormal_and_glVertex(x,z+1,top,left,height);
     143                                                call_glNormal_and_glVertex(x+1,z,top,left,height);
     144                                       
     145                                                call_glNormal_and_glVertex(x+1,z,top,left,height);
     146                                                call_glNormal_and_glVertex(x,z+1,top,left,height);
     147                                                call_glNormal_and_glVertex(x+1,z+1,top,left,height);
     148                                        }
     149                                       
     150                                        else
     151                                        {
     152                                                call_glNormal_and_glVertex(x,z,top,left,height);
     153                                                call_glNormal_and_glVertex(x,z+1,top,left,height);
     154                                                call_glNormal_and_glVertex(x+1,z+1,top,left,height);
     155                                               
     156                                                call_glNormal_and_glVertex(x+1,z,top,left,height);
     157                                                call_glNormal_and_glVertex(x,z,top,left,height);
     158                                                call_glNormal_and_glVertex(x+1,z+1,top,left,height);
     159                                        }
     160                                       
     161                                       
     162                                        // use the same trick to change the triangles everytime. once |/| and once |\|
     163                                        eightTriangles = !eightTriangles;
     164
    95165                                }
    96166                        }
    97                         glEnd();
    98167                }
    99                
     168                glEnd();
     169
    100170        }
    101171        glEndList();
     172
     173       
     174        delete[] normals;
    102175       
    103176        return true;
    104177}
    105178
    106 GLint HeightMapTerrain::heightValue(Uint8 x, Uint8 z)
    107 {
    108         return 100;
     179
     180void HeightMapTerrain::call_glNormal_and_glVertex(int x,int z,int top,int left,int height)
     181{
     182/*
     183        GLfloat color[4];           // color parameter for glMaterial
     184       
     185        int heightVal = getHeightValue(x,z);
     186
     187        color[0] = 1.0 / 256.0 * heightVal;
     188        color[1] = 1.0 / 256.0 * heightVal;
     189        color[2] = 1.0 / 256.0 * heightVal;
     190        color[3] = 1;  // alpha channel (1 = solid)
     191
     192        glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color);
     193*/
     194        glNormal3f(normals[(z-top) * height + (x-left)].x,normals[(z-top) * height + (x-left)].y,normals[(z-top) * height + (x-left)].z);
     195        glVertex3i(x,getHeightValue(x,z),z);
     196}
     197
     198
     199GLint HeightMapTerrain::getHeightValue(int x, int z)
     200{
     201        if (bitmap == NULL) return 0;
     202
     203        // catch out of image access
     204        int X = x % bitmap->w;
     205        int Z = z % bitmap->h;
    109206       
    110207        /*
    111         if (bitmap == NULL) return 0;
     208        if (X != x) cout << "x lies outside of image (" << x << "," << z << ")" << endl;
     209        if (Z != z) cout << "z lies outside of image (" << x << "," << z << ")" << endl;
     210        */
     211       
     212        // if x or z exeed the image size, return 0 as height value
     213        if (X != x || Z != z) return 0;
     214       
    112215       
    113216        Uint8 index;
    114         SDL_Color* color = NULL;
    115        
     217        SDL_Color color;
    116218       
    117219        SDL_LockSurface(bitmap);
    118        
    119         index = *((Uint8*)bitmap->pixels[z * bitmap->h + x]);
    120         color = &bitmap->format->palette->colors[index];
    121        
     220
     221        index = *((Uint8*)bitmap->pixels + Z * bitmap->pitch + X * bitmap->format->BytesPerPixel);
     222        color = bitmap->format->palette->colors[index];
     223
    122224        SDL_UnlockSurface(bitmap);
    123 
    124         return color->r;
    125          */
    126 }
     225       
     226        // return the red component, because in a grayscale pic, r,g and b are all the same value
     227        return (GLint)color.r;
     228}
  • orxonox/branches/nico/src/importer/heightMapTerrain.h

    r3384 r3390  
    1111using namespace std;
    1212
     13#include "vector.h"
    1314#include "../stdincl.h"
    14 
    15 #include "vector.h"
    16 
    1715
    1816class HeightMapTerrain
     
    3533       
    3634        // array of display lists
    37         GLuint* displayLists;
     35        //GLuint* displayLists;
    3836
    3937private:
    4038        // returns the height value between 0 and 256 for a point (x,z) in the picture
    41         GLint heightValue(Uint8 x, Uint8 z);
    42                
     39        GLint getHeightValue(int x, int z);
     40       
     41        // makes a call to glNormal and glVertex for supplied x and z
     42        void call_glNormal_and_glVertex(int x,int z,int top,int left,int height);
     43
    4344        // holds the loaded pixels
    4445        SDL_Surface* bitmap;
    4546       
     47        // holds normal vector for every vertex
     48        Vector* normals;
     49};
    4650
    47        
    48 };
     51
  • orxonox/branches/nico/src/importer/heightMapViewer.cc

    r3384 r3390  
    2121
    2222        upVector = Vector(0,1,0);
    23 
     23       
    2424        wireframe = false;
    2525        mousedown = false;
     26        smoothShading = false;
    2627       
    2728}
     
    3839        cout << "HeightMapViewer init()" << endl;
    3940       
    40         if (windowhandler.createOpenGLWindow(WIDTH,HEIGHT,FULLSCREEN) == false)
     41#ifdef FULLSCREEN
     42        if (!windowhandler.CreateGLWindow("height map viewer", WIDTH, HEIGHT, 32, true))
     43#else
     44        if (!windowhandler.CreateGLWindow("height map viewer", WIDTH, HEIGHT, 32, false))
     45#endif
    4146        {
    4247                cout << "could not create OpenGL-Window." << endl;
     
    4651        if (terrain.loadBitmap(fileName) == false)
    4752        {
    48                 cout << "could not create OpenGL-Window." << endl;
     53                cout << "could not load bitmap." << endl;
    4954                return false;
    5055        }
    5156       
    52         terrain.createDisplayLists(64, 64, 1);
     57        terrain.createDisplayLists(128, 128, 1);
    5358       
    5459        return true;
     
    7378                        {
    7479                                case SDL_MOUSEMOTION:
    75                                         if(mousedown)
     80                                        if (mousedown==true)
    7681                                        {
    77                                                 angleX = PI / 180 * event.motion.xrel * 0.1;
    78                                                 angleY = PI / 180 * event.motion.yrel * 0.1;
     82                                                angleX = PI / 180 * event.motion.xrel * 0.2;
     83                                                angleY = PI / 180 * event.motion.yrel * 0.2;
    7984
    8085                                                // Quaternion(angle,axis)
    81                                                 rotator = Quaternion(angleX,Vector(0,1,0));
     86                                                rotator = Quaternion(-angleX,Vector(0,1,0));
    8287                                                sightDirection = rotator.apply(sightDirection);
     88
     89                                        #ifdef FULLSCREEN
     90                                                rotator = Quaternion(angleY,perpendicular(sightDirection));
     91                                        #else
     92                                                rotator = Quaternion(-angleY,perpendicular(sightDirection));
     93                                        #endif
    8394                                               
    84                                                 rotator = Quaternion(-angleY,sightDirection.perpendicular());
    8595                                                sightDirection = rotator.apply(sightDirection);
    8696                                               
     
    99109                                        switch(event.key.keysym.sym)
    100110                                        {
     111                                                case SDLK_UP:
     112                                                        // move in direction of sight
     113                                                        cameraPos = cameraPos + sightDirection * 0.7;                                                   
     114                                                        updateView();
     115                                                        break;
     116
     117                                                case SDLK_DOWN:
     118                                                        // move in direction of sight
     119                                                        cameraPos = cameraPos - sightDirection * 0.7;
     120                                                        updateView();
     121                                                        break;
     122                                                       
     123                                                case SDLK_LEFT:
     124                                                        cameraPos = cameraPos + perpendicular(sightDirection) * 0.7;
     125                                                        updateView();
     126                                                        break;
     127                                                       
     128                                                case SDLK_RIGHT:
     129                                                        cameraPos = cameraPos - perpendicular(sightDirection) * 0.7;
     130                                                        updateView();
     131                                                        break;
     132                                                       
     133                                                case SDLK_s:
     134                                                        smoothShading = !smoothShading;
     135                                                        if (smoothShading)      glShadeModel(GL_SMOOTH);
     136                                                        else glShadeModel(GL_FLAT);
     137                                                        break;
     138                                                       
    101139                                                case SDLK_w:
    102140                                                        wireframe = !wireframe;
     
    105143                                                        break;
    106144                                                       
    107                                                 case SDLK_UP:
    108                                                         // move in direction of sight
    109                                                         cameraPos = cameraPos + sightDirection;
    110                                                         updateView();
    111                                                         break;
    112 
    113                                                 case SDLK_DOWN:
    114                                                         // move in direction of sight
    115                                                         cameraPos = cameraPos - sightDirection;
    116                                                         updateView();
    117                                                         break;
    118                                                        
    119                                                 case SDLK_LEFT:
    120                                                         cameraPos = cameraPos - sightDirection.perpendicular();
    121                                                         updateView();
    122                                                         break;
    123                                                        
    124                                                 case SDLK_RIGHT:
    125                                                         cameraPos = cameraPos + sightDirection.perpendicular();
    126                                                         updateView();
    127                                                         break;
    128                                                        
    129145                                                case SDLK_r:
    130                                                         // restore original view vectors
    131                                                         cameraPos = Vector(0,0,40);
    132                                                         sightDirection = Vector(0,0,-1);
     146                                                        // reset view vectors
     147                                                        //cameraPos = Vector(0,0,40);
     148                                                        //sightDirection = Vector(0,0,-1);                                                     
     149                                                        cameraPos = Vector(73.9871,172.496,286.137);
     150                                                        sightDirection = Vector(0.23429,-0.736527,-0.625574);
     151
    133152                                                        updateView();
    134153                                                        break;
     
    138157                                                        << "display list count:    " << terrain.displayListCount << endl
    139158                                                        << "first display list at: " << terrain.displayListStart << endl
     159                                                        << "camera position:       " << "(" << cameraPos.x << "," << cameraPos.y << "," << cameraPos.z << ")" << endl
     160                                                        << "sightDirection:        " << "(" << sightDirection.x << "," << sightDirection.y << "," << sightDirection.z << ")" << endl
    140161                                                        << endl;
    141162                                                       
     
    165186                SDL_GL_SwapBuffers();   
    166187
    167                 //SDL_Delay(100);
     188                SDL_Delay(1);
    168189        }
    169190       
     
    175196        // be sure to use up to date lookAt-vector
    176197        lookAt = cameraPos + sightDirection;
    177         upVector = sightDirection.cross(sightDirection.perpendicular()) * -1;
     198        upVector = sightDirection.cross(perpendicular(sightDirection));
    178199       
    179200        glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
     
    181202       
    182203        // Calculate The Aspect Ratio Of The Window
    183         gluPerspective(45.0f,(GLfloat)WIDTH/(GLfloat)HEIGHT,0.1f,100.0f);
     204        gluPerspective(45.0f,(GLfloat)WIDTH/(GLfloat)HEIGHT,0.5f,300.0f);
    184205        gluLookAt (cameraPos.x,cameraPos.y,cameraPos.z,
    185206                           lookAt.x,lookAt.y,lookAt.z,
     
    191212
    192213void HeightMapViewer::drawScene()
    193 {
     214{       
    194215        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    195216
    196         glutWireCube(1.0);
    197        
    198         int i;
    199        
    200         for (i=0; i<terrain.displayListCount; i++)
    201         {
    202                 glCallList(terrain.displayListStart + i);
    203         }
    204 }
     217        glPushMatrix();
     218        {
     219                //glTranslatef(0.0,1.0,0.0);
     220                glScalef(1,0.2,1);
     221       
     222                for (int i=0; i<terrain.displayListCount; i++)
     223                {
     224                        glCallList(terrain.displayListStart + i);
     225                }
     226        }
     227        glPopMatrix();
     228       
     229        glColor3f(0.0,1.0,0.0);
     230        //      glutWireCube(1.0);
     231       
     232        glColor3f(0.0,0.0,1.0);
     233        glBegin(GL_POINTS);
     234        for (float z=0;z<10;z++)
     235                for (float x=0;x<10;x++)
     236                        glVertex3f(x,0,z);
     237        glEnd();
     238       
     239}
     240
     241
     242
     243/**
     244   \brief returns a vector that is perpendicular and lies in the xz-plane
     245 */
     246Vector perpendicular (Vector perpendic)
     247{
     248        Vector r;
     249       
     250        r.x =  perpendic.z;
     251        r.z = -perpendic.x;
     252       
     253        r.normalize();
     254       
     255        return r;       
     256}
  • orxonox/branches/nico/src/importer/heightMapViewer.h

    r3384 r3390  
    1818#include "../stdincl.h"
    1919
     20
    2021#define WIDTH        640
    2122#define HEIGHT       480
    22 #define FULLSCREEN   false
    2323
    24 /*
    25 #define WIDTH        1280
    26 #define HEIGHT       854
    27 #define FULLSCREEN   true
    28 */
     24//#define WIDTH        1280
     25//#define HEIGHT       854
     26//#define FULLSCREEN
     27
    2928
    3029class HeightMapViewer
     
    4645        bool wireframe;
    4746        bool mousedown;
     47        bool smoothShading;
    4848
    4949        Vector cameraPos;
     
    5656        Quaternion rotator;
    5757};
     58
     59Vector perpendicular (Vector perpendic);
Note: See TracChangeset for help on using the changeset viewer.