Ejemplo n.º 1
0
bool Terrain::loadHeightmap(const string& rawFile, int width) 
{
    const float HEIGHT_SCALE = 10.0f; 
    std::ifstream fileIn(rawFile.c_str(), std::ios::binary);

    if (!fileIn.good()) 
    {
        std::cout << "File does not exist" << std::endl;
        return false;
    }

    
    string stringBuffer(std::istreambuf_iterator<char>(fileIn), (std::istreambuf_iterator<char>()));

    fileIn.close();

    if (stringBuffer.size() != (width * width)) 
    {
        std::cout << "Image size does not match passed width" << std::endl;
        return false;
    }

    vector<float> heights;
    heights.reserve(width * width); 

    
    for (int i = 0; i < (width * width); ++i) 
    {
        
        float value = (float)(unsigned char)stringBuffer[i] / 256.0f; 
    
        heights.push_back(value * HEIGHT_SCALE);
        m_colors.push_back(Color(value, value, value));
    }

    glGenBuffers(1, &m_colorBuffer); 
    glBindBuffer(GL_ARRAY_BUFFER, m_colorBuffer); 
    glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * m_colors.size() * 3, &m_colors[0], GL_STATIC_DRAW); 

    generateVertices(heights, width);
    generateIndices(width);
    generateTexCoords(width);
    generateNormals();

    generateWaterVertices(width);
    generateWaterIndices(width);
    generateWaterTexCoords(width);

    m_width = width;
    return true;
}
Ejemplo n.º 2
0
bool GLFont::createFontBitmap()
{
    // The font is drawn as a 10 x 10 grid of characters.

    if (!extractFontMetrics())
        return false;

    int w = 10 * m_cellWidth;
    int h = 10 * m_cellHeight;
    Bitmap bitmap;

    if (!bitmap.create(nextPower2(10 * m_cellWidth), nextPower2(10 * m_cellHeight)))
        return false;

    int x = 0;
    int y = 0;
    int ch = 32;
    HFONT hPrevFont = 0;
    COLORREF prevColor = 0;
    RECT rc = {0, 0, bitmap.width, bitmap.height};

    bitmap.selectObject();
    hPrevFont = reinterpret_cast<HFONT>(SelectObject(bitmap.dc, m_hFont));
    prevColor = SetTextColor(bitmap.dc, RGB(255,255,255));
    SetBkMode(bitmap.dc, TRANSPARENT);
    FillRect(bitmap.dc, &rc, reinterpret_cast<HBRUSH>(GetStockObject(BLACK_BRUSH)));

    for (int i = 0; i < 10; ++i)
    {
        y = m_cellHeight * i;

        for (int j = 0; j < 10; ++j)
        {
            x = m_cellWidth * j;

            if (ch > 31 && ch < 127)
                TextOut(bitmap.dc, x, y, reinterpret_cast<LPCSTR>(&ch), 1);

            ++ch;
        }
    }

    SetTextColor(bitmap.dc, prevColor);
    SelectObject(bitmap.dc, hPrevFont);
    bitmap.deselectObject();

    generateTexCoords(bitmap);
    return createTexture(bitmap);
}
Ejemplo n.º 3
0
void GLHeightMap::build(RawHeightMap heightmap) {
    m_heightmap = heightmap;
    vertexNumber = heightmap.getWidth()*(heightmap.getHeight()-1)*2;
    vertexes = new Vertex[vertexNumber];
    float range = m_heightmap.getMaxHeight() - m_heightmap.getMinHeight();
    for (int i = 0; i < heightmap.getHeight(); i++) {
        for (int j = 0; j < heightmap.getWidth(); j++) {
            unsigned short int value = heightmap.getData()[i*heightmap.getWidth()+j];
            int offset = i*heightmap.getWidth()+j;
            vertexes[offset].x = i * m_scaleFactor * 8 * 1/m_heightmap.getRatio();
            vertexes[offset].y = (m_heightmap.getMinHeight()+value*range/MAX_SHORT)*m_scaleFactor;
            vertexes[offset].z = j * m_scaleFactor * 8 * 1/m_heightmap.getRatio();
        }
    }
    computeNormals();
    generateIndexes();
    generateTexCoords();
}
Ejemplo n.º 4
0
antPlane::antPlane
( float width, float depth, int nColumns, int nRows,
 int nVbo, bool genColors,
 bool genNormals, bool genTexCoords ) :
antDrawable( nVbo, ATTR_POSITION ),
m_width( width ),
m_depth( depth ),
m_nColumns( nColumns ),
m_nRows( nRows )
{
    m_nVertices = ( m_nColumns * m_nRows * 6 );

    antRGBA color1( 0.f, 0.f, 0.f, 1.f );
    antRGBA color2( 1.f, 1.f, 1.f, 1.f );
    
    generateVertices();
    
    if ( genColors ) { generateColors( color1, color2 ); }
    if ( genNormals ) { generateNormals(); }
    if ( genTexCoords ) { generateTexCoords(); }    
}