//====================================================================== //====================================================================== void NvGraphicShaderGL::Load(const char* vert, const char* frag) { NvGLSLProgram *prog = NvGLSLProgram::createFromStrings(vert, frag); CHECK_GL_ERROR(); if (prog==NULL) { // !!!!TBD TODO return; } m_program = prog; prog->enable(); m_positionIndex = prog->getAttribLocation("position"); m_uvIndex = prog->getAttribLocation("tex"); prog->setUniform1i(prog->getUniformLocation("sampler"), 0); // texunit index zero. m_matrixIndex = prog->getUniformLocation("pixelToClipMat"); m_alphaIndex = prog->getUniformLocation("alpha"); m_colorIndex = prog->getUniformLocation("color"); prog->disable(); CHECK_GL_ERROR(); }
void SceneRenderer::drawScene(NvGLSLProgram& a_proc, const MatrixStorage& mats, RenderAlphaTest doAlpha, bool a_renderDepth) { CHECK_GL_ERROR(); MatrixStorage ncMats = mats; GLint positionAttrHandle = a_proc.getAttribLocation("g_Position"); GLint normalAttrHandle = a_proc.getAttribLocation("g_Normal", false); GLint texCoordAttrHandle = a_proc.getAttribLocation("g_TexCoord", false); CHECK_GL_ERROR(); // these matrices cam be set once a_proc.setUniformMatrix4fv("g_ProjectionMatrix", ncMats.m_projection._array); printMatrixLog("viewMatrix", ncMats.m_view); printMatrixLog("set g_ProjectionMatrix", ncMats.m_projection); a_proc.setUniform1f("g_lightAmbient", m_scene.m_lightAmbient); a_proc.setUniform1f("g_lightDiffuse", m_scene.m_lightDiffuse); a_proc.setUniform3f("g_lightDirection", m_scene.m_lightVector[0], m_scene.m_lightVector[1], m_scene.m_lightVector[2]); CHECK_GL_ERROR(); // Note that we are drawing the terrain first which is a sub-optimal rendering order and a deliberate mistake. if (!a_renderDepth && (doAlpha & RENDER_SOLID)) { nv::matrix4f modelMatrix; modelMatrix.make_identity(); modelMatrix.set_scale(nv::vec3f(1.0f, 1.0, 1.0f)); ncMats.m_model = modelMatrix; ncMats.multiply(); CHECK_GL_ERROR(); a_proc.setUniformMatrix4fv("g_ModelViewMatrix", ncMats.m_modelView._array); a_proc.setUniformMatrix4fv("g_ModelViewProjectionMatrix", ncMats.m_modelViewProjection._array); a_proc.setUniformMatrix4fv("g_LightModelViewMatrix", m_scene.m_lightView._array); CHECK_GL_ERROR(); a_proc.setUniform4f("g_color", 1.0f, 1.0f, 1.0f, 1.0f); CHECK_GL_ERROR(); a_proc.bindTexture2D("g_texture", 0, m_pTerrain->getColorTex()); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); CHECK_GL_ERROR(); m_pTerrain->Draw(positionAttrHandle, normalAttrHandle, texCoordAttrHandle); CHECK_GL_ERROR(); } // render objects for(uint32_t i = 0; i < m_models.size(); i++) { MeshObj& mesh = m_models[i]; if (doAlpha == RENDER_ALL || (doAlpha == RENDER_ALPHA && mesh.m_alphaTest) || (doAlpha == RENDER_SOLID && !mesh.m_alphaTest)) { ncMats.m_model = mesh.m_modelMatrix; ncMats.multiply(); CHECK_GL_ERROR(); a_proc.setUniformMatrix4fv("g_ModelViewMatrix", ncMats.m_modelView._array); a_proc.setUniformMatrix4fv("g_ModelViewProjectionMatrix", ncMats.m_modelViewProjection._array); printMatrixLog("set modelViewProjection", ncMats.m_modelViewProjection); CHECK_GL_ERROR(); if (doAlpha & RENDER_ALPHA) { a_proc.bindTexture2D("g_texture", 0, mesh.m_texId); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); } if(!a_renderDepth) { a_proc.setUniform4f("g_color", mesh.m_color.x, mesh.m_color.y, mesh.m_color.z, mesh.m_color.w); a_proc.setUniform1f("g_lightSpecular", mesh.m_specularValue); } if (mesh.m_cullFacing) glEnable(GL_CULL_FACE); else glDisable(GL_CULL_FACE); CHECK_GL_ERROR(); mesh.m_pModelData->drawElements(positionAttrHandle, normalAttrHandle, texCoordAttrHandle); CHECK_GL_ERROR(); } } glEnable(GL_CULL_FACE); }