GlShaderBlinnShadowMap() { std::string infoLog; initVertFragShadersFromDirectory("shader_blinn_shadow_map"); attachFragShader(SHADER_GLSL_DEFAULT_DIR"shader_blinn/fragment_shader_skeleton.glsl"); // link programs if (!link()) { std::string infoLog; getInfoLog(infoLog); std::cerr << "info Log: during linking: " << infoLog << std::endl; return; } setupUniform(texture0_enabled, "texture0_enabled"); setupUniform(shadow_map_matrix_uniform, "shadow_map_matrix"); use(); setUniform1i("texture0", 0); setUniform1i("texture_shadow_map", 1); disable(); initBlinnSkeleton(*this); }
void StaticEntity::DrawFirstPass() { GLuint shad; glm::vec3 minBBoxPoint; glm::vec3 distVec; if(visible) { glPushMatrix(); glMultMatrixf(&matrix[0][0]); glScalef(scale,scale,scale); for(unsigned int i=0; i<meshObj->mesh.size(); i++) { if(meshObj->mesh.at(i)) { if(meshObj->material.at(i)) { //minBBoxPoint = ClosestMeshBBoxPoint(i); //distVec = minBBoxPoint - Camera::getActiveCamera()->pos; //std::cout<<"dist "<<glm::dot(distVec,distVec)<<std::endl; //shad = SceneHandler::shaderLib.GetShaderFromDistance(meshObj->material.at(i)->type,glm::dot(distVec,distVec)); shad=SceneHandler::shaderLib.GetShaderFromType(meshObj->material.at(i)->type); glActiveTexture( GL_TEXTURE0 ); glBindTexture(GL_TEXTURE_2D, meshObj->material.at(i)->diffuseMap); setUniform1i(shad,0,"diffuseMap"); glActiveTexture( GL_TEXTURE1 ); glBindTexture(GL_TEXTURE_2D, meshObj->material.at(i)->normalMap); setUniform1i(shad,1,"normalMap"); glActiveTexture( GL_TEXTURE2 ); glBindTexture(GL_TEXTURE_2D, meshObj->material.at(i)->specularMap); setUniform1i(shad,2,"specularMap"); setAttributeTangent(shad, meshObj->mesh.at(i)->tangent, "tangent"); setUniform2f(shad,SceneHandler::near,SceneHandler::far,"cameraRange"); Camera *cam = Camera::getActiveCamera(); setUniform3f(shad,cam->pos.x,cam->pos.y,cam->pos.z,"cameraPos"); setUniform3f(shad,cam->dir.x,cam->dir.y,cam->dir.z,"cameraDir"); glMaterialfv(GL_FRONT, GL_DIFFUSE, meshObj->material.at(i)->diffuse); glMaterialfv(GL_FRONT, GL_AMBIENT, meshObj->material.at(i)->ambient); glMaterialfv(GL_FRONT, GL_SPECULAR, meshObj->material.at(i)->specular); glMaterialfv(GL_FRONT, GL_SHININESS, meshObj->material.at(i)->shininess); glUseProgram( shad ); meshObj->mesh.at(i)->draw(); glUseProgram( 0 ); } } } glPopMatrix(); } }
/* Redraws window contents */ void glutWidget::render() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //clears color and depth bits of framebuffer if(guide) { glBegin(GL_LINE_LOOP); glColor3f(1.0, 0.0, 0.0); glVertex2f((0.2+cx)/(scale*0.5), (-0.1+cy)/(scale*0.5)); glVertex2f((0.45+cx)/(scale*0.5), (-0.1+cy)/(scale*0.5)); glVertex2f((0.325+cx)/(scale*0.5), (0.15+cy)/(scale*0.5)); glEnd(); glBegin(GL_LINE_LOOP); glColor3f(1.0, 0.0, 0.0); glVertex2f((-0.2+cx)/(scale*0.5), (0.8+cy)/(scale*0.5)); glVertex2f((0.0+cx)/(scale*0.5), (0.8+cy)/(scale*0.5)); glVertex2f((0.0+cx)/(scale*0.5), (1.0+cy)/(scale*0.5)); glVertex2f((-0.2+cx)/(scale*0.5), (1.0+cy)/(scale*0.5)); glEnd(); //Circle glBegin(GL_LINE_LOOP); glColor3f(1.0, 0.0, 0.0); for(int i = 0; i < 360; i++) { glVertex2f((cos((float) i * (3.14159265/180.0)) / 6 + cx + (-1.35)) / (scale*0.5), (sin((float) i * (3.14159265/180.0)) / 6 + cy) / (scale*0.5)); } glEnd(); } glUseProgram(m_program); //Enables fragment shader //Pass parameters from main program to fragment shader setUniform2f(m_program, "center", glutWidget::cx, glutWidget::cy); setUniform1f(m_program, "scale", glutWidget::scale); setUniform1i(m_program, "itr", itr); setUniform1i(m_program, "color", color); glBegin(GL_QUADS); glTexCoord2f(0, 0); glVertex2f(-1, -1); glTexCoord2f(1, 0); glVertex2f(1, -1); glTexCoord2f(1, 1); glVertex2f(1, 1); glTexCoord2f(0, 1); glVertex2f(-1, 1); glEnd(); glUseProgram(0); //Disables fragment shader glutSwapBuffers(); //swaps front and back buffer for double buffering }
bool GLProgram::link() { glLinkProgram(m_program); glValidateProgram(m_program); GLint status; glGetProgramiv(m_program, GL_LINK_STATUS, &status); if (status == GL_FALSE) { FZLOGERROR("GLProgram: Error linking program: %i.", m_program); FZLOGERROR("%s", getProgramLog().c_str()); fzGLDeleteProgram( m_program ); m_program = 0; return false; } char uniformName[256]; GLint nuUniforms = 0; glGetProgramiv(m_program, GL_ACTIVE_UNIFORMS, &nuUniforms); for(fzInt index = 0; index < nuUniforms; ++index) { glGetActiveUniform(m_program, index, 256, NULL, NULL, NULL, uniformName); m_uniforms.insert(uniformsPair(fzHash(uniformName), glGetUniformLocation(m_program, uniformName))); } CHECK_GL_ERROR_DEBUG(); setUniform1i("u_texture", 0); return true; }
//-------------------------------------------------------------- void ofShader::setUniformTexture(const char* name, int textureTarget, GLint textureID, int textureLocation){ if(bLoaded) { glActiveTexture(GL_TEXTURE0 + textureLocation); glEnable(textureTarget); glBindTexture(textureTarget, textureID); glDisable(textureTarget); setUniform1i(name, textureLocation); glActiveTexture(GL_TEXTURE0); } }
void Shader::setupCommonUniforms() { std::set<std::string> commonUniformNames { "u_eyePositionW", "u_elapsedTime", "u_techniqueMax", "u_M_Mat", "u_MV_Mat", "u_MVP_Mat", "u_N_Mat", "s_colorTex", "s_normalHeightTex", "s_cubeTex", "s_lightMapTex", "s_tex1", "s_tex2", "s_tex3", "u_technique", "u_scale", "u_bias", "enabledLights", "u_enableShadows", "u_numLightSources", "u_shadowMapPixelOffsetX", "u_shadowMapPixelOffsetY", "u_shadowMapArray", }; char tmp[256]; const char* lightAttributes[7] = { "type", "pos", "ambient", "diffuse", "specular", "attenuation", "shadowMatrix" }; for (uint lightId = 0; lightId < 6; lightId++) { for (uint attribId = 0; attribId < 7; attribId++) { sprintf(tmp, "u_lights[%d].%s", lightId, lightAttributes[attribId]); commonUniformNames.insert(tmp); } } setupUniforms(commonUniformNames); // common uniform parameters setUniform1i("s_colorTex", 0); setUniform1i("s_normalHeightTex", 1); setUniform1i("s_cubeTex", 2); //additional textures setUniform1i("s_tex1", 3); setUniform1i("s_tex2", 4); setUniform1i("s_tex3", 5); setUniform1i("u_technique", 0); setUniform1f("u_scale", 0.04f); setUniform1f("u_bias", -0.02f); }
void ofxShader::setUniformTexture(const char* name, ofTexture& tex, int textureLocation) { if(bLoaded) { ofTextureData texData = tex.getTextureData(); glActiveTexture(GL_TEXTURE0 + textureLocation); glEnable(texData.textureTarget); glBindTexture(texData.textureTarget, texData.textureID); glDisable(texData.textureTarget); setUniform1i(name, textureLocation); glActiveTexture(GL_TEXTURE0); } }
void Shader::updateCommonUniforms(const Camera* camera, const Matrix& modelMatrix) const { //// update fog attributes //float m_fogStart = 20.0f, m_fogLength = 20.0f; //vec3 m_fogColor(0.5f); //if ((uniformLoc = glGetUniformLocation(m_shaderProg, "u_fogStart")) != -1) // glUniform1f(uniformLoc, m_fogStart); //if ((uniformLoc = glGetUniformLocation(m_shaderProg, "u_fogLength")) != -1) // glUniform1f(uniformLoc, m_fogLength); //if ((uniformLoc = glGetUniformLocation(m_shaderProg, "u_fogColor")) != -1) // glUniform3f(uniformLoc, m_fogColor.x, m_fogColor.y, m_fogColor.z); setUniform3f("u_eyePositionW", camera->getPos()); setUniform1i("u_elapsedTime", glutGet(GLUT_ELAPSED_TIME)); setUniform1i("u_techniqueMax", Shader::ms_maxShaderQuality); setUniformMatrix("u_M_Mat", modelMatrix.getArray()); // ModelView Matrix modelViewMatrix; Matrix modelViewProjectionMatrix; if (m_uniforms.at("u_MV_Mat") != -1) { modelViewMatrix = camera->getViewMatrix() * modelMatrix; setUniformMatrix("u_MV_Mat", modelViewMatrix.getArray()); modelViewProjectionMatrix = camera->getProjMatrix() * modelViewMatrix; } else { modelViewProjectionMatrix = camera->getProjMatrix() * camera->getViewMatrix() * modelMatrix; } setUniformMatrix("u_MVP_Mat", modelViewProjectionMatrix.getArray()); // Normal Matrix normalMatrix = modelMatrix; normalMatrix.setTranslation(vec3(0.0f)); setUniformMatrix("u_N_Mat", normalMatrix.getArray()); }
void StaticEntity::DrawShadow() { if(visible) { Light *nearestLight=SceneHandler::FindNearestLight(matrix[3].x, matrix[3].y, matrix[3].z); if(nearestLight) nearestLight->assignTo(0); glPushMatrix(); glMultMatrixf(&matrix[0][0]); glScalef(scale,scale,scale); glMatrixMode(GL_TEXTURE); glActiveTexture(GL_TEXTURE3); glPushMatrix(); glMultMatrixf(&matrix[0][0]); glScalef(scale,scale,scale); for(unsigned int i=0; i<meshObj->mesh.size(); i++) { if(meshObj->material.at(i) != NULL) { glMaterialfv(GL_FRONT, GL_DIFFUSE, meshObj->material.at(i)->diffuse); glMaterialfv(GL_FRONT, GL_AMBIENT, meshObj->material.at(i)->ambient); glMaterialfv(GL_FRONT, GL_SPECULAR, meshObj->material.at(i)->specular); glMaterialfv(GL_FRONT, GL_SHININESS, meshObj->material.at(i)->shininess); glActiveTexture( GL_TEXTURE3 ); glBindTexture(GL_TEXTURE_2D, nearestLight->depthTextureId); setUniform1i(SceneHandler::shadowShader,3,"ShadowMap"); setUniform1f(SceneHandler::shadowShader, nearestLight->getRadius(),"lightRadius"); glUseProgram( SceneHandler::shadowShader ); } if(meshObj->mesh.at(i)) { meshObj->mesh.at(i)->draw(); } } glUseProgram( 0 ); glPopMatrix(); glMatrixMode(GL_MODELVIEW); glPopMatrix(); glBindTexture(GL_TEXTURE_2D, 0); } }
//-------------------------------------------------------------- void ofShader::setUniformTexture(const string & name, int textureTarget, GLint textureID, int textureLocation) const{ if(bLoaded) { glActiveTexture(GL_TEXTURE0 + textureLocation); if (!ofIsGLProgrammableRenderer()){ glEnable(textureTarget); glBindTexture(textureTarget, textureID); glDisable(textureTarget); } else { glBindTexture(textureTarget, textureID); } setUniform1i(name, textureLocation); glActiveTexture(GL_TEXTURE0); } }
//-------------------------------------------------------------- void ofShader::setUniforms(const ofParameterGroup & parameters) const{ for(int i=0;i<parameters.size();i++){ if(parameters[i].type()==typeid(ofParameter<int>).name()){ setUniform1i(parameters[i].getEscapedName(),parameters[i].cast<int>()); }else if(parameters[i].type()==typeid(ofParameter<float>).name()){ setUniform1f(parameters[i].getEscapedName(),parameters[i].cast<float>()); }else if(parameters[i].type()==typeid(ofParameter<ofVec2f>).name()){ setUniform2f(parameters[i].getEscapedName(),parameters[i].cast<ofVec2f>()); }else if(parameters[i].type()==typeid(ofParameter<ofVec3f>).name()){ setUniform3f(parameters[i].getEscapedName(),parameters[i].cast<ofVec3f>()); }else if(parameters[i].type()==typeid(ofParameter<ofVec4f>).name()){ setUniform4f(parameters[i].getEscapedName(),parameters[i].cast<ofVec4f>()); }else if(parameters[i].type()==typeid(ofParameterGroup).name()){ setUniforms((ofParameterGroup&)parameters[i]); } } }
int main(int argc, char **argv) { glutInitWindowSize(800, 600); glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE); glutCreateWindow("opengl"); glutDisplayFunc(drawGLScene); glutIdleFunc(drawGLScene); glutKeyboardFunc(keyPressed); initColorMap(); glBindTexture(GL_TEXTURE_1D, 1); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexImage1D(GL_TEXTURE_1D, 0, 4, iterLimit, 0, GL_BGRA, GL_UNSIGNED_BYTE, colorMap); glEnable(GL_TEXTURE_1D); if(!(shaderProgram = setupShader("julia.glsl"))) { return -1; } setUniform1i(shaderProgram, "iterLimit", iterLimit); setUniform1f(shaderProgram, "checkDistanceSquare", checkDistanceSquare); if(!(shaderSky = setupShader("sky.glsl"))) { return -1; } glutFullScreen(); glutMainLoop(); return 0; }
void PerlinWavesShader::setUniformHeightMap(int in1) const{ setUniform1i("heightMap", in1); }
void PerlinWavesShader::setUniformDiffuseMap(int in1) const{ setUniform1i("diffuseMap", in1); }
void ParallaxShader::setUniformDiffuseTex(int in1) const{ setUniform1i("diffuseTex", in1); }
void StaticEntity::Draw() { if(visible) { Light *nearestLight=SceneHandler::FindNearestLight(matrix[3].x, matrix[3].y, matrix[3].z); if(nearestLight) nearestLight->assignTo(0); glPushMatrix(); glMultMatrixf(&matrix[0][0]); glScalef(scale,scale,scale); glMatrixMode(GL_TEXTURE); glActiveTexture(GL_TEXTURE3); glPushMatrix(); glMultMatrixf(&matrix[0][0]); glScalef(scale,scale,scale); //printf("pos: %f %f %f \n",m_matrix.m_posit.m_x, m_matrix.m_posit.m_y, m_matrix.m_posit.m_z); for(unsigned int i=0; i<meshObj->mesh.size(); i++) { if(meshObj->material.at(i) != NULL) { glActiveTexture( GL_TEXTURE0 ); glBindTexture(GL_TEXTURE_2D, meshObj->material.at(i)->diffuseMap); setUniform1i(meshObj->material.at(i)->shader,0,"diffuseMap"); glActiveTexture( GL_TEXTURE1 ); glBindTexture(GL_TEXTURE_2D, meshObj->material.at(i)->normalMap); setUniform1i(meshObj->material.at(i)->shader,1,"normalMap"); glActiveTexture( GL_TEXTURE2 ); glBindTexture(GL_TEXTURE_2D, meshObj->material.at(i)->specularMap); setUniform1i(meshObj->material.at(i)->shader,2,"specularMap"); setAttributeTangent(meshObj->material.at(i)->shader, meshObj->mesh.at(i)->tangent, "tangent"); glMaterialfv(GL_FRONT, GL_DIFFUSE, meshObj->material.at(i)->diffuse); glMaterialfv(GL_FRONT, GL_AMBIENT, meshObj->material.at(i)->ambient); glMaterialfv(GL_FRONT, GL_SPECULAR, meshObj->material.at(i)->specular); glMaterialfv(GL_FRONT, GL_SHININESS, meshObj->material.at(i)->shininess); setUniform1f(meshObj->material.at(i)->shader,nearestLight->getRadius(),"lightRadius"); setUniform2f(meshObj->material.at(i)->shader,(GLfloat)SceneHandler::width,(GLfloat)SceneHandler::height,"bufferSize"); setUniform1i(meshObj->material.at(i)->shader,SceneHandler::width,"screenWidth"); setUniform1i(meshObj->material.at(i)->shader,SceneHandler::height,"screenHeight"); glUseProgram( meshObj->material.at(i)->shader ); } if(meshObj->mesh.at(i)) { meshObj->mesh.at(i)->draw(); } } glUseProgram( 0 ); glPopMatrix(); glMatrixMode(GL_MODELVIEW); glPopMatrix(); glBindTexture(GL_TEXTURE_2D, 0); } }
void ParallaxShader::setUniformHeightMap(int in1) const{ setUniform1i("heightMap", in1); }
void BlurFilterShader::setUniformTextureImg(int in1) const{ setUniform1i("textureImg", in1); }
void setUniformI(const int& value) { setUniform1i(1, &value); }
void Shader::beginPass(const uint pass) const { setUniform1i("pass", pass); }
int ShaderProgram::setUniform(const std::string &uniformName, int value) { addUniform( uniformName ); setUniform1i( uniformName, value); }
void LuminanceFilterShader::setUniformTextureImg(int in1) const{ setUniform1i("textureImg", in1); }
void begin() { // old-BATB didn't like to be initialized more than once :( // hence, ignore re-init for specified parts, and continue with previous state static bool empty = true; // clear exit, making possible to start over again //do_exit = false; // copied from old::main: //MainWindow::winPosX = 100; //MainWindow::winPosY = 100; //MainWindow::winWidth = 640; //MainWindow::winHeight = 480; // init glut //glutInitWindowPosition(MainWindow::winPosX, MainWindow::winPosY); //glutInitWindowSize (MainWindow::winWidth, MainWindow::winHeight); //glutInit (&argc, argv); //glutInitDisplayMode (GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); //MainWindow::main_window = glutCreateWindow("Beat About the Bush"); MainWindow::enableDisplayFunc(); // implemented as GLFW: GLFWwindow* win = glfwGetCurrentContext(); glfwSetKeyCallback( win, MainWindow::glfwKey ); //glutKeyboardFunc (MainWindow::keyDownFn); //glutSpecialFunc (MainWindow::specialDownFn); ////glutKeyboardUpFunc (MainWindow::keyUpFn); ////glutSpecialUpFunc (MainWindow::specialUpFn); glfwSetCursorPosCallback( win, MainWindow::glfwCursorPos ); //glutMouseFunc (MainWindow::mousefn); glfwSetMouseButtonCallback( win, MainWindow::glfwMouseButton); //glutMotionFunc (MainWindow::motionfn); glfwSetWindowSizeCallback( win, MainWindow::glfwWindowSize ); //glutReshapeFunc (MainWindow::reshapefn); glfwSetWindowFocusCallback( win, MainWindow::glfwWindowFocus ); //glutVisibilityFunc (MainWindow::visibility); if ( empty ) { // init plib, with no GLUT-binding! puInitOLD(); puSetDefaultStyle ( PUSTYLE_SMALL_SHADED ); puSetDefaultColourScheme ( 0.3f, 0.4f, 0.6f, 1.0f); // Initialize the "OpenGL Extension Wrangler" library //glewInit(); } mainWindow.initLights(); if ( empty ) { mainWindow.init(); printVersions(); // Make sure that OpenGL 2.0 is supported by the driver int gl_major, gl_minor; getGlVersion(&gl_major, &gl_minor); printf("GL_VERSION major=%d minor=%d\n", gl_major, gl_minor); if (gl_major < 2) { printf("GL_VERSION major=%d minor=%d\n", gl_major, gl_minor); printf("Support for OpenGL 2.0 is required for this demo...exiting\n"); //exit(1); old::exit(1); return; } // init shaders GLchar *dayVSSource, *dayFSSource; readShaderSource( old::file("shader/day").c_str(), &dayVSSource, &dayFSSource); dayShader = installShaders(dayVSSource, dayFSSource); float forestGreen[] = {34.0/255, 139.0/255, 34.0/255}; //float auburn[] = {113.0/255, 47.0/255, 38.0/255}; float grey[] = {0.5, 0.5, 0.5}; //float sepia[] = {112.0/255, 66.0/255, 20.0/255}; setUniform3f(dayShader, "GrassColor", forestGreen[0], forestGreen[1], forestGreen[2]); setUniform3f(dayShader, "RockColor", grey[0], grey[1], grey[2]); //setUniform3f(dayShader, "DirtColor", sepia[0], sepia[1], sepia[2]); //setUniform4f(dayShader, "LightPos", 0.0, 0.0, 100.0, 1.0); setUniform1f(dayShader, "Scale", 1.0); setUniform1f(dayShader, "TerrainHigh", 0.1); setUniform1f(dayShader, "TerrainLow", 0.1); setUniform1i(dayShader, "Trees", 0); // sampler setUniform1i(dayShader, "AltGrad", 1); // sampler setUniform1i(dayShader, "Noise", 2); // sampler GLchar *nightVSSource, *nightFSSource; readShaderSource( old::file("shader/night3").c_str(), &nightVSSource, &nightFSSource); nightShader = installShaders(nightVSSource, nightFSSource); setUniform3f(nightShader, "BrickColor", 1.0, 0.3, 0.2); setUniform3f(nightShader, "MortarColor", 0.85, 0.86, 0.84); setUniform2f(nightShader, "BrickSize", 0.30, 0.15); setUniform2f(nightShader, "BrickPct", 0.90, 0.85); setUniform1i(nightShader, "numEnabledLights", 2); setUniform3f(nightShader, "GrassColor", forestGreen[0], forestGreen[1], forestGreen[2]); setUniform3f(nightShader, "RockColor", grey[0], grey[1], grey[2]); setUniform1f(nightShader, "Scale", 1.0); setUniform1f(nightShader, "TerrainHigh", 0.1); setUniform1f(nightShader, "TerrainLow", 0.1); setUniform1i(nightShader, "Trees", 0); // sampler setUniform1i(nightShader, "AltGrad", 1); // sampler setUniform1i(nightShader, "Noise", 2); // sampler GLchar *nightTreeVS, *nightTreeFS; readShaderSource( old::file("shader/nightTree").c_str(), &nightTreeVS, &nightTreeFS); nightTrees = installShaders(nightTreeVS, nightTreeFS); setUniform1i(nightTrees, "Trees", 0); // sampler // BUGFIX: free( dayVSSource ); free( dayFSSource ); free( nightVSSource ); free( nightFSSource ); free( nightTreeVS ); free( nightTreeFS ); } // enter main loop //if (dayShader && nightShader) // glutMainLoop(); //return 0; empty = false; }
void ParallaxShader::setUniformNormalMap(int in1) const{ setUniform1i("normalMap", in1); }