void DrawNode::drawTriangle(const Vec2& p1, const Vec2& p2, const Vec2& p3, const Color4F& color)
{
    Color4B col(color);
    _vdTriangles->append<V2F_C4B_T2F>({Vec2(p1.x, p1.y), col, Tex2F(0.f, 0.f)});
    _vdTriangles->append<V2F_C4B_T2F>({Vec2(p2.x, p2.y), col, Tex2F(0.f, 0.f)});
    _vdTriangles->append<V2F_C4B_T2F>({Vec2(p3.x, p3.y), col, Tex2F(0.f, 0.f)});
}
Exemple #2
0
void Triangle::drawTriangle()
{
    clear();
    ensureCapacity(3);
    
    _buffer[0] = {Vec3(0.0f,100.0f,0.0f), Tex2F(0.5f, 0.0f)};
    _buffer[1] = {Vec3(-100.0f,-100.0f,0.0f), Tex2F(0.0f, 1.0f)};
    _buffer[2] = {Vec3(100.0f,-100.0f,0.0f), Tex2F(1.0f, 1.0f)};
    
    _bufferCount += 3;
    _dirty = true;
}
void DrawNode::drawDot(const Vec2 &pos, float radius, const Color4F &color)
{
    V2F_C4B_T2F a = {Vec2(pos.x - radius, pos.y - radius), Color4B(color), Tex2F(-1.0, -1.0) };
    V2F_C4B_T2F b = {Vec2(pos.x - radius, pos.y + radius), Color4B(color), Tex2F(-1.0,  1.0) };
    V2F_C4B_T2F c = {Vec2(pos.x + radius, pos.y + radius), Color4B(color), Tex2F( 1.0,  1.0) };
    V2F_C4B_T2F d = {Vec2(pos.x + radius, pos.y - radius), Color4B(color), Tex2F( 1.0, -1.0) };
    _vdTriangles->append<V2F_C4B_T2F>(a);
    _vdTriangles->append<V2F_C4B_T2F>(b);
    _vdTriangles->append<V2F_C4B_T2F>(c);
    _vdTriangles->append<V2F_C4B_T2F>(a);
    _vdTriangles->append<V2F_C4B_T2F>(c);
    _vdTriangles->append<V2F_C4B_T2F>(d);
}
void MGRDrawNode::drawLine(const Vec2& origin, const Vec2& destination, const Color4F& color)
{
    ensureCapacityGLLine(2);

    V2F_C4B_T2F* point = (V2F_C4B_T2F*)_bufferGLLine + _bufferCountGLLine;
    V2F_C4B_T2F a = {origin, Color4B(color), Tex2F(0.0, 0.0)};
    V2F_C4B_T2F b = {destination, Color4B(color), Tex2F(0.0, 0.0)};

    *point = a;
    *(point + 1) = b;

    _bufferCountGLLine += 2;
    _dirtyGLLine = true;
}
void ShaderDemoNode::loadData()
{
    float w = getContentSize().width;
    float h = getContentSize().height;
    assert(w != 0 && h != 0);
    
    V2F_C4B_T2F vertexlist[] = {
        {Vec2(0.0f,0.0f),Color4B::RED,Tex2F(0.0f,0.0f)},
        {Vec2(w,0.0f),Color4B::GREEN,Tex2F(0.0f,0.0f)},
        {Vec2(w,h),Color4B::BLUE,Tex2F(0.0f,0.0f)},
        {Vec2(0.0f,h),Color4B::YELLOW,Tex2F(0.0f,0.0f)}
    };
    
    GLubyte indices[] = {
        0,1,2,
        0,2,3
    };
    
    assert (Configuration::getInstance()->supportsShareableVAO());
    
    // vao
    glGenVertexArrays(1, &m_vao);
    GL::bindVAO(m_vao);
    
    // vbo
    glGenBuffers(1, &m_vbo);
    glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertexlist), vertexlist, GL_STREAM_DRAW);
    // vbo input params
    // vertex
    glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION);
    glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, vertices));
    // color
    glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_COLOR);
    glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, colors));
    // texcood
    glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORD);
    glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, texCoords));
    
    // vbo index
    glGenBuffers(1, &m_indexVBO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_indexVBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices) , indices, GL_STATIC_DRAW);
    
    // clear
    glBindVertexArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    
    CHECK_GL_ERROR_DEBUG();
}
Exemple #6
0
void RoadNode::drawLine(const cocos2d::Vec3& src, const cocos2d::Vec3& dest)
{
    ensureCapacityGLLine(2);
    V3F_T2F *point = (V3F_T2F*)(_bufferGLLine + _bufferCountGLLine);

    V3F_T2F a = {src,Tex2F(0.0, 0.0)};
    V3F_T2F b = {dest,Tex2F(0.0, 0.0)};

    *point = a;
    *(point+1) = b;

    _bufferCountGLLine += 2;
    _dirtyGLLine = true;
}
void DrawNode::drawPoly(const Vec2* poli, unsigned int numberOfPoints, bool closePolygon, const Color4F& color)
{
    unsigned int i = 0;
    for(; i < numberOfPoints - 1; ++i)
    {
        _vdLines->append<V2F_C4B_T2F>({poli[i],   Color4B(color), Tex2F()});
        _vdLines->append<V2F_C4B_T2F>({poli[i+1], Color4B(color), Tex2F()});
    }

    if (closePolygon)
    {
        _vdLines->append<V2F_C4B_T2F>({poli[i], Color4B(color), Tex2F()});
        _vdLines->append<V2F_C4B_T2F>({poli[0], Color4B(color), Tex2F()});
    }
}
void DrawNode::drawTriangle3c(const Vec2 &p1, const Vec2 &p2, const Vec2 &p3, const Color4F &c1, const Color4F &c2, const Color4F &c3) {
    unsigned int vertex_count = 2*3;
    ensureCapacity(vertex_count);

    V2F_C4B_T2F a = {Vec2(p1.x, p1.y), Color4B(c1), Tex2F(0.0, 0.0) };
    V2F_C4B_T2F b = {Vec2(p2.x, p2.y), Color4B(c2), Tex2F(0.0,  0.0) };
    V2F_C4B_T2F c = {Vec2(p3.x, p3.y), Color4B(c3), Tex2F(0.0,  0.0) };

    V2F_C4B_T2F_Triangle *triangles = (V2F_C4B_T2F_Triangle *)(_buffer + _bufferCount);
    V2F_C4B_T2F_Triangle triangle = {a, b, c};
    triangles[0] = triangle;

    _bufferCount += vertex_count;
    _dirty = true;
}
void DrawNode::drawQuadraticBezier(const Vec2& from, const Vec2& control, const Vec2& to, unsigned int segments, const Color4F &color)
{
    unsigned int vertex_count = (segments + 1) * 3;
    ensureCapacity(vertex_count);

    Tex2F texCoord = Tex2F(0.0, 0.0);
    Color4B col = Color4B(color);
    Vec2 vertex;
    Vec2 firstVertex = Vec2(from.x, from.y);
    Vec2 lastVertex = Vec2(to.x, to.y);

    float t = 0;
    for(unsigned int i = segments + 1; i > 0; i--)
    {
        float x = powf(1 - t, 2) * from.x + 2.0f * (1 - t) * t * control.x + t * t * to.x;
        float y = powf(1 - t, 2) * from.y + 2.0f * (1 - t) * t * control.y + t * t * to.y;
        vertex = Vec2(x, y);

        V2F_C4B_T2F a = {firstVertex, col, texCoord };
        V2F_C4B_T2F b = {lastVertex, col, texCoord };
        V2F_C4B_T2F c = {vertex, col, texCoord };
        V2F_C4B_T2F_Triangle triangle = {a, b, c};
        ((V2F_C4B_T2F_Triangle *)(_buffer + _bufferCount))[0] = triangle;

        lastVertex = vertex;
        t += 1.0f / segments;
        _bufferCount += 3;
    }
    _dirty = true;
}
V3F_C4B_T2F_Quad GameLayerMapBorder::createQuadByBorderCell(const GridCell& cell,BorderMaskType _type,float bHeight)
{
    V3F_C4B_T2F_Quad t;
//    static Color4B   colors[]=
//    {
//        Color4B(rand()%255,rand()%255,rand()%255,255),
//        Color4B(rand()%255,rand()%255,rand()%255,255),
//        Color4B(rand()%255,rand()%255,rand()%255,255),
//        Color4B(rand()%255,rand()%255,rand()%255,255),
//        Color4B(rand()%255,rand()%255,rand()%255,255),
//        Color4B(rand()%255,rand()%255,rand()%255,255),
//        Color4B(rand()%255,rand()%255,rand()%255,255),
//        Color4B(rand()%255,rand()%255,rand()%255,255),
//        Color4B(rand()%255,rand()%255,rand()%255,255),
//        Color4B(rand()%255,rand()%255,rand()%255,255),
//        Color4B(rand()%255,rand()%255,rand()%255,255),
//        Color4B(rand()%255,rand()%255,rand()%255,255),
//        Color4B(rand()%255,rand()%255,rand()%255,255),
//        Color4B(rand()%255,rand()%255,rand()%255,255),
//        Color4B(rand()%255,rand()%255,rand()%255,255),
//        Color4B(rand()%255,rand()%255,rand()%255,255),
//        Color4B(rand()%255,rand()%255,rand()%255,255),
//        Color4B(rand()%255,rand()%255,rand()%255,255),
//        Color4B(rand()%255,rand()%255,rand()%255,255),
//    };
    float step =1/_quads->getTexture()->getContentSize().height;
    float h = 8.0f;
    Color4B color =Color4B::WHITE;//  Color4B(rand()%255,rand()%255,rand()%255,255);
    t.bl.texCoords = Tex2F(0,bHeight*step-1);
    t.bl.colors = color;//   Color4B::WHITE;
    t.br.texCoords = Tex2F(1,bHeight*step-1);
    t.br.colors = color;// Color4B::WHITE;
    t.tl.texCoords = Tex2F(0,(bHeight+h)*step-1);
    t.tl.colors = color;//Color4B::WHITE;
    t.tr.texCoords = Tex2F(1,(bHeight+h)*step-1);
    t.tr.colors = color;// Color4B::WHITE;
    
    QuadVec vec = _sQuadVecMap.find(_type)->second;
    auto p = _mapGrid->getPositionByMapGridCell(cell);
    Vec3 v(p.x,p.y,0);
    t.bl.vertices = vec.lb + v;
    t.tl.vertices = vec.lt + v;
    t.br.vertices = vec.rb + v;
    t.tr.vertices = vec.rt + v;
    
    return t;
}
void MGRDrawNode::drawTriangle(const Vec2& p1, const Vec2& p2, const Vec2& p3, const Color4F& color)
{
    unsigned int vertex_count = 3;
    ensureCapacity(vertex_count);

    Color4B col = Color4B(color);
    V2F_C4B_T2F a = {p1, col, Tex2F(0.0, 0.0)};
    V2F_C4B_T2F b = {p2, col, Tex2F(0.0, 0.0)};
    V2F_C4B_T2F c = {p3, col, Tex2F(0.0, 0.0)};

    V2F_C4B_T2F_Triangle* triangles = (V2F_C4B_T2F_Triangle*)(_buffer + _bufferCount);
    V2F_C4B_T2F_Triangle triangle = {a, b, c};
    triangles[0] = triangle;

    _bufferCount += vertex_count;
    _dirty = true;
}
Exemple #12
0
void GridNode::setUpBuffer(const Vec2 &lb, const Vec2 &lt, const Vec2 &rt, const Vec2& rb, const Color4B &color)
{
    unsigned int vertex_count = 2*3;
    ensureCapacity(vertex_count);
    
    V2F_C4B_T2F a = {lb, Color4B(color), Tex2F( 0.0,  1.0) };
    V2F_C4B_T2F b = {lt, Color4B(color), Tex2F( 0.0,  0.0) };
    V2F_C4B_T2F c = {rt, Color4B(color), Tex2F( 1.0,  0.0) };
    V2F_C4B_T2F d = {rb, Color4B(color), Tex2F( 1.0,  1.0) };
    
    V2F_C4B_T2F_Triangle *triangles = (V2F_C4B_T2F_Triangle *)(_buffer + _bufferCount);
    V2F_C4B_T2F_Triangle triangle0 = {a, b, c};
    V2F_C4B_T2F_Triangle triangle1 = {a, c, d};
    triangles[0] = triangle0;
    triangles[1] = triangle1;
    
    _bufferCount += vertex_count;
}
bool CameraBackgroundDepthBrush::init()
{
    auto shader = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_CAMERA_CLEAR);
    _glProgramState = GLProgramState::getOrCreateWithGLProgram(shader);
    _glProgramState->retain();

    _quad.bl.vertices = Vec3(-1,-1,0);
    _quad.br.vertices = Vec3(1,-1,0);
    _quad.tl.vertices = Vec3(-1,1,0);
    _quad.tr.vertices = Vec3(1,1,0);

    _quad.bl.colors = _quad.br.colors = _quad.tl.colors = _quad.tr.colors = Color4B(0,0,0,1);

    _quad.bl.texCoords = Tex2F(0,0);
    _quad.br.texCoords = Tex2F(1,0);
    _quad.tl.texCoords = Tex2F(0,1);
    _quad.tr.texCoords = Tex2F(1,1);
    return true;
}
Exemple #14
0
void DrawNode::drawDot(const Vec2 &pos, float radius, const Color4F &color)
{
    unsigned int vertex_count = 2*3;
    ensureCapacity(vertex_count);
    
    V2F_C4B_T2F a = {Vec2(pos.x - radius, pos.y - radius), Color4B(color), Tex2F(-1.0, -1.0) };
    V2F_C4B_T2F b = {Vec2(pos.x - radius, pos.y + radius), Color4B(color), Tex2F(-1.0,  1.0) };
    V2F_C4B_T2F c = {Vec2(pos.x + radius, pos.y + radius), Color4B(color), Tex2F( 1.0,  1.0) };
    V2F_C4B_T2F d = {Vec2(pos.x + radius, pos.y - radius), Color4B(color), Tex2F( 1.0, -1.0) };
    
    V2F_C4B_T2F_Triangle *triangles = (V2F_C4B_T2F_Triangle *)(_buffer + _bufferCount);
    V2F_C4B_T2F_Triangle triangle0 = {a, b, c};
    V2F_C4B_T2F_Triangle triangle1 = {a, c, d};
    triangles[0] = triangle0;
    triangles[1] = triangle1;
    
    _bufferCount += vertex_count;
    
    _dirty = true;
}
Exemple #15
0
void DrawNode::drawPoint(const Vec2& position, const float pointSize, const Color4F &color)
{
    ensureCapacityGLPoint(1);
    
    V2F_C4B_T2F *point = (V2F_C4B_T2F*)(_bufferGLPoint + _bufferCountGLPoint);
    V2F_C4B_T2F a = {position, Color4B(color), Tex2F(pointSize,0)};
    *point = a;
    
    _bufferCountGLPoint += 1;
    _dirtyGLPoint = true;
}
Exemple #16
0
void DrawNode::drawPoly(const Vec2 *poli, unsigned int numberOfPoints, bool closePolygon, const Color4F &color)
{
    unsigned int vertext_count;
    if(closePolygon)
    {
        vertext_count = 2 * numberOfPoints;
        ensureCapacityGLLine(vertext_count);
    }
    else
    {
        vertext_count = 2 * (numberOfPoints - 1);
        ensureCapacityGLLine(vertext_count);
    }
    
    V2F_C4B_T2F *point = (V2F_C4B_T2F*)(_bufferGLLine + _bufferCountGLLine);
 
    unsigned int i = 0;
    for(; i<numberOfPoints-1; i++)
    {
        V2F_C4B_T2F a = {poli[i], Color4B(color), Tex2F(0.0, 0.0)};
        V2F_C4B_T2F b = {poli[i+1], Color4B(color), Tex2F(0.0, 0.0)};
        
        *point = a;
        *(point+1) = b;
        point += 2;
    }
    if(closePolygon)
    {
        V2F_C4B_T2F a = {poli[i], Color4B(color), Tex2F(0.0, 0.0)};
        V2F_C4B_T2F b = {poli[0], Color4B(color), Tex2F(0.0, 0.0)};
        *point = a;
        *(point+1) = b;
    }
    
    _bufferCountGLLine += vertext_count;
}
Exemple #17
0
void DrawNode::drawPoints(const Vec2 *position, unsigned int numberOfPoints, const float pointSize, const Color4F &color)
{
    ensureCapacityGLPoint(numberOfPoints);
    
    V2F_C4B_T2F *point = (V2F_C4B_T2F*)(_bufferGLPoint + _bufferCountGLPoint);
    
    for(unsigned int i=0; i < numberOfPoints; i++,point++)
    {
        V2F_C4B_T2F a = {position[i], Color4B(color), Tex2F(pointSize,0)};
        *point = a;
    }
    
    _bufferCountGLPoint += numberOfPoints;
    _dirtyGLPoint = true;
}
///
//    @returns the vertex position from the texture coordinate
///
Tex2F ProgressTimer::textureCoordFromAlphaPoint(Vec2 alpha)
{
    Tex2F ret(0.0f, 0.0f);
    if (!_sprite) {
        return ret;
    }
    V3F_C4B_T2F_Quad quad = _sprite->getQuad();
    Vec2 min(quad.bl.texCoords.u,quad.bl.texCoords.v);
    Vec2 max(quad.tr.texCoords.u,quad.tr.texCoords.v);
    //  Fix bug #1303 so that progress timer handles sprite frame texture rotation
    if (_sprite->isTextureRectRotated()) {
        std::swap(alpha.x, alpha.y);
    }
    return Tex2F(min.x * (1.f - alpha.x) + max.x * alpha.x, min.y * (1.f - alpha.y) + max.y * alpha.y);
}
Exemple #19
0
void MotionStreak::update(float delta)
{
    if (!_startingPositionInitialized)
    {
        return;
    }

    delta *= _fadeDelta;

    unsigned int newIdx, newIdx2, i, i2;
    unsigned int mov = 0;

    // Update current points
    for(i = 0; i<_nuPoints; i++)
    {
        _pointState[i]-=delta;

        if(_pointState[i] <= 0)
            mov++;
        else
        {
            newIdx = i-mov;

            if(mov>0)
            {
                // Move data
                _pointState[newIdx] = _pointState[i];

                // Move point
                _pointVertexes[newIdx] = _pointVertexes[i];

                // Move vertices
                i2 = i*2;
                newIdx2 = newIdx*2;
                _vertices[newIdx2] = _vertices[i2];
                _vertices[newIdx2+1] = _vertices[i2+1];

                // Move color
                i2 *= 4;
                newIdx2 *= 4;
                _colorPointer[newIdx2+0] = _colorPointer[i2+0];
                _colorPointer[newIdx2+1] = _colorPointer[i2+1];
                _colorPointer[newIdx2+2] = _colorPointer[i2+2];
                _colorPointer[newIdx2+4] = _colorPointer[i2+4];
                _colorPointer[newIdx2+5] = _colorPointer[i2+5];
                _colorPointer[newIdx2+6] = _colorPointer[i2+6];
            } else
                newIdx2 = newIdx*8;

            const GLubyte op = (GLubyte)(_pointState[newIdx] * 255.0f);
            _colorPointer[newIdx2+3] = op;
            _colorPointer[newIdx2+7] = op;
        }
    }
    _nuPoints-=mov;

    // Append new point
    bool appendNewPoint = true;
    if(_nuPoints >= _maxPoints)
    {
        appendNewPoint = false;
    }

    else if(_nuPoints>0)
    {
        bool a1 = _pointVertexes[_nuPoints-1].getDistanceSq(_positionR) < _minSeg;
        bool a2 = (_nuPoints == 1) ? false : (_pointVertexes[_nuPoints-2].getDistanceSq(_positionR)< (_minSeg * 2.0f));
        if(a1 || a2)
        {
            appendNewPoint = false;
        }
    }

    if(appendNewPoint)
    {
        _pointVertexes[_nuPoints] = _positionR;
        _pointState[_nuPoints] = 1.0f;

        // Color assignment
        const unsigned int offset = _nuPoints*8;
        *((Color3B*)(_colorPointer + offset)) = _displayedColor;
        *((Color3B*)(_colorPointer + offset+4)) = _displayedColor;

        // Opacity
        _colorPointer[offset+3] = 255;
        _colorPointer[offset+7] = 255;

        // Generate polygon
        if(_nuPoints > 0 && _fastMode )
        {
            if(_nuPoints > 1)
            {
                ccVertexLineToPolygon(_pointVertexes, _stroke, _vertices, _nuPoints, 1);
            }
            else
            {
                ccVertexLineToPolygon(_pointVertexes, _stroke, _vertices, 0, 2);
            }
        }

        _nuPoints ++;
    }

    if( ! _fastMode )
    {
        ccVertexLineToPolygon(_pointVertexes, _stroke, _vertices, 0, _nuPoints);
    }

    // Updated Tex Coords only if they are different than previous step
    if( _nuPoints  && _previousNuPoints != _nuPoints ) {
        float texDelta = 1.0f / _nuPoints;
        for( i=0; i < _nuPoints; i++ ) {
            _texCoords[i*2] = Tex2F(0, texDelta*i);
            _texCoords[i*2+1] = Tex2F(1, texDelta*i);
        }

        _previousNuPoints = _nuPoints;
    }
}
//------------------------------------------------------------------
//
// Atlas1
//
//------------------------------------------------------------------
Atlas1::Atlas1()
{
    _textureAtlas = TextureAtlas::create(s_AtlasTest, 3); _textureAtlas->retain();
    
    auto s = Director::getInstance()->getWinSize();

    //
    // Notice: u,v tex coordinates are inverted
    //
    V3F_C4B_T2F_Quad quads[] = 
    {
        {
            {Vertex3F(0,0,0),Color4B(0,0,255,255),Tex2F(0.0f,1.0f),},                // bottom left
            {Vertex3F(s.width,0,0),Color4B(0,0,255,0),Tex2F(1.0f,1.0f),},            // bottom right
            {Vertex3F(0,s.height,0),Color4B(0,0,255,0),Tex2F(0.0f,0.0f),},            // top left
            {Vertex3F(s.width,s.height,0),Color4B(0,0,255,255),Tex2F(1.0f,0.0f),},    // top right
        },        
        {
            {Vertex3F(40,40,0),Color4B(255,255,255,255),Tex2F(0.0f,0.2f),},            // bottom left
            {Vertex3F(120,80,0),Color4B(255,0,0,255),Tex2F(0.5f,0.2f),},            // bottom right
            {Vertex3F(40,160,0),Color4B(255,255,255,255),Tex2F(0.0f,0.0f),},        // top left
            {Vertex3F(160,160,0),Color4B(0,255,0,255),Tex2F(0.5f,0.0f),},            // top right
        },

        {
            {Vertex3F(s.width/2,40,0),Color4B(255,0,0,255),Tex2F(0.0f,1.0f),},        // bottom left
            {Vertex3F(s.width,40,0),Color4B(0,255,0,255),Tex2F(1.0f,1.0f),},        // bottom right
            {Vertex3F(s.width/2-50,200,0),Color4B(0,0,255,255),Tex2F(0.0f,0.0f),},        // top left
            {Vertex3F(s.width,100,0),Color4B(255,255,0,255),Tex2F(1.0f,0.0f),},        // top right
        },
        
    };
    
    
    for( int i=0;i<3;i++) 
    {
        _textureAtlas->updateQuad(&quads[i], i);
    }
}
TrianglesCommand::Triangles AutoPolygon::triangulate(const std::vector<Vec2>& points)
{
    // if there are less than 3 points, then we can't triangulate
    if(points.size()<3)
    {
        log("AUTOPOLYGON: cannot triangulate %s with less than 3 points", _filename.c_str());
        return TrianglesCommand::Triangles();
    }
    std::vector<p2t::Point*> p2points;
    for(std::vector<Vec2>::const_iterator it = points.begin(); it<points.end(); it++)
    {
        p2t::Point * p = new p2t::Point(it->x, it->y);
        p2points.push_back(p);
    }
    p2t::CDT cdt(p2points);
    cdt.Triangulate();
    std::vector<p2t::Triangle*> tris = cdt.GetTriangles();
    
    V3F_C4B_T2F* verts= new V3F_C4B_T2F[points.size()];
    unsigned short* indices = new unsigned short[tris.size()*3];
    unsigned short idx = 0;
    unsigned short vdx = 0;

    for(std::vector<p2t::Triangle*>::const_iterator ite = tris.begin(); ite < tris.end(); ite++)
    {
        for(int i = 0; i < 3; i++)
        {
            auto p = (*ite)->GetPoint(i);
            auto v3 = Vec3(p->x, p->y, 0);
            bool found = false;
            size_t j;
            size_t length = vdx;
            for(j = 0; j < length; j++)
            {
                if(verts[j].vertices == v3)
                {
                    found = true;
                    break;
                }
            }
            if(found)
            {
                //if we found the same vertex, don't add to verts, but use the same vertex with indices
                indices[idx] = j;
                idx++;
            }
            else
            {
                //vert does not exist yet, so we need to create a new one,
                auto c4b = Color4B::WHITE;
                auto t2f = Tex2F(0,0); // don't worry about tex coords now, we calculate that later
                V3F_C4B_T2F vert = {v3,c4b,t2f};
                verts[vdx] = vert;
                indices[idx] = vdx;
                idx++;
                vdx++;
            }
        }
    }
    for(auto j : p2points)
    {
        delete j;
    };
    TrianglesCommand::Triangles triangles = {verts, indices, vdx, idx};
    return triangles;
}
Exemple #22
0
void Camera::clearBackground(float depth)
{
    GLboolean oldDepthTest;
    GLint oldDepthFunc;
    GLboolean oldDepthMask;
    {
        glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
        glStencilMask(0);

        oldDepthTest = glIsEnabled(GL_DEPTH_TEST);
        glGetIntegerv(GL_DEPTH_FUNC, &oldDepthFunc);
        glGetBooleanv(GL_DEPTH_WRITEMASK, &oldDepthMask);

        glDepthMask(GL_TRUE);
        glEnable(GL_DEPTH_TEST);
        glDepthFunc(GL_ALWAYS);
    }

    //draw
    static V3F_C4B_T2F_Quad quad;
    quad.bl.vertices = Vec3(-1,-1,0);
    quad.br.vertices = Vec3(1,-1,0);
    quad.tl.vertices = Vec3(-1,1,0);
    quad.tr.vertices = Vec3(1,1,0);
    
    quad.bl.colors = quad.br.colors = quad.tl.colors = quad.tr.colors = Color4B(0,0,0,1);
    
    quad.bl.texCoords = Tex2F(0,0);
    quad.br.texCoords = Tex2F(1,0);
    quad.tl.texCoords = Tex2F(0,1);
    quad.tr.texCoords = Tex2F(1,1);
    
    auto shader = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_CAMERA_CLEAR);
    auto programState = GLProgramState::getOrCreateWithGLProgram(shader);
    programState->setUniformFloat("depth", 1.0);
    programState->apply(Mat4());
    GLshort indices[6] = {0, 1, 2, 3, 2, 1};
    
    {
        GL::bindVAO(0);
        glBindBuffer(GL_ARRAY_BUFFER, 0);
        
        GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX);
        
        // vertices
        glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B_T2F), &quad.tl.vertices);
        
        // colors
        glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V3F_C4B_T2F), &quad.tl.colors);
        
        // tex coords
        glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B_T2F), &quad.tl.texCoords);
        
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
    }

    
    {
        if(GL_FALSE == oldDepthTest)
        {
            glDisable(GL_DEPTH_TEST);
        }
        glDepthFunc(oldDepthFunc);

        if(GL_FALSE == oldDepthMask)
        {
            glDepthMask(GL_FALSE);
        }

        /* IMPORTANT: We only need to update the states that are not restored.
         Since we don't know what was the previous value of the mask, we update the RenderState
         after setting it.
         The other values don't need to be updated since they were restored to their original values
         */
        glStencilMask(0xFFFFF);
//        RenderState::StateBlock::_defaultState->setStencilWrite(0xFFFFF);

        /* BUG: RenderState does not support glColorMask yet. */
        glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
    }
}
void DrawNode::drawLine(const Vec2& origin, const Vec2& destination, const Color4F& color)
{
    _vdLines->append<V2F_C4B_T2F>({origin,      Color4B(color), Tex2F()});
    _vdLines->append<V2F_C4B_T2F>({destination, Color4B(color), Tex2F()});
}
Exemple #24
0
SpritePolygonTest4::SpritePolygonTest4(){
    /*
     18, 48
     33.500000, 73.500000
     27.500000, 73.500000
     16.500000, 62.500000
     30.500000, 44.500000
     54.500000, 44.500000
     51.500000, 73.500000
     60.500000, 87.500000
     26.500000, 80.500000
     24.500000, 96.500000
     57.500000, 108.500000
     36.500000, 113.500000
     48.500000, 114.500000
     36.500000, 114.500000
     27.500000, 108.500000
     68.500000, 57.500000
     57.500000, 73.500000
     56.500000, 4.500000
     28.500000, 4.500000
     0, 1, 2,
     3, 0, 2,
     4, 0, 3,
     5, 0, 4,
     5, 6, 0,
     0, 6, 7,
     8, 7, 6,
     6, 9, 8,
     9, 10, 8,
     9, 11, 10,
     11, 12, 10,
     8, 10, 13,
     14, 5, 4,
     15, 5, 14,
     4, 3, 16,
     3, 17, 16,
     0.394118, 0.392562
     0.323529, 0.392562
     0.194118, 0.483471
     0.358824, 0.632231
     0.641176, 0.632231
     0.605882, 0.392562
     0.711765, 0.276859
     0.311765, 0.334711
     0.288235, 0.202479
     0.676471, 0.103306
     0.429412, 0.061983
     0.570588, 0.053719
     0.429412, 0.053719
     0.323529, 0.103306
     0.805882, 0.524793
     0.676471, 0.392562
     0.664706, 0.962810
     0.335294, 0.962810
     */
    Vec3 poss[] = {Vec3(33.500000, 73.500000,0),
    Vec3(27.500000, 73.500000,0),
    Vec3(16.500000, 62.500000,0),
    Vec3(30.500000, 44.500000,0),
    Vec3(54.500000, 44.500000,0),
    Vec3(51.500000, 73.500000,0),
    Vec3(60.500000, 87.500000,0),
    Vec3(26.500000, 80.500000,0),
    Vec3(24.500000, 96.500000,0),
    Vec3(57.500000, 108.500000,0),
    Vec3(36.500000, 113.500000,0),
    Vec3(48.500000, 114.500000,0),
    Vec3(36.500000, 114.500000,0),
    Vec3(27.500000, 108.500000,0),
    Vec3(68.500000, 57.500000,0),
    Vec3(57.500000, 73.500000,0),
    Vec3(56.500000, 4.500000,0),
    Vec3(28.500000, 4.50000, 0)
    };
    unsigned short idxs[] = {0, 1, 2, 3, 0, 2, 4, 0, 3, 5, 0, 4, 5, 6, 0, 0, 6, 7, 8, 7, 6, 6, 9, 8, 9, 10, 8, 9, 11, 10, 11, 12, 10, 8, 10, 13, 14, 5, 4, 15, 5, 14, 4, 3, 16, 3, 17, 16};
    std::vector<unsigned short> indices(idxs, idxs + sizeof idxs / sizeof idxs[0]);
    Tex2F t2f[] = {
        Tex2F(0.394118f, 0.392562f),
        Tex2F(0.323529f, 0.392562f),
        Tex2F(0.194118f, 0.483471f),
        Tex2F(0.358824f, 0.632231f),
        Tex2F(0.641176f, 0.632231f),
        Tex2F(0.605882f, 0.392562f),
        Tex2F(0.711765f, 0.276859f),
        Tex2F(0.311765f, 0.334711f),
        Tex2F(0.288235f, 0.202479f),
        Tex2F(0.676471f, 0.103306f),
        Tex2F(0.429412f, 0.061983f),
        Tex2F(0.570588f, 0.053719f),
        Tex2F(0.429412f, 0.053719f),
        Tex2F(0.323529f, 0.103306f),
        Tex2F(0.805882f, 0.524793f),
        Tex2F(0.676471f, 0.392562f),
        Tex2F(0.664706f, 0.962810f),
        Tex2F(0.335294f, 0.962810f)
    };
    std::vector<V3F_C4B_T2F> vs;
    for(int i = 0; i < 18; i++)
    {
        V3F_C4B_T2F t = {poss[i],Color4B::WHITE, t2f[i]};
        vs.push_back(t);
    }
    
    SpritePolygonCache::getInstance()->removeAllSpritePolygonCache();
    _title = "SpritePolygon Creation";
    _subtitle = "SpritePolygon::create(\"Images/grossini.png\", vector<V3F_C4B_T2F> v, vector<unsigned short> indices)";
    auto s = experimental::SpritePolygon::create(s_pathGrossini, vs, indices);
    initDefaultSprite(s_pathGrossini, s);
}
void DrawNode::drawPolygon(const Vec2* verts, int count, const Color4F& fillColor, float borderWidth, const Color4F& borderColor)
{
    CCASSERT(count >= 0, "invalid count value");
    
    Color4B fc(fillColor);
    Color4B bc(borderColor);
    
    bool outline = (borderColor.a > 0.0 && borderWidth > 0.0);
    
    for (int i = 0; i < count-2; ++i)
    {
        _vdTriangles->append<V2F_C4B_T2F>({verts[0],   fc, Tex2F()});
        _vdTriangles->append<V2F_C4B_T2F>({verts[i+1], fc, Tex2F()});
        _vdTriangles->append<V2F_C4B_T2F>({verts[i+2], fc, Tex2F()});
    }
    
    if (outline)
    {
        struct ExtrudeVerts {Vec2 offset, n;};
        auto extrude = (struct ExtrudeVerts*)CC_ALLOCA(sizeof(struct ExtrudeVerts)*count);
        memset(extrude, 0, sizeof(struct ExtrudeVerts)*count);
        
        for (auto i = 0; i < count; ++i)
        {
            Vec2 v0 = verts[(i-1+count)%count];
            Vec2 v1 = verts[i];
            Vec2 v2 = verts[(i+1)%count];
            
            Vec2 n1 = (v1 - v0).getPerp().getNormalized();
            Vec2 n2 = (v2 - v1).getPerp().getNormalized();
            
            Vec2 offset = (n1 + n2) * (1.f / (Vec2::dot(n1, n2) + 1.f));
            extrude[i] = {offset, n2};
        }
        
        for (auto i = 0; i < count; ++i)
        {
            int j = (i+1)%count;
            Vec2 v0 = verts[i];
            Vec2 v1 = verts[j];
            
            Vec2 n0 = extrude[i].n;
            
            Vec2 offset0 = extrude[i].offset;
            Vec2 offset1 = extrude[j].offset;
            
            Vec2 inner0 = v0 - offset0 * borderWidth;
            Vec2 inner1 = v1 - offset1 * borderWidth;
            Vec2 outer0 = v0 + offset0 * borderWidth;
            Vec2 outer1 = v1 + offset1 * borderWidth;
            
            _vdTriangles->append<V2F_C4B_T2F>({inner0, bc, -n0});
            _vdTriangles->append<V2F_C4B_T2F>({inner1, bc, -n0});
            _vdTriangles->append<V2F_C4B_T2F>({outer1, bc,  n0});
            
            _vdTriangles->append<V2F_C4B_T2F>({inner0, bc, -n0});
            _vdTriangles->append<V2F_C4B_T2F>({outer0, bc,  n0});
            _vdTriangles->append<V2F_C4B_T2F>({outer1, bc,  n0});
        }
    }
}
TrianglesCommand::Triangles AutoPolygon::triangulate(const std::vector<Vec2>& points)
{
    // if there are less than 3 points, then we can't triangulate
    if(points.size()<3)
    {
        log("AUTOPOLYGON: cannot triangulate %s with less than 3 points", _filename.c_str());
        return TrianglesCommand::Triangles();
    }
    std::vector<p2t::Point*> p2points;
    for(std::vector<Vec2>::const_iterator it = points.begin(); it<points.end(); it++)
    {
        p2t::Point * p = new (std::nothrow) p2t::Point(it->x, it->y);
        p2points.push_back(p);
    }
    p2t::CDT cdt(p2points);
    cdt.Triangulate();
    std::vector<p2t::Triangle*> tris = cdt.GetTriangles();
    
    // we won't know the size of verts and indices until we process all of the triangles!
    std::vector<V3F_C4B_T2F> verts;
    std::vector<unsigned short> indices;

    unsigned short idx = 0;
    unsigned short vdx = 0;

    for(std::vector<p2t::Triangle*>::const_iterator ite = tris.begin(); ite != tris.end(); ite++)
    {
        for(int i = 0; i < 3; i++)
        {
            auto p = (*ite)->GetPoint(i);
            auto v3 = Vec3(p->x, p->y, 0);
            bool found = false;
            size_t j;
            size_t length = vdx;
            for(j = 0; j < length; j++)
            {
                if(verts[j].vertices == v3)
                {
                    found = true;
                    break;
                }
            }
            if(found)
            {
                //if we found the same vertex, don't add to verts, but use the same vertex with indices
                indices.push_back(j);
                idx++;
            }
            else
            {
                //vert does not exist yet, so we need to create a new one,
                auto c4b = Color4B::WHITE;
                auto t2f = Tex2F(0,0); // don't worry about tex coords now, we calculate that later
                V3F_C4B_T2F vert = {v3,c4b,t2f};
                verts.push_back(vert);
                indices.push_back(vdx);
                idx++;
                vdx++;
            }
        }
    }
    for(auto j : p2points)
    {
        delete j;
    };

    // now that we know the size of verts and indices we can create the buffers
    V3F_C4B_T2F* vertsBuf = new (std::nothrow) V3F_C4B_T2F[verts.size()];
    memcpy(vertsBuf, verts.data(), verts.size() * sizeof(V3F_C4B_T2F));

    unsigned short* indicesBuf = new (std::nothrow) unsigned short[indices.size()];
    memcpy(indicesBuf, indices.data(), indices.size() * sizeof(short));

    // Triangles should really use std::vector and not arrays for verts and indices. 
    // Then the above memcpy would not be necessary
    TrianglesCommand::Triangles triangles = { vertsBuf, indicesBuf, static_cast<int>(verts.size()), static_cast<int>(indices.size()) };
    return triangles;
}