void drawTriangle( double x1, double y1, double z1,
                   double x2, double y2, double z2,
                   double x3, double y3, double z3 )
{
    ModelerDrawState *mds = ModelerDrawState::Instance();

	_setupOpenGl();

    if (mds->m_rayFile)
    {
        _dump_current_modelview();
        fprintf(mds->m_rayFile, 
            "polymesh { points=((%f,%f,%f),(%f,%f,%f),(%f,%f,%f)); faces=((0,1,2));\n", x1, y1, z1, x2, y2, z2, x3, y3, z3 );
        _dump_current_material();
        fprintf(mds->m_rayFile, "})\n" );
    }
    else
    {
        double a, b, c, d, e, f;
        
        /* the normal to the triangle is the cross product of two of its edges. */
        a = x2-x1;
        b = y2-y1;
        c = z2-z1;
        
        d = x3-x1;
        e = y3-y1;
        f = z3-z1;
        
        glBegin( GL_TRIANGLES );
        glNormal3d( b*f - c*e, c*d - a*f, a*e - b*d );
        glVertex3d( x1, y1, z1 );
        glVertex3d( x2, y2, z2 );
        glVertex3d( x3, y3, z3 );
        glEnd();
    }
}
void drawSphere(double r)
{
    ModelerDrawState *mds = ModelerDrawState::Instance();

	_setupOpenGl();
    
    if (mds->m_rayFile)
    {
        _dump_current_modelview();
        fprintf(mds->m_rayFile, "scale(%f,%f,%f,sphere {\n", r, r, r );
        _dump_current_material();
        fprintf(mds->m_rayFile, "}))\n" );
    }
    else
    {
        int divisions; 
        GLUquadricObj* gluq;
        
        switch(mds->m_quality)
        {
        case HIGH: 
            divisions = 32; break;
        case MEDIUM: 
            divisions = 20; break;
        case LOW:
            divisions = 12; break;
        case POOR:
            divisions = 8; break;
        }
        
        gluq = gluNewQuadric();
        gluQuadricDrawStyle( gluq, GLU_FILL );
        gluQuadricTexture( gluq, GL_TRUE );
        gluSphere(gluq, r, divisions, divisions);
        gluDeleteQuadric( gluq );
    }
}
Esempio n. 3
0
void drawCylinder( double h, double r1, double r2 )
{
    ModelerDrawState *mds = ModelerDrawState::Instance();
    int divisions;

	_setupOpenGl();
    
    switch(mds->m_quality)
    {
    case HIGH: 
        divisions = 32; break;
    case MEDIUM: 
        divisions = 20; break;
    case LOW:
        divisions = 12; break;
    case POOR:
        divisions = 8; break;
    }
    
    if (mds->m_rayFile)
    {
        _dump_current_modelview();
        fprintf(mds->m_rayFile, 
            "cone { height=%f; bottom_radius=%f; top_radius=%f;\n", h, r1, r2 );
        _dump_current_material();
        fprintf(mds->m_rayFile, "})\n" );
    }
    else
    {
        GLUquadricObj* gluq;
        
        /* GLU will again do the work.  draw the sides of the cylinder. */
        gluq = gluNewQuadric();
        gluQuadricDrawStyle( gluq, GLU_FILL );
        gluQuadricTexture( gluq, GL_TRUE );
        gluCylinder( gluq, r1, r2, h, divisions, divisions);
        gluDeleteQuadric( gluq );
        
        if ( r1 > 0.0 )
        {
        /* if the r1 end does not come to a point, draw a flat disk to
            cover it up. */
            
            gluq = gluNewQuadric();
            gluQuadricDrawStyle( gluq, GLU_FILL );
            gluQuadricTexture( gluq, GL_TRUE );
            gluQuadricOrientation( gluq, GLU_INSIDE );
            gluDisk( gluq, 0.0, r1, divisions, divisions);
            gluDeleteQuadric( gluq );
        }
        
        if ( r2 > 0.0 )
        {
        /* if the r2 end does not come to a point, draw a flat disk to
            cover it up. */
            
            /* save the current matrix mode. */	
            int savemode;
            glGetIntegerv( GL_MATRIX_MODE, &savemode );
            
            /* translate the origin to the other end of the cylinder. */
            glMatrixMode( GL_MODELVIEW );
            glPushMatrix();
            glTranslated( 0.0, 0.0, h );
            
            /* draw a disk centered at the new origin. */
            gluq = gluNewQuadric();
            gluQuadricDrawStyle( gluq, GLU_FILL );
            gluQuadricTexture( gluq, GL_TRUE );
            gluQuadricOrientation( gluq, GLU_OUTSIDE );
            gluDisk( gluq, 0.0, r2, divisions, divisions);
            gluDeleteQuadric( gluq );
            
            /* restore the matrix stack and mode. */
            glPopMatrix();
            glMatrixMode( savemode );
        }
    }
    
}
void drawQuadrilateral(double x1, double y1, double z1,
	double x2, double y2, double z2,
	double x3, double y3, double z3,
	double x4, double y4, double z4)
{
	ModelerDrawState *mds = ModelerDrawState::Instance();

	_setupOpenGl();

	if (mds->m_rayFile)
	{
		_dump_current_modelview();
		fprintf(mds->m_rayFile,
			"polymesh { points=((%f,%f,%f),(%f,%f,%f),(%f,%f,%f),(%f,%f,%f)); faces=((0,1,2,3));\n", x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4);
		_dump_current_material();
		fprintf(mds->m_rayFile, "})\n");
	}
	else
	{
		double x21, y21, z21, x31, y31, z31, x41, y41, z41, x23, y23, z23, x43, y43, z43;

		/* the normal to the triangle is the cross product of two of its edges. */
		x21 = x2 - x1;
		y21 = y2 - y1;
		z21 = z2 - z1;

		x31 = x3 - x1;
		y31 = y3 - y1;
		z31 = z3 - z1;

		x41 = x4 - x1;
		y41 = y4 - y1;
		z41 = z4 - z1;

		x23 = x2 - x3;
		y23 = y2 - y3;
		z23 = z2 - z3;

		x43 = x4 - x3;
		y43 = y4 - y3;
		z43 = z4 - z3;

		// triangle 1 2 3
		glBegin(GL_TRIANGLES);
		glNormal3d(y21*z31 - z21*y31, z21*x31 - x21*z31, x21*y31 - y21*x31);
		glVertex3d(x1, y1, z1);
		glVertex3d(x2, y2, z2);
		glVertex3d(x3, y3, z3);
		glEnd();

		// triangle 1 2 4
		glBegin(GL_TRIANGLES);
		glNormal3d(y41*z21 - z41*y21, z21*x41 - x21*z41, x41*y21 - y41*x21);
		glVertex3d(x1, y1, z1);
		glVertex3d(x2, y2, z2);
		glVertex3d(x4, y4, z4);
		glEnd();

		// triangle 2 3 4  ¥¼°µ
		glBegin(GL_TRIANGLES);
		glNormal3d(y23*z43 - z23*y43, z43*x23 - x43*z23, x23*y43 - y23*x43);
		glVertex3d(x2, y2, z2);
		glVertex3d(x3, y3, z3);
		glVertex3d(x4, y4, z4);
		glEnd();

		// triangle 1 3 4  ¥¼°µ
		glBegin(GL_TRIANGLES);
		glNormal3d(y31*z41 - z31*y41, z41*x31 - x41*z31, x31*y41 - y31*x41);
		glVertex3d(x1, y1, z1);
		glVertex3d(x3, y3, z3);
		glVertex3d(x4, y4, z4);
		glEnd();
	}
}
void drawWing()
{
    ModelerDrawState *mds = ModelerDrawState::Instance();

    _setupOpenGl();
    
    if (mds->m_rayFile)
    {
        _dump_current_modelview();
  
        _dump_current_material();
        fprintf(mds->m_rayFile,  "})))\n" );
    }
    else
    {
        /* remember which matrix mode OpenGL was in. */
        int savemode;
        glGetIntegerv( GL_MATRIX_MODE, &savemode );
        
        /* switch to the model matrix and scale by x,y,z. */
        glMatrixMode( GL_MODELVIEW );
        glPushMatrix();
        glBegin(GL_POLYGON);
        glVertex3d(0,1.4,0);
        glVertex3d(.1,1.37,0);
        glVertex3d(.2,1.35,0);
        glVertex3d(.2,0,0);
        glVertex3d(0,0,0);
        glEnd();
        glBegin(GL_POLYGON);
        glVertex3d(.2,1.35,0);
        glVertex3d(.4,2,0);
        glVertex3d(.5,1,0);
        glVertex3d(.5,-1,0);
        glVertex3d(.2,0,0);
        glEnd();
        glBegin(GL_POLYGON);
        glVertex3d(0,0,0);
        glVertex3d(.6,0,0);
        glVertex3d(.5,-1,0);
        glVertex3d(0,-2,0);
        glEnd();
        glBegin(GL_POLYGON);
        glVertex3d(0.3,0.9,0);
        glVertex3d(0.8,0.9,0);
        glVertex3d(1.1,-0.6,0);
        glVertex3d(0.5,-1,0);
        glEnd();
        glBegin(GL_POLYGON);
        glVertex3d(0,0,0);
        glVertex3d(0.85,-.75,0);
        glVertex3d(0.4,-1.15,0);
        glEnd();
        glBegin(GL_POLYGON);
        glVertex3d(0.8,0.9,0);
        glVertex3d(1.2,1,0);
        glVertex3d(2.35,-0.1,0);
        glEnd();
        glBegin(GL_POLYGON);
        glVertex3d(0.8,0.9,0);
        glVertex3d(1.1,-0.6,0);
        glVertex3d(2.35,-0.1,0);
        glEnd();
        glBegin(GL_POLYGON);
        glVertex3d(1.2,1,0);
        glVertex3d(1.4,1.1,0);
        glVertex3d(3.9,0,0);
        glVertex3d(2.35,-0.1,0);
        glEnd();
        glBegin(GL_POLYGON);
        glVertex3d(1.4,1.1,0);
        glVertex3d(1.6,1.3,0);
        glVertex3d(4.15,0.5,0);
        glVertex3d(3.9,0,0);
        glEnd();
        glBegin(GL_POLYGON);
        glVertex3d(1.6,1.3,0);
        glVertex3d(1.75,1.6,0);
        glVertex3d(4.4,0.85,0);
        glVertex3d(4.15,0.5,0);
        glEnd();
        glBegin(GL_POLYGON);
        glVertex3d(1.75,1.6,0);
        glVertex3d(1.85,1.9,0);
        glVertex3d(5,1.3,0);
        glVertex3d(4.4,0.85,0);
        glEnd();
        glBegin(GL_POLYGON);
        glVertex3d(1.85,1.9,0);
        glVertex3d(1.9,2.2,0);
        glVertex3d(7,2.2,0);
        glVertex3d(5,1.3,0);
        glEnd();
        
        //------------------------leftSIDE
        glBegin(GL_POLYGON);
        glVertex3d(-0,1.4,0);
        glVertex3d(-.1,1.37,0);
        glVertex3d(-.2,1.35,0);
        glVertex3d(-.2,0,0);
        glVertex3d(-0,0,0);
        glEnd();
        glBegin(GL_POLYGON);
        glVertex3d(-.2,1.35,0);
        glVertex3d(-.4,2,0);
        glVertex3d(-.5,1,0);
        glVertex3d(-.5,-1,0);
        glVertex3d(-.2,0,0);
        glEnd();
        glBegin(GL_POLYGON);
        glVertex3d(-0,0,0);
        glVertex3d(-.6,0,0);
        glVertex3d(-.5,-1,0);
        glVertex3d(-0,-2,0);
        glEnd();
        glBegin(GL_POLYGON);
        glVertex3d(-0.3,0.9,0);
        glVertex3d(-0.8,0.9,0);
        glVertex3d(-1.1,-0.6,0);
        glVertex3d(-0.5,-1,0);
        glEnd();
        glBegin(GL_POLYGON);
        glVertex3d(-0,0,0);
        glVertex3d(-0.85,-.75,0);
        glVertex3d(-0.4,-1.15,0);
        glEnd();
        glBegin(GL_POLYGON);
        glVertex3d(-0.8,0.9,0);
        glVertex3d(-1.2,1,0);
        glVertex3d(-2.35,-0.1,0);
        glEnd();
        glBegin(GL_POLYGON);
        glVertex3d(-0.8,0.9,0);
        glVertex3d(-1.1,-0.6,0);
        glVertex3d(-2.35,-0.1,0);
        glEnd();
        glBegin(GL_POLYGON);
        glVertex3d(-1.2,1,0);
        glVertex3d(-1.4,1.1,0);
        glVertex3d(-3.9,0,0);
        glVertex3d(-2.35,-0.1,0);
        glEnd();
        glBegin(GL_POLYGON);
        glVertex3d(-1.4,1.1,0);
        glVertex3d(-1.6,1.3,0);
        glVertex3d(-4.15,0.5,0);
        glVertex3d(-3.9,0,0);
        glEnd();
        glBegin(GL_POLYGON);
        glVertex3d(-1.6,1.3,0);
        glVertex3d(-1.75,1.6,0);
        glVertex3d(-4.4,0.85,0);
        glVertex3d(-4.15,0.5,0);
        glEnd();
        glBegin(GL_POLYGON);
        glVertex3d(-1.75,1.6,0);
        glVertex3d(-1.85,1.9,0);
        glVertex3d(-5,1.3,0);
        glVertex3d(-4.4,0.85,0);
        glEnd();
        glBegin(GL_POLYGON);
        glVertex3d(-1.85,1.9,0);
        glVertex3d(-1.9,2.2,0);
        glVertex3d(-7,2.2,0);
        glVertex3d(-5,1.3,0);
        glEnd();
        
        /* restore the model matrix stack, and switch back to the matrix
        mode we were in. */
        glPopMatrix();
        glMatrixMode( savemode );
    }
}
void drawBox( double x, double y, double z )
{
    ModelerDrawState *mds = ModelerDrawState::Instance();

	_setupOpenGl();
    
    if (mds->m_rayFile)
    {
        _dump_current_modelview();
        fprintf(mds->m_rayFile,  
            "scale(%f,%f,%f,translate(0.5,0.5,0.5,box {\n", x, y, z );
        _dump_current_material();
        fprintf(mds->m_rayFile,  "})))\n\n" );
    }
    else
    {
        /* remember which matrix mode OpenGL was in. */
        int savemode;
        glGetIntegerv( GL_MATRIX_MODE, &savemode );
        
        /* switch to the model matrix and scale by x,y,z. */
        glMatrixMode( GL_MODELVIEW );

        glPushMatrix();
        glScaled( x, y, z );
        
        glBegin( GL_QUADS );
        
        glNormal3d( 0.0, 0.0, -1.0 );
        glVertex3d( 0.0, 0.0, 0.0 );
		glVertex3d( 0.0, 1.0, 0.0 );
        glVertex3d( 1.0, 1.0, 0.0 );
		glVertex3d( 1.0, 0.0, 0.0 );
        
        glNormal3d( 0.0, -1.0, 0.0 );
        glVertex3d( 0.0, 0.0, 0.0 );
		glVertex3d( 1.0, 0.0, 0.0 );
        glVertex3d( 1.0, 0.0, 1.0 );
		glVertex3d( 0.0, 0.0, 1.0 );
        
        glNormal3d( -1.0, 0.0, 0.0 );
        glVertex3d( 0.0, 0.0, 0.0 );
		glVertex3d( 0.0, 0.0, 1.0 );
        glVertex3d( 0.0, 1.0, 1.0 );
		glVertex3d( 0.0, 1.0, 0.0 );
        
        glNormal3d( 0.0, 0.0, 1.0 );
        glVertex3d( 0.0, 0.0, 1.0 );
		glVertex3d( 1.0, 0.0, 1.0 );
        glVertex3d( 1.0, 1.0, 1.0 );
		glVertex3d( 0.0, 1.0, 1.0 );
        
        glNormal3d( 0.0, 1.0, 0.0 );
        glVertex3d( 0.0, 1.0, 0.0 );
		glVertex3d( 0.0, 1.0, 1.0 );
        glVertex3d( 1.0, 1.0, 1.0 );
		glVertex3d( 1.0, 1.0, 0.0 );
        
        glNormal3d( 1.0, 0.0, 0.0 );
        glVertex3d( 1.0, 0.0, 0.0 );
		glVertex3d( 1.0, 1.0, 0.0 );
        glVertex3d( 1.0, 1.0, 1.0 );
		glVertex3d( 1.0, 0.0, 1.0 );
        
        glEnd();
        
        /* restore the model matrix stack, and switch back to the matrix
        mode we were in. */
        glPopMatrix();
        glMatrixMode( savemode );
    }
}
void drawSphere(double r)
{
    ModelerDrawState *mds = ModelerDrawState::Instance();

	_setupOpenGl();
    
    if (mds->m_rayFile)
    {
        _dump_current_modelview();
        fprintf(mds->m_rayFile, "scale(%f,%f,%f,sphere {\n", r, r, r );
        _dump_current_material();
        fprintf(mds->m_rayFile, "}))\n\n" );
    }
    else
    {
        int divisions; 
        switch(mds->m_quality)
        {
        case HIGH: 
            divisions = 128; break;
        case MEDIUM: 
            divisions = 32; break;
        case LOW:
            divisions = 16; break;
        case POOR:
            divisions = 8; break;
        }


		const float PI = 3.14159265f;
		int n = divisions;
		float origin_x = 0;
		float origin_y = 0;
		float origin_z = 0;

		for (int i = 0; i < n / 2 ; i++) {
			float a1 = i * (PI * 2) / n - (PI / 2);
			float a2 = (i + 1) * (PI * 2) / n - (PI / 2);
			glBegin(GL_QUAD_STRIP);
				for (int j = 0; j <= n; j++) {
					float a3 = j * (PI * 2) / n;
					float n1_x = cos(a2) * sin(a3);
					float n1_y = cos(a2) * cos(a3);
					float n1_z = sin(a2);

					float p1_x = (float)(origin_x + r * n1_x);
					float p1_y = (float)(origin_y + r * n1_y);
					float p1_z = (float)(origin_z + r * n1_z);
					glNormal3f(n1_x, n1_y, n1_z);
					glTexCoord2f( (GLfloat)(1.0 - j / (float) n), (GLfloat)(2 * (i + 1) / (float) n));
					glVertex3f(p1_x, p1_y, p1_z);

					n1_x = cos(a1) * sin(a3);
					n1_y = cos(a1) * cos(a3);
					n1_z = sin(a1);

					p1_x = (float)(origin_x + r * n1_x);
					p1_y = (float)(origin_y + r * n1_y);
					p1_z = (float)(origin_z + r * n1_z);
					glNormal3f(n1_x, n1_y, n1_z);
					glTexCoord2f((GLfloat)(1.0 - j / (float) n), (GLfloat)(2 * i / (float) n));
					glVertex3f(p1_x, p1_y, p1_z);
				}
			glEnd();
		}
    }
}