void Shader::setUniform(const std::string & uniformName, float value) { if (m_uniforms_locations.count(uniformName)) { glProgramUniform1f(m_program_id, m_uniforms_locations[uniformName], value); } else { if (getUniformLocation(uniformName)) { glProgramUniform1f(m_program_id, m_uniforms_locations[uniformName], value); } } }
void Material::Bind() { glUseProgram(program); for (std::unordered_map<std::string, Texture2D*>::iterator it = textures.begin(); it != textures.end(); ++it) { it->second->Bind(); } for (std::unordered_map<std::string, ShaderVariable>::iterator it = uniforms.begin(); it != uniforms.end(); ++it) { ShaderVariable v = it->second; switch (v.size) { case 1: glProgramUniform1f(program, v.index, data[v.offset]); break; case 2: glProgramUniform2f(program, v.index, data[v.offset], data[v.offset + 1]); break; case 3: glProgramUniform3f(program, v.index, data[v.offset], data[v.offset + 1], data[v.offset + 2]); break; case 4: glProgramUniform4f(program, v.index, data[v.offset], data[v.offset + 1], data[v.offset + 2], data[v.offset + 3]); break; case 16: glProgramUniformMatrix4fv(program, v.index, 1, GL_TRUE, &data[v.offset]); break; } } }
void ShaderProgram::setUniform(const char* param, float val) { unsigned int loc = getUniformLocation(param); // gl_check(glUniform1f(m_programId, loc, val)); gl_check(glProgramUniform1f(m_programId, loc, val)); }
void shader_setfloat(shader_t shader, sparam_t param, float val) { if (matching_shader(shader, param)) { glProgramUniform1f(shader->program, param->param, val); gl_success("glProgramUniform1f"); } }
void VSShaderLib::setUniform(std::string name, float value) { float val = value; myUniforms u = pUniforms[name]; glProgramUniform1f(pProgram, u.location, val); }
void Material::SetFloat(std::string name, float& value) { int location = glGetUniformLocation(program, name.c_str()); if (location == -1) { //std::cout << "Attempt made to set invalid uniform variable: " << name << std::endl; } glProgramUniform1f(program, location, value); }
void VSShaderLib::setUniform(std::string name, float value) { // assert(pUniforms.count(name) != 0); float val = value; myUniforms u = pUniforms[name]; glProgramUniform1f(pProgram, u.location, val); }
//-------------------------------------------------------------------------- void PostProcessor::Draw( const Texture2D& _colorTex, float _toneExposure, const RenderTarget& _renderTarget) { glUseProgram(toneMapping.program.id); glProgramUniform1f(toneMapping.program.id, toneMapping.exposureVar, _toneExposure); _colorTex.Bind(toneMapping.colorTexUnit); _renderTarget.Draw(); }
void TransitionTriangulation::configurePrograms() { glProgramUniform3f (Parameters::getInstance()->g_programs[PROGRAM_TRANSITION_DRAW], Parameters::getInstance()->g_uniform_locations[LOCATION_TRANSITION_SCENE_SIZE], Parameters::getInstance()->g_geometry.scale[0], Parameters::getInstance()->g_geometry.scale[1], Parameters::getInstance()->g_geometry.scale[2]); glProgramUniform2f (Parameters::getInstance()->g_programs[PROGRAM_TRANSITION_DRAW], Parameters::getInstance()->g_uniform_locations[LOCATION_TRANSITION_VIEWPORT], Parameters::getInstance()->g_window.width, Parameters::getInstance()->g_window.height); glProgramUniform1f (Parameters::getInstance()->g_programs[PROGRAM_TRANSITION_DRAW], Parameters::getInstance()->g_uniform_locations[LOCATION_TRANSITION_TAN_FOVY], tanf (Parameters::getInstance()->g_camera.fovy / 360.f * 3.14159f)); glProgramUniform1f(Parameters::getInstance()->g_programs[PROGRAM_TRANSITION_DRAW], Parameters::getInstance()->g_uniform_locations[LOCATION_TRANSITION_SCALE], Parameters::getInstance()->g_scale); glProgramUniform1i (Parameters::getInstance()->g_programs[PROGRAM_TRANSITION_DRAW], Parameters::getInstance()->g_uniform_locations[LOCATION_TRANSITION_DENSITY], TEXTURE_DENSITY); glProgramUniform1i (Parameters::getInstance()->g_programs[PROGRAM_TRANSITION_DRAW], Parameters::getInstance()->g_uniform_locations[LOCATION_TRANSITION_CODE_CLASS_TR], TEXTURE_CODE_CLASS_TR); glProgramUniform1i (Parameters::getInstance()->g_programs[PROGRAM_TRANSITION_DRAW], Parameters::getInstance()->g_uniform_locations[LOCATION_TRANSITION_CODE_VERTICES_TR], TEXTURE_CODE_VERTICES_TR); glProgramUniform1i (Parameters::getInstance()->g_programs[PROGRAM_TRANSITION_DRAW], Parameters::getInstance()->g_uniform_locations[LOCATION_TRANSITION_CLASS_TRIANGLES_TR], TEXTURE_CLASS_TRIANGLES_TR); glProgramUniform1i (Parameters::getInstance()->g_programs[PROGRAM_TRANSITION_DRAW], Parameters::getInstance()->g_uniform_locations[LOCATION_TRANSITION_TESSEL], (int)Parameters::getInstance()->g_tessel); glProgramUniform1f(Parameters::getInstance()->g_programs[PROGRAM_TRANSITION_DRAW], Parameters::getInstance()->g_uniform_locations[LOCATION_TRANSITION_ISOSURFACE], 0);//Parameters::getInstance()->g_isosurface); glProgramUniform1i (Parameters::getInstance()->g_programs[PROGRAM_TRANSITION_DRAW], Parameters::getInstance()->g_uniform_locations[LOCATION_TRANSITION_FROMTEXTURE], 1);//Parameters::getInstance()->g_fromtexture); glProgramUniform1i (Parameters::getInstance()->g_programs[PROGRAM_TRANSITION_DRAW], Parameters::getInstance()->g_uniform_locations[LOCATION_TRANSITION_METRIC], 0);//Parameters::getInstance()->g_radial_length); glProgramUniform1f(Parameters::getInstance()->g_programs[PROGRAM_TRANSITION_DRAW], Parameters::getInstance()->g_uniform_locations[LOCATION_TRANSITION_TIME], Parameters::getInstance()->g_time_elapsed); }
//[-------------------------------------------------------] //[ Public virtual Renderer::IProgram methods ] //[-------------------------------------------------------] void ProgramMonolithicDsa::setUniform1f(handle uniformHandle, float value) { if (static_cast<OpenGLRenderer&>(getRenderer()).getExtensions().isGL_ARB_direct_state_access()) { glProgramUniform1f(mOpenGLProgram, static_cast<GLint>(uniformHandle), value); } else { glProgramUniform1fEXT(mOpenGLProgram, static_cast<GLint>(uniformHandle), value); } }
//[-------------------------------------------------------] //[ Public virtual Renderer::IProgram methods ] //[-------------------------------------------------------] void ProgramSeparateDsa::setUniform1f(handle uniformHandle, float value) { if (static_cast<OpenGLRenderer&>(getRenderer()).getExtensions().isGL_ARB_direct_state_access()) { glProgramUniform1f(mVertexShaderSeparate->getOpenGLShaderProgram(), static_cast<GLint>(uniformHandle), value); } else { glProgramUniform1fEXT(mVertexShaderSeparate->getOpenGLShaderProgram(), static_cast<GLint>(uniformHandle), value); } }
void draw(GLuint program, GLfloat* triangle, GLuint vertexbuffer) { glUseProgram(program); GLuint scaleID = glGetUniformLocation(program, "scale"); glBufferData(GL_ARRAY_BUFFER, NUMBER_OF_VERTEX * sizeof(triangle), triangle, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); glProgramUniform1f(program, scaleID, scale); glDrawArrays(GL_TRIANGLES, 0, 3); }
void Shader::SendFloat(int location, float value) const { if (location == -1) return; if (glProgramUniform1f) glProgramUniform1f(m_program, location, value); else { OpenGL::BindProgram(m_program); glUniform1f(location, value); } }
void program::set_uniform1f(char const* varname, GLfloat v0) const { GLint location = get_uniform_location(varname); if (location >= 0) { #if GPUCAST_GL_DIRECT_STATE_ACCESS glProgramUniform1f(id_, location, v0); #else glUniform1f(location, v0); #endif } }
void RenderCB(GlRunner *runner) { glClearColor(0.2, 0.1, 0.2, 0.0); glClearDepthf(1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); clock_t current_time = clock(); float delta = 0.00001*(current_time-start_time); glProgramUniform1f(VS, glGetUniformLocation(VS, "uTime"), delta); glProgramUniform4f(FS, glGetUniformLocation(FS, "uColor"), 1.0, 0.0, 0.0, 0.0); glDrawArrays(GL_POINTS, 0, xmesh*ymesh); glProgramUniform4f(FS, glGetUniformLocation(FS, "uColor"), 1.0, 1.0, 0.0, 0.0); glDrawElements(GL_TRIANGLE_STRIP, xmesh*(ymesh-1)*2, GL_UNSIGNED_INT, (void*)0); }
GLUniform::GLUniform(const char* name, GLuint program, int size, const char* type) : GLNode(name) { this->location = glGetUniformLocation(program, name); if(size == 1 && std::string(type) == std::string("i") ) glProgramUniform1i(program, this->location, 0); else if(size == 1 && std::string(type) == std::string("f") ) glProgramUniform1f(program, this->location, 0); else if(size == 2 && std::string(type) == std::string("f") ) glProgramUniform2f(program, this->location, 0, 0); else if(size == 3 && std::string(type) == std::string("f") ) glProgramUniform3f(program, this->location, 0, 0, 0); else if(size == 3 && std::string(type) == std::string("f") ) glProgramUniform3f(program, this->location, 0, 0, 0); else if(size == 4 && std::string(type) == std::string("f") ) glProgramUniform4f(program, this->location, 0, 0, 0, 0); this->id = UINT_MAX; }
void renderVis(double peak) { double transition = 0.0; time++; totaltime++; if(time > SHADER_TIME){ active++; if(active == SHADER_AMOUNT){ active = 0; } time = 0; } if(time <= TRANSITION_TIME){ transition = 1.0 - time / TRANSITION_TIME; int prev = active > 0 ? active - 1 : SHADER_AMOUNT - 1; glUseProgram(progs[prev].program); glClearColor(0, 0, 0, 1); glClear(GL_COLOR_BUFFER_BIT); glProgramUniform1f(progs[prev].program, progs[prev].peak, peak); glProgramUniform1f(progs[prev].program, progs[prev].transition, 0); glProgramUniform1f(progs[prev].program, progs[prev].time, totaltime); glProgramUniform2f(progs[prev].program, progs[prev].size, width, height); glProgramUniform1i(progs[prev].program, progs[prev].texture, 0); glBindVertexArray(vao); glDrawArrays(GL_TRIANGLES, 0, 6); glBindVertexArray(0); glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, width, height, 0); glUseProgram(progs[active].program); glProgramUniform1i(progs[active].program, progs[active].texture, 0); } glClearColor(0, 0, 0, 1); glClear(GL_COLOR_BUFFER_BIT); glProgramUniform1f(progs[active].program, progs[active].peak, peak); glProgramUniform1f(progs[active].program, progs[active].transition, transition); glProgramUniform1f(progs[active].program, progs[active].time, totaltime); glProgramUniform2f(progs[active].program, progs[active].size, width, height); glBindVertexArray(vao); glDrawArrays(GL_TRIANGLES, 0, 6); glBindVertexArray(0); }
void pass(int width, int height, PassType type) { glClear(GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT); glProgramUniform1f(gs.program, COLOR, gs.color); //gs.lightPosition = glm::vec3(sin(gs.time / 10) * 1.5, cos(gs.time / 10) * 1.5, 3.5); glProgramUniform3fv(gs.program, LIGHT, 1, &gs.lightPosition[0]); switch (type) { case PassType::light: { glm::mat4 perspective = glm::perspective(45.0, (double)width / (double)height, 1.0, 100.0); glm::mat4 lookAt = glm::lookAt(gs.lightPosition, glm::vec3(0, 0, 0), glm::vec3(0, 0, 1)); gs.mat = perspective*lookAt; gs.matLight = gs.mat; break; } default: case PassType::mainCamera: { glm::mat4 perspective = glm::perspective(45.0, (double)width / (double)height, 1.0, 100.0); //glm::mat4 lookAt = glm::lookAt(glm::vec3(3.5, 3.5, 2.5), glm::vec3(0, 0, 0), glm::vec3(0, 0, 1)); glm::mat4 lookAt = glm::lookAt(glm::vec3(sin(gs.time / 10)*5.0, cos(gs.time / 10)*5.0, 1.5), glm::vec3(0, 0, 0), glm::vec3(0, 0, 1)); gs.mat = perspective*lookAt; break; } } glProgramUniformMatrix4fv(gs.program, MATRIX, 1, false, &gs.mat[0][0]); glProgramUniformMatrix4fv(gs.program, MATRIXLIGHT, 1, false, &gs.matLight[0][0]); glUseProgram(gs.program); glBindVertexArray(gs.vao); { glDrawArrays(GL_TRIANGLES, 0, gs.bufferSize); } glBindVertexArray(0); glUseProgram(0); }
void ShaderProgram::setUniform(const std::string& name, float value) { if(!ext::separateShaderObjects()) //If we can't do uniforms without binding { //Get the currently-bound program and bind this program auto previous = getBound(); bind(); //Set the uniform glCheck(glUniform1f(uniformLocation(name), value)); //Set the previous program back glCheck(glUseProgram(previous)); } else //If we can do uniforms without binding { //Set the uniform with this program glCheck(glProgramUniform1f(program_, uniformLocation(name), value)); } }
void RenderCB(GlRunner *runner) { glClearColor(0.0, 0.0, 0.0, 1.0); glClearDepthf(1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // fix point light location, and move ground instead model_mat = glm::translate(glm::vec3(roam_x, roam_y, roam_z)); glProgramUniformMatrix4fv(VS, glGetUniformLocation(VS, "uModel"), 1, GL_FALSE, &model_mat[0][0]); glProgramUniformMatrix4fv(VS, glGetUniformLocation(VS, "uView"), 1, GL_FALSE, &view_mat[0][0]); glProgramUniformMatrix4fv(VS, glGetUniformLocation(VS, "uProj"), 1, GL_FALSE, &proj_mat[0][0]); glProgramUniform3fv(FS, glGetUniformLocation(FS, "uLightLoc"), 1, &light_loc[0]); glProgramUniform3fv(FS, glGetUniformLocation(FS, "uAntenna"), 1, &light_antenna[0]); glProgramUniform1f(FS, glGetUniformLocation(FS, "uRadians"), 3.1415926/6.0); glDrawArrays(GL_TRIANGLES, 0, 6); }
void ParticleSystem::loadShaders() { if (m_updateProg) { glDeleteProgram(m_updateProg); m_updateProg = 0; } glGenProgramPipelines( 1, &m_programPipeline); std::string src = loadShaderSourceWithUniformTag("shaders/uniforms.h", "shaders/particlesCS.glsl"); m_updateProg = createShaderPipelineProgram(GL_COMPUTE_SHADER, src.c_str()); glBindProgramPipeline(m_programPipeline); GLint loc = glGetUniformLocation(m_updateProg, "invNoiseSize"); glProgramUniform1f(m_updateProg, loc, 1.0f / m_noiseSize); loc = glGetUniformLocation(m_updateProg, "noiseTex3D"); glProgramUniform1i(m_updateProg, loc, 0); glBindProgramPipeline(0); }
void ParticleSystem::update(float timeDelta) { static bool updated = false; if (!updated) { LOGI("ParticleSystem: First Update Time: %f", timeDelta); updated = true; } m_mcpolygonizer->generateMesh(m_pos, m_vel, timeDelta); // Invoke the compute shader to integrate the particles glBindProgramPipeline(m_programPipeline); glBindBufferBase( GL_SHADER_STORAGE_BUFFER, 1, m_pos->getBuffer() ); glBindBufferBase( GL_SHADER_STORAGE_BUFFER, 2, m_vel->getBuffer() ); // Update the timestep in the shaders GLuint loc = glGetUniformLocation(m_updateProg, "timeStep"); glProgramUniform1f(m_updateProg, loc, timeDelta); uint xGroups = (m_size + (WORK_GROUP_SIZE - 1)) / WORK_GROUP_SIZE; glDispatchCompute(xGroups, 1, 1); // We need to block here on compute completion to ensure that the // computation is done before we render glMemoryBarrier( GL_SHADER_STORAGE_BARRIER_BIT ); glBindBufferBase( GL_SHADER_STORAGE_BUFFER, 2, 0 ); glBindBufferBase( GL_SHADER_STORAGE_BUFFER, 1, 0 ); // Update the timestep in the shaders //GLuint loc = glGetUniformLocation(m_updateProg, "timeStep"); //glProgramUniform1f(m_updateProg, loc, timeDelta); glBindProgramPipeline(0); }
GLuint OpenSubdivPtexShader::bindProgram(const MHWRender::MDrawContext & mDrawContext, OpenSubdiv::OsdGLDrawContext *osdDrawContext, const OpenSubdiv::OsdPatchArray & patch) { CHECK_GL_ERROR("bindProgram begin\n"); // Build shader Effect effect; effect.color = _enableColor; effect.occlusion = _enableOcclusion; effect.displacement = _enableDisplacement; effect.normal = _enableNormal; EffectDesc effectDesc( patch.desc, effect ); EffectDrawRegistry::ConfigType * config = effectRegistry.GetDrawConfig(effectDesc); // Install shader GLuint program = config->program; glUseProgram(program); // Update and bind transform state struct Transform { float ModelViewMatrix[16]; float ProjectionMatrix[16]; float ModelViewProjectionMatrix[16]; } transformData; setMatrix(mDrawContext.getMatrix(MHWRender::MDrawContext::kWorldViewMtx), transformData.ModelViewMatrix); setMatrix(mDrawContext.getMatrix(MHWRender::MDrawContext::kProjectionMtx), transformData.ProjectionMatrix); setMatrix(mDrawContext.getMatrix(MHWRender::MDrawContext::kWorldViewProjMtx), transformData.ModelViewProjectionMatrix); if (!g_transformUB) { glGenBuffers(1, &g_transformUB); glBindBuffer(GL_UNIFORM_BUFFER, g_transformUB); glBufferData(GL_UNIFORM_BUFFER, sizeof(transformData), NULL, GL_STATIC_DRAW); }; glBindBuffer(GL_UNIFORM_BUFFER, g_transformUB); glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(transformData), &transformData); glBindBuffer(GL_UNIFORM_BUFFER, 0); glBindBufferBase(GL_UNIFORM_BUFFER, g_transformBinding, g_transformUB); // Update and bind tessellation state struct Tessellation { float TessLevel; int GregoryQuadOffsetBase; int PrimitiveIdBase; } tessellationData; tessellationData.TessLevel = static_cast<float>(1 << _tessFactor); tessellationData.GregoryQuadOffsetBase = patch.GetQuadOffsetBase; tessellationData.PrimitiveIdBase = patch.GetPatchIndex();; if (!g_tessellationUB) { glGenBuffers(1, &g_tessellationUB); glBindBuffer(GL_UNIFORM_BUFFER, g_tessellationUB); glBufferData(GL_UNIFORM_BUFFER, sizeof(tessellationData), NULL, GL_STATIC_DRAW); }; glBindBuffer(GL_UNIFORM_BUFFER, g_tessellationUB); glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(tessellationData), &tessellationData); glBindBuffer(GL_UNIFORM_BUFFER, 0); glBindBufferBase(GL_UNIFORM_BUFFER, g_tessellationBinding, g_tessellationUB); #ifdef USE_NON_IMAGE_BASED_LIGHTING // Update and bind lighting state int numLights = mDrawContext.numberOfActiveLights(); struct Lighting { struct Light { float position[4]; float diffuse[4]; float ambient[4]; float specular[4]; } lightSource[2]; } lightingData; memset(&lightingData, 0, sizeof(lightingData)); for (int i = 0; i < numLights && i < 1; ++i) { MFloatPointArray positions; MFloatVector direction; float intensity; MColor color; bool hasDirection, hasPosition; mDrawContext.getLightInformation(i, positions, direction, intensity, color, hasDirection, hasPosition); Lighting::Light &light = lightingData.lightSource[i]; if (hasDirection) { light.position[0] = -direction[0]; light.position[1] = -direction[1]; light.position[2] = -direction[2]; for (int j = 0; j < 4; ++j) { light.diffuse[j] = color[j] * intensity; light.ambient[j] = color[j] * intensity; light.specular[j] = color[j] * intensity; } } } if (!g_lightingUB) { glGenBuffers(1, &g_lightingUB); glBindBuffer(GL_UNIFORM_BUFFER, g_lightingUB); glBufferData(GL_UNIFORM_BUFFER, sizeof(lightingData), NULL, GL_STATIC_DRAW); }; glBindBuffer(GL_UNIFORM_BUFFER, g_lightingUB); glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(lightingData), &lightingData); glBindBuffer(GL_UNIFORM_BUFFER, 0); glBindBufferBase(GL_UNIFORM_BUFFER, g_lightingBinding, g_lightingUB); #endif GLint eye = glGetUniformLocation(program, "eyePositionInWorld"); MPoint e = MPoint(0, 0, 0) * mDrawContext.getMatrix(MHWRender::MDrawContext::kWorldViewInverseMtx); glProgramUniform3f(program, eye, static_cast<float>(e.x), static_cast<float>(e.y), static_cast<float>(e.z)); // update other uniforms float color[4] = { 0, 0, 0, 1 }; _diffuse.get(color); glProgramUniform4fv(program, glGetUniformLocation(program, "diffuseColor"), 1, color); _ambient.get(color); glProgramUniform4fv(program, glGetUniformLocation(program, "ambientColor"), 1, color); _specular.get(color); glProgramUniform4fv(program, glGetUniformLocation(program, "specularColor"), 1, color); glProgramUniform1f(program, glGetUniformLocation(program, "fresnelBias"), _fresnelBias); glProgramUniform1f(program, glGetUniformLocation(program, "fresnelScale"), _fresnelScale); glProgramUniform1f(program, glGetUniformLocation(program, "fresnelPower"), _fresnelPower); // Ptex bindings // color ptex if (effectRegistry.getPtexColorValid()) { GLint texData = glGetUniformLocation(program, "textureImage_Data"); glProgramUniform1i(program, texData, CLR_TEXTURE_UNIT + 0); GLint texPacking = glGetUniformLocation(program, "textureImage_Packing"); glProgramUniform1i(program, texPacking, CLR_TEXTURE_UNIT + 1); GLint texPages = glGetUniformLocation(program, "textureImage_Pages"); glProgramUniform1i(program, texPages, CLR_TEXTURE_UNIT + 2); } // displacement ptex if (effectRegistry.getPtexDisplacementValid()) { GLint texData = glGetUniformLocation(program, "textureDisplace_Data"); glProgramUniform1i(program, texData, DISP_TEXTURE_UNIT + 0); GLint texPacking = glGetUniformLocation(program, "textureDisplace_Packing"); glProgramUniform1i(program, texPacking, DISP_TEXTURE_UNIT + 1); GLint texPages = glGetUniformLocation(program, "textureDisplace_Pages"); glProgramUniform1i(program, texPages, DISP_TEXTURE_UNIT + 2); } // occlusion ptex if (effectRegistry.getPtexOcclusionValid()) { GLint texData = glGetUniformLocation(program, "textureOcclusion_Data"); glProgramUniform1i(program, texData, OCC_TEXTURE_UNIT + 0); GLint texPacking = glGetUniformLocation(program, "textureOcclusion_Packing"); glProgramUniform1i(program, texPacking, OCC_TEXTURE_UNIT + 1); GLint texPages = glGetUniformLocation(program, "textureOcclusion_Pages"); glProgramUniform1i(program, texPages, OCC_TEXTURE_UNIT + 2); } // diffuse environment map if (effectRegistry.getDiffuseEnvironmentId() != 0) { GLint difmap = glGetUniformLocation(program, "diffuseEnvironmentMap"); glProgramUniform1i(program, difmap, DIFF_TEXTURE_UNIT); } // specular environment map if (effectRegistry.getSpecularEnvironmentId() != 0) { GLint envmap = glGetUniformLocation(program, "specularEnvironmentMap"); glProgramUniform1i(program, envmap, ENV_TEXTURE_UNIT); } glActiveTexture(GL_TEXTURE0); CHECK_GL_ERROR("bindProgram leave\n"); return program; }
void Shader::uniform1f(GLint location, float f) const { glProgramUniform1f(id_, location, f); }
void Shader::uniform1i(GLint location, int i) const { glProgramUniform1f(id_, location, i); }
void Variable::set(const float& value) { glProgramUniform1f(program, location, value); }
// #### bindProgram // // Do all the work to build and install shader including // set up buffer blocks for uniform variables, set up // default lighting parameters, pass material uniforms // and bind texture buffers used by texture maps and by // OpenSubdiv's built-in shading code. // GLuint OpenSubdivShader::bindProgram(const MHWRender::MDrawContext & mDrawContext, OpenSubdiv::OsdGLDrawContext *osdDrawContext, const OpenSubdiv::OsdDrawContext::PatchArray & patch) { CHECK_GL_ERROR("bindProgram begin\n"); // Primitives are triangles for Loop subdivision, quads otherwise Effect effect = kFill; EffectDesc effectDesc( patch.GetDescriptor(), effect ); // Build shader EffectDrawRegistry::ConfigType * config = g_effectRegistry.GetDrawConfig(effectDesc); // Install shader GLuint program = config->program; glUseProgram(program); // Update and bind transform state struct Transform { float ModelViewMatrix[16]; float ProjectionMatrix[16]; float ModelViewProjectionMatrix[16]; } transformData; setMatrix(mDrawContext.getMatrix(MHWRender::MDrawContext::kWorldViewMtx), transformData.ModelViewMatrix); setMatrix(mDrawContext.getMatrix(MHWRender::MDrawContext::kProjectionMtx), transformData.ProjectionMatrix); setMatrix(mDrawContext.getMatrix(MHWRender::MDrawContext::kWorldViewProjMtx), transformData.ModelViewProjectionMatrix); if (!g_transformUB) { glGenBuffers(1, &g_transformUB); glBindBuffer(GL_UNIFORM_BUFFER, g_transformUB); glBufferData(GL_UNIFORM_BUFFER, sizeof(transformData), NULL, GL_STATIC_DRAW); }; glBindBuffer(GL_UNIFORM_BUFFER, g_transformUB); glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(transformData), &transformData); glBindBuffer(GL_UNIFORM_BUFFER, 0); glBindBufferBase(GL_UNIFORM_BUFFER, g_transformBinding, g_transformUB); // Update and bind tessellation state struct Tessellation { float TessLevel; } tessellationData; tessellationData.TessLevel = static_cast<float>(1 << _tessFactor); if (!g_tessellationUB) { glGenBuffers(1, &g_tessellationUB); glBindBuffer(GL_UNIFORM_BUFFER, g_tessellationUB); glBufferData(GL_UNIFORM_BUFFER, sizeof(tessellationData), NULL, GL_STATIC_DRAW); }; glBindBuffer(GL_UNIFORM_BUFFER, g_tessellationUB); glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(tessellationData), &tessellationData); glBindBuffer(GL_UNIFORM_BUFFER, 0); glBindBufferBase(GL_UNIFORM_BUFFER, g_tessellationBinding, g_tessellationUB); // Update and bind lighting state int numLights = mDrawContext.numberOfActiveLights(); struct Lighting { struct Light { float position[4]; float diffuse[4]; float ambient[4]; float specular[4]; } lightSource[2]; } lightingData; memset(&lightingData, 0, sizeof(lightingData)); for (int i = 0; i < numLights && i < 2; ++i) { MFloatPointArray positions; MFloatVector direction; float intensity; MColor color; bool hasDirection, hasPosition; mDrawContext.getLightInformation(i, positions, direction, intensity, color, hasDirection, hasPosition); MMatrix modelView = mDrawContext.getMatrix(MHWRender::MDrawContext::kWorldViewMtx); direction = MVector(direction) * modelView; Lighting::Light &light = lightingData.lightSource[i]; if (hasDirection) { light.position[0] = -direction[0]; light.position[1] = -direction[1]; light.position[2] = -direction[2]; for (int j = 0; j < 4; ++j) { light.diffuse[j] = color[j] * intensity; light.ambient[j] = color[j] * intensity; light.specular[j] = color[j] * intensity; } } } if (!g_lightingUB) { glGenBuffers(1, &g_lightingUB); glBindBuffer(GL_UNIFORM_BUFFER, g_lightingUB); glBufferData(GL_UNIFORM_BUFFER, sizeof(lightingData), NULL, GL_STATIC_DRAW); }; glBindBuffer(GL_UNIFORM_BUFFER, g_lightingUB); glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(lightingData), &lightingData); glBindBuffer(GL_UNIFORM_BUFFER, 0); glBindBufferBase(GL_UNIFORM_BUFFER, g_lightingBinding, g_lightingUB); // Update other uniforms float color[4] = { 0, 0, 0, 1 }; _diffuse.get(color); glProgramUniform4fv(program, glGetUniformLocation(program, "diffuseColor"), 1, color); _ambient.get(color); glProgramUniform4fv(program, glGetUniformLocation(program, "ambientColor"), 1, color); _specular.get(color); glProgramUniform4fv(program, glGetUniformLocation(program, "specularColor"), 1, color); glProgramUniform1f(program, glGetUniformLocation(program, "shininess"), _shininess); // Bind diffuse map if (g_effectRegistry.getDiffuseId()!=0) { GLint difmap = glGetUniformLocation(program, "diffuseMap"); glProgramUniform1i(program, difmap, DIFF_TEXTURE_UNIT); } // Bind all texture buffers // OpenSubdiv's geometric shading code depends on additional // GL texture buffers. These are managed by the DrawContext // and must be bound for use by the program in addition to // any buffers used by the client/application shading code. if (osdDrawContext->GetVertexTextureBuffer()) { glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_BUFFER, osdDrawContext->GetVertexTextureBuffer()); } if (osdDrawContext->GetVertexValenceTextureBuffer()) { glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_BUFFER, osdDrawContext->GetVertexValenceTextureBuffer()); } if (osdDrawContext->GetQuadOffsetsTextureBuffer()) { glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_BUFFER, osdDrawContext->GetQuadOffsetsTextureBuffer()); } if (osdDrawContext->GetPatchParamTextureBuffer()) { glActiveTexture(GL_TEXTURE3); glBindTexture(GL_TEXTURE_BUFFER, osdDrawContext->GetPatchParamTextureBuffer()); } if (osdDrawContext->GetFvarDataTextureBuffer()) { glActiveTexture(GL_TEXTURE4); glBindTexture(GL_TEXTURE_BUFFER, osdDrawContext->GetFvarDataTextureBuffer() ); } glActiveTexture(GL_TEXTURE0); CHECK_GL_ERROR("bindProgram leave\n"); return program; }
void GLSeparableProgram::setUniformFloat( GLint uniformLocation, float x ) { assert( isValid() ); glProgramUniform1f( id(), uniformLocation, x ); }
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_GL41_nglProgramUniform1f(JNIEnv *env, jclass clazz, jint program, jint location, jfloat v0, jlong function_pointer) { glProgramUniform1fPROC glProgramUniform1f = (glProgramUniform1fPROC)((intptr_t)function_pointer); glProgramUniform1f(program, location, v0); }
void ae3d::Shader::SetFloat( const char* name, float value ) { glProgramUniform1f( id, uniformLocations[ name ].i, value ); }