Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/heightMap/src/lib/graphics/importer/heightmap.cc @ 4093

Last change on this file since 4093 was 4093, checked in by nico, 19 years ago

branches/heightMap: challenging the model class

File size: 2.2 KB
Line 
1
2
3#include "heightmap.h"
4
5#include <stdio.h>
6#include <string.h>
7#include <stdlib.h>
8
9#include "debug.h"
10#include "compiler.h"
11
12
13Heightmap::Heightmap(const char* fileName, float scaling, int displaylistResolution, int vertexResolution)
14{
15        this->scaleFactor = scaling;
16        this->setName(fileName);
17       
18        this->import(fileName, displaylistResolution, vertexResolution);
19       
20        this->finalize();
21}
22
23Heightmap::~Heightmap ()
24{
25}
26
27bool Heightmap::import (const char* fileName, int displaylistResolution, int vertexResolution)
28{
29        PRINTF(4)("preparing to read in file: %s\n", fileName);
30       
31        // using SDL-image to load the bitmap
32        this->bitmap = IMG_Load(fileName);
33
34        // how many pixels wide is a display list?
35        int pixPerDLSide = displaylistResolution * vertexResolution;
36
37        int howManyListsWide = bitmap->w / pixPerDLSide + 1;
38        int howManyListsHigh = bitmap->h / pixPerDLSide + 1;
39       
40        // calculated pixel dimensions of one display list. usually equals pixPerDLSide, but at the end of the picture
41        // this needs to be shorter, that we do not make a segfault
42        int width = pixPerDLSide;
43        int height = pixPerDLSide;
44       
45        for (int i=0; i<howManyListsWide; i++)
46        {
47                for (int j=0; j<howManyListsHigh; j++)
48                {
49                        // catch out of image access in x-direction
50                        if ( (j+1)*pixPerDLSide - 1 >= bitmap->w )      width = bitmap->w - j*pixPerDLSide;
51                        else                                                                            width = pixPerDLSide;
52                        // y-direction
53                        if ( (i+1)*pixPerDLSide - 1 >= bitmap->h )      height = bitmap->h - i*pixPerDLSide;
54                        else                                                                            height = pixPerDLSide;
55                       
56                        this->readIntoGroup(j*pixPerDLSide,i*pixPerDLSide,width,height,vertexResolution);
57                }
58        }
59       
60        return true;
61}
62
63void Heightmap::readIntoGroup(int left, int top,int width, int height, int vertexResolution)
64{
65        //this->newGroup();
66       
67        for (int z=top; z < top+height; z++)
68        {
69                for (int x=left; x < left+width; x++)
70                {
71                        this->addVertex(x,this->getHeightAt(x,z),z);
72                }
73        }
74}
75
76float Heightmap::getHeightAt(int x, int z)
77{
78        Uint8 index;
79        SDL_Color color;
80       
81        SDL_LockSurface(bitmap);
82       
83        index = *((Uint8*)bitmap->pixels + z * bitmap->pitch + x * bitmap->format->BytesPerPixel);
84        color = bitmap->format->palette->colors[index];
85       
86        SDL_UnlockSurface(bitmap);
87       
88        // return the red component, because in a grayscale pic, r,g and b are all the same value
89        return (GLint)color.r;
90}
Note: See TracBrowser for help on using the repository browser.