啄木鸟图形库的顶点封装类测试代码

啄木鸟图形库的顶点封装类测试代码,分别使用VBO和非VBO的形式实现ArrayOfStructs以及StructsOfArrays这两种顶点数据结构。CVertexIndexDrawArrayGLES2类将提供顶点的相关操作。通过配置VEXIDXDRAWITEM的值配置顶点缓冲,这个以后可以通过模型文件进行集中初始化。

效果图:

啄木鸟图形库的顶点封装类测试代码

 shader的代码,使用GLSL ES 1.0搞的 (OPENGLES 2.0的程序不看shader还真的不知道具体怎么画的)

      static const Char strVertexShader[] =
      "attribute vec4 aPosition;\n"
      "attribute vec4 aColor;\n"
      "uniform  mat4 uFixedViewMatrix;\n"
      "varying vec4 vColor;\n"
      "void main() {\n"
      "gl_Position =  uFixedViewMatrix * aPosition;\n"
      "vColor = aColor;"
      "}\n";

      static const Char strFragmentShader[] =
      "precision mediump float;\n"
      "varying vec4 vColor;\n"
      "void main() {\n"
      "gl_FragColor = vColor;\n"
      "}\n";

测试程序主窗体代码

class CTestForm : public CPeckerActivityForm
 {
private:
	IPeckerCGDriver* m_pCGDriver;

	CGPUProgramGLES2 m_ShaderProgram;
	CVertexIndexDrawArrayGLES2 m_ArrayOfStructsWithOutVBODrawItems;
	CVertexIndexDrawArrayGLES2 m_StructsOfArraysWithOutVBODrawItems;

	CVertexIndexDrawArrayGLES2 m_ArrayOfStructsWithVBODrawItems;
	CVertexIndexDrawArrayGLES2 m_StructsOfArraysWithVBODrawItems;

	CMatrix4 m_MatView;
	CMatrix4 m_MatWorld;
	CMatrix4 m_MatProj;
	CMatrix4 m_MatScale;
	CMatrix4 m_MatOgMvp;
	UInt m_MvpMatHandle;

	typedef struct stVecCoord
	{
		Vector4f pos;
		Vector3f color;
	} VECCOR;
public:
	CTestForm()
	{
		m_pCGDriver = CPeckerCGDrivers::GetCGDriver();
	}
	virtual ~CTestForm()
	{
		;
	}

	virtual HResult InitDisplay(int w, int h)
	{
		if (null == m_pCGDriver)
		{
			return P_ERR;
		}

		static const VECCOR vecClr[] =
		{
		{
		{ -0.5f, 0.0f, 0.0f, 1.0f },
		{ 0.0f, 0.0f, 0.0f } },
		{
		{ -0.5f, 1.0f, 0.0f, 1.0f },
		{ 0.0f, 1.0f, 0.0f } },
		{
		{ 0.5f, 0.0f, 0.0f, 1.0f },
		{ 1.0f, 0.0f, 0.0f } },
		{
		{ 0.5f, 1.0f, 0.0f, 1.0f },
		{ 1.0f, 1.0f, 1.0f } } };

		static const GLfloat fPos[] =
		{ -0.5f, 0.0f, 0.0f, 1.0f, -0.5f, 1.0f, 0.0f, 1.0f, 0.5f, 0.0f, 0.0f,
				1.0f, 0.5f, 1.0f, 0.0f, 1.0f };

		static const GLfloat fColor[] =
				{ 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
						1.0f, 1.0f };

		m_ArrayOfStructsWithOutVBODrawItems.ClearDrawItem();

		VEXIDXDRAWITEM drawItem;

		// only vertex;
		//ArrayOfStructs
		drawItem.m_BuffID = 0;
		drawItem.m_BuffTargetType = PG_ARRAY_BUFFER;
		drawItem.m_BuffUseType = PG_STATIC_DRAW;
		drawItem.m_DrawData = vecClr;
		drawItem.m_DrawBufLen = sizeof(vecClr);
		drawItem.m_FVFFormat = 0;
		drawItem.m_bNormalized = false;
		drawItem.m_nDataFormat = PG_FLOAT;
		drawItem.m_nAttrDataCount = 4;
		drawItem.m_nShaderAttrID = 0;
		drawItem.m_nOffset = 0;
		drawItem.m_Stride = sizeof(VECCOR);

		m_ArrayOfStructsWithOutVBODrawItems.AddDrawItem(drawItem);

		drawItem.m_BuffID = 0;
		drawItem.m_BuffTargetType = PG_ARRAY_BUFFER;
		drawItem.m_BuffUseType = PG_STATIC_DRAW;
		drawItem.m_DrawData = vecClr;
		drawItem.m_DrawBufLen = sizeof(vecClr);
		drawItem.m_FVFFormat = 0;
		drawItem.m_bNormalized = false;
		drawItem.m_nDataFormat = PG_FLOAT;
		drawItem.m_nAttrDataCount = 3;
		drawItem.m_nShaderAttrID = 1;
		drawItem.m_nOffset = sizeof(Vector4f);
		drawItem.m_Stride = sizeof(VECCOR);

		m_ArrayOfStructsWithOutVBODrawItems.AddDrawItem(drawItem);
		m_ArrayOfStructsWithOutVBODrawItems.InitDrawArray(
				NORMAL_VERTEX_DRAWITEM_MODE);

		//StructsOfArrays
		m_StructsOfArraysWithOutVBODrawItems.ClearDrawItem();

		drawItem.m_BuffID = 0;
		drawItem.m_BuffTargetType = PG_ARRAY_BUFFER;
		drawItem.m_BuffUseType = PG_STATIC_DRAW;
		drawItem.m_DrawData = fPos;
		drawItem.m_DrawBufLen = sizeof(fPos);
		drawItem.m_FVFFormat = 0;
		drawItem.m_bNormalized = false;
		drawItem.m_nDataFormat = PG_FLOAT;
		drawItem.m_nAttrDataCount = 4;
		drawItem.m_nShaderAttrID = 0;
		drawItem.m_nOffset = 0;
		drawItem.m_Stride = sizeof(Vector4f);

		m_StructsOfArraysWithOutVBODrawItems.AddDrawItem(drawItem);

		drawItem.m_BuffID = 0;
		drawItem.m_BuffTargetType = PG_ARRAY_BUFFER;
		drawItem.m_BuffUseType = PG_STATIC_DRAW;
		drawItem.m_DrawData = fColor;
		drawItem.m_DrawBufLen = sizeof(fColor);
		drawItem.m_FVFFormat = 0;
		drawItem.m_bNormalized = false;
		drawItem.m_nDataFormat = PG_FLOAT;
		drawItem.m_nAttrDataCount = 3;
		drawItem.m_nShaderAttrID = 1;
		drawItem.m_nOffset = 0;
		drawItem.m_Stride = sizeof(Vector3f);

		m_StructsOfArraysWithOutVBODrawItems.AddDrawItem(drawItem);
		m_StructsOfArraysWithOutVBODrawItems.InitDrawArray(
				NORMAL_VERTEX_DRAWITEM_MODE);

		//VBO ArrayOfStructs
		m_ArrayOfStructsWithVBODrawItems.ClearDrawItem();
		glGenBuffers(1, &(drawItem.m_BuffID));
		drawItem.m_BuffTargetType = PG_ARRAY_BUFFER;
		drawItem.m_BuffUseType = PG_STATIC_DRAW;
		drawItem.m_DrawData = vecClr;
		drawItem.m_DrawBufLen = sizeof(vecClr);
		drawItem.m_FVFFormat = 0;
		drawItem.m_bNormalized = false;
		drawItem.m_nDataFormat = PG_FLOAT;
		drawItem.m_nAttrDataCount = 4;
		drawItem.m_nShaderAttrID = 0;
		drawItem.m_nOffset = 0;
		drawItem.m_Stride = sizeof(VECCOR);
		m_ArrayOfStructsWithVBODrawItems.AddDrawItem(drawItem);

		glGenBuffers(1, &(drawItem.m_BuffID));
		drawItem.m_BuffTargetType = PG_ARRAY_BUFFER;
		drawItem.m_BuffUseType = PG_STATIC_DRAW;
		drawItem.m_DrawData = vecClr;
		drawItem.m_DrawBufLen = sizeof(vecClr);
		drawItem.m_FVFFormat = 0;
		drawItem.m_bNormalized = false;
		drawItem.m_nDataFormat = PG_FLOAT;
		drawItem.m_nAttrDataCount = 3;
		drawItem.m_nShaderAttrID = 1;
		drawItem.m_nOffset = sizeof(Vector4f);
		drawItem.m_Stride = sizeof(VECCOR);

		m_ArrayOfStructsWithVBODrawItems.AddDrawItem(drawItem);
		m_ArrayOfStructsWithVBODrawItems.InitDrawArray(
				VERTEX_VBO_DRAWITEM_MODE);

		//VBO StructsOfArrays
		m_StructsOfArraysWithVBODrawItems.ClearDrawItem();

		glGenBuffers(1, &(drawItem.m_BuffID));
		drawItem.m_BuffTargetType = PG_ARRAY_BUFFER;
		drawItem.m_BuffUseType = PG_STATIC_DRAW;
		drawItem.m_DrawData = fPos;
		drawItem.m_DrawBufLen = sizeof(fPos);
		drawItem.m_FVFFormat = 0;
		drawItem.m_bNormalized = false;
		drawItem.m_nDataFormat = PG_FLOAT;
		drawItem.m_nAttrDataCount = 4;
		drawItem.m_nShaderAttrID = 0;
		drawItem.m_nOffset = 0;
		drawItem.m_Stride = sizeof(Vector4f);

		m_StructsOfArraysWithVBODrawItems.AddDrawItem(drawItem);

		glGenBuffers(1, &(drawItem.m_BuffID));
		drawItem.m_BuffTargetType = PG_ARRAY_BUFFER;
		drawItem.m_BuffUseType = PG_STATIC_DRAW;
		drawItem.m_DrawData = fColor;
		drawItem.m_DrawBufLen = sizeof(fColor);
		drawItem.m_FVFFormat = 0;
		drawItem.m_bNormalized = false;
		drawItem.m_nDataFormat = PG_FLOAT;
		drawItem.m_nAttrDataCount = 3;
		drawItem.m_nShaderAttrID = 1;
		drawItem.m_nOffset = 0;
		drawItem.m_Stride = sizeof(Vector3f);

		m_StructsOfArraysWithVBODrawItems.AddDrawItem(drawItem);
		m_StructsOfArraysWithVBODrawItems.InitDrawArray(
				VERTEX_VBO_DRAWITEM_MODE);

		m_ShaderProgram.CreateShaderSysDefaultProgram(MVP_POS_COLOR_TYPE); //(POS_TYPE);

		ConstStringChars strUniformIName =
		{ "uFixedViewMatrix", strlen("uFixedViewMatrix") };
		m_MvpMatHandle = m_ShaderProgram.GetUniformID(strUniformIName);

		m_MatView = CMatrix4::LookAtRH(CVector3(0.0f, 0.0f, 5.0f),
				CVector3(0.0f, 0.0f, 0.0f), CVector3(0.0f, 1.0f, 0.0f));
		m_MatProj = CMatrix4::PerspectiveFov(PECKER_PI / 4.0f,
				(float) h / (float) w, 0.01f, 1.0f, true);
		m_MatWorld = CMatrix4::IdentityMatrix4(); //CMatrix4::RotationZ(-PECKER_PI);
		m_MatScale = CMatrix4::Scale(0.5f, 0.5f, 1.0f);

		m_MatOgMvp = m_MatProj * m_MatView * m_MatScale * m_MatWorld;

		m_pCGDriver->SetContextViewport(0, 0, w, h);

		return 0;
	}

	virtual HResult RenderFrame(struct stOsContextEngine* pContextEngine)
	{
		if (null == m_pCGDriver)
		{
			return P_ERR;
		}

		UInt iShaderAttr = glGetAttribLocation(m_ShaderProgram.GetProgramID(),
				"aPosition");

		m_pCGDriver->ClearColor(0.5f, 0.0f, 1.0f, 1.0f);
		m_pCGDriver->Clear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);

		HResult hSelectProRes = m_pCGDriver->SelectGPUProgram(&m_ShaderProgram);
		if (P_OK == hSelectProRes && P_OK == m_pCGDriver->Begin())
		{
			static char logInfo[256];
			static Float fAngle = 0.0f;
			CMatrix4 mvp = m_MatOgMvp * CMatrix4::Translation(1.5f, -1.5f, 0.0f);
			//* CMatrix4::RotationZ(fAngle);
			fAngle += 0.1f;
			if (fAngle > 6.29)
				fAngle = 0.0f;

			// ArrayOfStructs Without VBO
			m_ShaderProgram.SetUniformMatrixArray(m_MvpMatHandle, 1,
					(Matrix4*) mvp.GetData());
			m_ArrayOfStructsWithOutVBODrawItems.PreArrayDraw();
			m_ArrayOfStructsWithOutVBODrawItems.DrawVertexArray(
					PG_TRIANGLE_STRIP, 0, 4);
			m_ArrayOfStructsWithOutVBODrawItems.FinArrayDraw();

			// StructsOfArrays Without VBO
			mvp = m_MatOgMvp * CMatrix4::Translation(1.5f, 1.5f, 0.0f);
					//* CMatrix4::RotationZ(-fAngle);
			m_ShaderProgram.SetUniformMatrixArray(m_MvpMatHandle, 1,
					(Matrix4*) mvp.GetData());
			m_StructsOfArraysWithOutVBODrawItems.PreArrayDraw();
			m_StructsOfArraysWithOutVBODrawItems.DrawVertexArray(
					PG_TRIANGLE_STRIP, 0, 4);
			m_StructsOfArraysWithOutVBODrawItems.FinArrayDraw();

			// ArrayOfStructs With VBO
			mvp = m_MatOgMvp * CMatrix4::Translation(-1.5f, 1.5f, 0.0f);
				//	* CMatrix4::RotationZ(fAngle);
			m_ShaderProgram.SetUniformMatrixArray(m_MvpMatHandle, 1,
					(Matrix4*) mvp.GetData());
			m_ArrayOfStructsWithVBODrawItems.PreArrayDraw();
			m_ArrayOfStructsWithVBODrawItems.DrawVertexArray(
					PG_TRIANGLE_STRIP, 0, 4);
			m_ArrayOfStructsWithVBODrawItems.FinArrayDraw();

			// StructsOfArrays With VBO
			mvp = m_MatOgMvp * CMatrix4::Translation(-1.5f, -1.5f, 0.0f);
			//* CMatrix4::RotationZ(-fAngle);
			m_ShaderProgram.SetUniformMatrixArray(m_MvpMatHandle, 1,
					(Matrix4*) mvp.GetData());
			m_StructsOfArraysWithVBODrawItems.PreArrayDraw();
			m_StructsOfArraysWithVBODrawItems.DrawVertexArray(
					PG_TRIANGLE_STRIP, 0, 4);
			m_StructsOfArraysWithVBODrawItems.FinArrayDraw();

			m_pCGDriver->End();
		}
		else
		{
			static char logInfo[256];
			sprintf(logInfo, "SelectGPUProgram RES = %X", hSelectProRes);
			CPeckerLogger::GetSignleton()->LogOut(LOGINFO_ERROR, "RenderFrame",
					logInfo);
		}

		return 0;
	}
	//virtual HResult TermDisplay(struct stOsContextEngine* pContextEngine) = 0;
	virtual HResult SaveState(struct stOsContextEngine* pContextEngine)
	{
		return 0;
	}
	virtual HResult LoadState(struct stOsContextEngine* pContextEngine)
	{
		return 0;
	}
	virtual SInt OnInputEvent(Handle hEventHandle)
	{
		return 0;
	}
	virtual void OnWndCmdProc(struct stOsContextEngine* pContextEngine,
			Long nCmdCode, Long wPrams)
	{

	}
};

你可能感兴趣的:(啄木鸟图形库的顶点封装类测试代码)