Esempio n. 1
0
vector<vec3> ModelLoaderMD3::Surface::getVertices(const Vertex *v,
  size_t count) {
	vector<vec3> result(count);
	
	for (size_t i=0; i<count; ++i) {
		result[i] = transformVertex(v[i]);
	}
	return result;
}
Esempio n. 2
0
vector<vec3> ModelLoaderMD2::getVertices(const Vertex *v,
  const size_t count,
  VEC3 scale,
  VEC3 translate) {
	vector<vec3> result(count);
	//transform(v, v+count, result.begin(), bind(transformVertex, _1, &scale, &translate));
	for (size_t i=0; i<count; ++i) {
		result[i] = transformVertex(v[i], &scale, &translate);
	}
	return result;
}
Esempio n. 3
0
  void transformVertices(const vector<Vec3r>& vertices,
			 const Matrix44r& trans,
			 vector<Vec3r>& res) {
    for (vector<Vec3r>::const_iterator v = vertices.begin();
	 v != vertices.end();
	 v++) {
      Vec3r *res_tmp = new Vec3r;
      transformVertex(*v, trans, *res_tmp);
      res.push_back(*res_tmp);
    }
  }
Esempio n. 4
0
osg::Geometry* ReaderWriterOBJ::convertElementListToGeometry(obj::Model& model, obj::Model::ElementList& elementList, bool& rotate) const
{
    
    unsigned int numVertexIndices = 0;
    unsigned int numNormalIndices = 0;
    unsigned int numTexCoordIndices = 0;
    
    unsigned int numPointElements = 0;
    unsigned int numPolylineElements = 0;
    unsigned int numPolygonElements = 0;

    obj::Model::ElementList::iterator itr;
    for(itr=elementList.begin();
        itr!=elementList.end();
        ++itr)
    {
        obj::Element& element = *(*itr);

        numVertexIndices += element.vertexIndices.size();
        numNormalIndices += element.normalIndices.size();
        numTexCoordIndices += element.texCoordIndices.size();

        numPointElements += (element.dataType==obj::Element::POINTS) ? 1 : 0;
        numPolylineElements += (element.dataType==obj::Element::POLYLINE) ? 1 : 0;
        numPolygonElements += (element.dataType==obj::Element::POLYGON) ? 1 : 0;

    }

    if (numVertexIndices==0) return 0;
    
    if (numNormalIndices!=0 && numNormalIndices!=numVertexIndices)
    {
        osg::notify(osg::NOTICE)<<"Incorrect number of normals, ignore them"<<std::endl;
        numNormalIndices = 0;
    }
    
    if (numTexCoordIndices!=0 && numTexCoordIndices!=numVertexIndices)
    {
        osg::notify(osg::NOTICE)<<"Incorrect number of normals, ignore them"<<std::endl;
        numTexCoordIndices = 0;
    }
    
    osg::Vec3Array* vertices = numVertexIndices ? new osg::Vec3Array : 0;
    osg::Vec3Array* normals = numNormalIndices ? new osg::Vec3Array : 0;
    osg::Vec2Array* texcoords = numTexCoordIndices ? new osg::Vec2Array : 0;
    
    if (vertices) vertices->reserve(numVertexIndices);
    if (normals) normals->reserve(numNormalIndices);
    if (texcoords) texcoords->reserve(numTexCoordIndices);
    
    osg::Geometry* geometry = new osg::Geometry;
    if (vertices) geometry->setVertexArray(vertices);
    if (normals)
    {
        geometry->setNormalArray(normals);
        geometry->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
    }
    if (texcoords)
    {
        geometry->setTexCoordArray(0,texcoords);
    }
    

    if (numPointElements>0)
    {
        unsigned int startPos = vertices->size();
        unsigned int numPoints = 0;
        for(itr=elementList.begin();
            itr!=elementList.end();
            ++itr)
        {
            obj::Element& element = *(*itr);
            if (element.dataType==obj::Element::POINTS)
            {
                for(obj::Element::IndexList::iterator index_itr = element.vertexIndices.begin();
                    index_itr != element.vertexIndices.end();
                    ++index_itr)
                {
                    vertices->push_back(transformVertex(model.vertices[*index_itr],rotate));
                    ++numPoints;
                }
                if (numNormalIndices)
                {
                    for(obj::Element::IndexList::iterator index_itr = element.normalIndices.begin();
                        index_itr != element.normalIndices.end();
                        ++index_itr)
                    {
                        normals->push_back(transformNormal(model.normals[*index_itr],rotate));
                    }
                }
                if (numTexCoordIndices)
                {
                    for(obj::Element::IndexList::iterator index_itr = element.texCoordIndices.begin();
                        index_itr != element.texCoordIndices.end();
                        ++index_itr)
                    {
                        texcoords->push_back(model.texcoords[*index_itr]);
                    }
                }
            }
        }

        osg::DrawArrays* drawArrays = new osg::DrawArrays(GL_POINTS,startPos,numPoints);
        geometry->addPrimitiveSet(drawArrays);
    }
    
    if (numPolylineElements>0)
    {
        unsigned int startPos = vertices->size();
        osg::DrawArrayLengths* drawArrayLengths = new osg::DrawArrayLengths(GL_LINES,startPos);

        for(itr=elementList.begin();
            itr!=elementList.end();
            ++itr)
        {
            obj::Element& element = *(*itr);
            if (element.dataType==obj::Element::POLYLINE)
            {
                drawArrayLengths->push_back(element.vertexIndices.size());

                for(obj::Element::IndexList::iterator index_itr = element.vertexIndices.begin();
                    index_itr != element.vertexIndices.end();
                    ++index_itr)
                {
                    vertices->push_back(transformVertex(model.vertices[*index_itr],rotate));
                }
                if (numNormalIndices)
                {
                    for(obj::Element::IndexList::iterator index_itr = element.normalIndices.begin();
                        index_itr != element.normalIndices.end();
                        ++index_itr)
                    {
                        normals->push_back(transformNormal(model.normals[*index_itr],rotate));
                    }
                }
                if (numTexCoordIndices)
                {
                    for(obj::Element::IndexList::iterator index_itr = element.texCoordIndices.begin();
                        index_itr != element.texCoordIndices.end();
                        ++index_itr)
                    {
                        texcoords->push_back(model.texcoords[*index_itr]);
                    }
                }
            }
        }

        geometry->addPrimitiveSet(drawArrayLengths);

    }

    // #define USE_DRAWARRAYLENGTHS

    if (numPolygonElements>0)
    {
        unsigned int startPos = vertices->size();
        
        #ifdef USE_DRAWARRAYLENGTHS
            osg::DrawArrayLengths* drawArrayLengths = new osg::DrawArrayLengths(GL_POLYGON,startPos);
            geometry->addPrimitiveSet(drawArrayLengths);
        #endif

        for(itr=elementList.begin();
            itr!=elementList.end();
            ++itr)
        {
            obj::Element& element = *(*itr);
            if (element.dataType==obj::Element::POLYGON)
            {

                #ifdef USE_DRAWARRAYLENGTHS
                    drawArrayLengths->push_back(element.vertexIndices.size());
                #else
                    if (element.vertexIndices.size()>4)
                    {
                        osg::DrawArrays* drawArrays = new osg::DrawArrays(GL_POLYGON,startPos,element.vertexIndices.size());
                        startPos += element.vertexIndices.size();
                        geometry->addPrimitiveSet(drawArrays);
                    }
                    else
                    {
                        osg::DrawArrays* drawArrays = new osg::DrawArrays(GL_TRIANGLE_FAN,startPos,element.vertexIndices.size());
                        startPos += element.vertexIndices.size();
                        geometry->addPrimitiveSet(drawArrays);
                    }
                #endif

            
                if (model.needReverse(element))
                {
                    // need to reverse so add to OSG arrays in same order as in OBJ, as OSG assume anticlockwise ordering.
                    for(obj::Element::IndexList::reverse_iterator index_itr = element.vertexIndices.rbegin();
                        index_itr != element.vertexIndices.rend();
                        ++index_itr)
                    {
                        vertices->push_back(transformVertex(model.vertices[*index_itr],rotate));
                    }
                    if (numNormalIndices)
                    {
                        for(obj::Element::IndexList::reverse_iterator index_itr = element.normalIndices.rbegin();
                            index_itr != element.normalIndices.rend();
                            ++index_itr)
                        {
                            normals->push_back(transformNormal(model.normals[*index_itr],rotate));
                        }
                    }
                    if (numTexCoordIndices)
                    {
                        for(obj::Element::IndexList::reverse_iterator index_itr = element.texCoordIndices.rbegin();
                            index_itr != element.texCoordIndices.rend();
                            ++index_itr)
                        {
                            texcoords->push_back(model.texcoords[*index_itr]);
                        }
                    }
                }
                else
                {
                    // no need to reverse so add to OSG arrays in same order as in OBJ.
                    for(obj::Element::IndexList::iterator index_itr = element.vertexIndices.begin();
                        index_itr != element.vertexIndices.end();
                        ++index_itr)
                    {
                        vertices->push_back(transformVertex(model.vertices[*index_itr],rotate));
                    }
                    if (numNormalIndices)
                    {
                        for(obj::Element::IndexList::iterator index_itr = element.normalIndices.begin();
                            index_itr != element.normalIndices.end();
                            ++index_itr)
                        {
                            normals->push_back(transformNormal(model.normals[*index_itr],rotate));
                        }
                    }
                    if (numTexCoordIndices)
                    {
                        for(obj::Element::IndexList::iterator index_itr = element.texCoordIndices.begin();
                            index_itr != element.texCoordIndices.end();
                            ++index_itr)
                        {
                            texcoords->push_back(model.texcoords[*index_itr]);
                        }
                    }
                }
            }
        }


    }
    
    return geometry;
}
void RSPVertexManager::_processVertex( unsigned int v )
{
//    float intensity;
//    float r, g, b;

    transformVertex( m_matrixMgr->getViewProjectionMatrix(), &m_vertices[v].x, &m_vertices[v].x);
    //TransformVertex( &m_vertices[v].x, m_worldProject );


    if ( m_billboard )
    {
        m_vertices[v].x += m_vertices[0].x;
        m_vertices[v].y += m_vertices[0].y;
        m_vertices[v].z += m_vertices[0].z;
        m_vertices[v].w += m_vertices[0].w;
    }


    if ( !OpenGLManager::getSingleton().getZBufferEnabled() )
    {
        m_vertices[v].z = -m_vertices[v].w;
    }

    //Temporary variables
    float intensity;
    float r, g, b;

    if ( m_lightMgr->getLightEnabled() )
    {
        //Transform normal
        transformVector( m_matrixMgr->getModelViewMatrix(), &m_vertices[v].nx, &m_vertices[v].nx );
        Vec3Normalize( &m_vertices[v].nx );

        //Get Ambient Color
        const float* ambientColor = m_lightMgr->getAmbientLight();
        r = ambientColor[0];
        g = ambientColor[1];
        b = ambientColor[2];

        for (int i=0; i<m_lightMgr->getNumLights(); ++i)
        {
            intensity = DotProduct( (float*)&m_vertices[v].nx, (float*)m_lightMgr->getLightDirection(i) );

            if (intensity < 0.0f) intensity = 0.0f;

            const float* lightColor = m_lightMgr->getLightColor(i);
            r += lightColor[0] * intensity;
            g += lightColor[1] * intensity;
            b += lightColor[2] * intensity;
        }

        //Set Color
        m_vertices[v].r = r;
        m_vertices[v].g = g;
        m_vertices[v].b = b;    
    }

    //Texture Generation
    if ( m_texCoordGenType != TCGT_NONE )
    {
        transformVector( m_matrixMgr->getProjectionMatrix(), &m_vertices[v].nx, &m_vertices[v].nx );

        Vec3Normalize( &m_vertices[v].nx );

        if ( m_texCoordGenType == TCGT_LINEAR )
        {   
            m_vertices[v].s = acosf(m_vertices[v].nx) * 325.94931f;
            m_vertices[v].t = acosf(m_vertices[v].ny) * 325.94931f;
        }
        else // TGT_GEN
        {
            m_vertices[v].s = (m_vertices[v].nx + 1.0f) * 512.0f;
            m_vertices[v].t = (m_vertices[v].ny + 1.0f) * 512.0f;
        }
    }

    //Clipping
    if (m_vertices[v].x < -m_vertices[v].w)  
        m_vertices[v].xClip = -1.0f;
    else if (m_vertices[v].x > m_vertices[v].w)
        m_vertices[v].xClip = 1.0f;
    else
        m_vertices[v].xClip = 0.0f;

    if (m_vertices[v].y < -m_vertices[v].w)
        m_vertices[v].yClip = -1.0f;
    else if (m_vertices[v].y > m_vertices[v].w)
        m_vertices[v].yClip = 1.0f;
    else
        m_vertices[v].yClip = 0.0f;

    if (m_vertices[v].w <= 0.0f)
        m_vertices[v].zClip = -1.0f;
    else if (m_vertices[v].z < -m_vertices[v].w)
        m_vertices[v].zClip = -0.1f;
    else if (m_vertices[v].z > m_vertices[v].w)
        m_vertices[v].zClip = 1.0f;
    else
        m_vertices[v].zClip = 0.0f;

}
Esempio n. 6
0
osg::Geometry* ReaderWriterOBJ::convertElementListToGeometry(obj::Model& model, obj::Model::ElementList& elementList, ObjOptionsStruct& localOptions) const
{

    unsigned int numVertexIndices = 0;
    unsigned int numNormalIndices = 0;
    unsigned int numTexCoordIndices = 0;

    unsigned int numPointElements = 0;
    unsigned int numPolylineElements = 0;
    unsigned int numPolygonElements = 0;

    obj::Model::ElementList::iterator itr;

    if (localOptions.generateFacetNormals == true) {
        for(itr=elementList.begin();
                itr!=elementList.end();
                    ++itr)
        {
            obj::Element& element = *(*itr);
            if (element.dataType==obj::Element::POINTS || element.dataType==obj::Element::POLYLINE)
                continue;

            if (element.normalIndices.size() == 0) {
                // fill in the normals
                int a = element.vertexIndices[0];
                int b = element.vertexIndices[1];
                int c = element.vertexIndices[2];

                osg::Vec3f ab(model.vertices[b]);
                osg::Vec3f ac(model.vertices[c]);

                ab -= model.vertices[a];
                ac -= model.vertices[a];

                osg::Vec3f Norm( ab ^ ac );
                Norm.normalize();
                int normal_idx = model.normals.size();
                model.normals.push_back(Norm);

                for (unsigned i=0 ; i < element.vertexIndices.size() ; i++)
                    element.normalIndices.push_back(normal_idx);
            }
        }
    }



    for(itr=elementList.begin();
        itr!=elementList.end();
        ++itr)
    {
        obj::Element& element = *(*itr);

        numVertexIndices += element.vertexIndices.size();
        numNormalIndices += element.normalIndices.size();
        numTexCoordIndices += element.texCoordIndices.size();

        numPointElements += (element.dataType==obj::Element::POINTS) ? 1 : 0;
        numPolylineElements += (element.dataType==obj::Element::POLYLINE) ? 1 : 0;
        numPolygonElements += (element.dataType==obj::Element::POLYGON) ? 1 : 0;

    }

    if (numVertexIndices==0) return 0;

    if (numNormalIndices!=0 && numNormalIndices!=numVertexIndices)
    {
        OSG_NOTICE<<"Incorrect number of normals, ignore them"<<std::endl;
        numNormalIndices = 0;
    }

    if (numTexCoordIndices!=0 && numTexCoordIndices!=numVertexIndices)
    {
        OSG_NOTICE<<"Incorrect number of normals, ignore them"<<std::endl;
        numTexCoordIndices = 0;
    }

    osg::Vec3Array* vertices = numVertexIndices ? new osg::Vec3Array : 0;
    osg::Vec3Array* normals = numNormalIndices ? new osg::Vec3Array : 0;
    osg::Vec2Array* texcoords = numTexCoordIndices ? new osg::Vec2Array : 0;

    if (vertices) vertices->reserve(numVertexIndices);
    if (normals) normals->reserve(numNormalIndices);
    if (texcoords) texcoords->reserve(numTexCoordIndices);

    osg::Geometry* geometry = new osg::Geometry;
    if (vertices) geometry->setVertexArray(vertices);
    if (normals)
    {
        geometry->setNormalArray(normals, osg::Array::BIND_PER_VERTEX);
    }
    if (texcoords)
    {
        geometry->setTexCoordArray(0,texcoords);
    }


    if (numPointElements>0)
    {
        unsigned int startPos = vertices->size();
        unsigned int numPoints = 0;
        for(itr=elementList.begin();
            itr!=elementList.end();
            ++itr)
        {
            obj::Element& element = *(*itr);
            if (element.dataType==obj::Element::POINTS)
            {
                for(obj::Element::IndexList::iterator index_itr = element.vertexIndices.begin();
                    index_itr != element.vertexIndices.end();
                    ++index_itr)
                {
                    vertices->push_back(transformVertex(model.vertices[*index_itr],localOptions.rotate));
                    ++numPoints;
                }
                if (numNormalIndices)
                {
                    for(obj::Element::IndexList::iterator index_itr = element.normalIndices.begin();
                        index_itr != element.normalIndices.end();
                        ++index_itr)
                    {
                        normals->push_back(transformNormal(model.normals[*index_itr],localOptions.rotate));
                    }
                }
                if (numTexCoordIndices)
                {
                    for(obj::Element::IndexList::iterator index_itr = element.texCoordIndices.begin();
                        index_itr != element.texCoordIndices.end();
                        ++index_itr)
                    {
                        texcoords->push_back(model.texcoords[*index_itr]);
                    }
                }
            }
        }

        osg::DrawArrays* drawArrays = new osg::DrawArrays(GL_POINTS,startPos,numPoints);
        geometry->addPrimitiveSet(drawArrays);
    }

    if (numPolylineElements>0)
    {
        unsigned int startPos = vertices->size();
        osg::DrawArrayLengths* drawArrayLengths = new osg::DrawArrayLengths(GL_LINES,startPos);

        for(itr=elementList.begin();
            itr!=elementList.end();
            ++itr)
        {
            obj::Element& element = *(*itr);
            if (element.dataType==obj::Element::POLYLINE)
            {
                drawArrayLengths->push_back(element.vertexIndices.size());

                for(obj::Element::IndexList::iterator index_itr = element.vertexIndices.begin();
                    index_itr != element.vertexIndices.end();
                    ++index_itr)
                {
                    vertices->push_back(transformVertex(model.vertices[*index_itr],localOptions.rotate));
                }
                if (numNormalIndices)
                {
                    for(obj::Element::IndexList::iterator index_itr = element.normalIndices.begin();
                        index_itr != element.normalIndices.end();
                        ++index_itr)
                    {
                        normals->push_back(transformNormal(model.normals[*index_itr],localOptions.rotate));
                    }
                }
                if (numTexCoordIndices)
                {
                    for(obj::Element::IndexList::iterator index_itr = element.texCoordIndices.begin();
                        index_itr != element.texCoordIndices.end();
                        ++index_itr)
                    {
                        texcoords->push_back(model.texcoords[*index_itr]);
                    }
                }
            }
        }

        geometry->addPrimitiveSet(drawArrayLengths);

    }

    // #define USE_DRAWARRAYLENGTHS

    if (numPolygonElements>0)
    {
        unsigned int startPos = vertices->size();

        #ifdef USE_DRAWARRAYLENGTHS
            osg::DrawArrayLengths* drawArrayLengths = new osg::DrawArrayLengths(GL_POLYGON,startPos);
            geometry->addPrimitiveSet(drawArrayLengths);
        #endif

        for(itr=elementList.begin();
            itr!=elementList.end();
            ++itr)
        {
            obj::Element& element = *(*itr);
            if (element.dataType==obj::Element::POLYGON)
            {






                #ifdef USE_DRAWARRAYLENGTHS
                    drawArrayLengths->push_back(element.vertexIndices.size());
                #else
                    if (element.vertexIndices.size()>4)
                    {
                        osg::DrawArrays* drawArrays = new osg::DrawArrays(GL_POLYGON,startPos,element.vertexIndices.size());
                        startPos += element.vertexIndices.size();
                        geometry->addPrimitiveSet(drawArrays);
                    }
                    else
                    {
                        osg::DrawArrays* drawArrays = new osg::DrawArrays(GL_TRIANGLE_FAN,startPos,element.vertexIndices.size());
                        startPos += element.vertexIndices.size();
                        geometry->addPrimitiveSet(drawArrays);
                    }
                #endif


                if (model.needReverse(element))
                {
                    // need to reverse so add to OSG arrays in same order as in OBJ, as OSG assume anticlockwise ordering.
                    for(obj::Element::IndexList::reverse_iterator index_itr = element.vertexIndices.rbegin();
                        index_itr != element.vertexIndices.rend();
                        ++index_itr)
                    {
                        vertices->push_back(transformVertex(model.vertices[*index_itr],localOptions.rotate));
                    }
                    if (numNormalIndices)
                    {
                        for(obj::Element::IndexList::reverse_iterator index_itr = element.normalIndices.rbegin();
                            index_itr != element.normalIndices.rend();
                            ++index_itr)
                        {
                            normals->push_back(transformNormal(model.normals[*index_itr],localOptions.rotate));
                        }
                    }


                    if (numTexCoordIndices)
                    {
                        for(obj::Element::IndexList::reverse_iterator index_itr = element.texCoordIndices.rbegin();
                            index_itr != element.texCoordIndices.rend();
                            ++index_itr)
                        {
                            texcoords->push_back(model.texcoords[*index_itr]);
                        }
                    }
                }
                else
                {
                    // no need to reverse so add to OSG arrays in same order as in OBJ.
                    for(obj::Element::IndexList::iterator index_itr = element.vertexIndices.begin();
                        index_itr != element.vertexIndices.end();
                        ++index_itr)
                    {
                        vertices->push_back(transformVertex(model.vertices[*index_itr],localOptions.rotate));
                    }
                    if (numNormalIndices)
                    {
                        for(obj::Element::IndexList::iterator index_itr = element.normalIndices.begin();
                            index_itr != element.normalIndices.end();
                            ++index_itr)
                        {
                            normals->push_back(transformNormal(model.normals[*index_itr],localOptions.rotate));
                        }
                    }
                    if (numTexCoordIndices)
                    {
                        for(obj::Element::IndexList::iterator index_itr = element.texCoordIndices.begin();
                            index_itr != element.texCoordIndices.end();
                            ++index_itr)
                        {
                            texcoords->push_back(model.texcoords[*index_itr]);
                        }
                    }
                }
            }
        }


    }

    return geometry;
}
Esempio n. 7
0
void Box::draw(float animationFrame)
{
    glPushMatrix();
    {
        float widthT = m_overallObjectDimensions[0] * 0.5f;
        float heightT = m_overallObjectDimensions[1] * 0.5f;
        float depthT = m_overallObjectDimensions[2] * 0.5f;

        float longestAxis = std::max(heightT,std::max(widthT, depthT));
        glScalef(1.0f/longestAxis, 1.0f/longestAxis, 1.0f/longestAxis);

        glBegin(GL_QUADS);
        {
            glColor3f(0.0f,0.0f,0.0f);
            for(float y=-heightT; y<=heightT-(m_stepSizes[1]*0.5f); y +=m_stepSizes[1])
            {
                for(float x=-widthT; x<=widthT-(m_stepSizes[0]*0.5f); x += m_stepSizes[0])
                {
                    // front
                    glNormal3f(0.0f,0.0f,1.0f);
                    transformVertex(QVector3D(x,y,depthT), animationFrame);
                    transformVertex(QVector3D(x+m_stepSizes[0],y,depthT), animationFrame);
                    transformVertex(QVector3D(x+m_stepSizes[0],y+m_stepSizes[1],depthT), animationFrame);
                    transformVertex(QVector3D(x,y+m_stepSizes[1],depthT), animationFrame);

                    // back
                    glNormal3f(0.0f,0.0f,-1.0f);
                    transformVertex(QVector3D(x,y+m_stepSizes[1],-depthT), animationFrame);
                    transformVertex(QVector3D(x+m_stepSizes[0],y+m_stepSizes[1],-depthT), animationFrame);
                    transformVertex(QVector3D(x+m_stepSizes[0],y,-depthT), animationFrame);
                    transformVertex(QVector3D(x,y,-depthT), animationFrame);
                }
            }

            for(float x=-widthT; x<=widthT-(m_stepSizes[0]*0.5f); x +=m_stepSizes[0])
            {
                for(float z=-depthT; z<=depthT-(m_stepSizes[2]*0.5f); z += m_stepSizes[2])
                {
                    // top
                    glNormal3f(0.0f,1.0f,0.0f);
                    transformVertex(QVector3D(x,heightT,z+m_stepSizes[2]), animationFrame);
                    transformVertex(QVector3D(x+m_stepSizes[0],heightT,z+m_stepSizes[2]), animationFrame);
                    transformVertex(QVector3D(x+m_stepSizes[0],heightT,z), animationFrame);
                    transformVertex(QVector3D(x,heightT,z), animationFrame);

                    // bottom
                    glNormal3f(0.0f,-1.0f,0.0f);
                    transformVertex(QVector3D(x,-heightT,z), animationFrame);
                    transformVertex(QVector3D(x+m_stepSizes[0],-heightT,z), animationFrame);
                    transformVertex(QVector3D(x+m_stepSizes[0],-heightT,z+m_stepSizes[2]), animationFrame);
                    transformVertex(QVector3D(x,-heightT,z+m_stepSizes[2]), animationFrame);
                }
            }

            for(float z=-depthT; z<=depthT-(m_stepSizes[2]*0.5f); z +=m_stepSizes[2])
            {
                for(float y=-heightT; y<=heightT-(m_stepSizes[1]*0.5f); y += m_stepSizes[1])
                {
                    // left
                    glNormal3f(-1.0f,0.0f,0.0f);
                    transformVertex(QVector3D(-widthT,y,z), animationFrame);
                    transformVertex(QVector3D(-widthT,y,z+m_stepSizes[2]), animationFrame);
                    transformVertex(QVector3D(-widthT,y+m_stepSizes[1],z+m_stepSizes[2]), animationFrame);
                    transformVertex(QVector3D(-widthT,y+m_stepSizes[1],z), animationFrame);

                    // right
                    glNormal3f(1.0f,0.0f,0.0f);
                    transformVertex(QVector3D(widthT,y+m_stepSizes[1],z), animationFrame);
                    transformVertex(QVector3D(widthT,y+m_stepSizes[1],z+m_stepSizes[2]), animationFrame);
                    transformVertex(QVector3D(widthT,y,z+m_stepSizes[2]), animationFrame);
                    transformVertex(QVector3D(widthT,y,z), animationFrame);
                }
            }
        }
        glEnd();
    }
    glPopMatrix();
}
Esempio n. 8
0
/* coordinates are in tile coordinates. */
void graphics_redraw( float zoom, uint32_t top, uint32_t bottom, uint32_t left,
		      uint32_t right )
{
  // int tileZoom = floor( zoom );
  // at zoom level 11, one tile is 2^(32-11) tile units wide
  // this * 256 pixels = 2^(32-11+8) tile pixels
  // want 300 tile pixels to map to 1. 2^(32-11+8) ska bli 256 pixels = 256/300
  // 2^(32-11+8) * 300 * s / 256 = 1
  // s = 256 / (2^(32-11+8) * 300)
  //   = 1 / (2^(32-11) * 300)
  //   = 2^(11-32) / 300 
  // at zoom level 1, one tile is 2^31
  // float scale = screenWidth / pow( 2, 31 - zoom + 8); // in pixels / tile coordinate
  // float scale = pow( 2, (31 - zoom + 1) / screenWidth ; // in pixels / tile coordinate
  float scalex = 1.5 * pow( 2, zoom - 32 + 9 ) / screenWidth ; // in pixels / tile coordinate
  float scaley = 1.5 * pow( 2, zoom - 32 + 9 ) / screenHeight ; // in pixels / tile coordinate
  // zoom = 11. screenWidth = 600 -> scale = 600 / (2^28) = 
  int i;
  /* NOTE: must be -scale * tileCenter.x below, not scale * -tileCenter.x, or 
     it will try to make the uint32_t tileCenter.x signed, which will not give 
     the desired result */
  GLfloat scaleMatrix[] = { scalex, 0, 0, 0,
			    0, scaley, 0, 0,
			    0, 0, 1, 0,
			    // -scalex * tileCenter.x, -scaley * tileCenter.y, 0, 1 };
			    0, 0, 0, 1 };
  // left tile = 2 359 750 565 * scale = 5274
  // shift is scale * -tileCenter.x = 5279.9 -> -6, scale * -tileCenter.y = 3 025 055 848 * scale = 6762
  // right tile = 2 364 665 765 * scale = 5285
  // -> +6
  // top tile = 3 029 250 152 * scale = 6771 = +9
  // bottom tile = 3 020 861 544 * scale = 6752 = -10
  //
  // but we want these to map to +-1. 
  // 
  // 5274 - (2362208165 * 600 / 2^28) = 
  /*
  float scaleMatrix[] = { scale, 0, 0, scale * -tileCenter.x,
		  	  0, scale, 0, scale * -tileCenter.y,
			  0, 0, 1, 0,
			  0, 0, 0,      1 };
  */
  // float viewMatrix[16] = { };
  // float tx = 0; // -scalex * tileCenter.x;
  // float ty = 0; // -scaley * tileCenter.y;
  // uint32_t centerTileLeftEdge = tileCenter.x & (~((uint32_t) 0) << (32-(int)zoom));
  // uint32_t centerTileRightEdge = centerTileLeftEdge + (1 << (32-(int)zoom));
  //uint32_t centerTileBottomEdge =
  //  tileCenter.y & (~((uint32_t) 0) << (32-(int)zoom));
  // uint32_t centerTileTopEdge = centerTileBottomEdge + (1 << (32-(int)zoom));

  // float transformed[4];
#if 0
  printf( "scale=(%f,%f), tx=%f, ty=%f\n", scalex, scaley, tx, ty );
  printf( "tileCenter = %"PRIu32", %"PRIu32"\n", tileCenter.x, tileCenter.y );
  printf( "center x,y=%f, %f\n",
	  ( (int64_t) 2362208165 - tileCenter.x) * scalex + tx,
	  ( (int64_t) 3025055848 - tileCenter.y) * scaley + ty );
  
  printf( "Center tile left edge=%"PRIu32", @ %f\n", centerTileLeftEdge,
	  ( (int64_t) centerTileLeftEdge - tileCenter.x) * scalex + tx );
  printf( "Center tile right edge=%"PRIu32", @ %f\n", centerTileRightEdge,
	  ( (int64_t) centerTileRightEdge - tileCenter.x) * scalex + tx );
  
  printf( "Center tile top edge=%"PRIu32", @ %f\n", centerTileTopEdge,
	  ( (int64_t) centerTileTopEdge - tileCenter.y) * scaley + ty );
  printf( "Center tile bottom edge=%"PRIu32", @ %f\n", centerTileBottomEdge,
	  ( (int64_t) centerTileBottomEdge - tileCenter.y) * scaley + ty );
#endif
  glUseProgram( programObject );
  assert( glGetError() == GL_NO_ERROR );

  glViewport( 0, 0, screenWidth, screenHeight );
  assert( glGetError() == GL_NO_ERROR );

  // changes in OpenGL ES 2
  // glMatrixMode( GL_PROJECTION );
  // glLoadIdentity();
  // glOrthof( left, right, top, bottom, -1, 1 );

  // glMatrixMode( GL_MODELVIEW );
  // glLoadIdentity();

  // clear the screen
  glClear( GL_COLOR_BUFFER_BIT );
  assert( glGetError() == GL_NO_ERROR );
  
  glUniformMatrix4fv( viewMatrixLoc, 1, GL_FALSE, scaleMatrix);
  assert( glGetError() == GL_NO_ERROR );

  // wait for textures to be loaded
  tileTex_waitVisibleLoaded();
  
  // loop over tiles
  // it seems inefficent to have a triangle strip for each quad, but I spent
  // a day trying to figure how I could do a Triangle Strip with different
  // textures in each triangle, but without success.
  for( i = 0; i < visibleTileCount; i++ )
  {
    TileData tile = &(tiles[i]);
    float transformed[4][2];
    TileTexture tileTex;
    GLuint textureID;
    
    tileTex = tile->tileTexture;
    /*
    if( !tileTex_isLoaded( tileTex ) )
      continue;
    */
    textureID = tileTex_makeTextureID( tileTex );

    if( textureID == -1 )
    {
      // printf( "Skipping tile %d because it has no textureID\n", i );
      continue;
    }

      // set up Vertex buffer stuff
    if( tiles[i].vertexBufferID == -1 )
      glGenBuffers( 1, &(tiles[i].vertexBufferID) );
    
    glBindBuffer( GL_ARRAY_BUFFER, tiles[ i ].vertexBufferID );
    assert( glGetError() == GL_NO_ERROR );
    glBufferData( GL_ARRAY_BUFFER, sizeof( tiles[i].vertexData ),
		  tiles[i].vertexData, GL_STATIC_DRAW ); // copy data to GPU
    assert( glGetError() == GL_NO_ERROR );
    // generate texture id

    // debug
    transformVertex( tile, scaleMatrix, (float *) &transformed );
#if 0
    printf( "tile %d transformed to (%f,%f), (%f,%f), (%f,%f), (%f,%f)\n",
	    i,
	    transformed[0][0], transformed[0][1],
	    transformed[1][0], transformed[1][1],
	    transformed[2][0], transformed[2][1],
	    transformed[3][0], transformed[3][1] );
#endif
    glBindTexture( GL_TEXTURE_2D, textureID );
    assert( glGetError() == GL_NO_ERROR );

    glBindBuffer( GL_ARRAY_BUFFER, tile->vertexBufferID );
    assert( glGetError() == GL_NO_ERROR );
    
    glVertexAttribPointer( positionLoc, 2, GL_FLOAT /* was uint */, GL_FALSE,
			   sizeof(sVertexData),
			   (void *) offsetof( sVertexData, position));
    assert( glGetError() == GL_NO_ERROR );
    glVertexAttribPointer( texCoordLoc, 2, GL_FLOAT, GL_FALSE,
			   sizeof(sVertexData), (void *) offsetof( sVertexData, uv ) );
    assert( glGetError() == GL_NO_ERROR );

    glEnableVertexAttribArray( positionLoc );
    glEnableVertexAttribArray( texCoordLoc );
    
    glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, indexBufferID );
    assert( glGetError() == GL_NO_ERROR );

    glDrawElements( GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_BYTE, NULL );

    assert( glGetError() == GL_NO_ERROR );
  }

  eglSwapBuffers( display, surface );
  
}
	/**
	 * 回転行列を適応して座標変換します。
	 * @param i_angle
	 * @param o_out
	 */
	void NyARDoubleMatrix33::transformVertex(const TNyARDoublePoint3d& i_position,TNyARDoublePoint3d& o_out)
	{
		transformVertex(i_position.x,i_position.y,i_position.z,o_out);
		return;
	}
	void NyARTransMatResult::transformVertex(const TNyARDoublePoint3d& i_in,TNyARDoublePoint3d& o_out)
	{
		transformVertex(i_in.x,i_in.y,i_in.z,o_out);
	}