void initShader(void) { int maxImageUnits; loadETC1FragProg(); const GLsizei vertexShaderLines = sizeof(vertexShaderSource) / sizeof(char*); GLuint vertexShader = compileShaderSource(GL_VERTEX_SHADER, vertexShaderLines, vertexShaderSource); const GLsizei fragmentShaderLines = sizeof(fragmentShaderSource) / sizeof(char*); GLuint fragmentShader = compileShaderSource( GL_FRAGMENT_SHADER, fragmentShaderLines, fragmentShaderSource); GLuint program = createProgram(vertexShader, fragmentShader); checkError("createProgram"); u_TextureRender = glGetUniformLocation(program, "myt"); checkError("GetRenderUniform"); a_Position = glGetAttribLocation(program, "a_Position"); glEnableVertexAttribArray(a_Position); checkError("initRender"); computeProgramETC1 = glCreateShaderProgramv(GL_COMPUTE_SHADER, computeShaderSourceETC1LineCount, (const char**)computeShaderSourceETC1); if (!checkProgram(computeProgramETC1, "computeProgramETC1")) { exit(1); } renderProgram = program; u_TextureComputeETC1 = glGetUniformLocation(computeProgramETC1, "u_Texture"); u_OutComputeETC1 = glGetUniformLocation(computeProgramETC1, "u_Output"); printf( "u_TextureRender: %u, u_TextureComputeETC1: %u, u_OutComputeETC1: %u.\n", u_TextureRender, u_TextureComputeETC1, u_OutComputeETC1); computeProgramS3TC = glCreateShaderProgramv(GL_COMPUTE_SHADER, computeShaderSourceS3TCLineCount, (const char**)computeShaderSourceS3TC); if (!checkProgram(computeProgramS3TC, "computeProgramS3TC")) { exit(1); } u_TextureComputeS3TC = glGetUniformLocation(computeProgramS3TC, "u_Texture"); printf("u_TextureComputeS3TC: %u.\n", u_TextureComputeS3TC); glGetIntegerv(GL_MAX_IMAGE_UNITS, &maxImageUnits); printf("maxImageUnits: %d.\n", maxImageUnits); checkError("initCompute"); }
bool createProgram(GLuint & program, GLuint vertexShader, GLuint fragShader, GLuint geometryShader, GLenum inputType, GLenum outputType, int vertOut) { program = glCreateProgram(); if(vertexShader) { glAttachShader(program,vertexShader); } if(fragShader) { glAttachShader(program,fragShader); } if(geometryShader) { glAttachShader(program,geometryShader); glProgramParameteriEXT(program, GL_GEOMETRY_INPUT_TYPE_EXT, inputType); glProgramParameteriEXT(program, GL_GEOMETRY_OUTPUT_TYPE_EXT, outputType); glProgramParameteriEXT(program,GL_GEOMETRY_VERTICES_OUT_EXT,vertOut); } glLinkProgram(program); return checkProgram(program); }
static void linkAndCheck(GLuint program) { GLint programBinaryLength; glLinkProgram(program); if (!checkProgram(program, "render")) { exit(1); } programBinaryLength = 0; glGetProgramiv(program, GL_PROGRAM_BINARY_LENGTH, &programBinaryLength); if (programBinaryLength > 0) { void* buf; GLenum format; GLsizei length; FILE* file; printf("programBinaryLength: %d.\n", programBinaryLength); buf = malloc(programBinaryLength); glGetProgramBinary(program, programBinaryLength, &length, &format, buf); printf("programBinaryFormat: %x.\n", format); file = fopen("p_binary.dat", "wb"); if (file) { printf("write binary to p_binary.dat, length: %d.\n", length); fwrite(buf, 1, length, file); fclose(file); } } }
bool initShaders(const char* vertShaderPath, const char* fragShaderPath) { // create a program before linking shaders program = glCreateProgram(); glUseProgram(program); // compile shader sources GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER); const char* vertexShaderSource = readShader(vertShaderPath); if (vertexShaderSource == NULL) return false; GLint vertexShaderLength = strlen(vertexShaderSource); glShaderSource(vertexShader, 1, &vertexShaderSource, &vertexShaderLength); glCompileShader(vertexShader); if (!checkShader(vertexShader, "vertexShader")){ printf("Unable to compile vertex shader\n"); return false; } GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); const char* fragmentShaderSource = readShader(fragShaderPath); if (fragmentShaderSource == NULL) return false; GLint fragmentShaderLength = strlen(fragmentShaderSource); glShaderSource(fragmentShader, 1, &fragmentShaderSource, &fragmentShaderLength); glCompileShader(fragmentShader); if (!checkShader(fragmentShader, "fragmentShader")){ printf("Unable to compile fragment shader\n"); return false; } // attach vertex/fragments shaders and link program glAttachShader(program, vertexShader); glAttachShader(program, fragmentShader); glLinkProgram(program); if (!checkProgram(program, "program")){ printf("Unable to link program\n"); return false; } // deallocate string free((void*)vertexShaderSource); free((void*)fragmentShaderSource); return true; }
Program::Program(int files, char** fileNames, char *options) { program = -1; vertexShader = -1; fragmentShader = -1; const char **contents = (const char**) malloc((files + 2) * sizeof(char*)); int i; for (i = 0; i < files; ++i) { contents[i + 2] = readFile(fileNames[i]); } if (program == -1) { program = glCreateProgram(); vertexShader = glCreateShader(GL_VERTEX_SHADER); fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); glAttachShader(program, vertexShader); glAttachShader(program, fragmentShader); assert(glGetError() == 0); } contents[0] = "#define _VERTEXSHADER_\n"; contents[1] = options == NULL ? "" : options; glShaderSource(vertexShader, files + 2, contents, NULL); glCompileShader(vertexShader); checkShader(vertexShader); assert(glGetError() == 0); contents[0] = "#define _FRAGMENTSHADER_\n"; glShaderSource(fragmentShader, files + 2, contents, NULL); glCompileShader(fragmentShader); checkShader(fragmentShader); assert(glGetError() == 0); for (i = 0; i < files; ++i) { free((void*) contents[i + 2]); } glBindAttribLocation(program, 1, "normal"); glBindAttribLocation(program, 2, "color"); glLinkProgram(program); GLint logLength; glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logLength); if (logLength > 0) { char *log = new char[logLength]; glGetProgramInfoLog(program, logLength, &logLength, log); printf("%s", log); } if (checkProgram(program)) { assert(glGetError() == 0); } else { printShaderLog(vertexShader); printShaderLog(fragmentShader); // exit(-1); } }
struct node *parse(char *program) { struct node *head = NULL; checkProgram(program); // checking syntax of the program head = tokenize(program); return read_from_list(&head); }
void Shader::bind() { string err = checkProgram(); if (err == "") { glUseProgram(*program); setUniforms(); } else LOG << "OpenGL Shader " << err << "\n"; }
void checkAll() { map<string,vector<ParticipantTask> >::iterator i; for (i=participants.begin(); i!=participants.end(); i++) { addLog("Name "+i->first+" path "+i->second[0].path); //int t=i->second[0].taskNumber; for (unsigned int j=0; j<i->second.size(); j++) checkProgram(i->second[j], i->first); }; }
unsigned int ShaderManager::createProgram(const std::string &vertexShaderFilename, const std::string &geometryShaderFilename, const std::string &fragmentShaderFilename, const std::map<std::string, std::string> &tokens) { unsigned int programID = glCreateProgram(); programs.push_back(programID); //vertex shader const std::string &vertexShaderFileSource = readEntireFile(shadersParentDirectory + shadersDirectoryName + vertexShaderFilename); const std::string &vertexShaderSourceStep1 = tokenReplacerShader.replaceTokens(vertexShaderFileSource, tokens); const std::string &vertexShaderSource = loopUnrollerShader.unrollLoops(vertexShaderSourceStep1); const char *vertexShaderSourceChar = vertexShaderSource.c_str(); unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertexShader, 1, &vertexShaderSourceChar, nullptr); glCompileShader(vertexShader); checkShader(vertexShader, vertexShaderFilename); glAttachShader(programID, vertexShader); //geometry shader if(!geometryShaderFilename.empty()) { const std::string &geometryShaderFileSource = readEntireFile(shadersParentDirectory + shadersDirectoryName + geometryShaderFilename); const std::string &geometryShaderSourceStep1 = tokenReplacerShader.replaceTokens(geometryShaderFileSource, tokens); const std::string &geometryShaderSource = loopUnrollerShader.unrollLoops(geometryShaderSourceStep1); const char *geometryShaderSourceChar = geometryShaderSource.c_str(); unsigned int geometryShader = glCreateShader(GL_GEOMETRY_SHADER); glShaderSource(geometryShader, 1, &geometryShaderSourceChar, nullptr); glCompileShader(geometryShader); checkShader(geometryShader, geometryShaderFilename); glAttachShader(programID, geometryShader); } //fragment shader const std::string &fragmentShaderFileSource = readEntireFile(shadersParentDirectory + shadersDirectoryName + fragmentShaderFilename); const std::string &fragmentShaderSourceStep1 = tokenReplacerShader.replaceTokens(fragmentShaderFileSource, tokens); const std::string &fragmentShaderSource = loopUnrollerShader.unrollLoops(fragmentShaderSourceStep1); const char *fragmentShaderSourceChar = fragmentShaderSource.c_str(); unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragmentShader, 1, &fragmentShaderSourceChar, nullptr); glCompileShader(fragmentShader); checkShader(fragmentShader, fragmentShaderFilename); glAttachShader(programID, fragmentShader); //link glLinkProgram(programID); checkProgram(programID, vertexShaderFilename + ", " + geometryShaderFilename + ", " + fragmentShaderFilename); return programID; }
void Shader::loadShaders(std::string s) { GLuint vs = 0, fs = 0, gs = 0, tes = 0, tcs = 0; try { vs = loadShader(GL_VERTEX_SHADER, s); } catch_err(Error::ShaderNoFile, Error::ShaderNoVertex) try { fs = loadShader(GL_FRAGMENT_SHADER, s); } catch_err(Error::ShaderNoFile, Error::ShaderNoFragment) try { gs = loadShader(GL_GEOMETRY_SHADER, s); } catch_ignore(Error::ShaderNoFile) try { tes = loadShader(GL_TESS_EVALUATION_SHADER, s); } catch_ignore(Error::ShaderNoFile) try { tcs = loadShader(GL_TESS_CONTROL_SHADER, s); } catch_ignore(Error::ShaderNoFile) glLinkProgram(program); if(vs != 0) glDeleteShader(vs); if(fs != 0) glDeleteShader(fs); if(gs != 0) glDeleteShader(gs); if(tes != 0) glDeleteShader(tes); if(tcs != 0) glDeleteShader(tcs); if(!checkProgram(program, GL_LINK_STATUS)) throw Error::ShaderLinkerError; GLuint transBlockIndex = glGetUniformBlockIndex(program, TRANSFORMATION_BLOCK_NAME.c_str()); if(transBlockIndex != GL_INVALID_INDEX) glUniformBlockBinding(program, transBlockIndex, TRANSFORMATION_BLOCK_BINDING_POINT); GLuint matBlockIndex = glGetUniformBlockIndex(program, MATERIAL_BLOCK_NAME.c_str()); if(matBlockIndex != GL_INVALID_INDEX) glUniformBlockBinding(program, matBlockIndex, MATERIAL_BLOCK_BINDING_POINT); GLint texUnitLoc = glGetUniformLocation(program, "diffuseTex"); if(texUnitLoc != -1) glProgramUniform1i(program, texUnitLoc , 0); texUnitLoc = glGetUniformLocation(program, "normalTex"); if(texUnitLoc != -1) glProgramUniform1i(program, texUnitLoc , 1); texUnitLoc = glGetUniformLocation(program, "specularTex"); if(texUnitLoc != -1) glProgramUniform1i(program, texUnitLoc , 2); texUnitLoc = glGetUniformLocation(program, "environmentTex"); if(texUnitLoc != -1) glProgramUniform1i(program, texUnitLoc , 3); texUnitLoc = glGetUniformLocation(program, "shadowTex"); if(texUnitLoc != -1) glProgramUniform1i(program, texUnitLoc , 4); }
/** * @brief ParticleSystem activate method */ bool ParticleSystem::Activate(void) { // -------------------------------------------------- // Get the path name of the plugin folder, e.g. // "/path/to/oglcore/Plugins/PCVC/ParticleSystem" // -------------------------------------------------- std::string pathName = this->GetCurrentPluginPath(); // -------------------------------------------------- // Initialize manipulator for camera view // -------------------------------------------------- int camHandle = this->AddManipulator("View", &this->viewMX, Manipulator::MANIPULATOR_ORBIT_VIEW_3D); this->SelectCurrentManipulator(camHandle); this->SetManipulatorRotation(camHandle, glm::vec3(0, 1, 0), 180.0f); this->SetManipulatorRotation(camHandle, glm::vec3(1, 0, 0), 180.0f); this->SetManipulatorDolly(camHandle, -1.5f); modelMX = glm::scale(glm::translate(glm::mat4(), glm::vec3(0.0f, -0.25f, 0.0f)), glm::vec3(0.5f, 0.5f, 0.5f)); // -------------------------------------------------- // Initialize API variables here // -------------------------------------------------- fovY.Set(this, "FoVy"); fovY.Register(); fovY.SetMinMax(5.0f, 90.0f); fovY = 45.0f; simulationSpeed.Set(this, "simulationSpeed"); simulationSpeed.Register("step=0.01 min=0.01 max=1.0"); simulationSpeed = 0.2f; num_points.Set(this, "numOfPoints", &OnnumPointsChanged); num_points.Register("step=1000 min=1 max=500000"); num_points = 100000; num_instances.Set(this, "num_instances"); num_instances.Register("step=1 min=1 max=3"); num_instances = 1; pointSize.Set(this, "pointSize"); pointSize.Register("step=0.1 min=0.1 max=10.0"); pointSize = 0.1f; scale.Set(this, "scale"); scale.Register("step=0.01 min=0.0 max=2.0"); scale = 0.5f; velocity.Set(this, "velocity", &OnColorChanged); velocity.Register(); velocity = glm::vec3(0.01f, 0.01f, 0.01f); targetSpeed = velocity; showBox.Set(this, "showBox"); showBox.Register(); showBox = true; stopMotion.Set(this, "stopMotion"); stopMotion.Register(); stopMotion = false; EnumPair ep[] = {{0, "None"},{1, "cube"}, {2, "sphere"}, {3, "torus"}, {4, "ef"}, {5, "ef5"}}; importObject.Set(this, "importObject", ep, 6, &onObjectChanged); importObject.Register(); // -------------------------------------------------- // Initialize shaders and VAs // -------------------------------------------------- vsQuad = pathName + std::string("/resources/quad.vert"); fsQuad = pathName + std::string("/resources/quad.frag"); shaderQuad.CreateProgramFromFile(vsQuad.c_str(), fsQuad.c_str()); vaQuad.Create(4); vaQuad.SetArrayBuffer(0, GL_FLOAT, 2, ogl4_2dQuadVerts); vsBox = pathName + std::string("/resources/box.vert"); fsBox = pathName + std::string("/resources/box.frag"); shaderBox.CreateProgramFromFile(vsBox.c_str(), fsBox.c_str()); vsCube = pathName + std::string("/resources/cube.vert"); gsCube = pathName + std::string("/resources/cube.geom"); fsCube = pathName + std::string("/resources/cube.frag"); shaderCube.CreateProgramFromFile( vsCube.c_str(), gsCube.c_str(), fsCube.c_str() ); createCube(); vsObject = pathName + std::string("/resources/object.vert"); gsObject = pathName + std::string("/resources/object.geom"); fsObject = pathName + std::string("/resources/object.frag"); shaderObject.CreateProgramFromFile( vsObject.c_str(), fsObject.c_str() );//gsObject.c_str(), createTransformProgram(pathName); createBox(); createSphere(); createTorus(); createCube(); createEF(pathName, true); createEF(pathName, false); if(!checkProgram(pathName, "/resources/ctrlPoints.vert", "/resources/ctrlPoints.frag")){//, "/resources/ctrlPoints.geom" std::cout.flush(); std::cout << "******** shader error ********" <<std::endl; exit(0); } createCtrlPoints(); vsCtrlPoints = pathName + std::string("/resources/ctrlPoints.vert"); fsCtrlPoints = pathName + std::string("/resources/ctrlPoints.frag"); shaderCtrlPoints.CreateProgramFromFile(vsCtrlPoints.c_str(),//gsCtrlPoints.c_str(), fsCtrlPoints.c_str()); // -------------------------------------------------- // fbo and textures // -------------------------------------------------- initFBO(); // -------------------------------------------------- // random textures // -------------------------------------------------- srand (time(NULL)); for(int i = 0; i < random_tex_size; i++) { for(int j = 0; j < random_tex_size; j++) { texdata[4 * (i * random_tex_size + j) + 0] = (static_cast <float> (rand()) / static_cast <float> (RAND_MAX));// - 0.5f; texdata[4 * (i * random_tex_size + j) + 1] = (static_cast <float> (rand()) / static_cast <float> (RAND_MAX));// - 0.5f; texdata[4 * (i * random_tex_size + j) + 2] = (static_cast <float> (rand()) / static_cast <float> (RAND_MAX));// - 0.5f; texdata[4 * (i * random_tex_size + j) + 3] = 1.0f; } } glGenTextures(1, &randomTex); glBindTexture(GL_TEXTURE_2D, randomTex); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, random_tex_size, random_tex_size, 0, GL_RGBA, GL_FLOAT, texdata); int tex_cube_width = 800; int tex_cube_height = 400; char *texpathBoard = (pathName + std::string("/resources/textures/board.ppm")).c_str(); texBoard = LoadPPMTexture(texpathBoard, tex_cube_width, tex_cube_height); //target point targetPoint= glm::vec3(static_cast <float> (rand()) / static_cast <float> (RAND_MAX), static_cast <float> (rand()) / static_cast <float> (RAND_MAX), static_cast <float> (rand()) / static_cast <float> (RAND_MAX)); direction = glm::vec3(1.0f,1.0f,1.0f); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClearDepth(1.0f); glEnable(GL_DEPTH_TEST); return true; }