Esempio n. 1
0
void PtexViewer::render(int selx, int sely)
{
    if (!_shaderProgram) makeShader();

    if (_options.bgColorLight) glClearColor(0.8,0.8,0.8,1);
    else glClearColor(0.0,0.0,0.0,1);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    
    GLint vp[4];  
    glGetIntegerv(GL_VIEWPORT, vp);
    float width = vp[2];
    float height = vp[3];
    float winasp = width/height;
    
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    bool selectMode = selx>=0 && sely>=0;
    
    if (selectMode)
        gluPickMatrix((double)selx,(double)vp[3]-sely,3,3,vp);
    
    if (!_mode3d||_displayFace>=0)
    {
        float sc = _cam.getDistance()/2;
        glOrtho(-sc, sc, -sc/winasp, sc/winasp, -100, 100);

        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glTranslatef(-_cam.getLookAt()[0],-_cam.getLookAt()[1],-_cam.getLookAt()[2]);
    }
    else
    {
        _cam.applyProjectionTransform(winasp, _bounds);

        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        _cam.applyModelViewTransform();
    }
    
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_POLYGON_OFFSET_FILL);
    glPolygonOffset(1.0,1.0);
    glColor4f(1.0, 1.0, 1.0, 1.0);
    glUseProgramObjectARB(_shaderProgram);
    _geometry.draw(_displayFace);
    glUseProgramObjectARB(0);
    if (!selectMode && _options.showGridLines)
    {
        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        glEnable(GL_LINE_SMOOTH);
        glColor4f(0.0, 0.0, 0.0, 0.25);
        _geometry.draw(_displayFace);
        glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
        glDisable(GL_BLEND);
        glDisable(GL_LINE_SMOOTH);
    }
    glDisable(GL_POLYGON_OFFSET_FILL);
    glDisable(GL_DEPTH_TEST);
    
    if (selectMode) return;
    
    // Display text information
    char str[64];
    
    if (_options.bgColorLight) glColor3f(0.0, 0.0, 0.0);
    else glColor3f(0.9, 0.9, 0.9);
    
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    
    if (!_options.envMode)
    {
        if (_numFaces==1) strcpy(str, "1 face");
        else sprintf(str,"%i faces",_numFaces);

        renderChars(-0.8,-0.92,GLUT_BITMAP_8_BY_13,str);
        if (_displayFace>=0 && _numFaces!=1)
        {
            sprintf(str,"Face ID: %i",_displayFace);
            renderChars(-0.8,0.92,GLUT_BITMAP_8_BY_13,str);
        }
        if (_numFaces==1)
        {
            int ures = _geometry.getFace(0)->ures;
            int vres = _geometry.getFace(0)->vres;
            sprintf(str,"Res: %i x %i",ures,vres);
            renderChars(-0.8,0.92,GLUT_BITMAP_8_BY_13,str);
        }
        else if (_displayFace>=0)
        {
            int ures = _geometry.getFace(_displayFace)->ures;
            int vres = _geometry.getFace(_displayFace)->vres;
            sprintf(str,"Face Res: %i x %i",ures,vres);
            renderChars(-0.2,0.92,GLUT_BITMAP_8_BY_13,str);
        }
        renderChars(0.25,-0.92,GLUT_BITMAP_8_BY_13,(char*)_typeStr.c_str());
    }
}
Esempio n. 2
0
bool PtexViewer::loadFile(std::string filename, int face)
{
    Ptex::String ptexError;
    PtexPtr<PtexTexture> r(PtexTexture::open(filename.c_str(), ptexError, true));
    
    _quads = true;
    
    if (!r)
    {
        fprintf(stderr,"%s\n",ptexError.c_str());
        return false;
    }
    
    if (face>=r->numFaces())
    {
        std::cerr << "Invalid face ID requested" << std::endl;
        return false;
    }
    
    removeSceneData();
    
    _numFaces = r->numFaces();
    _typeStr = Ptex::DataTypeName(r->dataType());
    _quads = r->meshType()==Ptex::mt_quad;
    _mode3d = false;
    _envCube = false;
    
    if (_enable3D && r->numFaces()==6 && face<0)
    {
        // Test for environment cube case
        
        _envCube = true;
        
        int adjfaces[6][4] = {
            { 3, 5, 2, 4 }, // px
            { 3, 4, 2, 5 }, // nx
            { 4, 0, 5, 1 }, // py
            { 5, 0, 4, 1 }, // ny
            { 3, 0, 2, 1 }, // pz
            { 3, 1, 2, 0 }  // nz
        };
        
        int fi = 0;
        while (fi<6 && _envCube)
        {
            const Ptex::FaceInfo& f = r->getFaceInfo(fi);
            
            for (int v=0; v<4; v++)
                if (f.adjfaces[v]!=adjfaces[fi][v]) _envCube = false;
            
            fi++;
        }
    }
    
    if (_envCube)
    {
        // Construct environment cube geometry
        
        float cubeVerts[8][3] = { 
            {-1,-1,-1}, {1,-1,-1}, {1,-1, 1}, {-1,-1, 1},  
            {-1, 1,-1}, {1, 1,-1}, {1, 1, 1}, {-1, 1, 1} };

        int cubeInds[6][4] = {
            {2,1,5,6}, {0,3,7,4},
            {7,6,5,4}, {0,1,2,3},
            {3,2,6,7}, {1,0,4,5} };
        
        for (int i=0; i<8; i++) _geometry.addVertex(cubeVerts[i]);
        
        for (int fi=0; fi<6; fi++)
        {
            const Ptex::FaceInfo& f = r->getFaceInfo(fi);

            polygon q;
            for (int v=0; v<4; v++) q.indices[v] = cubeInds[fi][v];
            q.ures = f.res.u();
            q.vres = f.res.v();
            _geometry.addFace(q);
        }
        
        _mode3d = true;
    }
    else
    {
        if (_enable3D) _mode3d = _geometry.loadFromMetaData(r);
        if (_geometry.empty()) _geometry.makeFlatGeom(r);

        for (int fi=0; fi<r->numFaces(); fi++)
        {
            polygon* q = _geometry.getFace(fi);
            if (!q) continue;
            const Ptex::FaceInfo& f = r->getFaceInfo(fi);
            q->ures = f.res.u();
            q->vres = f.res.v();
        }
    }
    _geometry.makePages(r);
    
    computeBounds();
    
    return true;
}