GLuint Shaders::loadShaderFromString(const std::string& vertexSourceCode, const std::string& fragmentSourceCode) { // Create the shaders GLuint vertexShaderID = glCreateShader(GL_VERTEX_SHADER); GLuint fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER); GLint result = GL_FALSE; int infoLogLength; compileShader(vertexShaderID, vertexSourceCode, &result, &infoLogLength); compileShader(fragmentShaderID, fragmentSourceCode, &result, &infoLogLength); // Link the program GLuint programID = glCreateProgram(); glAttachShader(programID, vertexShaderID); glAttachShader(programID, fragmentShaderID); glLinkProgram(programID); // Check the program glGetProgramiv(programID, GL_LINK_STATUS, &result); glGetProgramiv(programID, GL_INFO_LOG_LENGTH, &infoLogLength); if ( infoLogLength > 0 ){ std::vector<char> programErrorMessage(infoLogLength+1); glGetProgramInfoLog(programID, infoLogLength, NULL, &programErrorMessage[0]); printf("%s\n", &programErrorMessage[0]); } glDeleteShader(vertexShaderID); glDeleteShader(fragmentShaderID); return programID; }
bool Shader::LoadAndCompile(const std::string& sVert, const std::string& sFrag) { GLhandleARB so[2]; memset(so, 0, sizeof(GLhandleARB)*2); ///Loading the shader so[0] = loadShader(sVert.c_str()); if(so[0]==0){ std::cerr << "loading shader "+sVert+" failed" << std::endl; return false; } ///Compiling the shader, done by the graphic card if(!compileShader(so[0])){ std::cerr << "compiling shader "+sVert+" failed" << std::endl; return false; } so[1] = loadShader(sFrag.c_str()); if(so[1]==0){ std::cerr << "loading shader "+sFrag+" failed " << std::endl; return false; } if(!compileShader(so[1])){ std::cerr << "compiling shader "+sFrag+" failed " << std::endl; return false; } m_oProgram = linkShaders(so,2); m_bIsReady = true; return true; }
static GLboolean compileShaders(const GLchar* vertexShaderSource, const GLchar* fragmentShaderSource, GLuint* shaderVertexObjectId, GLuint* shaderFragmentObjectId, GLuint* programObjectId) { if(!compileShader(&vertexShaderSource[0], NULL, GL_VERTEX_SHADER, shaderVertexObjectId)) { // Undo compilation } if(!compileShader(&fragmentShaderSource[0], NULL, GL_FRAGMENT_SHADER, shaderFragmentObjectId)){ // Undo compilation return GL_FALSE; } *programObjectId = glCreateProgram(); glAttachShader(*programObjectId, *shaderVertexObjectId); glAttachShader(*programObjectId, *shaderFragmentObjectId); glLinkProgram(*programObjectId); GLint linkSuccess = GL_FALSE; glGetProgramiv(*programObjectId, GL_LINK_STATUS, &linkSuccess); if (linkSuccess == GL_FALSE) { GLchar messages[256] = {0}; glGetProgramInfoLog(*programObjectId, sizeof(messages), 0, &messages[0]); printf("Prgram compilation failed: %s", messages); // Undo compilation return GL_FALSE; } return GL_TRUE; }
void NMS_ShaderManager::loadShaders(string vertexShaderFilename, string fragmentShaderFilename) { string key = vertexShaderFilename+fragmentShaderFilename; if(programs.count(key) == 0) { GLint vertexShader = glCreateShader(GL_VERTEX_SHADER); GLint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); const char * vsc = readFile(vertexShaderFilename); const char * fsc = readFile(fragmentShaderFilename); glShaderSource(vertexShader, 1, &vsc, NULL); glShaderSource(fragmentShader, 1, &fsc, NULL); compileShader(vertexShader, vertexShaderFilename); compileShader(fragmentShader, fragmentShaderFilename); program = glCreateProgram(); glAttachShader(program,vertexShader); glAttachShader(program,fragmentShader); linkProgram(program); programs[key] = program; } glUseProgram(programs[key]); }
bool GLSLProgram::compileShaders(const std::string& vertexShaderFilePath, const std::string& fragmentShaderFilePath) { //Create the program to bind shader in _programID = glCreateProgram(); //Create shader programs: _vertexShaderID = glCreateShader(GL_VERTEX_SHADER); if (_vertexShaderID == 0) { fatalError("Vertex shader failed to be created"); return false; } _fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER); if (_fragmentShaderID == 0) { fatalError("Fragment shader failed to be created"); return false; } //open, read and compile each program //compile vertex shader if (!compileShader(vertexShaderFilePath, _vertexShaderID)) { return false; } //compile fragment shader if (!compileShader(fragmentShaderFilePath, _fragmentShaderID)) { return false; } return true; }
//------------------------------------------------------------------------------ GLuint linkProgram(const char *define) { GLuint vertexShader = compileShader(GL_VERTEX_SHADER, "VERTEX_SHADER", define); GLuint geometryShader = compileShader(GL_GEOMETRY_SHADER, "GEOMETRY_SHADER", define); GLuint fragmentShader = compileShader(GL_FRAGMENT_SHADER, "FRAGMENT_SHADER", define); GLuint program = glCreateProgram(); glAttachShader(program, vertexShader); glAttachShader(program, geometryShader); glAttachShader(program, fragmentShader); glBindAttribLocation(program, 0, "position"); glBindAttribLocation(program, 1, "normal"); glLinkProgram(program); glDeleteShader(vertexShader); glDeleteShader(geometryShader); glDeleteShader(fragmentShader); GLint status; glGetProgramiv(program, GL_LINK_STATUS, &status ); if( status == GL_FALSE ) { GLchar emsg[1024]; glGetProgramInfoLog(program, sizeof(emsg), 0, emsg); fprintf(stderr, "Error linking GLSL program : %s\n", emsg ); exit(0); } return program; }
void GLSLProgram::compileShaders(const std::string& vertexShaderFilePath, const std::string& fragmentShaderFilePath) { //Vertex and fragment shaders are successfully compiled. //Now time to link them together into a program. //Get a program object. programID = glCreateProgram(); vertexShaderID = glCreateShader(GL_VERTEX_SHADER); if (vertexShaderID == 0) { std::printf("ERROR : could not create Vertex shader"); } fragShaderID = glCreateShader(GL_FRAGMENT_SHADER); if (fragShaderID == 0) { std::printf("ERROR : could not create Fragment shader"); } compileShader(vertexShaderFilePath, vertexShaderID); compileShader(fragmentShaderFilePath, fragShaderID); }
void GpuProgram::create(const char* vs, const char* fs) { GLuint vertexID = compileShader(vs, GL_VERTEX_SHADER); GLuint fragmentID = compileShader(fs, GL_FRAGMENT_SHADER); name_ = glCreateProgram(); OGLERROR; glAttachShader(name_, vertexID); OGLERROR; glAttachShader(name_, fragmentID); OGLERROR; glLinkProgram(name_); OGLERROR; #ifdef _DEBUG glGetProgramiv(name_, GL_LINK_STATUS, &success_); OGLERROR; if (!success_) { glGetProgramInfoLog(name_, 512, nullptr, logProgram_); OGLERROR; printf("%s", logProgram_); } #endif glDeleteShader(vertexID); OGLERROR; glDeleteShader(fragmentID); OGLERROR; }
GLProgram buildProgram(VSrc&& vsrc, GSrc&& gsrc, FSrc&& fsrc) { GLShader vs = compileShader(GL_VERTEX_SHADER, std::forward<VSrc>(vsrc)); GLShader gs = compileShader(GL_GEOMETRY_SHADER, std::forward<GSrc>(gsrc)); GLShader fs = compileShader(GL_FRAGMENT_SHADER, std::forward<FSrc>(fsrc)); return buildProgram({ std::move(vs), std::move(gs), std::move(fs) }); }
void loadShaders(GLuint *_positionSlot, GLuint *_colorSlot) { GLuint vertex = compileShader(GL_VERTEX_SHADER, VERTEX_SHADER_CODE); GLuint fragment = compileShader(GL_FRAGMENT_SHADER, FRAGMENT_SHADER_CODE); GLuint program = glCreateProgram(); glAttachShader(program, vertex); glAttachShader(program, fragment); glLinkProgram(program); GLint linkSuccess; glGetShaderiv(program, GL_COMPILE_STATUS, &linkSuccess); if (linkSuccess == GL_FALSE) { GLchar messages[256]; glGetProgramInfoLog(program, sizeof(messages), NULL, &messages[0]); printf("Program linking error: %s\n", messages); exit(1); } glUseProgram(program); *_positionSlot = glGetAttribLocation(program, "Position"); *_colorSlot = glGetAttribLocation(program, "SourceColor"); glEnableVertexAttribArray(*_positionSlot); glEnableVertexAttribArray(*_colorSlot); return; }
void init(Data*data){ data->cameraTransform = std::make_shared<OrbitCamera>(); data->cameraProjection = std::make_shared<PerspectiveCamera>( glm::half_pi<float>(), (float)data->window.width/data->window.height); data->program = createProgram( compileShader(GL_VERTEX_SHADER ,loadFile("shaders/orbitManipulator.vp")), compileShader(GL_FRAGMENT_SHADER,loadFile("shaders/orbitManipulator.fp"))); data->mvpUniform = glGetUniformLocation(data->program,"mvp"); glGenBuffers(1,&data->vbo); glBindBuffer(GL_ARRAY_BUFFER,data->vbo); float vertices[]={0,0,0,1, 1,0,0,1, 0,1,0,1}; glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_STATIC_DRAW); glGenVertexArrays(1,&data->vao); glBindVertexArray(data->vao); glBindBuffer(GL_ARRAY_BUFFER,data->vbo); glEnableVertexAttribArray(0); glVertexAttribPointer(0,4,GL_FLOAT,GL_FALSE,sizeof(float)*4,0); glBindVertexArray(0); glClearColor(0,0,0,1); glDisable(GL_CULL_FACE); }
GLuint ShaderManager::buildProgram(const Shader& vshaderInstance, const Shader& fhaderInstance, const Shader& ghaderInstance) { if (!compileShader(vshaderInstance)) { showShaderInfoLog(vshaderInstance); } if (!compileShader(fhaderInstance)) { showShaderInfoLog(fhaderInstance); } if (!compileShader(ghaderInstance)) { showShaderInfoLog(ghaderInstance); } GLuint program = glCreateProgram(); glAttachShader(program, vshaderInstance.getID()); glAttachShader(program, fhaderInstance.getID()); glLinkProgram(program); GLint linked; glGetProgramiv(program, GL_LINK_STATUS, &linked); if (!linked) { showProgramInfoLog(program); } glDeleteShader(vshaderInstance.getID()); glDeleteShader(fhaderInstance.getID()); return program; }
GLuint LoadShaders(const char *vertFile,const char * fragFile) { GLuint vShaderID = glCreateShader(GL_VERTEX_SHADER); GLuint fShaderID = glCreateShader(GL_FRAGMENT_SHADER); char * vShaderSource = readFile(vertFile); char * fShaderSource = readFile(fragFile); compileShader(vShaderID, vShaderSource); compileShader(fShaderID, fShaderSource); GLuint programID = glCreateProgram(); glAttachShader(programID, vShaderID); glAttachShader(programID, fShaderID); glLinkProgram(programID); GLint result; int infoLogLength; glGetProgramiv(programID, GL_LINK_STATUS, &result); if (result != GL_TRUE) { glGetProgramiv(programID, GL_INFO_LOG_LENGTH, &infoLogLength); char *buf = (char *)malloc(infoLogLength+1); glGetProgramInfoLog(programID, infoLogLength, NULL, buf); fprintf(stderr,"%s\n", buf); free(buf); } glDeleteShader(vShaderID); glDeleteShader(fShaderID); return programID; }
void Shader::load() { Renderer *renderer = Naquadah::getRenderer(); if (renderer != nullptr) { std::string vertexCode = FileIO::mergeLines(FileIO::readTextFile(vertexFilename)); std::string fragmentCode = FileIO::mergeLines(FileIO::readTextFile(fragmentFilename)); std::string geometryCode = FileIO::mergeLines(FileIO::readTextFile(geometryFilename)); std::string tessCtrlCode = FileIO::mergeLines(FileIO::readTextFile(tessCtrlFilename)); std::string tessEvalCode = FileIO::mergeLines(FileIO::readTextFile(tessEvalFilename)); this->program = glCreateProgram(); vertexId = compileShader(program, GL_VERTEX_SHADER, vertexCode.c_str()); fragmentId = compileShader(program, GL_FRAGMENT_SHADER, fragmentCode.c_str()); if (geometryCode.size() > 0) geometryId = compileShader(program, GL_GEOMETRY_SHADER, geometryCode.c_str()); if (tessCtrlCode.size() > 0 && tessEvalCode.size() > 0) { tessCtrlId = compileShader(program, GL_TESS_CONTROL_SHADER, tessCtrlCode.c_str()); tessEvalId = compileShader(program, GL_TESS_EVALUATION_SHADER, tessEvalCode.c_str()); } setDefaultAttributes(); if (linkProgram(program)) valid = true; } else valid = false; loaded = true; }
GLuint shaderhelper::createProgram(const std::string & vertexFilename, const std::string & fragmentFilename) { GLuint vertexShaderId = compileShader(vertexFilename, GL_VERTEX_SHADER); GLuint fragmentShaderId = compileShader(fragmentFilename, GL_FRAGMENT_SHADER); GLuint programId = glCreateProgram(); glAttachShader(programId, vertexShaderId); glAttachShader(programId, fragmentShaderId); glLinkProgram(programId); GLint compilationSuccess = GL_FALSE; int infoLogLength; glGetProgramiv(programId, GL_LINK_STATUS, &compilationSuccess); glGetProgramiv(programId, GL_INFO_LOG_LENGTH, &infoLogLength); if (infoLogLength > 0) { std::vector<char> programErrorMessage((unsigned long) (infoLogLength + 1)); glGetProgramInfoLog(programId, infoLogLength, nullptr, &programErrorMessage[0]); throw std::runtime_error(&programErrorMessage[0]); } glDeleteShader(vertexShaderId); glDeleteShader(fragmentShaderId); return programId; }
GLuint ShaderProgram::loadShaders(const char * vertex_file_path, const char * fragment_file_path ) { // Create the shaders GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER); GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER); // Compile both shaders. Exit if compile errors. if ( !compileShader(vertex_file_path, VertexShaderID) || !compileShader(fragment_file_path, FragmentShaderID) ) { return 0; } // Link the program GLuint ProgramID = glCreateProgram(); glAttachShader(ProgramID, VertexShaderID); glAttachShader(ProgramID, FragmentShaderID); glLinkProgram(ProgramID); // Check the program GLint Result = GL_FALSE; int InfoLogLength; glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result); glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength); if ( InfoLogLength > 0 ) { std::vector<char> ProgramErrorMessage(InfoLogLength+1); glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]); std::cerr << &ProgramErrorMessage[0] << std::endl; } glDeleteShader(VertexShaderID); glDeleteShader(FragmentShaderID); return ProgramID; }
GLuint buildProgram(const char *vert_shader_source, const char *frag_shader_source) { /* compile the vertex shader */ GLuint vert_shader = compileShader(GL_VERTEX_SHADER, vert_shader_source); if (vert_shader == 0) { return 0; } /* compile the fragment shader */ GLuint frag_shader = compileShader(GL_FRAGMENT_SHADER, frag_shader_source); if (frag_shader == 0) { glDeleteShader(vert_shader); return 0; } /* link the shaders together */ GLuint program = linkProgram(2, vert_shader, frag_shader); /* delete shader objects, so that when user destroys the program object, the shaders will be freed too */ glDeleteShader(frag_shader); glDeleteShader(vert_shader); return program; }
void linkDebugProgram() { if (g_debugProgram) glDeleteProgram(g_debugProgram); GLuint vertexShader = compileShader(GL_VERTEX_SHADER, "PTEX_DEBUG_VERTEX_SHADER"); GLuint fragmentShader = compileShader(GL_FRAGMENT_SHADER, "PTEX_DEBUG_FRAGMENT_SHADER"); g_debugProgram = glCreateProgram(); glAttachShader(g_debugProgram, vertexShader); glAttachShader(g_debugProgram, fragmentShader); glLinkProgram(g_debugProgram); glDeleteShader(fragmentShader); GLint status; glGetProgramiv(g_debugProgram, GL_LINK_STATUS, &status ); if( status == GL_FALSE ) { GLchar emsg[1024]; glGetProgramInfoLog(g_debugProgram, sizeof(emsg), 0, emsg); fprintf(stderr, "Error linking GLSL program : %s\n", emsg ); exit(0); } GLint texData = glGetUniformLocation(g_debugProgram, "ptexDebugData"); glProgramUniform1i(g_debugProgram, texData, 0); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D_ARRAY, g_osdPTexImage->GetTexelsTexture()); }
void init(Data*data){ if(data->args.size()<2){ std::cerr<<"expected name of video"<<std::endl; std::exit(0); } data->video = std::make_shared<Video>(data->args[1]); data->window->setSize(data->video->getWidth(),data->video->getHeight()); glEnable(GL_DEBUG_OUTPUT); glDebugMessageCallback((GLDEBUGPROC)defaultDebugMessage,NULL); glViewport(0,0,data->video->getWidth(),data->video->getHeight()); data->frame = std::make_shared<TextureObject>(); data->program = std::make_shared<ProgramObject>(createProgram( compileShader(GL_VERTEX_SHADER , "#version 450\n", loadFile("shaders/video_gray.vp")), compileShader(GL_FRAGMENT_SHADER, "#version 450\n", loadFile("shaders/noiseFunctions.vp"), loadFile("shaders/gradients.vp"), loadFile("shaders/video_gray.fp")))); glGenVertexArrays(1,&data->vao); glClearColor(0,0,0,1); }
bool GLProgram::initWithByteArrays(const GLchar* vShaderByteArray, const GLchar* fShaderByteArray) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) GLboolean hasCompiler = false; glGetBooleanv(GL_SHADER_COMPILER, &hasCompiler); _hasShaderCompiler = (hasCompiler == GL_TRUE); if(!_hasShaderCompiler) { return initWithPrecompiledProgramByteArray(vShaderByteArray,fShaderByteArray); } #endif _program = glCreateProgram(); CHECK_GL_ERROR_DEBUG(); _vertShader = _fragShader = 0; if (vShaderByteArray) { if (!compileShader(&_vertShader, GL_VERTEX_SHADER, vShaderByteArray)) { CCLOG("cocos2d: ERROR: Failed to compile vertex shader"); return false; } } // Create and compile fragment shader if (fShaderByteArray) { if (!compileShader(&_fragShader, GL_FRAGMENT_SHADER, fShaderByteArray)) { CCLOG("cocos2d: ERROR: Failed to compile fragment shader"); return false; } } if (_vertShader) { glAttachShader(_program, _vertShader); } CHECK_GL_ERROR_DEBUG(); if (_fragShader) { glAttachShader(_program, _fragShader); } _hashForUniforms.clear(); CHECK_GL_ERROR_DEBUG(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || defined(WP8_SHADER_COMPILER) _shaderId = CCPrecompiledShaders::getInstance()->addShaders(vShaderByteArray, fShaderByteArray); #endif return true; }
Renderer::Renderer(const char *rootDirectory) { char file[200]; // create color program string src; sprintf(file, "%s/shader/color.vertexshader", rootDirectory); loadShaderCodeFromFile(file, src); compileShader(src, GL_VERTEX_SHADER, shader["color_vertex"]); sprintf(file, "%s/shader/color.fragmentshader", rootDirectory); loadShaderCodeFromFile(file, src); compileShader(src, GL_FRAGMENT_SHADER, shader["color_fragment"]); if (createRenderProgram(shader["color_vertex"], shader["color_fragment"], program["color"]) == GL_FALSE) return; MatrixID = glGetUniformLocation(program["color"], "MVP"); ViewMatrixID = glGetUniformLocation(program["color"], "ViewMatrix"); ModelMatrixID= glGetUniformLocation(program["color"], "ModelMatrix"); LightPositionID = glGetUniformLocation(program["color"], "LightPosition_worldspace"); Mat cameraMatrix, distCoeffs; sprintf(file, "%s/intrinsics.xml", rootDirectory); cv::FileStorage fs(file, cv::FileStorage::READ); fs["camera_matrix"] >> cameraMatrix; fs["distortion_coefficients"] >> distCoeffs; fs.release(); // calculate undistortion mapping Mat img_rectified, map1, map2; initUndistortRectifyMap(cameraMatrix, distCoeffs, Mat(), getOptimalNewCameraMatrix(cameraMatrix, distCoeffs, cv::Size(WIDTH, HEIGHT), 1, cv::Size(WIDTH, HEIGHT), 0), cv::Size(WIDTH, HEIGHT), CV_16SC2, map1, map2); ViewMatrix = Matrix4f::Identity(); ViewMatrix.topRightCorner(3,1) << 0,0,-2; float n = 0.01; // near field float f = 100; // far field ProjectionMatrix << cameraMatrix.at<double>(0, 0) / cameraMatrix.at<double>(0, 2), 0.0, 0.0, 0.0, 0.0, cameraMatrix.at<double>(1, 1) / cameraMatrix.at<double>(1, 2), 0.0, 0.0, 0.0, 0.0, -(f + n) / (f - n), (-2.0f * f * n) / (f - n), 0.0, 0.0, -1.0, 0.0; K << cameraMatrix.at<double>(0, 0), cameraMatrix.at<double>(0, 1), cameraMatrix.at<double>(0, 2), cameraMatrix.at<double>(1, 0), cameraMatrix.at<double>(1, 1), cameraMatrix.at<double>(1, 2), cameraMatrix.at<double>(2, 0), cameraMatrix.at<double>(2, 1), cameraMatrix.at<double>(2, 2); cout << "K\n" << K << endl; Kinv = K.inverse(); // background ccolor glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Enable depth test glEnable(GL_DEPTH_TEST); // Accept fragment if it closer to the camera than the former one glDepthFunc(GL_LESS); // Cull triangles which normal is not towards the camera glEnable(GL_CULL_FACE); }
bool Shader::loadShaders() { GLuint vertShader, fragShader; // cria o programa shader program = glCreateProgram(); // cria e compila o vertex shader if(!compileShader(&vertShader, GL_VERTEX_SHADER, vshPath)) { printf("Erro ao criar o vertex shader! %s\n", vshPath); } // cria e compila o fragment shader if(!compileShader(&fragShader, GL_FRAGMENT_SHADER, fshPath)) { printf("Erro ao criar o fragment shader! %s\n", fshPath); } printf("Attaching shaders...\n"); // dah attach nos shaders glAttachShader(program, vertShader); glAttachShader(program, fragShader); // linka if (!linkProgram(program)) { printf("Falha ao linkar o programa: %d", program); if (vertShader) { glDeleteShader(vertShader); vertShader = 0; } if (fragShader) { glDeleteShader(fragShader); fragShader = 0; } if (program) { glDeleteProgram(program); program = 0; } return false; } /* // Release vertex and fragment shaders. if (vertShader) glDeleteShader(vertShader); if (fragShader) glDeleteShader(fragShader); return true; */ return true; }
GLuint ShaderProgram::compile() { this->program = -1; // Make sure both shaders are set if (this->fragmentShaderFile == NULL) { printf("Fragmentshader file not set!"); return 0; } if (this->vertexShaderFile == NULL) { printf("Vertexshader file not set!"); return 0; } // Read the Vertex Shader code from the file std::string VertexShaderCode = this->loadShaderContent(this->vertexShaderFile); std::string FragmentShaderCode = this->loadShaderContent(this->fragmentShaderFile); // Compile Vertex Shader printf("Compiling shader : %s\n", this->vertexShaderFile); GLuint vertexShaderID = glCreateShader(GL_VERTEX_SHADER); bool vertexShaderCompiled = compileShader(VertexShaderCode, vertexShaderID); // Compile Fragment Shader printf("Compiling shader : %s\n", this->fragmentShaderFile); GLuint fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER); bool fragmentShaderCompiled = compileShader(FragmentShaderCode, fragmentShaderID); if(!vertexShaderCompiled || !fragmentShaderCompiled) { return 0; } // Link the program printf("Linking program\n"); GLuint ProgramID = glCreateProgram(); glAttachShader(ProgramID, vertexShaderID); glAttachShader(ProgramID, fragmentShaderID); glLinkProgram(ProgramID); // Check the program int infoLogLength; GLint result = GL_FALSE; glGetProgramiv(ProgramID, GL_LINK_STATUS, &result); glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &infoLogLength); if (!result) { std::vector<char> ProgramErrorMessage(infoLogLength + 1); glGetProgramInfoLog(ProgramID, infoLogLength, NULL, &ProgramErrorMessage[0]); printf("%s\n", &ProgramErrorMessage[0]); } glDeleteShader(vertexShaderID); glDeleteShader(fragmentShaderID); printf("Done\n"); this->program = ProgramID; return ProgramID; }
int main(int /*argc*/, char ** /*argv*/) { BaseApp app; ProgramObject program; auto mainWindow = app.getMainWindow(); std::string prefix = app.getResourceDir() + "Shaders/Examples/e06_ModelLoader/"; PerspectiveCamera cam; OrbitManipulator manipulator(&cam); manipulator.setupCallbacks(app); NodeShared root; GLuint query[2]; app.addInitCallback([&]() { auto vs = compileShader(GL_VERTEX_SHADER, Loader::text(prefix + "phong.vert")); auto fs = compileShader(GL_FRAGMENT_SHADER, Loader::text(prefix + "phong.frag")); program = createProgram(vs, fs); root = Loader::scene(app.getResourceDir() + "Models/sponza/sponza.fbx"); glCreateQueries(GL_TIMESTAMP, 2, query); SDL_GL_SetSwapInterval(0); }); app.addResizeCallback([&](int w, int h) { glViewport(0, 0, w, h); cam.setAspect(float(w) / float(h)); }); app.addDrawCallback([&]() { glQueryCounter(query[0], GL_TIMESTAMP); glClearColor(0.2, 0.2, 0.2, 1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_DEPTH_TEST); //bunny program.use(); program.setMatrix4fv("p", value_ptr(cam.getProjection())); program.setMatrix4fv("v", value_ptr(cam.getView())); drawNode(program, root); glQueryCounter(query[1], GL_TIMESTAMP); GLuint64 time1, time2; glGetQueryObjectui64v(query[0], GL_QUERY_RESULT, &time1); glGetQueryObjectui64v(query[1], GL_QUERY_RESULT, &time2); std::string s = "fps: " + std::to_string(1e9 / (time2 - time1)) + " (" + std::to_string(ImGui::GetIO().Framerate) + ")"; label(s, 0, 0, 300, 100); }); return app.run(); }
Shader::Shader(const char* vert_path, const char* frag_path) { printf("Compiling vertex shader: %s\n", vert_path); GLuint vertex_handle = compileShader(GL_VERTEX_SHADER, vert_path); printf("Compiling fragment shader: %s\n", frag_path); GLuint frag_handle = compileShader(GL_FRAGMENT_SHADER, frag_path); printf("Linking program\n"); m_program = linkProgram(vertex_handle, frag_handle); printf("Complete\n"); }
void nuShaderList::compileData(Data& data) { data.vsh_id = compileShader(GL_VERTEX_SHADER, (const GLchar**) &data.vsh_data, static_cast< GLint >(data.vsh_size)); data.fsh_id = compileShader(GL_FRAGMENT_SHADER, (const GLchar**) &data.fsh_data, static_cast< GLint >(data.fsh_size)); data.initialized = 1; }
/** * Return ID of the linked and activated shader. */ GLuint initShaders() { auto vshader = compileShader("vshader.glsl", ShaderType::vertex); auto fshader = compileShader("fshader.glsl", ShaderType::fragment); GLuint shaderProgram = glCreateProgram(); glAttachShader(shaderProgram, vshader); glAttachShader(shaderProgram, fshader); glLinkProgram(shaderProgram); glUseProgram(shaderProgram); printGlErrors(); return shaderProgram; }
void Shader::loadShader() { m_ShaderID = glCreateProgram(); GLuint vertexShaderId = glCreateShader(GL_VERTEX_SHADER); compileShader(m_VertexShaderPath, vertexShaderId); GLuint fragmentShaderId = glCreateShader(GL_FRAGMENT_SHADER); compileShader(m_FragmentShaderPath, fragmentShaderId); linkShader(m_ShaderID, vertexShaderId, fragmentShaderId); }
MyAppView::MyAppView(const MyAppModel& model, const MyAppSharedContext& sharedContext, const VRGraphicsState &renderState) : model(model), sharedContext(sharedContext) { // Init GL glEnable(GL_DEPTH_TEST); glClearDepth(1.0f); glDepthFunc(GL_LEQUAL); glClearColor(0, 0, 0, 1); // Create vao glGenVertexArrays(1, &vao); glBindVertexArray(vao); glBindBuffer(GL_ARRAY_BUFFER, sharedContext.getVbo()); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (char*)0); glEnableVertexAttribArray(1); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (char*)0 + sizeof(GLfloat)*model.vertices.size()); glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (char*)0 + sizeof(GLfloat)*model.vertices.size() + sizeof(GLfloat)*model.normals.size()); // Create shader std::string vertexShader = "#version 330 \n" "layout(location = 0) in vec3 position; " "layout(location = 1) in vec3 normal; " "layout(location = 2) in vec3 color; " "" "uniform mat4 ProjectionMatrix; " "uniform mat4 ViewMatrix; " "uniform mat4 ModelMatrix; " "uniform mat4 NormalMatrix; " "" "out vec3 col;" "" "void main() { " " gl_Position = ProjectionMatrix*ViewMatrix*ModelMatrix*vec4(position, 1.0); " " col = color;" "}"; vshader = compileShader(vertexShader, GL_VERTEX_SHADER); std::string fragmentShader = "#version 330 \n" "in vec3 col;" "out vec4 colorOut;" "" "void main() { " " colorOut = vec4(col, 1.0); " "}"; fshader = compileShader(fragmentShader, GL_FRAGMENT_SHADER); // Create shader program shaderProgram = glCreateProgram(); glAttachShader(shaderProgram, vshader); glAttachShader(shaderProgram, fshader); linkShaderProgram(shaderProgram); }
int linkProgram(const char* vshader_src, const char* fshader_src) { GLuint program = glCreateProgram(); GLuint vshader = compileShader(GL_VERTEX_SHADER, vshader_src); GLuint fshader = compileShader(GL_FRAGMENT_SHADER, fshader_src); if (!linkProgram(program, vshader, fshader)) { glDeleteProgram(program); program = 0; } glDeleteShader(vshader); glDeleteShader(fshader); return program; }