Ejemplo n.º 1
0
Graphics::ShaderProgram* ShaderManager::GetNewShaderProgram(const istring &filename)
{
    auto strings = Utility::File::ParseToStrings(filename);
    auto shader = new Graphics::ShaderProgram();
    shader->CreateProgram();
    for (auto str : strings)
    {
        auto type = str[0];
        auto sfile = str.substr(2);
        switch (type)
        {
        case 'F':
            shader->CompileShader(sfile.c_str(), GL_FRAGMENT_SHADER, false);
            break;
        case 'V':
            shader->CompileShader(sfile.c_str(), GL_VERTEX_SHADER, false);
            break;
        default:
            std::cout << "Invalid shader Type=[" << type << "] specified in FileName=[" << filename << "]";
            return nullptr;
        }
    }
    shader->LinkProgram();
    shader->SetShaderName(filename);
    return shader;
}
Ejemplo n.º 2
0
CQuadMT::CQuadMT()
{
	m_iNumVtx = QUAD_NUM;
	m_pPoints = NULL; m_pNormals = NULL; m_pTex1 = NULL;

	m_pPoints  = new vec4[m_iNumVtx];
	m_pNormals = new vec3[m_iNumVtx];
	m_pColors  = new vec4[m_iNumVtx]; 
	m_pTex1 = new vec2[m_iNumVtx];	// 產生所需的貼圖座標
	m_pTex2 = new vec2[m_iNumVtx];  // 產生 light map 所需的貼圖座標

	m_pPoints[0] = vec4( -0.5f, 0.0f,  0.5f, 1.0f);
	m_pPoints[1] = vec4(  0.5f, 0.0f,  0.5f, 1.0f);
	m_pPoints[2] = vec4(  0.5f, 0.0f, -0.5f, 1.0f);
	m_pPoints[3] = vec4( -0.5f, 0.0f,  0.5f, 1.0f);
	m_pPoints[4] = vec4(  0.5f, 0.0f, -0.5f, 1.0f);
	m_pPoints[5] = vec4( -0.5f, 0.0f, -0.5f, 1.0f);

	m_pNormals[0] = vec3(  0, 1.0f, 0);  // Normal Vector 的 W 為 0
	m_pNormals[1] = vec3(  0, 1.0f, 0);
	m_pNormals[2] = vec3(  0, 1.0f, 0);
	m_pNormals[3] = vec3(  0, 1.0f, 0);
	m_pNormals[4] = vec3(  0, 1.0f, 0);
	m_pNormals[5] = vec3(  0, 1.0f, 0);

	m_pColors[0] = vec4( 1.0f, 1.0f,  1.0f, 1.0f);  // (r, g, b, a)
	m_pColors[1] = vec4( 1.0f, 1.0f,  1.0f, 1.0f);
	m_pColors[2] = vec4( 1.0f, 1.0f,  1.0f, 1.0f);
	m_pColors[3] = vec4( 1.0f, 1.0f,  1.0f, 1.0f);
	m_pColors[4] = vec4( 1.0f, 1.0f,  1.0f, 1.0f);
	m_pColors[5] = vec4( 1.0f, 1.0f,  1.0f, 1.0f);


	m_pTex1[0] = vec2(0.0f, 0.0f);
	m_pTex1[1] = vec2(1.0f, 0.0f);
	m_pTex1[2] = vec2(1.0f, 1.0f);
	m_pTex1[3] = vec2(0.0f, 0.0f);
	m_pTex1[4] = vec2(1.0f, 1.0f);
	m_pTex1[5] = vec2(0.0f, 1.0f);

	m_pTex2[0] = vec2(0.0f, 0.0f);
	m_pTex2[1] = vec2(1.0f, 0.0f);
	m_pTex2[2] = vec2(1.0f, 1.0f);
	m_pTex2[3] = vec2(0.0f, 0.0f);
	m_pTex2[4] = vec2(1.0f, 1.0f);
	m_pTex2[5] = vec2(0.0f, 1.0f);


	for( int i = 0 ; i < m_iNumVtx ; i++ ) m_pColors[i] = vec4(-1.0f,-1.0f,-1.0f,1.0f);
	SetShaderName("vsLightMapping.glsl", "fsLightMapping.glsl");

	// Create and initialize a buffer object ,將此部分的設定移入 SetShader 中
	// CreateBufferObject();
}
Ejemplo n.º 3
0
CWireSphere::CWireSphere(const GLfloat fRadius, const int iSlices, const  int iStacks)
{
    GLfloat drho = (GLfloat)(3.141592653589) / (GLfloat) iStacks;  
    GLfloat dtheta = 2.0f * (GLfloat)(3.141592653589) / (GLfloat) iSlices;  
    GLfloat ds = 1.0f / (GLfloat) iSlices;  
    GLfloat dt = 1.0f / (GLfloat) iStacks;  
    GLfloat t = 1.0f;      
    GLfloat s = 0.0f;  
    GLint i, j;     // Looping variables  
	int idx = 0; // 儲存 vertex 順序的索引值

	m_fRadius = fRadius;
	m_iSlices = iSlices;
	m_iStacks = iStacks;
	m_iNumVtx = iStacks*(2*(iSlices+1));

	m_pPoints = NULL; m_pNormals = NULL; m_pTex1 = NULL;

	m_pPoints  = new vec4[m_iNumVtx];
	m_pNormals = new vec3[m_iNumVtx];
	m_pColors  = new vec4[m_iNumVtx]; 
	m_pTex1    = new vec2[m_iNumVtx];


	for (i = 0; i < iStacks; i++ ) {  
		GLfloat rho = (GLfloat)i * drho;  
		GLfloat srho = (GLfloat)(sin(rho));  
		GLfloat crho = (GLfloat)(cos(rho));  
		GLfloat srhodrho = (GLfloat)(sin(rho + drho));  
		GLfloat crhodrho = (GLfloat)(cos(rho + drho));  
		
		// Many sources of OpenGL sphere drawing code uses a triangle fan  
		// for the caps of the sphere. This however introduces texturing   
		// artifacts at the poles on some OpenGL implementations  
		s = 0.0f;  
		for ( j = 0; j <= iSlices; j++) {  
            GLfloat theta = (j == iSlices) ? 0.0f : j * dtheta;  
            GLfloat stheta = (GLfloat)(-sin(theta));  
            GLfloat ctheta = (GLfloat)(cos(theta));  
  
            GLfloat x = stheta * srho;  
            GLfloat y = ctheta * srho;  
            GLfloat z = crho;  
              
			m_pPoints[idx].x = x * m_fRadius;
			m_pPoints[idx].y = y * m_fRadius;
			m_pPoints[idx].z = z * m_fRadius;
			m_pPoints[idx].w = 1;

			m_pNormals[idx].x = x;
			m_pNormals[idx].y = y;
			m_pNormals[idx].z = z;

			m_pTex1[idx].x = s;
			m_pTex1[idx].y = t; // 設定貼圖座標
			idx++;

            x = stheta * srhodrho;  
            y = ctheta * srhodrho;  
            z = crhodrho;

			m_pPoints[idx].x = x * m_fRadius;
			m_pPoints[idx].y = y * m_fRadius;
			m_pPoints[idx].z = z * m_fRadius;
			m_pPoints[idx].w = 1;

			m_pNormals[idx].x = x;
			m_pNormals[idx].y = y;
			m_pNormals[idx].z = z;

			m_pTex1[idx].x = s;
			m_pTex1[idx].y = t - dt; // 設定貼圖座標
			idx++;
			s += ds; 
		}   
		t -= dt;  
	}  
	// 預設將所有的面都設定成灰色
	for( int i = 0 ; i < m_iNumVtx ; i++ ) m_pColors[i] = vec4(-1.0f,-1.0f,-1.0f,1.0f);

#ifdef LIGHTING_WITHCPU
	// Default Set shader's name
	SetShaderName("vsLighting_CPU.glsl", "fsLighting_CPU.glsl");
#else // lighting with GPU
#ifdef PERVERTEX_LIGHTING
	SetShaderName("vsLighting_GPU.glsl", "fsLighting_GPU.glsl");
#else
	SetShaderName("vsPerPixelLighting.glsl", "fsPerPixelLighting.glsl");
#endif
#endif  

	// Create and initialize a buffer object ,將此部分的設定移入 SetShader 中
	// CreateBufferObject();

	// 設定材質
	SetMaterials(vec4(0), vec4(0.5f, 0.5f, 0.5f, 1), vec4(1.0f, 1.0f, 1.0f, 1.0f));
	SetKaKdKsShini(0, 0.8f, 0.2f, 1);
}
Ejemplo n.º 4
0
CQuad::CQuad()
{
	m_iNumVtx = QUAD_NUM;
	m_pPoints = NULL; m_pNormals = NULL; m_pTex1 = NULL;

	m_pPoints  = new vec4[m_iNumVtx];
	m_pNormals = new vec3[m_iNumVtx];
	m_pColors  = new vec4[m_iNumVtx]; 
	m_pTex1 = new vec2[m_iNumVtx];		// 產生所需的貼圖座標
#if MULTITEXTURE >= LIGHT_MAP
	m_pTex2 = new vec2[m_iNumVtx];  // 產生 light map 所需的貼圖座標
#endif
#if MULTITEXTURE >= NORMAL_MAP
	m_pTex3 = new vec2[m_iNumVtx];	// 產生 normal map 所需的貼圖座標
	// 產生所需要的 Tanget vector for each vertex
	m_pTangentV = new vec3[m_iNumVtx];
#endif

	m_pPoints[0] = vec4( -0.5f, 0.0f,  0.5f, 1.0f);
	m_pPoints[1] = vec4(  0.5f, 0.0f,  0.5f, 1.0f);
	m_pPoints[2] = vec4(  0.5f, 0.0f, -0.5f, 1.0f);
	m_pPoints[3] = vec4( -0.5f, 0.0f,  0.5f, 1.0f);
	m_pPoints[4] = vec4(  0.5f, 0.0f, -0.5f, 1.0f);
	m_pPoints[5] = vec4( -0.5f, 0.0f, -0.5f, 1.0f);

	m_pNormals[0] = vec3(  0, 1.0f, 0);  // Normal Vector 的 W 為 0
	m_pNormals[1] = vec3(  0, 1.0f, 0);
	m_pNormals[2] = vec3(  0, 1.0f, 0);
	m_pNormals[3] = vec3(  0, 1.0f, 0);
	m_pNormals[4] = vec3(  0, 1.0f, 0);
	m_pNormals[5] = vec3(  0, 1.0f, 0);

	m_pColors[0] = vec4( 1.0f, 1.0f,  1.0f, 1.0f);  // (r, g, b, a)
	m_pColors[1] = vec4( 1.0f, 1.0f,  1.0f, 1.0f);
	m_pColors[2] = vec4( 1.0f, 1.0f,  1.0f, 1.0f);
	m_pColors[3] = vec4( 1.0f, 1.0f,  1.0f, 1.0f);
	m_pColors[4] = vec4( 1.0f, 1.0f,  1.0f, 1.0f);
	m_pColors[5] = vec4( 1.0f, 1.0f,  1.0f, 1.0f);

	m_pTex1[0] = vec2(0.0f, 0.0f);
	m_pTex1[1] = vec2(1.0f, 0.0f);
	m_pTex1[2] = vec2(1.0f, 1.0f);
	m_pTex1[3] = vec2(0.0f, 0.0f);
	m_pTex1[4] = vec2(1.0f, 1.0f);
	m_pTex1[5] = vec2(0.0f, 1.0f);

#if MULTITEXTURE >= LIGHT_MAP
	m_pTex2[0] = vec2(0.0f, 0.0f);
	m_pTex2[1] = vec2(1.0f, 0.0f);
	m_pTex2[2] = vec2(1.0f, 1.0f);
	m_pTex2[3] = vec2(0.0f, 0.0f);
	m_pTex2[4] = vec2(1.0f, 1.0f);
	m_pTex2[5] = vec2(0.0f, 1.0f);
#endif
#if MULTITEXTURE >= NORMAL_MAP
	m_pTex3[0] = vec2(0.0f, 0.0f);
	m_pTex3[1] = vec2(1.0f, 0.0f);
	m_pTex3[2] = vec2(1.0f, 1.0f);
	m_pTex3[3] = vec2(0.0f, 0.0f);
	m_pTex3[4] = vec2(1.0f, 1.0f);
	m_pTex3[5] = vec2(0.0f, 1.0f);
	// 計算 tangent vector
	for (int i = 0; i < 6; i += 3) { // 三個 vertex 一組
		float dU1 = m_pTex1[i + 1].x - m_pTex1[i].x;
		float dV1 = m_pTex1[i + 1].y - m_pTex1[i].y;
		float dU2 = m_pTex1[i + 2].x - m_pTex1[i].x;
		float dV2 = m_pTex1[i + 2].y - m_pTex1[i].y;
		float f = 1.0f/(dU1 * dV2 - dU2*dV1);
		vec4 E1 = m_pPoints[i + 1] - m_pPoints[i];
		vec4 E2 = m_pPoints[i + 2] - m_pPoints[i];

		vec3 tangent;
		tangent.x = f*(dV2 * E1.x + E2.x * (-dV1));
		tangent.y = f*(dV2 * E1.y + E2.y * (-dV1));
		tangent.z = f*(dV2 * E1.z + E2.z * (-dV1));

		m_pTangentV[i] += tangent;
		m_pTangentV[i + 1] += tangent;
		m_pTangentV[i + 2] = tangent;
	}
	for (int i = 0; i < 6; i++)
		m_pTangentV[i] = normalize(m_pTangentV[i]);
#endif

	for( int i = 0 ; i < m_iNumVtx ; i++ ) m_pColors[i] = vec4(-1.0f,-1.0f,-1.0f,1.0f);

#ifdef PERVERTEX_LIGHTING
	SetShaderName("vsPerVtxLighting.glsl", "fsPerVtxLighting.glsl");
#else
	SetShaderName("vsPerPixelLighting.glsl", "fsPerPixelLighting.glsl");
#endif

}