Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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}
Note: See TracChangeset for help on using the changeset viewer.