void deferred_spot::UpdateUniforms(graphics::Material* p_pxMaterial,core::Transform* p_pxTransform,graphics::RenderingEngine* p_pxRenderingEngine) { SetUniform("normalTexture",0); SetUniform("diffuseTexture",1); SetUniform("positionTexture",2); SetUniform("cameraPosition",p_pxRenderingEngine->GetMainCamera()->Transform().Position()); graphics::SpotLight* pxLight=(graphics::SpotLight*)p_pxRenderingEngine->GetActiveLight(); SetUniform("specularPower",pxLight->m_fSpecularPower); SetUniform("specularIntensity",pxLight->m_fSpecularIntensity); SetUniform("light",pxLight); };
void CRenderingContext::EndRenderVertexArrayIndexed(size_t iBuffer, size_t iVertices) { CRenderContext& oContext = GetContext(); if (!oContext.m_bProjectionUpdated) SetUniform("mProjection", oContext.m_mProjection); if (!oContext.m_bViewUpdated) SetUniform("mView", oContext.m_mView); if (!oContext.m_bTransformUpdated) SetUniform("mGlobal", oContext.m_mTransformations); oContext.m_bProjectionUpdated = oContext.m_bViewUpdated = oContext.m_bTransformUpdated = true; glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iBuffer); glDrawElements(r_wireframe.GetBool()?GL_LINES:GL_TRIANGLES, iVertices, GL_UNSIGNED_INT, nullptr); glDisableVertexAttribArray(m_pShader->m_iPositionAttribute); for (size_t i = 0; i < MAX_TEXTURE_CHANNELS; i++) { if (m_pShader->m_aiTexCoordAttributes[i] != ~0) glDisableVertexAttribArray(m_pShader->m_aiTexCoordAttributes[i]); } if (m_pShader->m_iNormalAttribute != ~0) glDisableVertexAttribArray(m_pShader->m_iNormalAttribute); if (m_pShader->m_iTangentAttribute != ~0) glDisableVertexAttribArray(m_pShader->m_iTangentAttribute); if (m_pShader->m_iBitangentAttribute != ~0) glDisableVertexAttribArray(m_pShader->m_iBitangentAttribute); if (m_pShader->m_iColorAttribute != ~0) glDisableVertexAttribArray(m_pShader->m_iColorAttribute); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); }
void MaterialNode::load() { if (this->colorMap != 0) SetTextureSlot(UNIFORM_TEXTURE_COLOR_MAP, this->colorMap); if (this->normalMap != 0) SetTextureSlot(UNIFORM_TEXTURE_NORMAL_MAP, this->normalMap); if (this->specularMap != 0) SetTextureSlot(UNIFORM_TEXTURE_SPECULAR_MAP, this->specularMap); SetUniform(UNIFORM_UV_REPEAT, &this->uvRepeat); SetUniform(UNIFORM_UV_OFFSET, &this->uvOffset); SetUniform(UNIFORM_OVERLAY_COLOR, &this->overlay); SetUniform(UNIFORM_HIGHLIGHT_COLOR, &this->highlight); SetUniform(UNIFORM_ROUGHNESS, this->roughness); SetUniform(UNIFORM_REFRACTION_INDEX, this->refIndex); }
void ForwardDirectional::UpdateUniforms(const mat4& modelMatrix, const Material& material) { static Texture WHITE = Texture(1, 1, whitePixel); if (material.GetTexture() != NULL) material.GetTexture()->Bind(0); else { WHITE.Bind(0); } mat4 MVP = GetRenderEngine()->GetCamera()->GetProjectionMatrix() * GetRenderEngine()->GetCamera()->GetViewMatrix() * modelMatrix; SetUniform("MVP", MVP); SetUniform("ModelMatrix", modelMatrix); SetUniform("NormalMatrix", glm::inverseTranspose(mat3(modelMatrix))); SetUniform("directionalLight.direction", static_cast<DirectionalLight*>(GetRenderEngine()->GetActiveLight())->GetDirection()); SetUniform("directionalLight.base.color", static_cast<DirectionalLight*>(GetRenderEngine()->GetActiveLight())->GetColor()); SetUniform("directionalLight.base.intensity", static_cast<DirectionalLight*>(GetRenderEngine()->GetActiveLight())->GetIntensity()); SetUniform("specularIntensity", material.GetSpecularIntensity()); SetUniform("specularPower", material.GetSpecularPower()); SetUniform("eyePosition", GetRenderEngine()->GetCamera()->GetPosition()); }
/** * @brief Fog::render * @param commandBuffer * @return */ bool Fog::render(RenderGraph::Parameters & parameters, RHI::CommandBuffer & commandBuffer) { rmt_ScopedOpenGLSample(Fog); if (parameters.size() < 1) { glClearColor(0.0f, 1.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT); return false; } const GLuint inputTexture = parameters.pop().asUInt; commandBuffer.BeginRenderPass(m_renderPass, m_framebuffer, ivec2(0, 0), ivec2(m_rendering.GetWidth(), m_rendering.GetHeight())); { commandBuffer.Bind(m_pipeline); SetTexture<GL_TEXTURE_2D>(m_pipeline.m_uShaderObject, "depthMapSampler", 0, inputTexture, m_samplerDepthMap); SetUniform(m_pipeline.m_uShaderObject, "FogScattering", m_rendering.GetScene().fog.Scattering); SetUniform(m_pipeline.m_uShaderObject, "FogExtinction", m_rendering.GetScene().fog.Extinction); SetUniform(m_pipeline.m_uShaderObject, "FogColor", m_rendering.GetScene().fog.Color); SetUniform(m_pipeline.m_uShaderObject, "camera_near", 1.0f); SetUniform(m_pipeline.m_uShaderObject, "camera_far", 1000.0f); SetUniform(m_pipeline.m_uShaderObject, "near_plane_half_size", vec2(m_rendering.GetHeight() * float(m_rendering.GetWidth()/(float)m_rendering.GetHeight()), tanf(75.0f/2.0))); m_rendering.m_pQuadMesh->draw(commandBuffer); } commandBuffer.EndRenderPass(); { GLint texture = 0; glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &texture); RenderGraph::Value v; v.asUInt = texture; parameters.push(v); } return(true); }
void VertexLerpShader::SetLerp(float t) { ASSERT(IsReadyForUse() == true); SetUniform(m_lerpUniform, t); }
/* ============= OpenGLProgram::SetUniform Sets a uniform vec4 value. ============= */ void OpenGLProgram::SetUniform( const char* name, const glm::vec4& value ) { SetUniform( name, &value[0], 4 ); }
void ShaderProgram::SetUniform(const std::string& name, GLint v0) { GLint loc = GetUniformLocation(name); if (loc >= 0) SetUniform(loc, v0); }
void ShaderProgram::SetUniform(const std::string& name, const Matrix4f &mat) { GLint loc = GetUniformLocation(name); if( loc >= 0 ) SetUniform(loc, mat); }
void MotionStretchEffect::SetObjectFadeEnd( Vector2 displacement ) { SetUniform( MOTION_STRETCH_OBJECT_FADE_END_PROPERTY_NAME, displacement ); }
void DisplacementEffect::SetStateProperty(const float state) { SetUniform( DISPLACEMENT_EFFECT_STATE_PROPERTY_NAME, state); }
void DisplacementEffect::SetLightingMultiplierProperty(const float lightMultiplier) { SetUniform( DISPLACEMENT_EFFECT_LIGHT_MULTIPLIER_PROPERTY_NAME, lightMultiplier); }
void DisplacementEffect::SetDiffuseLightColorProperty(const Vector3 diffuseLight) { SetUniform( DISPLACEMENT_EFFECT_DIFFUSE_LIGHT_COLOR_PROPERTY_NAME, diffuseLight); }
void DisplacementEffect::SetAmbientLightColorProperty(const Vector3 ambientLight) { SetUniform( DISPLACEMENT_EFFECT_AMBIENT_LIGHT_COLOR_PROPERTY_NAME, ambientLight); }
void DisplacementEffect::SetLightDirection(const Vector3 lightDirection) { SetUniform( DISPLACEMENT_EFFECT_LIGHT_DIRECTION_PROPERTY_NAME, lightDirection); }
void MotionStretchEffect::SetGeometryStretchFactor( float scalingFactor ) { SetUniform( MOTION_STRETCH_GEOMETRY_STRETCH_SCALING_FACTOR_PROPERTY_NAME, scalingFactor ); }
void MotionStretchEffect::SetSpeedScalingFactor( float scalingFactor ) { SetUniform( MOTION_STRETCH_SPEED_SCALING_FACTOR_PROPERTY_NAME, scalingFactor ); }
void DisplacementEffect::SetHeightScaleProperty(const float heightScale) { SetUniform( DISPLACEMENT_EFFECT_HEIGHT_SCALE_PROPERTY_NAME, heightScale); }
void MotionStretchEffect::SetAlphaScale( float alphaScale ) { SetUniform( MOTION_STRETCH_ALPHA_SCALE_PROPERTY_NAME, alphaScale ); }
void TerrainVert::BindPerModel(TerrainGeometry& _geometry, TerrainMaterial& _material) { // Textures SetTexture("s_heightData", _geometry.HeightData().GetRead()); SetTexture("s_velocityData", _geometry.VelocityData().GetRead()); SetTexture("s_miscData", _geometry.MiscData().GetRead()); SetTexture("s_normalData", _geometry.NormalData().GetRead()); SetTexture("s_smudgeData", _geometry.SmudgeData().GetRead()); SetTexture("s_diffuseMap", _material.someTexture); SetTexture("s_rockDiffuse", _material.rockDiffuseTexture); SetTexture("s_moltenMapData", _geometry.MoltenMapData().GetRead()); // Matrices SetUniform("u_mvpMatrix", RenderParams::ModelViewProjectionMatrix()); SetUniform("u_modelViewMatrix", RenderParams::ModelViewMatrix()); SetUniform("u_viewMatrix", RenderParams::ViewMatrix() ); // Uniforms SetUniform("u_cameraPos", RenderParams::CameraPosition()); SetUniform("u_fogDensity", _material.fogDensity); SetUniform("u_fogHeight", _material.fogHeight); SetUniform("u_fogColorAway", _material.fogColorAway); SetUniform("u_fogColorTowards", _material.fogColorTowards); SetUniform("u_rockDetailBumpStrength", _material.rockDetailBumpStrength); SetUniform("u_moltenColor", _material.moltenColor); SetUniform("u_moltenColorScalar", _material.moltenColorScalar); SetUniform("u_moltenAlphaScalar", _material.moltenAlphaScalar); SetUniform("u_moltenAlphaPower", _material.moltenAlphaPower); SetUniform("u_dirtHeightToOpaque", _material.dirtHeightToOpaque); SetUniform("u_mapHeightOffset", _material.moltenMapOffset); SetUniform("u_lightDir", -glm::euclidean(vec2(_material.lightAltitude, _material.lightAzimuth))); SetUniform("u_lightDistance", _material.lightDistance); }
void ShaderProgram::SetUniform(const std::string& name, const Vector4f &vec) { GLint loc = GetUniformLocation(name); if (loc >= 0) SetUniform(loc, vec); }
void TerrainFrag::BindPerModel(TerrainGeometry& _geometry, TerrainMaterial& _material) { //PRINTF("viewPosition %2f, %2f, %2f\n", RenderParams::CameraPosition().x, RenderParams::CameraPosition().y, RenderParams::CameraPosition().z); float phase = fmodf( (float)glfwGetTime() * _material.creaseFlowSpeed, 1.0f ); float phaseA = fmodf( phase + 0.0f, 1.0f ) * 2.0f - 1.0f; float phaseB = fmodf( phase + 0.5f, 1.0f ) * 2.0f - 1.0f; float alphaB = fabs( 0.5f - phase ) * 2.0f; float alphaA = 1.0f - alphaB; SetUniform( "u_phaseA", phaseA ); SetUniform( "u_phaseB", phaseB ); SetUniform( "u_phaseAlpha", alphaB ); SetUniform( "u_flowOffset", _material.creaseFlowOffset ); // Uniforms SetUniform("u_cameraPos", RenderParams::CameraPosition()); SetUniform("u_rockColorA", _material.rockColorA); SetUniform("u_rockColorB", _material.rockColorB); SetUniform("u_rockRoughnessA", _material.rockRoughnessA); SetUniform("u_rockRoughnessB", _material.rockRoughnessB); SetUniform("u_rockFresnelA", _material.rockFresnelA); SetUniform("u_rockFresnelB", _material.rockFresnelB); SetUniform("u_hotRockColor", _material.hotRockColor); SetUniform("u_hotRockRoughness", _material.hotRockRoughness); SetUniform("u_hotRockFresnel", _material.hotRockFresnel); SetUniform("u_moltenPlateAlpha", _material.moltenPlateAlpha); SetUniform("u_moltenPlateAlphaPower", _material.moltenPlateAlphaPower); SetUniform("u_moltenCreaseAlpha", _material.moltenCreaseAlpha); SetUniform("u_moltenCreaseAlphaPower", _material.moltenCreaseAlphaPower); SetUniform("u_glowScalar", _material.glowScalar); SetUniform("u_glowPower", _material.glowPower); SetUniform("u_glowDetailScalar", _material.glowDetailScalar); SetUniform("u_glowDetailPower", _material.glowDetailPower); SetUniform("u_moltenColor", _material.moltenColor); SetUniform("u_moltenColorScalar", _material.moltenColorScalar); SetUniform("u_rockDetailBumpStrength", _material.rockDetailBumpStrength); SetUniform("u_rockDetailBumpSlopePower", _material.rockDetailBumpSlopePower); SetUniform("u_dirtColor", _material.dirtColor); SetUniform("u_lightDir", -glm::euclidean(vec2(_material.lightAltitude, _material.lightAzimuth))); SetUniform("u_lightDistance", _material.lightDistance); SetUniform("u_lightIntensity", _material.directLightIntensity); SetUniform("u_ambientLightIntensity", _material.ambientLightIntensity); // Creases SetUniform("u_bearingCreaseScalar", _material.bearingCreaseScalar); SetUniform("u_lateralCreaseScalar", _material.lateralCreaseScalar); SetUniform("u_creaseRatio", _material.creaseRatio); SetUniform("u_creaseMipLevel", _material.creaseMipLevel); SetUniform("u_creaseForwardScalar", _material.creaseForwardScalar); SetUniform("u_creaseMapRepeat", _material.creaseMapRepeat); SetUniform("u_creaseGridRepeat", _material.creaseGridRepeat); // Textures SetTexture("s_rockDiffuse", _material.rockDiffuseTexture); SetTexture("s_creaseMap", _material.creaseTexture); SetTexture("s_moltenMapData", _geometry.MoltenMapData().GetRead()); SetTexture("s_smudgeData", _geometry.SmudgeData().GetRead()); SetTexture("s_velocityData", _geometry.VelocityData().GetRead()); SetTexture("s_miscData", _geometry.MiscData().GetRead()); SetTexture("s_heightData", _geometry.HeightData().GetRead()); TerrainMousePos terrainMousePos = _geometry.GetTerrainMousePos(); terrainMousePos.z = INT_MAX; glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(terrainMousePos), &terrainMousePos, GL_DYNAMIC_COPY); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, _geometry.MousePositionBuffer()); }
/* ============= OpenGLProgram::SetUniform Sets a single float uniform value ============= */ void OpenGLProgram::SetUniform( const char* name, const float value ) { SetUniform( name, &value, 1 ); }
// Set a uniform global parameter of the program by name. void STShaderProgram::SetUniform(const std::string& name, const STVector3& value) { SetUniform(name, value.x, value.y, value.z); }
/* ============= OpenGLProgram::SetUniform Sets a uniform mat4 value. ============= */ void OpenGLProgram::SetUniform( const char* name, const glm::mat4& value ) { SetUniform( name, &value[0][0], 16 ); }
// Set a uniform global parameter of the program by name. void STShaderProgram::SetUniform(const std::string& name, const STColor4f& value) { SetUniform(name, value.r, value.g, value.b, value.a); }
void CRenderingContext::SetupMaterial() { if (!GetContext().m_hMaterial.IsValid()) return; if (!m_pShader) return; const tstring& sMaterialBlend = GetContext().m_hMaterial->m_sBlend; if (sMaterialBlend == "alpha") SetBlend(BLEND_ALPHA); else if (sMaterialBlend == "additive") SetBlend(BLEND_ADDITIVE); else { TAssert(!sMaterialBlend.length()); SetBlend(BLEND_NONE); } for (auto it = m_pShader->m_asUniforms.begin(); it != m_pShader->m_asUniforms.end(); it++) { CShader::CUniform& pUniformName = it->second; CShader::CParameter::CUniform* pUniform = it->second.m_pDefault; if (pUniform) { if (pUniformName.m_sUniformType == "float") SetUniform(it->first.c_str(), pUniform->m_flValue); else if (pUniformName.m_sUniformType == "vec2") SetUniform(it->first.c_str(), pUniform->m_vec2Value); else if (pUniformName.m_sUniformType == "vec3") SetUniform(it->first.c_str(), pUniform->m_vecValue); else if (pUniformName.m_sUniformType == "vec4") SetUniform(it->first.c_str(), pUniform->m_vec4Value); else if (pUniformName.m_sUniformType == "int") SetUniform(it->first.c_str(), pUniform->m_iValue); else if (pUniformName.m_sUniformType == "bool") SetUniform(it->first.c_str(), pUniform->m_bValue); else if (pUniformName.m_sUniformType == "mat4") { TUnimplemented(); } else if (pUniformName.m_sUniformType == "sampler2D") { TUnimplemented(); } else TUnimplemented(); } else { if (pUniformName.m_sUniformType == "float") SetUniform(it->first.c_str(), 0.0f); else if (pUniformName.m_sUniformType == "vec2") SetUniform(it->first.c_str(), Vector2D()); else if (pUniformName.m_sUniformType == "vec3") SetUniform(it->first.c_str(), Vector()); else if (pUniformName.m_sUniformType == "vec4") SetUniform(it->first.c_str(), Vector4D()); else if (pUniformName.m_sUniformType == "int") SetUniform(it->first.c_str(), 0); else if (pUniformName.m_sUniformType == "bool") SetUniform(it->first.c_str(), false); else if (pUniformName.m_sUniformType == "mat4") SetUniform(it->first.c_str(), Matrix4x4()); else if (pUniformName.m_sUniformType == "sampler2D") SetUniform(it->first.c_str(), 0); else TUnimplemented(); } } for (size_t i = 0; i < GetContext().m_hMaterial->m_aParameters.size(); i++) { auto& oParameter = GetContext().m_hMaterial->m_aParameters[i]; CShader::CParameter* pShaderParameter = oParameter.m_pShaderParameter; TAssert(pShaderParameter); if (!pShaderParameter) continue; for (size_t j = 0; j < pShaderParameter->m_aActions.size(); j++) { auto& oAction = pShaderParameter->m_aActions[j]; tstring& sName = oAction.m_sName; tstring& sValue = oAction.m_sValue; tstring& sType = m_pShader->m_asUniforms[sName].m_sUniformType; if (sValue == "[value]") { if (sType == "float") SetUniform(sName.c_str(), oParameter.m_flValue); else if (sType == "vec2") SetUniform(sName.c_str(), oParameter.m_vec2Value); else if (sType == "vec3") SetUniform(sName.c_str(), oParameter.m_vecValue); else if (sType == "vec4") SetUniform(sName.c_str(), oParameter.m_vec4Value); else if (sType == "int") SetUniform(sName.c_str(), oParameter.m_iValue); else if (sType == "bool") SetUniform(sName.c_str(), oParameter.m_bValue); else if (sType == "mat4") { TUnimplemented(); } else if (sType == "sampler2D") { // No op, handled below. } else TUnimplemented(); } else { if (sType == "float") SetUniform(sName.c_str(), oAction.m_flValue); else if (sType == "vec2") SetUniform(sName.c_str(), oAction.m_vec2Value); else if (sType == "vec3") SetUniform(sName.c_str(), oAction.m_vecValue); else if (sType == "vec4") SetUniform(sName.c_str(), oAction.m_vec4Value); else if (sType == "int") SetUniform(sName.c_str(), oAction.m_iValue); else if (sType == "bool") SetUniform(sName.c_str(), oAction.m_bValue); else if (sType == "mat4") { TUnimplemented(); } else if (sType == "sampler2D") { TUnimplemented(); SetUniform(sName.c_str(), 0); } else TUnimplemented(); } } } for (size_t i = 0; i < m_pShader->m_asTextures.size(); i++) { if (!GetContext().m_hMaterial->m_ahTextures[i].IsValid()) continue; glActiveTexture(GL_TEXTURE0+i); glBindTexture(GL_TEXTURE_2D, (GLuint)GetContext().m_hMaterial->m_ahTextures[i]->m_iGLID); SetUniform(m_pShader->m_asTextures[i].c_str(), (int)i); } }
// set uniform to texture slot index (0 to 7) - same as using SetUniform(name, int) but less error prone void Shader::SetUniform(const c8 * const name, const TextureHandle tex) { SetUniform(name, (GLint)tex->GetTextureSlotIndex()); };
ReflectionExample(void) : torus_indices(make_torus.Indices()) , torus_instr(make_torus.Instructions()) , vs_norm(ObjectDesc("Vertex-Normal")) , vs_refl(ObjectDesc("Vertex-Reflection")) , gs_refl(ObjectDesc("Geometry-Reflection")) { namespace se = oglplus::smart_enums; // Set the normal object vertex shader source vs_norm.Source( "#version 330\n" "in vec4 Position;" "in vec3 Normal;" "out vec3 geomColor;" "out vec3 geomNormal;" "out vec3 geomLight;" "uniform mat4 ProjectionMatrix, CameraMatrix, ModelMatrix;" "uniform vec3 LightPos;" "void main(void)" "{" " gl_Position = ModelMatrix * Position;" " geomColor = Normal;" " geomNormal = mat3(ModelMatrix)*Normal;" " geomLight = LightPos-gl_Position.xyz;" " gl_Position = ProjectionMatrix * CameraMatrix * gl_Position;" "}" ); // compile it vs_norm.Compile(); // Set the reflected object vertex shader source // which just passes data to the geometry shader vs_refl.Source( "#version 330\n" "in vec4 Position;" "in vec3 Normal;" "out vec3 vertNormal;" "void main(void)" "{" " gl_Position = Position;" " vertNormal = Normal;" "}" ); // compile it vs_refl.Compile(); // Set the reflected object geometry shader source // This shader creates a reflection matrix that // relies on the fact that the reflection is going // to be done by the y-plane gs_refl.Source( "#version 330\n" "layout(triangles) in;" "layout(triangle_strip, max_vertices = 6) out;" "in vec3 vertNormal[];" "uniform mat4 ProjectionMatrix;" "uniform mat4 CameraMatrix;" "uniform mat4 ModelMatrix;" "out vec3 geomColor;" "out vec3 geomNormal;" "out vec3 geomLight;" "uniform vec3 LightPos;" "mat4 ReflectionMatrix = mat4(" " 1.0, 0.0, 0.0, 0.0," " 0.0,-1.0, 0.0, 0.0," " 0.0, 0.0, 1.0, 0.0," " 0.0, 0.0, 0.0, 1.0 " ");" "void main(void)" "{" " for(int v=0; v!=gl_in.length(); ++v)" " {" " vec4 Position = gl_in[v].gl_Position;" " gl_Position = ModelMatrix * Position;" " geomColor = vertNormal[v];" " geomNormal = mat3(ModelMatrix)*vertNormal[v];" " geomLight = LightPos - gl_Position.xyz;" " gl_Position = " " ProjectionMatrix *" " CameraMatrix *" " ReflectionMatrix *" " gl_Position;" " EmitVertex();" " }" " EndPrimitive();" "}" ); // compile it gs_refl.Compile(); // set the fragment shader source fs.Source( "#version 330\n" "in vec3 geomColor;" "in vec3 geomNormal;" "in vec3 geomLight;" "out vec4 fragColor;" "void main(void)" "{" " float l = length(geomLight);" " float d = l > 0.0 ? dot(" " geomNormal, " " normalize(geomLight)" " ) / l : 0.0;" " float i = 0.2 + max(d, 0.0) * 2.0;" " fragColor = vec4(abs(geomNormal)*i, 1.0);" "}" ); // compile it fs.Compile(); // attach the shaders to the normal rendering program prog_norm.AttachShader(vs_norm); prog_norm.AttachShader(fs); // link it prog_norm.Link(); // attach the shaders to the reflection rendering program prog_refl.AttachShader(vs_refl); prog_refl.AttachShader(gs_refl); prog_refl.AttachShader(fs); // link it prog_refl.Link(); // bind the VAO for the torus torus.Bind(); // bind the VBO for the torus vertices torus_verts.Bind(se::Array()); { std::vector<GLfloat> data; GLuint n_per_vertex = make_torus.Positions(data); // upload the data Buffer::Data(se::Array(), data); // setup the vertex attribs array for the vertices typedef VertexAttribArray VAA; VertexAttribSlot loc_norm = VAA::GetLocation(prog_norm, "Position"), loc_refl = VAA::GetLocation(prog_refl, "Position"); assert(loc_norm == loc_refl); VertexAttribArray attr(loc_norm); attr.Setup<GLfloat>(n_per_vertex); attr.Enable(); } // bind the VBO for the torus normals torus_normals.Bind(se::Array()); { std::vector<GLfloat> data; GLuint n_per_vertex = make_torus.Normals(data); // upload the data Buffer::Data(se::Array(), data); // setup the vertex attribs array for the normals typedef VertexAttribArray VAA; VertexAttribSlot loc_norm = VAA::GetLocation(prog_norm, "Normal"), loc_refl = VAA::GetLocation(prog_refl, "Normal"); assert(loc_norm == loc_refl); VertexAttribArray attr(loc_norm); attr.Setup<GLfloat>(n_per_vertex); attr.Enable(); } // bind the VAO for the plane plane.Bind(); // bind the VBO for the plane vertices plane_verts.Bind(se::Array()); { GLfloat data[4*3] = { -2.0f, 0.0f, 2.0f, -2.0f, 0.0f, -2.0f, 2.0f, 0.0f, 2.0f, 2.0f, 0.0f, -2.0f }; // upload the data Buffer::Data(se::Array(), 4*3, data); // setup the vertex attribs array for the vertices prog_norm.Use(); VertexAttribArray attr(prog_norm, "Position"); attr.Setup<Vec3f>(); attr.Enable(); } // bind the VBO for the torus normals plane_normals.Bind(se::Array()); { GLfloat data[4*3] = { -0.1f, 1.0f, 0.1f, -0.1f, 1.0f, -0.1f, 0.1f, 1.0f, 0.1f, 0.1f, 1.0f, -0.1f }; // upload the data Buffer::Data(se::Array(), 4*3, data); // setup the vertex attribs array for the normals prog_norm.Use(); VertexAttribArray attr(prog_norm, "Normal"); attr.Setup<Vec3f>(); attr.Enable(); } VertexArray::Unbind(); Vec3f lightPos(2.0f, 2.0f, 3.0f); prog_norm.Use(); SetUniform(prog_norm, "LightPos", lightPos); prog_refl.Use(); SetUniform(prog_refl, "LightPos", lightPos); // gl.ClearColor(0.2f, 0.2f, 0.2f, 0.0f); gl.ClearDepth(1.0f); gl.ClearStencil(0); }
void BL_Shader::Update( const RAS_MeshSlot & ms, RAS_IRasterizer* rasty ) { if (!Ok() || !mPreDef.size()) return; if ( GLEW_ARB_fragment_shader && GLEW_ARB_vertex_shader && GLEW_ARB_shader_objects ) { MT_Matrix4x4 model; model.setValue(ms.m_OpenGLMatrix); const MT_Matrix4x4& view = rasty->GetViewMatrix(); if (mAttr==SHD_TANGENT) ms.m_mesh->SetMeshModified(true); BL_UniformVecDef::iterator it; for (it = mPreDef.begin(); it!= mPreDef.end(); it++) { BL_DefUniform *uni = (*it); if (uni->mLoc == -1) continue; switch (uni->mType) { case MODELMATRIX: { SetUniform(uni->mLoc, model); break; } case MODELMATRIX_TRANSPOSE: { SetUniform(uni->mLoc, model, true); break; } case MODELMATRIX_INVERSE: { model.invert(); SetUniform(uni->mLoc, model); break; } case MODELMATRIX_INVERSETRANSPOSE: { model.invert(); SetUniform(uni->mLoc, model, true); break; } case MODELVIEWMATRIX: { SetUniform(uni->mLoc, view*model); break; } case MODELVIEWMATRIX_TRANSPOSE: { MT_Matrix4x4 mat(view*model); SetUniform(uni->mLoc, mat, true); break; } case MODELVIEWMATRIX_INVERSE: { MT_Matrix4x4 mat(view*model); mat.invert(); SetUniform(uni->mLoc, mat); break; } case MODELVIEWMATRIX_INVERSETRANSPOSE: { MT_Matrix4x4 mat(view*model); mat.invert(); SetUniform(uni->mLoc, mat, true); break; } case CAM_POS: { MT_Point3 pos(rasty->GetCameraPosition()); SetUniform(uni->mLoc, pos); break; } case VIEWMATRIX: { SetUniform(uni->mLoc, view); break; } case VIEWMATRIX_TRANSPOSE: { SetUniform(uni->mLoc, view, true); break; } case VIEWMATRIX_INVERSE: { MT_Matrix4x4 viewinv = view; viewinv.invert(); SetUniform(uni->mLoc, view); break; } case VIEWMATRIX_INVERSETRANSPOSE: { MT_Matrix4x4 viewinv = view; viewinv.invert(); SetUniform(uni->mLoc, view, true); break; } case CONSTANT_TIMER: { SetUniform(uni->mLoc, (float)rasty->GetTime()); break; } default: break; } } } }