⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sparsematrixvectormultiply3.cpp

📁 PDE simulator on GPU.
💻 CPP
字号:
// SparseMatrixVectorMultiply3.cpp: implementation of the SparseMatrixVectorMultiply3 class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "shadow.h"
#include "SparseMatrixOnGPU3.h"
#include "SparseMatrixVectorMultiply3.h"

#include "FragmentProgram.h"
#include "CommonHW.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////////
/// From Kruger Method in Siggraph2003
//////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////
/// Vector(result) = Matrix(A) * Vector(x)
/// Here Matrix A is a sparse one which is different from the above one
/// g_Float ---- false
/// Just fixed pipeline the following to get the correct result
////////////////////////////////////////////////////////////////////////////////////////
void KrugerSparseMatVect(const CKrugerMatrix& A, const CVectorOnGPU& x, CVectorOnGPU& result)
{
	////////////////////////////////////////////
	///If the size is compatible, then error occurs
	int i, nPass = A.GetDim();
	if(nPass != x.GetVectorLength())
	{
		AfxMessageBox("Error to Operate on these two vectors for different size in KrugerSparseMatVect!");
		return;
	}

	//////////////////////////////////////////////
	///// Clear the framebuffer firstly to avoid the previous computation
	///// This step is important to avoid computation errors
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	
	glClearColor(0,0,0,0);
	glDisable(GL_DEPTH_TEST);
	glPointSize(1);

	/// Draw all the points in GL_POINTS with texture
	glEnable(GL_BLEND);
	glBlendFunc(GL_ONE, GL_ONE);

	unsigned int _iTextureTarget = x.GetTextureTarget();
	glEnable(_iTextureTarget);
	glBindTexture(_iTextureTarget, x.GetTextureID());
	for(i=0; i<nPass; i++)
	{
		glEnableClientState ( GL_VERTEX_ARRAY );   
		glVertexPointer(2, GL_INT, 0, A.VertexArray[i]);

		glEnableClientState( GL_COLOR_ARRAY );
		glColorPointer(3, GL_FLOAT, 0, A.ColorArray[i]);

		glEnableClientState( GL_TEXTURE_COORD_ARRAY );
		glTexCoordPointer(2, GL_FLOAT, 0, A.TextureArray[i]);
		glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
		
		glDrawElements(GL_POINTS, A.TotalNonZeroNumOfEachColumn[i], GL_UNSIGNED_INT, A.IndexArray[i]);
	}
	
	glDisable(GL_BLEND);

	////save the immediate texture result into 
	int Width  = x.GetWidth();
	int Height = x.GetHeight();
	glBindTexture(_iTextureTarget, result.GetTextureID());
	glCopyTexSubImage2D(_iTextureTarget, 0, 0, 0, 0, 0, Width, Height);

	return;
}

////////////////////////////////////////////////////////////////////////////////////////
/// Vector(result) = Matrix(A) * Vector(x) in fragment program
/// Here Matrix A is a sparse one which is different from the above one
/// Latest Date : 2004/1/6
/// with Fragment Program
/// BUT now not support GL_POINTS here, WHY?
/// This DON'T work
////////////////////////////////////////////////////////////////////////////////////////
void KrugerSparseMatVectFP(const CKrugerMatrix& A, const CVectorOnGPU& x, CVectorOnGPU& result)
{
	////////////////////////////////////////////
	///If the size is compatible, then error occurs
	int i, nPass = A.GetDim();
	if(nPass != x.GetVectorLength())
	{
		AfxMessageBox("Error to Operate on these two vectors for different size in KrugerSparseMatVectFP!");
		return;
	}

	////save the immediate texture result into result 
	int Width  = x.GetWidth();
	int Height = x.GetHeight();
	unsigned int ShaderID;

	///// Bind the Fragment Program to current drawing
	
	ShaderID = GlobeShaderID[CL_KRUGER_SPARSE_MAT_VECT].ShaderID;
	//LoadFragmentProgramFromFile("..\\ssmatrixvectorRECT.fp", ShaderID);//ssmatrixvectorRECT

	glEnable(GL_FRAGMENT_PROGRAM_NV);

	//////////////////////////////////////////////
	///// Clear the framebuffer firstly to avoid the previous computation
	///// This step is important to avoid computation errors
	//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	
	//glDisable(GL_DEPTH_TEST);
	glPointSize(1);

	/// Bind the predefined fragment program object 
	glBindProgramNV(GL_FRAGMENT_PROGRAM_NV, ShaderID);

	/// Draw all the points in GL_POINTS with texture
	unsigned int _iTextureTarget = x.GetTextureTarget();	

	/// x ---Tex0
	/// result ---Tex1
	glActiveTextureARB(GL_TEXTURE0_ARB);
	glBindTexture(_iTextureTarget, x.GetTextureID());
	glActiveTextureARB(GL_TEXTURE1_ARB);
	glBindTexture(_iTextureTarget, result.GetTextureID());
	result.SetZero();

	for(i=0; i<nPass; i++)
	{
	    glClientActiveTexture(GL_TEXTURE0_ARB);
		glEnableClientState( GL_TEXTURE_COORD_ARRAY );
		glTexCoordPointer(2, GL_FLOAT, 0, A.TextureArray[i]);
		
		glEnableClientState( GL_COLOR_ARRAY );
		glColorPointer(3, GL_FLOAT, 0, A.ColorArray[i]);

		glEnableClientState ( GL_VERTEX_ARRAY );   
		glVertexPointer(2, GL_INT, 0, A.VertexArray[i]);

		glDrawElements(GL_POINTS, A.TotalNonZeroNumOfEachColumn[i], GL_UNSIGNED_INT, A.IndexArray[i]);
	
	    glBindTexture(_iTextureTarget, result.GetTextureID());
		glCopyTexSubImage2D(_iTextureTarget, 0, 0, 0, 0, 0, Width, Height);
	}
	
	glDisable(GL_FRAGMENT_PROGRAM_NV);

	return;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -