/*!**************************************************************************** @Function SetupView() @Return N/A @Description Sets up the view matrices required for the training course ******************************************************************************/ void OGLES3EdgeDetection::SetupView(bool bRotate) { PVRTVec3 vEyePos, vLookAt, vCamUp=PVRTVec3(0.00f, 1.0001f, 0.00f); // Checks if a camera is in the scene, if there is, uses it, otherwise creates one. if(m_Scene.nNumCamera>0) { // vLookAt is taken from the target node, or.. if(m_Scene.pCamera[0].nIdxTarget != -1) m_Scene.GetCameraPos(vEyePos, vLookAt, 0); // ..it is calculated from the rotation else m_Scene.GetCamera(vEyePos, vLookAt, vCamUp, 0); } else { //Creates a camera if none exist. vEyePos = PVRTVec3(0, 0, 200); vLookAt = PVRTVec3(0, 0, 0); } // Set the view and projection matrix for rendering to texture. m_mR2TView = PVRTMat4::LookAtRH(vEyePos, vLookAt, vCamUp); m_mR2TProjection = PVRTMat4::PerspectiveFovRH(PVRT_PI*0.125, (float)m_i32TexWidth/(float)m_i32TexHeight, g_fCameraNear, g_fCameraFar, PVRTMat4::OGL, bRotate); // The textured quad this program renders to will be rendered full screen, in orthographic mode, so doesn't need camera variables to be set. }
/*!**************************************************************************** @Function DrawIntoParaboloids @Description Draws the scene from the position of the ball into the two paraboloid textures. ******************************************************************************/ void OGLES2Glass::DrawIntoParaboloids(PVRTVec3 position) { // Bind and clear the paraboloid framebuffer glBindFramebuffer(GL_FRAMEBUFFER, m_uiParaboloidFramebuffer); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Set the viewport to the left glViewport(0, 0, g_ParaboloidTexSize, g_ParaboloidTexSize); // Create the first view matrix and make it flip the X coordinate PVRTMat4 mView = PVRTMat4::LookAtRH(position, position + PVRTVec3(0, 0, 1), PVRTVec3(0, 1, 0)); mView = PVRTMat4::Scale(-1.0f, 1.0f, 1.0f) * mView; // Switch to front face culling due to flipped winding order glCullFace(GL_FRONT); // Draw the balloons DrawBalloons(&m_ParaboloidProgram, PVRTMat4::Identity(), mView, m_mModels, 2); // Switch back to back face culling glCullFace(GL_BACK); // Shift the viewport to the right glViewport(g_ParaboloidTexSize, 0, g_ParaboloidTexSize, g_ParaboloidTexSize); // Create the second view matrix mView = PVRTMat4::LookAtRH(position, position - PVRTVec3(0, 0, 1), PVRTVec3(0, 1, 0)); // Draw the balloons DrawBalloons(&m_ParaboloidProgram, PVRTMat4::Identity(), mView, m_mModels, 2); // Bind back the original framebuffer and reset the viewport glBindFramebuffer(GL_FRAMEBUFFER, m_iOriginalFramebuffer); glViewport(0, 0, PVRShellGet(prefWidth), PVRShellGet(prefHeight)); }
/*!**************************************************************************** @Function InitView @Return bool true if no error occurred @Description Code in InitView() will be called by PVRShell upon initialization or after a change in the rendering context. Used to initialize variables that are dependent on the rendering context (e.g. textures, vertex buffers, etc.) ******************************************************************************/ bool OGLESPVRScopeRemote::InitView() { CPPLProcessingScoped PPLProcessingScoped(m_psSPSCommsData, __FUNCTION__, static_cast<unsigned int>(strlen(__FUNCTION__)), m_i32FrameCounter); CPVRTString ErrorStr; /* Initialize Print3D */ bool bRotate = PVRShellGet(prefIsRotated) && PVRShellGet(prefFullScreen); if(m_Print3D.SetTextures(0,PVRShellGet(prefWidth),PVRShellGet(prefHeight), bRotate) != PVR_SUCCESS) { PVRShellSet(prefExitMessage, "ERROR: Cannot initialise Print3D\n"); return false; } // Sets the clear color glClearColor(0.6f, 0.8f, 1.0f, 1.0f); // Enables texturing glEnable(GL_TEXTURE_2D); // Initialize VBO data if(!LoadVbos(&ErrorStr)) { PVRShellSet(prefExitMessage, ErrorStr.c_str()); return false; } /* Load textures */ if(!LoadTextures(&ErrorStr)) { PVRShellSet(prefExitMessage, ErrorStr.c_str()); return false; } /* Calculate the projection and view matrices */ m_mProjection = PVRTMat4::PerspectiveFovRH(PVRT_PIf/6, (float)PVRShellGet(prefWidth)/(float)PVRShellGet(prefHeight), CAM_NEAR, CAM_FAR, PVRTMat4::OGL, bRotate); m_mView = PVRTMat4::LookAtRH(PVRTVec3(0, 0, 75.0f), PVRTVec3(0, 0, 0), PVRTVec3(0, 1, 0)); // Enable the depth test glEnable(GL_DEPTH_TEST); // Enable culling glEnable(GL_CULL_FACE); // Initialise variables used for the animation m_fFrame = 0; m_iTimePrev = PVRShellGetTime(); return true; }
void Material::loadPODMaterialValues(const SPODMaterial& sPODMaterial) { // set material values m_fOpacity = sPODMaterial.fMatOpacity; /*! Material opacity (used with vertex alpha ?) */ m_vAmbient = PVRTVec3(sPODMaterial.pfMatAmbient); /*! Ambient RGB value */ m_vDiffuse = PVRTVec3(sPODMaterial.pfMatDiffuse); /*! Diffuse RGB value */ m_vSpecular = PVRTVec3(sPODMaterial.pfMatSpecular); /*! Specular RGB value */ m_fShininess = sPODMaterial.fMatShininess; /*! Material shininess */ }
void SimpleCamera::getToAndUp(PVRTVec3& vTo, PVRTVec3& vUp) const { vTo = PVRTVec3(f2vt(0.0f),f2vt(0.0f),f2vt(1.0f)); vUp = PVRTVec3(f2vt(0.0f),f2vt(1.0f),f2vt(0.0f)); PVRTMat3 mRotY = PVRTMat3::RotationY(m_fHeading); PVRTMat3 mRotX = PVRTMat3::RotationX(m_fElevation); vTo = (vTo*mRotX)*mRotY; vUp = (vUp*mRotX)*mRotY; }
OGLESEvilSkull() : m_puiVbo(0), m_puiIndexVbo(0), m_pMorphedVertices(0), m_pAVGVertices(0), m_i32BaseAnim(0), m_i32TargetAnim(1), m_i32Frame(0) { for(unsigned int i = 0; i < g_ui32NoOfMorphTargets; ++i) m_pDiffVertices[i] = 0; // Setup base constants in contructor // Camera and Light details m_LightPos = PVRTVec4(f2vt(-1.0f), f2vt(1.0f), f2vt(1.0f), f2vt(0.0f)); m_CameraPos = PVRTVec3(f2vt(0.0f), f2vt(0.0f), f2vt(300.0f)); m_CameraTo = PVRTVec3(f2vt(0.0f), f2vt(-30.0f), f2vt(0.0f)); m_CameraUp = PVRTVec3(f2vt(0.0f), f2vt(1.0f), f2vt(0.0f)); // Animation Table m_fSkullWeights[0] = 0.0f; m_fSkullWeights[1] = 1.0f; m_fSkullWeights[2] = 0.0f; m_fSkullWeights[3] = 0.0f; m_fSkullWeights[4] = 0.0f; m_fExprTable[0][0] = 1.0f; m_fExprTable[1][0] = 1.0f; m_fExprTable[2][0] = 1.0f; m_fExprTable[3][0] = 1.0f; m_fExprTable[0][1] = 0.0f; m_fExprTable[1][1] = 0.0f; m_fExprTable[2][1] = 0.0f; m_fExprTable[3][1] = 1.0f; m_fExprTable[0][2] = 0.0f; m_fExprTable[1][2] = 0.0f; m_fExprTable[2][2] = 1.0f; m_fExprTable[3][2] = 1.0f; m_fExprTable[0][3] = 0.3f; m_fExprTable[1][3] = 0.0f; m_fExprTable[2][3] = 0.3f; m_fExprTable[3][3] = 0.0f; m_fExprTable[0][4] =-1.0f; m_fExprTable[1][4] = 0.0f; m_fExprTable[2][4] = 0.0f; m_fExprTable[3][4] = 0.0f; m_fExprTable[0][5] = 0.0f; m_fExprTable[1][5] = 0.0f; m_fExprTable[2][5] =-0.7f; m_fExprTable[3][5] = 0.0f; m_fExprTable[0][6] = 0.0f; m_fExprTable[1][6] = 0.0f; m_fExprTable[2][6 ]= 0.0f; m_fExprTable[3][6] =-0.7f; m_fJawRotation[0] = 45.0f; m_fJawRotation[1] = 25.0f; m_fJawRotation[2] = 40.0f; m_fJawRotation[3] = 20.0f; m_fJawRotation[4] = 45.0f; m_fJawRotation[5] = 25.0f; m_fJawRotation[6] = 30.0f; m_fBackRotation[0] = 0.0f; m_fBackRotation[1] = 25.0f; m_fBackRotation[2] = 40.0f; m_fBackRotation[3] = 90.0f; m_fBackRotation[4] = 125.0f; m_fBackRotation[5] = 80.0f; m_fBackRotation[6] = 30.0f; }
void SimpleCamera::getTargetAndUp(PVRTVec3& vTarget, PVRTVec3& vUp) const { vTarget = PVRTVec3(f2vt(0.0f),f2vt(0.0f),f2vt(1.0f)); vUp = PVRTVec3(f2vt(0.0f),f2vt(1.0f),f2vt(0.0f)); PVRTMat3 mRotY = PVRTMat3::RotationY(m_fHeading); PVRTMat3 mRotX = PVRTMat3::RotationX(m_fElevation); vTarget = (vTarget*mRotX)*mRotY; vUp = (vUp*mRotX)*mRotY; vTarget +=m_vPosition; }
// --------------------------------------------------------------- bool MyPVRDemo::RenderScene() { // --- Work out DT unsigned long ulPrevTime = m_ulCurrTime; m_ulCurrTime = PVRShellGetTime(); m_fDT = ((float)m_ulCurrTime - (float)ulPrevTime) * 0.001f; // Calculate a new light matrix PVRTVec3 vLightPos = PVRTVec4(m_vLightPos, 1.0f) * PVRTMat4::RotationY(m_fLightAngle); m_mxLightView = PVRTMat4::LookAtRH(vLightPos, PVRTVec3(0,25,0), PVRTVec3(0,1,0)); // --- Render the scene from the light's POV RenderShadowScene(); // --- Clear buffers glViewport(0, 0, PVRShellGet(prefWidth), PVRShellGet(prefHeight)); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); PVRTMat4 mxCam = m_mxCam * PVRTMat4::RotationY(m_fAngleY); PVRTMat4 mxModel = PVRTMat4::Identity(); // --- Draw the Statue glUseProgram(m_StatueShader.uiID); glBindTexture(GL_TEXTURE_2D, m_tex[enumTEXTURE_StatueNormals]); RenderStatue(mxModel, mxCam, vLightPos, &m_StatueShader); // --- Draw the Statue reflected glCullFace(GL_FRONT); PVRTMat4 mxModelRefl = PVRTMat4::Scale(1,-1,1) * mxModel; RenderStatue(mxModelRefl, mxCam, vLightPos, &m_StatueShader); glCullFace(GL_BACK); // --- Draw the Floor (with shadow) RenderCurch(mxCam); // --- Render the bloom effect RenderBloom(mxModel, mxCam, vLightPos); // --- Increment the camera angle m_fAngleY += 0.5f * m_fDT; // --- Increment the light angle m_fLightAngle += 0.5f * m_fDT; return true; }
/*!**************************************************************************** @Function LoadTextures @Return bool true if no error occured @Description Loads the textures required for this training course ******************************************************************************/ bool OGLES3EdgeDetection::LoadTextures(CPVRTString* pErrorStr) { // Reads in and stores the diffuse color of every material used in the scene. m_pvColorData = new PVRTVec3[m_Scene.nNumMaterial]; for (int i=0; i<(int)m_Scene.nNumMaterial; i++) m_pvColorData[i] = PVRTVec3(m_Scene.pMaterial[i].pfMatDiffuse); /* By setting textures up to active textures other than GL_TEXTURE0 (the default) we can avoid needing to bind them again later, as Print3D binds to 0, meaning we'd need to reset the binding each frame. This way keeps the rebindings to a minimum; however there are only 8 active texture units so this can only be done up to a point.*/ // Create the color texture and bind it to texture unit 1. glGenTextures(1, &m_uiColorTexture); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, m_uiColorTexture); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_i32TexWidth, m_i32TexHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); // Sets texture parameters. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); // Error checks color texture creation. GLint status = glGetError(); if (status != GL_NO_ERROR) { *pErrorStr += "Error: Could not create color textures."; return false; } // Rebinds the initial texture unit. glActiveTexture(GL_TEXTURE0); return true; }
/*!**************************************************************************** @Function RenderScene @Return bool true if no error occured @Description Main rendering loop function of the program. The shell will call this function every frame. eglSwapBuffers() will be performed by PVRShell automatically. PVRShell will also manage important OS events. Will also manage relevent OS events. The user has access to these events through an abstraction layer provided by PVRShell. ******************************************************************************/ bool OGLES2Glass::RenderScene() { if (PVRShellIsKeyPressed(PVRShellKeyNameLEFT)) m_iEffect -= 1; if (PVRShellIsKeyPressed(PVRShellKeyNameRIGHT)) m_iEffect += 1; m_iEffect = (m_iEffect + g_iNumEffects) % g_iNumEffects; UpdateScene(); DrawIntoParaboloids(PVRTVec3(0, 0, 0)); // Clear the color and depth buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Draw the ball DrawBall(); // Draw the balloons DrawBalloons(&m_DefaultProgram, m_mProjection, m_mView, m_mModels, 2); // Draw the skybox DrawSkybox(); // Displays the demo name using the tools. For a detailed explanation, see the training course IntroducingPVRTools m_Print3D.DisplayDefaultTitle("Glass", g_aszEffectNames[m_iEffect], ePVRTPrint3DSDKLogo); m_Print3D.Flush(); return true; }
/*!**************************************************************************** @Function RenderScene @Return bool true if no error occured @Description Main rendering loop function of the program. The shell will call this function every frame. eglSwapBuffers() will be performed by PVRShell automatically. PVRShell will also manage important OS events. Will also manage relevent OS events. The user has access to these events through an abstraction layer provided by PVRShell. ******************************************************************************/ bool OGLES2Fog::RenderScene() { // Clear the color and depth buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Keyboard input (cursor to change fog function) if (PVRShellIsKeyPressed(PVRShellKeyNameLEFT)) { m_eFogMode = EFogMode((m_eFogMode + eNumFogModes - 1) % eNumFogModes); } if (PVRShellIsKeyPressed(PVRShellKeyNameRIGHT)) { m_eFogMode = EFogMode((m_eFogMode + 1) % eNumFogModes); } // Use the loaded shader program glUseProgram(m_ShaderProgram.uiId); // Bind texture glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, m_uiTexture); // Set uniforms glUniform1i(m_ShaderProgram.uiFogFuncLoc, m_eFogMode); // Rotate and translate the model matrix PVRTMat4 mModel = PVRTMat4::RotationY(m_fAngleY); m_fAngleY += PVRT_PI / 90; mModel.preTranslate(0, 0, 500 * cos(m_fPositionZ) - 450); m_fPositionZ += (2*PVRT_PI)*0.0008f; // Feed Projection and Model View matrices to the shaders PVRTMat4 mModelView = m_mView * mModel; PVRTMat4 mMVP = m_mProjection * mModelView; glUniformMatrix4fv(m_ShaderProgram.uiModelViewLoc, 1, GL_FALSE, mModelView.ptr()); glUniformMatrix4fv(m_ShaderProgram.uiMVPMatrixLoc, 1, GL_FALSE, mMVP.ptr()); // Pass the light direction transformed with the inverse of the ModelView matrix // This saves the transformation of the normals per vertex. A simple dot3 between this direction // and the un-transformed normal will allow proper smooth shading. PVRTVec3 vMsLightDir = (PVRTMat3(mModel).inverse() * PVRTVec3(1, 1, 1)).normalized(); glUniform3fv(m_ShaderProgram.uiLightDirLoc, 1, vMsLightDir.ptr()); /* Now that the model-view matrix is set and the materials ready, call another function to actually draw the mesh. */ DrawMesh(0); // Displays the demo name using the tools. For a detailed explanation, see the training course IntroducingPVRTools m_Print3D.DisplayDefaultTitle("Fog", "", ePVRTPrint3DLogoIMG); m_Print3D.Print3D(0.3f, 7.5f, 0.75f, PVRTRGBA(255,255,255,255), "Fog Mode: %s", g_FogFunctionList[m_eFogMode]); m_Print3D.Flush(); return true; }
void BulletNode::ShootState() { SetupTarget(); mBulletStartingPos = mBulletPos = mShooterNode->GetWorldTranslation(); if (mTargetNode) mTargetPos = mTargetNode->GetWorldTranslation(); PVRTVec3 dir = mBulletPos - mTargetPos; mBulletPos -= dir * 0.1f; float rotAngle = atan2f(dir.z, dir.x) + PVRT_PI*0.5f; static int randIndex = 0; const static float randTable[RAND_TABLE_SIZE] = {1,-4,3,-2,5,-3,6,-5,2,-3,4,-2,4,-5,5,-3,4,-5,6,-3}; if (mNoisy) { rotAngle = rotAngle + 0.25f * randTable[randIndex++ % RAND_TABLE_SIZE] * (0.017453292f); } PVRTQUATERNION shooterQuat; PVRTMatrixQuaternionRotationAxis(shooterQuat, PVRTVec3(0,1,0),rotAngle); PVRTMATRIX shooterRotaionMtx; PVRTMatrixRotationQuaternion(shooterRotaionMtx, shooterQuat); PVRTMat4 mat(shooterRotaionMtx.f); PVRTVec4 zVec(0, 0 , 1.0f, 1.0f); zVec = mat * zVec; /* if (mAutoAim) { mDir = mBulletPos - mTargetNode->GetWorldTranslation(); mDir.normalize(); } */ mDir.x = zVec.x; mDir.y = zVec.y; mDir.z = zVec.z; //mDir.z = 0; //mDir.x = -1; mModelData->GetRoot()->SetRotation(shooterQuat); mState = state_fly; }
/*!**************************************************************************** @Function RenderScene @Return bool true if no error occured @Description Main rendering loop function of the program. The shell will call this function every frame. eglSwapBuffers() will be performed by PVRShell automatically. PVRShell will also manage important OS events. Will also manage relevent OS events. The user has access to these events through an abstraction layer provided by PVRShell. ******************************************************************************/ bool OGLES2ParticleSystem::RenderScene() { HandleInput(); UpdateParticles(); UpdateFramerateCounter(); float time_delta = PVRShellGetTime() / 10000.0f; PVRTVec3 vFrom = PVRTVec3((float) sin(time_delta) * 50.0f, 30.0f, (float) cos(time_delta) * 50.0f); m_mView = PVRTMat4::LookAtRH(vFrom, PVRTVec3(0.0f, 5.0f, 0.0f), PVRTVec3(0.0f, 1.0f, 0.0f)); m_mViewProjection = m_mProjection * m_mView; // Clear colour and depth buffers glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Enables depth testing glEnable(GL_DEPTH_TEST); // Render floor RenderFloor(); for (unsigned int i=0; i < g_cuiNumSpheres; i++) RenderSphere(g_caSpheres[i].aPosition, g_caSpheres[i].fRadius); // Render particles RenderParticles(); // Display info text. char lower_buffer[64]; unsigned int numParticles = m_pParticleSystem->GetNumberOfParticles(); sprintf(lower_buffer, "No. of Particles: %d", numParticles); m_Print3D.DisplayDefaultTitle("Particle System", NULL, ePVRTPrint3DSDKLogo); m_Print3D.Print3D(2.0f, 90.0f, 1.0f, 0xFFFFFFFF, "No. of Particles: %d", numParticles); m_Print3D.Flush(); return true; }
/*!**************************************************************************** @Function UpdateScene @Description Moves the scene. ******************************************************************************/ void OGLES2Glass::UpdateScene() { // Fetch current time and make sure the previous time isn't greater unsigned long ulCurrentTime = PVRShellGetTime(); if (ulCurrentTime < m_ulTime) m_ulTime = ulCurrentTime; // Calculate the time difference unsigned long ulTimeDifference = ulCurrentTime - m_ulTime; // Store the current time for the next frame m_ulTime = ulCurrentTime; m_afAngles[0] += ulTimeDifference * 0.0002f; m_afAngles[1] -= ulTimeDifference * 0.00008f; float fRise = sin(m_afAngles[0] * 3.0f); // Rotate the camera m_mView = PVRTMat4::LookAtRH(PVRTVec3(0, 0, -10), PVRTVec3(0, 0, 0), PVRTVec3(0, 1, 0)) * PVRTMat4::RotationY(m_afAngles[0] * 0.2f); // Rotate the balloon model matrices m_mModels[0] = PVRTMat4::RotationY(m_afAngles[0]) * PVRTMat4::Translation(120.0f, fRise * 20.0f, 0.0f) * PVRTMat4::Scale(3.0f, 3.0f, 3.0f); m_mModels[1] = PVRTMat4::RotationY(m_afAngles[1]) * PVRTMat4::Translation(-180.0f, -fRise * 20.0f, 0.0f) * PVRTMat4::Scale(3.0f, 3.0f, 3.0f); }
/*!**************************************************************************** @Function SetUpMatrices @Description Creates the view and projection matrices for the light and camera ******************************************************************************/ void OGLES2ShadowMapping::SetUpMatrices() { PVRTVec3 vFrom = PVRTVec3(-130.0f, 130.0f, -130.0f ), vTo = PVRTVec3( 0, 10, 0 ), vUp = PVRTVec3( 0, 1, 0 ); float fFOV= 0.78539819f; m_BiasMatrix = PVRTMat4(0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.5f, 0.5f, 0.5f, 1.0f); bool bRotate = PVRShellGet(prefIsRotated) && PVRShellGet(prefFullScreen); m_Projection = PVRTMat4::PerspectiveFovRH(fFOV, (float)PVRShellGet(prefWidth)/(float)PVRShellGet(prefHeight), g_fCameraNear, g_fCameraFar, PVRTMat4::OGL, bRotate); m_View = PVRTMat4::LookAtRH(vFrom, vTo, vUp); m_LightProjection = PVRTMat4::PerspectiveFovRH(fFOV, 1.0f, 70.0f, 270.0f, PVRTMat4::OGL, m_bRotate); m_LightView = PVRTMat4::LookAtRH(m_vLightPosition, vTo, vUp); }
/*!**************************************************************************** @Function RenderScene @Return bool true if no error occured @Description Main rendering loop function of the program. The shell will call this function every frame. eglSwapBuffers() will be performed by PVRShell automatically. PVRShell will also manage important OS events. Will also manage relevent OS events. The user has access to these events through an abstraction layer provided by PVRShell. ******************************************************************************/ bool OGLES2FastTnL::RenderScene() { // Clear the color and depth buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Use shader program glUseProgram(m_ShaderProgram.uiId); // Bind texture glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, m_uiTexture); /* Now that the uniforms are set, call another function to actually draw the mesh. */ DrawMesh(0); // Rotate the model matrix PVRTMat4 mModel = PVRTMat4::RotationY(m_fAngleY); m_fAngleY += 0.02f; // Calculate model view projection matrix PVRTMat4 mMVP = m_mViewProj * mModel; // Feeds Projection Model View matrix to the shaders glUniformMatrix4fv(m_ShaderProgram.uiMVPMatrixLoc, 1, GL_FALSE, mMVP.ptr()); /* The inverse of a rotation matrix is the transposed matrix Because of v * M = transpose(M) * v, this means: v * R == inverse(R) * v So we don't have to actually invert or transpose the matrix to transform back from world space to model space */ PVRTVec3 vMsLightDir = (PVRTVec3(1, 1, 1) * PVRTMat3(mModel)).normalized(); glUniform3fv(m_ShaderProgram.uiLightDirLoc, 1, vMsLightDir.ptr()); // Displays the demo name using the tools. For a detailed explanation, see the training course IntroducingPVRTools m_Print3D.DisplayDefaultTitle("FastTnL", "", ePVRTPrint3DLogoIMG); m_Print3D.Flush(); return true; }
/*!**************************************************************************** @Function RenderScene @Return bool true if no error occured @Description Main rendering loop function of the program. The shell will call this function every frame. eglSwapBuffers() will be performed by PVRShell automatically. PVRShell will also manage important OS events. Will also manage relevent OS events. The user has access to these events through an abstraction layer provided by PVRShell. ******************************************************************************/ bool OGLES3CellShading::RenderScene() { // Clears the color and depth buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Use the loaded shader program glUseProgram(m_ShaderProgram.uiId); // Bind textures glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, m_uiShadingTex); // Calculate the model matrix PVRTMat4 mModel = PVRTMat4::RotationY(m_fAngleY); m_fAngleY += PVRT_PI / 210; // Set model view projection matrix PVRTMat4 mMVP = m_mViewProj * mModel; glUniformMatrix4fv(m_ShaderProgram.uiMVPMatrixLoc, 1, GL_FALSE, mMVP.ptr()); // Set eye position in model space PVRTVec4 vMsEyePos = PVRTVec4(0, 0, 125, 1) * mModel; glUniform3fv(m_ShaderProgram.uiEyePosLoc, 1, vMsEyePos.ptr()); // transform directional light from world space to model space PVRTVec3 vMsLightDir = PVRTVec3(PVRTVec4(1, 2, 1, 0) * mModel).normalized(); glUniform3fv(m_ShaderProgram.uiLightDirLoc, 1, vMsLightDir.ptr()); DrawMesh(0); // Displays the demo name using the tools. For a detailed explanation, see the training course IntroducingPVRTools m_Print3D.DisplayDefaultTitle("CellShading", "", ePVRTPrint3DSDKLogo); m_Print3D.Flush(); return true; }
// --------------------------------------------------------------- bool MyPVRDemo::InitView() { CPVRTString ErrorStr; LoadVBOs(); bool bResult = true; bResult &= LoadTextures(&ErrorStr); bResult &= LoadShaders(&ErrorStr); bResult &= CreateFBOs(&ErrorStr); if(!bResult) { PVRShellSet(prefExitMessage, ErrorStr.c_str()); return false; } m_bRotated = PVRShellGet(prefIsRotated) && PVRShellGet(prefFullScreen); // --- Set up light position, projection and view m_vLightPos = PVRTVec3(0, 125, 200); m_mxLightProj = PVRTMat4::PerspectiveFovRH(PVRT_PI / 4, 1.0f, 10.0f, 1000.0f, PVRTMat4::OGL, m_bRotated); m_mxLightView = PVRTMat4::LookAtRH(m_vLightPos, PVRTVec3(0,25,0), PVRTVec3(0,1,0)); m_mxLightBias = PVRTMat4(0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.5f, 0.5f, 0.5f, 1.0f); // --- Set up Camera projection and view float fAspect = PVRShellGet(prefWidth) / (float)PVRShellGet(prefHeight); m_mxProjection = PVRTMat4::PerspectiveFovFloatDepthRH(0.75f, fAspect, 10.0f, PVRTMat4::OGL, m_bRotated); m_mxCam = PVRTMat4::LookAtRH(PVRTVec3(0, 55, 150), PVRTVec3(0, 35, 0), PVRTVec3(0, 1, 0)); // --- Set GL states glCullFace(GL_BACK); glEnable(GL_CULL_FACE); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_GEQUAL); glClearDepthf(0.0f); glClearColor(0,0,0,1); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); return true; }
/*!**************************************************************************** @Function InitView @Return bool true if no error occured @Description Code in InitView() will be called by PVRShell upon initialization or after a change in the rendering context. Used to initialize variables that are dependant on the rendering context (e.g. textures, vertex buffers, etc.) ******************************************************************************/ bool OGLES3Fog::InitView() { CPVRTString ErrorStr; /* Initialize VBO data */ LoadVbos(); /* Load textures */ if (!LoadTextures(&ErrorStr)) { PVRShellSet(prefExitMessage, ErrorStr.c_str()); return false; } /* Load and compile the shaders & link programs */ if (!LoadShaders(&ErrorStr)) { PVRShellSet(prefExitMessage, ErrorStr.c_str()); return false; } // Is the screen rotated bool bRotate = PVRShellGet(prefIsRotated) && PVRShellGet(prefFullScreen); /* Initialize Print3D */ if(m_Print3D.SetTextures(0,PVRShellGet(prefWidth),PVRShellGet(prefHeight), bRotate) != PVR_SUCCESS) { PVRShellSet(prefExitMessage, "ERROR: Cannot initialise Print3D\n"); return false; } /* Calculate the projection and view matrices */ float fAspect = PVRShellGet(prefWidth) / (float)PVRShellGet(prefHeight); m_mProjection = PVRTMat4::PerspectiveFovFloatDepthRH(CAM_FOV, fAspect, CAM_NEAR, PVRTMat4::OGL, bRotate); m_mView = PVRTMat4::LookAtRH(PVRTVec3(0.f, 0.f, 150.f), PVRTVec3(0.f), PVRTVec3(0.f, 1.f, 0.f)); /* Set OpenGL ES render states needed for this training course */ // Enable z-buffer test // We are using a projection matrix optimized for a floating point depth buffer, // so the depth test and clear value need to be inverted (1 becomes near, 0 becomes far). glEnable(GL_DEPTH_TEST); glDepthFunc(GL_GEQUAL); glClearDepthf(0.0f); // Use a nice bright blue as clear colour glClearColor(0.6f, 0.8f, 1.0f, 1.0f); /* Set up constant fog shader uniforms */ const float fFogStart = 0.0f; const float fFogEnd = 1200.0f; const float fFogDensity = 0.002f; const float fFogRcpEndStartDiff = 1.0f / (fFogEnd - fFogStart); const float afFogColor[3] = { 0.6f, 0.8f, 1.0f }; // the fog RGB color glUniform1f(m_ShaderProgram.uiFogEndLoc, fFogEnd); glUniform1f(m_ShaderProgram.uiFogRcpDiffLoc, fFogRcpEndStartDiff); glUniform1f(m_ShaderProgram.uiFogDensityLoc, fFogDensity); glUniform3fv(m_ShaderProgram.uiFogColorLoc, 1, afFogColor); return true; }
/*!**************************************************************************** @Function InitView @Return bool true if no error occured @Description Code in InitView() will be called by PVRShell upon initialization or after a change in the rendering context. Used to initialize variables that are dependant on the rendering context (e.g. textures, vertex buffers, etc.) ******************************************************************************/ bool OGLES3Refraction::InitView() { CPVRTString ErrorStr; /* Initialize VBO data */ LoadVbos(); /* Load textures */ if (!LoadTextures(&ErrorStr)) { PVRShellSet(prefExitMessage, ErrorStr.c_str()); return false; } /* Load and compile the shaders & link programs */ if (!LoadShaders(&ErrorStr)) { PVRShellSet(prefExitMessage, ErrorStr.c_str()); return false; } // Set the sampler2D uniforms to corresponding texture units glUniform1i(glGetUniformLocation(m_ShaderProgram.uiId, "sTexture"), 0); // Is the screen rotated? m_bRotate = PVRShellGet(prefIsRotated) && PVRShellGet(prefFullScreen); /* Initialize Print3D */ if(m_Print3D.SetTextures(0,PVRShellGet(prefWidth),PVRShellGet(prefHeight), m_bRotate) != PVR_SUCCESS) { PVRShellSet(prefExitMessage, "ERROR: Cannot initialise Print3D\n"); return false; } /* Initalise the background */ if(m_Background.Init(0, m_bRotate, &ErrorStr) != PVR_SUCCESS) { PVRShellSet(prefExitMessage, ErrorStr.c_str()); return false; } /* Calculate the projection and view matrices */ m_mProjection = PVRTMat4::PerspectiveFovRH(PVRT_PI/6, (float)PVRShellGet(prefWidth)/(float)PVRShellGet(prefHeight), CAM_NEAR, CAM_FAR, PVRTMat4::OGL, m_bRotate); m_mView = PVRTMat4::LookAtRH(PVRTVec3(0, 0, 150), PVRTVec3(0, 0, 0), PVRTVec3(0, 1, 0)); /* Set OpenGL ES render states needed for this training course */ // Use a nice bright blue as clear colour glClearColor(0.6f, 0.8f, 1.0f, 1.0f); return true; }
/*!**************************************************************************** @Function RenderScene @Return bool true if no error occured @Description Main rendering loop function of the program. The shell will call this function every frame. eglSwapBuffers() will be performed by PVRShell automatically. PVRShell will also manage important OS events. Will also manage relevent OS events. The user has access to these events through an abstraction layer provided by PVRShell. ******************************************************************************/ bool OGLES2AnisotropicLighting::RenderScene() { // Clear the color and depth buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Keyboard input (cursor to change render mode) if (PVRShellIsKeyPressed(PVRShellKeyNameLEFT)) { m_eRenderMode = ERenderMode((m_eRenderMode + eNumRenderModes - 1) % eNumRenderModes); } if (PVRShellIsKeyPressed(PVRShellKeyNameRIGHT)) { m_eRenderMode = ERenderMode((m_eRenderMode + 1) % eNumRenderModes); } // Rotate the model matrix PVRTMat4 mModel = PVRTMat4::RotationY(m_fAngleY); m_fAngleY += 0.02f; // Calculate model view projection matrix PVRTMat4 mMVP = m_mViewProj * mModel; if (m_eRenderMode == eTexLookup) { glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, m_uiTexture); glUseProgram(m_FastShader.uiId); glUniformMatrix4fv(m_FastShader.uiMVPMatrixLoc, 1, GL_FALSE, mMVP.ptr()); /* The inverse of a rotation matrix is the transposed matrix Because of v * M = transpose(M) * v, this means: v * R == inverse(R) * v So we don't have to actually invert or transpose the matrix to transform back from world space to model space */ PVRTVec3 vMsEyePos = PVRTVec3(PVRTVec4(0, 0, 150, 1) * mModel); glUniform3fv(m_FastShader.uiMsEyePosLoc, 1, vMsEyePos.ptr()); PVRTVec3 vMsLightDir = PVRTVec3(PVRTVec4(1, 1, 1, 1) * mModel).normalized(); glUniform3fv(m_FastShader.uiMsLightDirLoc, 1, vMsLightDir.ptr()); } else { glUseProgram(m_SlowShader.uiId); glUniformMatrix4fv(m_SlowShader.uiMVPMatrixLoc, 1, GL_FALSE, mMVP.ptr()); PVRTVec3 vMsEyeDir = PVRTVec3(PVRTVec4(0, 0, 150, 1) * mModel).normalized(); glUniform3fv(m_SlowShader.uiMsEyeDirLoc, 1, vMsEyeDir.ptr()); PVRTVec3 vMsLightDir = PVRTVec3(PVRTVec4(1, 1, 1, 1) * mModel).normalized(); glUniform3fv(m_SlowShader.uiMsLightDirLoc, 1, vMsLightDir.ptr()); } /* Now that the uniforms are set, call another function to actually draw the mesh. */ DrawMesh(0); // Displays the demo name using the tools. For a detailed explanation, see the training course IntroducingPVRTools m_Print3D.DisplayDefaultTitle("AnisotropicLighting", "", ePVRTPrint3DLogoIMG); m_Print3D.Print3D(0.3f, 7.5f, 0.75f, PVRTRGBA(255,255,255,255), c_aszRenderModes[m_eRenderMode]); m_Print3D.Flush(); return true; }
/*!**************************************************************************** @Function InitView @Return bool true if no error occured @Description Code in InitView() will be called by PVRShell upon initialization or after a change in the rendering context. Used to initialize variables that are dependant on the rendering context (e.g. textures, vertex buffers, etc.) ******************************************************************************/ bool OGLES2AnisotropicLighting::InitView() { CPVRTString ErrorStr; /* Initialize VBO data */ LoadVbos(); /* Load textures */ if (!LoadTextures(&ErrorStr)) { PVRShellSet(prefExitMessage, ErrorStr.c_str()); return false; } /* Load and compile the shaders & link programs */ if (!LoadShaders(&ErrorStr)) { PVRShellSet(prefExitMessage, ErrorStr.c_str()); return false; } // Is the screen rotated? bool bRotate = PVRShellGet(prefIsRotated) && PVRShellGet(prefFullScreen); /* Initialize Print3D */ if(m_Print3D.SetTextures(0,PVRShellGet(prefWidth),PVRShellGet(prefHeight), bRotate) != PVR_SUCCESS) { PVRShellSet(prefExitMessage, "ERROR: Cannot initialise Print3D\n"); return false; } /* Calculate the projection and view matrices */ float fAspect = PVRShellGet(prefWidth) / (float)PVRShellGet(prefHeight); m_mViewProj = PVRTMat4::PerspectiveFovFloatDepthRH(CAM_FOV, fAspect, CAM_NEAR, PVRTMat4::OGL, bRotate); m_mViewProj *= PVRTMat4::LookAtRH(PVRTVec3(0.f, 0.f, 150.f), PVRTVec3(0.f), PVRTVec3(0.f, 1.f, 0.f)); /* Set uniforms that are constant throughout this training course */ // Set the sampler2D variable to the first texture unit glUseProgram(m_FastShader.uiId); glUniform1i(glGetUniformLocation(m_FastShader.uiId, "sTexture"), 0); // Define material properties glUseProgram(m_SlowShader.uiId); float afMaterial[4] = { 0.4f, // Diffuse intensity scale 0.6f, // Diffuse intensity bias 0.82f, // Specular intensity scale 0.0f, // Specular bias }; glUniform4fv(glGetUniformLocation(m_SlowShader.uiId, "Material"), 1, afMaterial); // Set surface grain direction PVRTVec3 vMsGrainDir = PVRTVec3(2, 1, 0).normalized(); glUniform3fv(glGetUniformLocation(m_SlowShader.uiId, "GrainDir"), 1, vMsGrainDir.ptr()); /* Set OpenGL ES render states needed for this training course */ // Enable backface culling and depth test glCullFace(GL_BACK); glEnable(GL_CULL_FACE); // Enable z-buffer test // We are using a projection matrix optimized for a floating point depth buffer, // so the depth test and clear value need to be inverted (1 becomes near, 0 becomes far). glEnable(GL_DEPTH_TEST); glDepthFunc(GL_GEQUAL); glClearDepthf(0.0f); // Use a nice bright blue as clear colour glClearColor(0.6f, 0.8f, 1.0f, 1.0f); m_fAngleY = 0; m_eRenderMode = eTexLookup; return true; }
void ParametricSurface::ComputeVertexAndNormals(PFUNCTION function, float dMinU, float dMaxU, float dMinV, float dMaxV) { int nVertex = nSampleU * nSampleV; pVertex = new float[nVertex*3]; pNormal = new float[nVertex*3]; pUV = new float[nVertex*2]; fMinU = dMinU; fMaxU = dMaxU; fMinV = dMinV; fMaxV = dMaxV; for (int i=0; i<nSampleU; i++) { for (int j=0; j<nSampleV; j++) { float u = fMinU + i * (fMaxU-fMinU) / (float)(nSampleU-1); float v = fMinV + j * (fMaxV-fMinV) / (float)(nSampleV-1); float x,y,z; function(u,v, &x,&y,&z); pVertex[(j*nSampleU+i)*3 + 0] = x; pVertex[(j*nSampleU+i)*3 + 1] = y; pVertex[(j*nSampleU+i)*3 + 2] = z; } } for (int i=0; i<nSampleU; i++) { for (int j=0; j<nSampleV; j++) { pUV[ (j*nSampleU+i)*2 + 0 ] = (float)i / (float)(nSampleU-1); pUV[ (j*nSampleU+i)*2 + 1 ] = (float)j / (float)(nSampleV-1); } } for (int i=0; i<nSampleU-1; i++) { for (int j=0; j<nSampleV-1; j++) { PVRTVec3 ptA = PVRTVec3(pVertex[(j*nSampleU+i)*3+0],pVertex[(j*nSampleU+i)*3+1],pVertex[(j*nSampleU+i)*3+2]); PVRTVec3 ptB = PVRTVec3(pVertex[(j*nSampleU+i+1)*3+0],pVertex[(j*nSampleU+i+1)*3+1],pVertex[(j*nSampleU+i+1)*3+2]); PVRTVec3 ptC = PVRTVec3(pVertex[((j+1)*nSampleU+i)*3+0],pVertex[((j+1)*nSampleU+i)*3+1],pVertex[((j+1)*nSampleU+i)*3+2]); PVRTVec3 AB = PVRTVec3(ptB.x-ptA.x, ptB.y-ptA.y, ptB.z-ptA.z); PVRTVec3 AC = PVRTVec3(ptC.x-ptA.x, ptC.y-ptA.y, ptC.z-ptA.z); PVRTVec3 normal; normal = AB.cross(AC); normal.normalize(); pNormal[(j*nSampleU+i)*3 + 0] = -normal.x; pNormal[(j*nSampleU+i)*3 + 1] = -normal.y; pNormal[(j*nSampleU+i)*3 + 2] = -normal.z; } } for (int i=0; i<nSampleU-1; i++) { pNormal[((nSampleV-1)*nSampleU+i)*3+0] = pNormal[(i)*3+0]; pNormal[((nSampleV-1)*nSampleU+i)*3+1] = pNormal[(i)*3+1]; pNormal[((nSampleV-1)*nSampleU+i)*3+2] = pNormal[(i)*3+2]; } for (int j=0; j<nSampleV-1; j++) { pNormal[(j*nSampleU+nSampleU-1)*3+0] = pNormal[(j*nSampleU)*3+0]; pNormal[(j*nSampleU+nSampleU-1)*3+1] = pNormal[(j*nSampleU)*3+1]; pNormal[(j*nSampleU+nSampleU-1)*3+2] = pNormal[(j*nSampleU)*3+2]; } pNormal[((nSampleV-1)*nSampleU + (nSampleU-1))*3+0]= pNormal[((nSampleV-2)*nSampleU + (nSampleU-2))*3+0]; pNormal[((nSampleV-1)*nSampleU + (nSampleU-1))*3+1]= pNormal[((nSampleV-2)*nSampleU + (nSampleU-2))*3+1]; pNormal[((nSampleV-1)*nSampleU + (nSampleU-1))*3+2]= pNormal[((nSampleV-2)*nSampleU + (nSampleU-2))*3+2]; // Insert generated data into vertex buffer objects. glBindBuffer(GL_ARRAY_BUFFER, iVertexVBO); glBufferData(GL_ARRAY_BUFFER, nVertex * 3 * sizeof (float), pVertex, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, iUvVBO); glBufferData(GL_ARRAY_BUFFER, nVertex * 2 * sizeof (float), pUV, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, iNormalVBO); glBufferData(GL_ARRAY_BUFFER, nVertex * 3 * sizeof (float), pNormal, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); // Unbind the last buffer used. delete[] pVertex; delete[] pNormal; delete[] pUV; }
/*!**************************************************************************** @Function RenderScene @Return bool true if no error occured @Description Main rendering loop function of the program. The shell will call this function every frame. eglSwapBuffers() will be performed by PVRShell automatically. PVRShell will also manage important OS events. Will also manage relevent OS events. The user has access to these events through an abstraction layer provided by PVRShell. ******************************************************************************/ bool OGLESParticles::RenderScene() { int i; PVRTMat4 mRotY; // Clear colour and depth buffers myglClearColor(f2vt(0.0f), f2vt(0.0f), f2vt(0.0f), f2vt(1.0f)); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Enables depth testing glEnable(GL_DEPTH_TEST); // Modify per-frame variables controlling the particle mouvements. float fSpeedCtrl = (float) (PVRTFSIN(m_fRot*0.01f)+1.0f)/2.0f; float fStopNo = 0.8f; float fStep = 0.1f; if(fSpeedCtrl > fStopNo) fStep = 0.0f; // Generate particles as needed. if((m_i32NumParticles < (int) g_ui32MaxParticles) && (fSpeedCtrl <= fStopNo)) { int num_to_gen = (int) (RandPositiveFloat()*(g_ui32MaxParticles/100.0)); if(num_to_gen == 0) num_to_gen = 1; for(i = 0; (i < num_to_gen) && (m_i32NumParticles < (int) g_ui32MaxParticles); ++i) SpawnParticle(&m_Particles[m_i32NumParticles++]); } // Build rotation matrix around axis Y. mRotY = PVRTMat4::RotationY(f2vt((m_fRot2*PVRT_PIf)/180.0f)); for(i = 0; i < m_i32NumParticles; ++i) { // Transform particle with rotation matrix m_sParticleVTXPSBuf[i].x = VERTTYPEMUL(mRotY.f[ 0], m_Particles[i].m_fPosition.x) + VERTTYPEMUL(mRotY.f[ 4], m_Particles[i].m_fPosition.y) + VERTTYPEMUL(mRotY.f[ 8], m_Particles[i].m_fPosition.z) + mRotY.f[12]; m_sParticleVTXPSBuf[i].y = VERTTYPEMUL(mRotY.f[ 1], m_Particles[i].m_fPosition.x) + VERTTYPEMUL(mRotY.f[ 5], m_Particles[i].m_fPosition.y) + VERTTYPEMUL(mRotY.f[ 9], m_Particles[i].m_fPosition.z) + mRotY.f[13]; m_sParticleVTXPSBuf[i].z = VERTTYPEMUL(mRotY.f[ 2], m_Particles[i].m_fPosition.x) + VERTTYPEMUL(mRotY.f[ 6], m_Particles[i].m_fPosition.y) + VERTTYPEMUL(mRotY.f[10], m_Particles[i].m_fPosition.z) + mRotY.f[14]; m_sParticleVTXPSBuf[i].fSize = m_Particles[i].m_fSize; m_sNormalColour[i].r = vt2b(m_Particles[i].m_fColour.x); m_sNormalColour[i].g = vt2b(m_Particles[i].m_fColour.y); m_sNormalColour[i].b = vt2b(m_Particles[i].m_fColour.z); m_sNormalColour[i].a = (unsigned char)255; m_sReflectColour[i].r = vt2b(VERTTYPEMUL(m_Particles[i].m_fColour.x, g_fFactor)); m_sReflectColour[i].g = vt2b(VERTTYPEMUL(m_Particles[i].m_fColour.y, g_fFactor)); m_sReflectColour[i].b = vt2b(VERTTYPEMUL(m_Particles[i].m_fColour.z, g_fFactor)); m_sReflectColour[i].a = (unsigned char)255; } glBindBuffer(GL_ARRAY_BUFFER, m_i32VertVboID); glBufferData(GL_ARRAY_BUFFER, sizeof(SVtxPointSprite)*m_i32NumParticles, m_sParticleVTXPSBuf,GL_DYNAMIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, m_i32ColAVboID); glBufferData(GL_ARRAY_BUFFER, sizeof(SColors)*m_i32NumParticles, m_sNormalColour,GL_DYNAMIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, m_i32ColBVboID); glBufferData(GL_ARRAY_BUFFER, sizeof(SColors)*m_i32NumParticles, m_sReflectColour,GL_DYNAMIC_DRAW); // clean up render states glDisable(GL_BLEND); glDisable(GL_TEXTURE_2D); glEnable(GL_LIGHTING); // Draw floor. // Save modelview matrix glMatrixMode(GL_MODELVIEW); glPushMatrix(); myglRotate(f2vt(-m_fRot), f2vt(0.0f), f2vt(1.0f), f2vt(0.0f)); // setup render states glDisable(GL_LIGHTING); glEnable(GL_TEXTURE_2D); glDisable(GL_CULL_FACE); glEnable(GL_BLEND); // Set texture and texture environment glBindTexture(GL_TEXTURE_2D, m_ui32FloorTexName); glBlendFunc(GL_ONE, GL_ONE); // Render floor RenderFloor(); // clean up render states glDisable(GL_BLEND); glDisable(GL_TEXTURE_2D); glEnable(GL_LIGHTING); glPopMatrix(); // Render particles reflections. // set up render states glDisable(GL_LIGHTING); glEnable(GL_TEXTURE_2D); glDepthFunc(GL_ALWAYS); glDisable(GL_CULL_FACE); glEnable(GL_BLEND); glBlendFunc(GL_ONE, GL_ONE); myglTexEnv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glBindTexture(GL_TEXTURE_2D, m_ui32TexName); // Set model view matrix glMatrixMode(GL_MODELVIEW); glPushMatrix(); myglScale(f2vt(1.0f), f2vt(-1.0f), f2vt(1.0f)); myglTranslate(f2vt(0.0f), f2vt(0.01f), f2vt(0.0f)); glEnable(GL_POINT_SPRITE_OES); if(((int)(m_i32NumParticles * 0.5f)) > 0) RenderParticle(((int)(m_i32NumParticles*0.5f)),true); glPopMatrix(); // Render particles. // Sets the model view matrix glMatrixMode(GL_MODELVIEW); glPushMatrix(); if(m_i32NumParticles > 0) RenderParticle(m_i32NumParticles,false); glPopMatrix(); glDisable(GL_POINT_SPRITE_OES); PVRTVec3 Force = PVRTVec3(f2vt(0.0f), f2vt(0.0f), f2vt(0.0f)); Force.x = f2vt(1000.0f*(float)PVRTFSIN(m_fRot*0.01f)); for(i = 0; i < m_i32NumParticles; ++i) { /* Move the particle. If the particle exceeds its lifetime, create a new one in its place. */ if(m_Particles[i].Step(f2vt(fStep), Force)) SpawnParticle(&m_Particles[i]); } // clean up render states glDisable(GL_BLEND); glDisable(GL_TEXTURE_2D); glEnable(GL_LIGHTING); // Increase rotation angles m_fRot += 1; m_fRot2 = m_fRot + 36; // Unbinds the vertex buffer if we are using OpenGL ES 1.1 glBindBuffer(GL_ARRAY_BUFFER, 0); // Display info text. m_Print3D.DisplayDefaultTitle("Particles", "Using point sprites", ePVRTPrint3DSDKLogo); m_Print3D.Flush(); return true; }
/*!**************************************************************************** @Function InitView @Return bool true if no error occured @Description Code in InitView() will be called by PVRShell upon initialization or after a change in the rendering context. Used to initialize variables that are dependant on the rendering context (e.g. textures, vertex buffers, etc.) ******************************************************************************/ bool OGLES2LevelOfDetail::InitView() { CPVRTString ErrorStr; /* Initialize VBO data */ LoadVbos(); /* Load textures */ if (!LoadTextures(&ErrorStr)) { PVRShellSet(prefExitMessage, ErrorStr.c_str()); return false; } /* Load and compile the shaders & link programs */ if (!LoadShaders(&ErrorStr)) { PVRShellSet(prefExitMessage, ErrorStr.c_str()); return false; } // Set the sampler2D uniforms to corresponding texture units glUniform1i(glGetUniformLocation(m_ShaderProgram.uiId, "sReflectTex"), 0); glUniform1i(glGetUniformLocation(m_ShaderProgram.uiId, "sNormalMap"), 1); // Is the screen rotated? bool bRotate = PVRShellGet(prefIsRotated) && PVRShellGet(prefFullScreen); /* Initialize Print3D */ if(m_Print3D.SetTextures(0,PVRShellGet(prefWidth),PVRShellGet(prefHeight), bRotate) != PVR_SUCCESS) { PVRShellSet(prefExitMessage, "ERROR: Cannot initialise Print3D\n"); return false; } /* Calculate the projection and view matrices */ m_mProjection = PVRTMat4::PerspectiveFovRH(PVRT_PI/6, (float)PVRShellGet(prefWidth)/(float)PVRShellGet(prefHeight), CAM_NEAR, CAM_FAR, PVRTMat4::OGL, bRotate); m_mView = PVRTMat4::LookAtRH(PVRTVec3(0, 0, 150), PVRTVec3(0, 0, 0), PVRTVec3(0, 1, 0)); /* Set OpenGL ES render states needed for this training course */ // Enable backface culling and depth test glCullFace(GL_BACK); glEnable(GL_CULL_FACE); glEnable(GL_DEPTH_TEST); // Use a nice bright blue as clear colour glClearColor(0.6f, 0.8f, 1.0f, 1.0f); return true; }
/*!**************************************************************************** @Function InitView @Return bool true if no error occurred @Description Code in InitView() will be called by PVRShell upon initialization or after a change in the rendering context. Used to initialize variables that are dependant on the rendering context (e.g. textures, vertex buffers, etc.) ******************************************************************************/ bool OGLES2ParallaxBumpMap::InitView() { CPVRTString ErrorStr; /* Initialize VBO data */ LoadVbos(); /* Load textures */ if (!LoadTextures(&ErrorStr)) { PVRShellSet(prefExitMessage, ErrorStr.c_str()); return false; } /* Load and compile the shaders & link programs */ if (!LoadShaders(&ErrorStr)) { PVRShellSet(prefExitMessage, ErrorStr.c_str()); return false; } // Set the sampler2D uniforms to corresponding texture units glUniform1i(glGetUniformLocation(m_ShaderProgram.uiId, "basemap"), 0); glUniform1i(glGetUniformLocation(m_ShaderProgram.uiId, "normalmap"), 1); glUniform1i(glGetUniformLocation(m_ShaderProgram.uiId, "heightmap"), 2); // Is the screen rotated bool bRotate = PVRShellGet(prefIsRotated) && PVRShellGet(prefFullScreen); /* Initialize Print3D */ if(m_Print3D.SetTextures(0,PVRShellGet(prefWidth),PVRShellGet(prefHeight), bRotate) != PVR_SUCCESS) { PVRShellSet(prefExitMessage, "ERROR: Cannot initialise Print3D\n"); return false; } /* Calculate the projection and view matrices */ float fAspect = PVRShellGet(prefWidth) / (float)PVRShellGet(prefHeight); m_mView = PVRTMat4::LookAtRH(PVRTVec3(0, 0, 150), PVRTVec3(0, 0, 0), PVRTVec3(0, 1, 0)); m_mViewProj = PVRTMat4::PerspectiveFovFloatDepthRH(CAM_FOV, fAspect, CAM_NEAR, PVRTMat4::OGL, bRotate); m_mViewProj *= m_mView; /* Set OpenGL ES render states needed for this training course */ // Enable backface culling and depth test glCullFace(GL_BACK); glEnable(GL_CULL_FACE); // Enable z-buffer test // We are using a projection matrix optimized for a floating point depth buffer, // so the depth test and clear value need to be inverted (1 becomes near, 0 becomes far). glEnable(GL_DEPTH_TEST); glDepthFunc(GL_GEQUAL); glClearDepthf(0.0f); // Use a nice bright blue as clear colour glClearColor(0.6f, 0.8f, 1.0f, 1.0f); return true; }
******************************************************************************/ #include "PVRShell.h" #include "OGLES2Tools.h" /****************************************************************************** Constants ******************************************************************************/ // Camera constants used to generate the projection matrix const float CAM_FOV = PVRT_PI / 6; const float CAM_NEAR = 75.0f; const float g_SomeRotation = 45.0f/(2*PVRT_PI); const PVRTVec3 g_CubeTranslation = PVRTVec3(0.0f, -20.0f, 0.f); const PVRTVec3 g_CubeScale = PVRTVec3(1.4f, 1.4f, 1.4f); const PVRTVec4 g_LightPos = PVRTVec4(0.f, 30.f, 10.f, 1); /****************************************************************************** shader attributes ******************************************************************************/ // vertex attributes enum EVertexAttrib { VERTEX_ARRAY, NORMAL_ARRAY, TEXCOORD_ARRAY, TANGENT_ARRAY, eNumAttribs }; const char* g_aszAttribNames[] = { "vertPos", "vertNormal", "vertUV", "vertTangent" }; // shader uniforms enum EUniform {
/*!**************************************************************************** @Function InitView @Return bool true if no error occured @Description Code in InitView() will be called by PVRShell upon initialization or after a change in the rendering context. Used to initialize variables that are dependant on the rendering context (e.g. textures, vertex buffers, etc.) ******************************************************************************/ bool OGLESRenderToTexture::InitView() { CPVRTString ErrorStr; /* Initialise Print3D */ bool bRotate = PVRShellGet(prefIsRotated) && PVRShellGet(prefFullScreen); if(m_Print3D.SetTextures(0,PVRShellGet(prefWidth),PVRShellGet(prefHeight), bRotate) != PVR_SUCCESS) { PVRShellSet(prefExitMessage, "ERROR: Cannot initialise Print3D\n"); return false; } // Enables texturing glEnable(GL_TEXTURE_2D); // Initialize VBO data if(!LoadVbos(&ErrorStr)) { PVRShellSet(prefExitMessage, ErrorStr.c_str()); return false; } /* Load textures */ if(!LoadTextures(&ErrorStr)) { PVRShellSet(prefExitMessage, ErrorStr.c_str()); return false; } // Create a FBO or PBuffer if(!CreateFBOorPBuffer()) return false; // Setup some render states // Enable the depth test glEnable(GL_DEPTH_TEST); // Enable culling glEnable(GL_CULL_FACE); // Setup the material parameters our meshes will use glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, PVRTVec4(1.0f).ptr()); glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, PVRTVec4(1.0f).ptr()); // Setup view and projection matrices used for when rendering to the texture // Caculate the view matrix m_mR2TView = PVRTMat4::LookAtRH(PVRTVec3(0, 0, 60), PVRTVec3(0, 0, 0), PVRTVec3(0, 1, 0)); // Calculate the projection matrix // Note: As we'll be rendering to a texture we don't need to take the screen rotation into account m_mR2TProjection = PVRTMat4::PerspectiveFovRH(1, 1, g_fCameraNear, g_fCameraFar, PVRTMat4::OGL, false); // Setup view and projection matrices used for when rendering the main scene // Caculate the view matrix m_mView = PVRTMat4::LookAtRH(PVRTVec3(0, 0, 125), PVRTVec3(0, 0, 0), PVRTVec3(0, 1, 0)); // Calculate the projection matrix m_mProjection = PVRTMat4::PerspectiveFovRH(PVRT_PI/6, (float)PVRShellGet(prefWidth)/(float)PVRShellGet(prefHeight), g_fCameraNear, g_fCameraFar, PVRTMat4::OGL, bRotate); return true; }
/*!**************************************************************************** @Function InitView @Return bool true if no error occured @Description Code in InitView() will be called by PVRShell upon initialization or after a change in the rendering context. Used to initialize variables that are dependant on the rendering context (e.g. textures, vertex buffers, etc.) ******************************************************************************/ bool OGLES3AlphaTest::InitView() { CPVRTString ErrorStr; /* Initialize VBO data */ LoadVbos(); /* Load textures */ if (!LoadTextures(&ErrorStr)) { PVRShellSet(prefExitMessage, ErrorStr.c_str()); return false; } /* Load and compile the shaders & link programs */ if (!LoadShaders(&ErrorStr)) { PVRShellSet(prefExitMessage, ErrorStr.c_str()); return false; } // Is the screen rotated? bool bRotate = PVRShellGet(prefIsRotated) && PVRShellGet(prefFullScreen); /* Initialize Print3D */ if(m_Print3D.SetTextures(0, PVRShellGet(prefWidth), PVRShellGet(prefHeight), bRotate) != PVR_SUCCESS) { PVRShellSet(prefExitMessage, "ERROR: Cannot initialise Print3D\n"); return false; } /* Calculate the projection and view matrices */ float fAspect = PVRShellGet(prefWidth) / (float)PVRShellGet(prefHeight); m_mViewProj = PVRTMat4::PerspectiveFovFloatDepthRH(g_fFOV, fAspect, g_fNear, PVRTMat4::OGL, bRotate); m_mViewProj *= PVRTMat4::LookAtRH(PVRTVec3(0, 2, -2.5f), PVRTVec3(0, 0.2f, 0), PVRTVec3(0, 1, 0)); /* Set OpenGL ES render states needed for this training course */ // Use a nice bright blue as clear colour glClearColor(0.6f, 0.8f, 1.0f, 1.0f); // Enable z-buffer test // We are using a projection matrix optimized for a floating point depth buffer, // so the depth test and clear value need to be inverted (1 becomes near, 0 becomes far). glEnable(GL_DEPTH_TEST); glDepthFunc(GL_GEQUAL); glClearDepthf(0.0f); return true; }
/*!**************************************************************************** @Function InitView @Return bool true if no error occured @Description Code in InitView() will be called by PVRShell upon initialization or after a change in the rendering context. Used to initialize variables that are dependant on the rendering context (e.g. textures, vertex buffers, etc.) ******************************************************************************/ bool OGLES2PVRScopeRemote::InitView() { CPPLProcessingScoped PPLProcessingScoped(m_psSPSCommsData, __FUNCTION__, static_cast<unsigned int>(strlen(__FUNCTION__)), m_i32FrameCounter); CPVRTString ErrorStr; /* Initialize VBO data */ LoadVbos(); /* Load textures */ if (!LoadTextures(&ErrorStr)) { PVRShellSet(prefExitMessage, ErrorStr.c_str()); return false; } // Take our initial vert shader source { CPVRTResourceFile VertShaderFile(c_szVertShaderSrcFile); m_pszVertShader = new char[VertShaderFile.Size() + 1]; strncpy(m_pszVertShader, (char*)VertShaderFile.DataPtr(), VertShaderFile.Size()); m_pszVertShader[VertShaderFile.Size()] = 0; } // Take our initial frag shader source { CPVRTResourceFile FragShaderFile(c_szFragShaderSrcFile); m_pszFragShader = new char[FragShaderFile.Size() + 1]; strncpy(m_pszFragShader, (char*)FragShaderFile.DataPtr(), FragShaderFile.Size()); m_pszFragShader[FragShaderFile.Size()] = 0; } /* Load and compile the shaders & link programs */ if (!LoadShaders(&ErrorStr, m_pszFragShader, m_pszVertShader)) { PVRShellSet(prefExitMessage, ErrorStr.c_str()); return false; } // Is the screen rotated? bool bRotate = PVRShellGet(prefIsRotated) && PVRShellGet(prefFullScreen); /* Initialize Print3D */ if(m_Print3D.SetTextures(0,PVRShellGet(prefWidth),PVRShellGet(prefHeight), bRotate) != PVR_SUCCESS) { PVRShellSet(prefExitMessage, "ERROR: Cannot initialise Print3D\n"); return false; } /* Calculate the projection and view matrices */ m_mProjection = PVRTMat4::PerspectiveFovRH(PVRT_PI/6, (float)PVRShellGet(prefWidth)/(float)PVRShellGet(prefHeight), CAM_NEAR, CAM_FAR, PVRTMat4::OGL, bRotate); m_mView = PVRTMat4::LookAtRH(PVRTVec3(0, 0, 75), PVRTVec3(0, 0, 0), PVRTVec3(0, 1, 0)); /* Set OpenGL ES render states needed for this training course */ // Enable backface culling and depth test glCullFace(GL_BACK); glEnable(GL_CULL_FACE); glEnable(GL_DEPTH_TEST); // Use a nice bright blue as clear colour glClearColor(0.6f, 0.8f, 1.0f, 1.0f); return true; }