view native/PhantomOmni/GraphicManager.cpp @ 8:ea7885bd9bff tip

fixed bug : render solid line as dotted/dashed when moving the stylus from dotted/dashed to solid
author ccmi-guest
date Thu, 03 Jul 2014 16:12:20 +0100
parents 9e67171477bc
children
line wrap: on
line source
#include "GraphicManager.h"

void GraphicManager::init(void){

    // Enable depth buffering for hidden surface removal.
    glDepthFunc(GL_LEQUAL);
    glEnable(GL_DEPTH_TEST);
    
    // Cull back faces.
    glCullFace(GL_BACK);
    glEnable(GL_CULL_FACE);
    
    // Setup other misc features.
    glEnable(GL_LIGHTING);
    glEnable(GL_NORMALIZE);
    glShadeModel(GL_SMOOTH);
    
    // Setup lighting model.
    glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_FALSE);
    glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);    
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, light_model_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
    glLightfv(GL_LIGHT0, GL_POSITION, light0_direction);
    glEnable(GL_LIGHT0);  

	
}

void GraphicManager::draw(void){
	 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);   
    // Draw 3D cursor at haptic device position.
    drawCursor();

	glPushAttrib(GL_CURRENT_BIT | GL_ENABLE_BIT);
	glEnable(GL_COLOR_MATERIAL);
	   
	// Draw each of the nodes as lit sphere.
	int numNodes = collectionsManager->getNodesNum();
    GLdouble dPointSize;
	
	for ( int i = 0; i < numNodes; i++){
		dPointSize = nodeSize;
        glColor3dv(nodeColor);
			
		glPushMatrix();
		CollectionsManager::NodeData & nd = collectionsManager->getNodeData(i); 
		glTranslated(nd.x,nd.y, 0);
		double dPointScale = dPointSize * gWorldScale;
		glScaled(dPointScale, dPointScale, dPointScale);        
		/* draw shpere */
		glutSolidSphere(0.5, 10, 10);
        glPopMatrix();
    }

	//draw edges
	glColor3f(1.0, 0.0, 0.0);
    glLineWidth(2.0);
	glPushAttrib(GL_ENABLE_BIT);
	glDisable(GL_LIGHTING);  
	glEnable(GL_COLOR_MATERIAL);
	
	int numEdges = collectionsManager->getEdgesNum();
	for(int i = 0; i < numEdges; i++){
		CollectionsManager::EdgeData & ed = collectionsManager->getEdgeData(i);
		glPushAttrib(GL_ENABLE_BIT);
		glLineStipple(1, ed.stipplePattern);
		glEnable(GL_LINE_STIPPLE);
		glBegin(GL_LINES);
		for(unsigned int j = 0; j < ed.getSize(); j++){
			for(unsigned int k = j; k < ed.getSize(); k++){
				if(ed.adjMatrix[j][k]){
					glVertex3d(ed.x[j],ed.y[j],0);
					glVertex3d(ed.x[k],ed.y[k],0);
				}
			}
		}
		glEnd();		
		glPopAttrib();
	}
    glPopAttrib();
	glPopMatrix();

}

void GraphicManager::drawCursor(){
    HLdouble proxyxform[16];

    GLUquadricObj *qobj = 0;

    glPushAttrib(GL_CURRENT_BIT | GL_ENABLE_BIT | GL_LIGHTING_BIT);
    glPushMatrix();
    if (!gCursorDisplayList){
        gCursorDisplayList = glGenLists(1);
        glNewList(gCursorDisplayList, GL_COMPILE);
        qobj = gluNewQuadric();
               
        gluCylinder(qobj, 0.0, kCursorRadius, kCursorHeight,
                    kCursorTess, kCursorTess);
        glTranslated(0.0, 0.0, kCursorHeight);
        gluCylinder(qobj, kCursorRadius, 0.0, kCursorHeight / 5.0,
                    kCursorTess, kCursorTess);
    
        gluDeleteQuadric(qobj);
        glEndList();
    }
    
    // Get the proxy transform in world coordinates.
    hlGetDoublev(HL_PROXY_TRANSFORM, proxyxform);
    glMultMatrixd(proxyxform);

    // Apply the local cursor scale factor.
    glScaled(gCursorScale, gCursorScale, gCursorScale);
	
    glEnable(GL_COLOR_MATERIAL);

	if(hapticManager->isDraggingNode()){
		glColor3dv(nodeColor);
		glutSolidSphere(0.25, 10, 10);
	}else if(hapticManager->isDraggingEdge()){
		glColor3dv(edgeColor);
		glutSolidSphere(0.12,10,10);
	}
	glColor3f(0.0, 0.5, 1.0);
    glCallList(gCursorDisplayList);

    glPopMatrix(); 
    glPopAttrib();
}

const double GraphicManager::kCursorRadius = 0.5;
const double GraphicManager::kCursorHeight = 1.5;
const int GraphicManager::kCursorTess = 15;

const GLfloat GraphicManager::light_model_ambient[] = {0.3f, 0.3f, 0.3f, 1.0f};
const GLfloat GraphicManager::light0_diffuse[] = {0.9f, 0.9f, 0.9f, 0.9f};   
const GLfloat GraphicManager::light0_direction[] = {0.0f, -0.4f, 1.0f, 0.0f}; 


const double GraphicManager::nodeSize = 10;

const hduVector3Dd GraphicManager::nodeColor(1.0, 1.0, 1.0);
const hduVector3Dd GraphicManager::edgeColor(1.0,0.0,0.0);