void GenericSolver::buildInteractionMatrix(ReactiveSet& R) {
    if(verbose) printf("Building interaction matrix for %i DF...\n", R.nDF());
    ProgressBar pb = ProgressBar(R.nDF(),1,verbose);

#ifdef WITH_LAPACKE
    the_ixn = mmat(R.nDF(),R.nDF());
#else
    the_GF = gsl_matrix_alloc(R.nDF(),R.nDF());
#endif
    R.startInteractionScan();
    for(unsigned int DF=0; DF<R.nDF(); DF++) {
        R.setInteractionDF(DF,1.0);
        for(unsigned int phi=0; phi<R.nPhi; phi++) {
            mvec v = R.getReactionTo(&R,phi);
            assert(v.size() == R.nDF()/R.nPhi);
            for(unsigned int i=0; i<v.size(); i++)
#ifdef WITH_LAPACKE
                the_ixn(i*R.nPhi+phi, DF) = v[i];
#else
                gsl_matrix_set(the_GF, i*R.nPhi+phi, DF, (i*R.nPhi+phi==DF) ? 1-v[i] : -v[i]);
#endif
        }
        pb.update(DF);
    }
    R.setInteractionDF(R.nDF(),0);
}
mmat PlaneSource::fieldAtComponents(vec3 p0) const {
    
    vec3 v = p0 - p.o;
    double a = v.dot(p.sn);
    double aa = a*a;
    double x0 = v.dot(p.dx)/p.wx;
    double x1 = x0-0.5*p.wx;
    double x2 = x0+0.5*p.wx;
    double y0 = v.dot(p.dz)/p.wz;
    double y1 = y0-0.5*p.wz;
    double y2 = y0+0.5*p.wz;
    
    // perpendicular component
    double c11 = sqrt(aa+x1*x1+y1*y1);
    double c12 = sqrt(aa+x1*x1+y2*y2);
    double c21 = sqrt(aa+x2*x2+y1*y1);
    double c22 = sqrt(aa+x2*x2+y2*y2);
    double perp1 = -1.0/(8*M_PI)*log( (x1+c12)*(x2-c22)*(x1-c11)*(x2+c21) /
                                    ((x1-c12)*(x2+c22)*(x1+c11)*(x2-c21)) );
    double perp2 = 1.0/(8*M_PI)*log( (y2+c12)*(y2-c22)*(y1-c11)*(y1+c21) /
                                   ((y2-c12)*(y2+c22)*(y1+c11)*(y1-c21)) );
    
    // parallel component
    double z11 = x1*y1/(a*c11);
    double z12 = x1*y2/(a*c12);
    double z21 = x2*y1/(a*c21);
    double z22 = x2*y2/(a*c22);
    double par = 1.0/(4*M_PI)*(atan(z22)-atan(z21)+atan(z11)-atan(z12));
    
    // filter out NANs
    if(!(perp1 == perp1)) perp1 = 0;
    if(!(perp2 == perp2)) perp2 = 0;
    if(!(par == par)) par = 0;
    
    vec3 xsourced = p.sn*perp1 + p.dz*par/p.wz;
    vec3 zsourced = -(p.sn*perp2 - p.dx*par/p.wx);
    
    mmat f = mmat(3,2);
    for(unsigned int i=0; i<3; i++) {
        f(i,0) = xsourced[i];
        f(i,1) = zsourced[i];
    }
    
    return f;
    
}
Exemple #3
0
void Display::init()
{
	glewInit();
	
	world = new World( "" );
	camera = new Camera();

	// assign locations
	positionLocation = 0;
	shadowPositionLocation = 0;
	normalLocation = 1;
	colorLocation = 2;
	
	//Everybody does this
	glClearColor(0.6f, 0.8f, 1.0f, 1.0f);
	glEnable(GL_DEPTH_TEST);
	glClearDepth(1.0);
	glDepthFunc(GL_LEQUAL);
	glEnable(GL_CULL_FACE);
	glFrontFace(GL_CCW);

	initShaders();

	//Get the uniform locations for our shaders, unfortunately they can not be set by us, we have
	//to ask OpenGL for them
	u_modelMatrixLocation		= glGetUniformLocation(shaderProgram, "u_modelMatrix");
	u_projMatrixLocation		= glGetUniformLocation(shaderProgram, "u_projMatrix");
	u_lightPositionLocation		= glGetUniformLocation(shaderProgram, "u_lightPosition");
	u_lightColorLocation		= glGetUniformLocation(shaderProgram, "u_lightColor");
	u_camPositionLocation		= glGetUniformLocation(shaderProgram, "u_camPosition");
	u_shadowMapLocation			= glGetUniformLocation(shaderProgram, "u_shadowMap");
	u_shadowBiasMatrixLocation	= glGetUniformLocation(shaderProgram, "u_shadowProjMatrix");

	u_resolutionLocation		= glGetUniformLocation(raymarchShaderProgram, "u_resolution");
	u_rayCamPositionLocation	= glGetUniformLocation(raymarchShaderProgram, "u_camPosition");
	u_distanceMapLocation		= glGetUniformLocation(raymarchShaderProgram, "u_distanceMap");
	u_cMinLocation				= glGetUniformLocation(raymarchShaderProgram, "u_cMin");
	u_cMaxLocation				= glGetUniformLocation(raymarchShaderProgram, "u_cMax");
	
	u_shadowModelMatrixLocation	= glGetUniformLocation(shadowShaderProgram, "u_modelMatrix");
	u_shadowProjMatrixLocation	= glGetUniformLocation(shadowShaderProgram, "u_projMatrix");
	

	lightCol = vec3( 1.0f, 1.0f, 1.0f );
	lightPos = vec3( -5.0f, 10.0f, 3.0f );

	camera->setViewport( 640, 480 );

	vec3 cpos = camera->getPos();
	mat4 cmat = camera->getMat4();
	mat4 mmat( 1.0f );
	
	glUseProgram(shaderProgram);
	glUniformMatrix4fv(u_modelMatrixLocation, 1, GL_FALSE, &mmat[0][0]);
	glUniformMatrix4fv(u_projMatrixLocation, 1, GL_FALSE, &cmat[0][0]);
	glUniform3f(u_lightPositionLocation, lightPos.x, lightPos.y, lightPos.z );
	glUniform3f(u_lightColorLocation, lightCol.x, lightCol.y, lightCol.z );
	glUniform3f(u_camPositionLocation, cpos.x, cpos.y, cpos.z );

	glUseProgram(raymarchShaderProgram);
	glUniform2i(u_resolutionLocation, camera->getWidth(), camera->getHeight() );
	glUniform3f(u_rayCamPositionLocation, cpos.x, cpos.y, cpos.z );
	
	// The framebuffer
	glGenFramebuffers(1, &fbo);
	glBindFramebuffer(GL_FRAMEBUFFER, fbo);
 
	// Depth texture. Slower than a depth buffer, but you can sample it later in your shader
	glGenTextures(1, &depthTexture);
	glBindTexture(GL_TEXTURE_2D, depthTexture);
	glTexImage2D(GL_TEXTURE_2D, 0,GL_DEPTH_COMPONENT16, SHADOW_MAP_SIZE, SHADOW_MAP_SIZE,
		0,GL_DEPTH_COMPONENT, GL_FLOAT, 0);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
 
	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthTexture, 0);

	glDrawBuffer(GL_NONE); // No color buffer is drawn to.
 
	// Always check that our framebuffer is ok
	assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
	
	glBindFramebuffer(GL_FRAMEBUFFER, 0);
	glBindTexture(GL_TEXTURE_2D, 0);
	
	// request 1 texture name from OpenGL
	glGenTextures(1, &fluidTexture);	
	// tell OpenGL we're going to be setting up the texture name it gave us	
	glBindTexture(GL_TEXTURE_3D, fluidTexture);	
	// when this texture needs to be shrunk to fit on small polygons, use linear interpolation of the texels to determine the color
	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	// when this texture needs to be magnified to fit on a big polygon, use linear interpolation of the texels to determine the color
	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	// we want the texture to repeat over the S axis, so if we specify coordinates out of range we still get textured.
	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP);
	// same as above for T axis
	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP);
	// same as above for R axis
	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP);
}