void Shader::build() { m_vertexShader = compileVertexShader(m_strVertexShader->cstr()); m_fragmentShader = compileFragmentShader(m_strFragmentShader->cstr()); m_shaderProgram = linkShaderProgram(m_vertexShader, m_fragmentShader); const SPVRTPFXUniformSemantic* semantics = PVRTPFXSemanticsGetSemanticList(); for (int i = 0; i < m_uniforms->count(); ++i) { SPVRTPFXUniform& uniform = m_uniforms->get(i); const SPVRTPFXUniformSemantic& semantic = semantics[uniform.nSemantic]; if (semantic.isAttrib) { uniform.nLocation = glGetAttribLocation(m_shaderProgram, uniform.sValueName->cstr()); m_isAttributeRequired[semantic.n] = true; ++m_numAttributesRequired; } else { uniform.nLocation = glGetUniformLocation(m_shaderProgram, uniform.sValueName->cstr()); // Check for array. Workaround for some OpenGL:ES implementations which require array element appended to uniform name // in order to return the correct location. if (uniform.nLocation == -1) { ByteArray* szTmpUniformName = ByteArray::create(uniform.sValueName->cstr()); szTmpUniformName->append("[0]"); uniform.nLocation = glGetUniformLocation(m_shaderProgram, szTmpUniformName->cstr()); } } } }
GLhandleARB linkVertexAndFragmentShader(const char* shaderFileName) { /* Compile the vertex and fragment shaders: */ GLhandleARB vertexShader=compileVertexShader(shaderFileName); GLhandleARB fragmentShader=compileFragmentShader(shaderFileName); /* Link the shader program: */ GLhandleARB shaderProgram=glLinkShader(vertexShader,fragmentShader); /* Release the compiled shaders (won't get deleted until shader program is released): */ glDeleteObjectARB(vertexShader); glDeleteObjectARB(fragmentShader); return shaderProgram; }
bool OpenGLComposite::InitOpenGLState() { if (! ResolveGLExtensions()) return false; // Prepare the shader used to perform colour space conversion on the video texture char compilerErrorMessage[1024]; if (! compileFragmentShader(sizeof(compilerErrorMessage), compilerErrorMessage)) { MessageBoxA(NULL, compilerErrorMessage, "OpenGL Shader failed to compile", MB_OK); return false; } // Setup the scene glShadeModel( GL_SMOOTH ); // Enable smooth shading glClearColor( 0.0f, 0.0f, 0.0f, 0.5f ); // Black background glClearDepth( 1.0f ); // Depth buffer setup glEnable( GL_DEPTH_TEST ); // Enable depth testing glDepthFunc( GL_LEQUAL ); // Type of depth test to do glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST ); if (! mFastTransferExtensionAvailable) { glGenBuffers(1, &mUnpinnedTextureBuffer); } // Setup the texture which will hold the captured video frame pixels glEnable(GL_TEXTURE_2D); glGenTextures(1, &mCaptureTexture); glBindTexture(GL_TEXTURE_2D, mCaptureTexture); // Parameters to control how texels are sampled from the texture glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); // Create texture with empty data, we will update it using glTexSubImage2D each frame. // The captured video is YCbCr 4:2:2 packed into a UYVY macropixel. OpenGL has no YCbCr format // so treat it as RGBA 4:4:4:4 by halving the width and using GL_RGBA internal format. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, mFrameWidth/2, mFrameHeight, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL); glBindTexture(GL_TEXTURE_2D, 0); // Create Frame Buffer Object (FBO) to perform off-screen rendering of scene. // This allows the render to be done on a framebuffer with width and height exactly matching the video format. glGenFramebuffersEXT(1, &mIdFrameBuf); glGenRenderbuffersEXT(1, &mIdColorBuf); glGenRenderbuffersEXT(1, &mIdDepthBuf); glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mIdFrameBuf); // Texture for FBO glGenTextures(1, &mFBOTexture); glBindTexture(GL_TEXTURE_2D, mFBOTexture); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, mFrameWidth, mFrameHeight, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL); // Attach a depth buffer glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, mIdDepthBuf); glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, mFrameWidth, mFrameHeight); glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, mIdDepthBuf); // Attach the texture which stores the playback image glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, mFBOTexture, 0); glBindTexture(GL_TEXTURE_2D, 0); glDisable(GL_TEXTURE_2D); GLenum glStatus = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT); if (glStatus != GL_FRAMEBUFFER_COMPLETE_EXT) { MessageBox(NULL, _T("Cannot initialize framebuffer."), _T("OpenGL initialization error."), MB_OK); return false; } return true; }
void PShader::loadFragmentShader(std::string src) { this->fragSrc = loadBytes(src); compileFragmentShader(); glAttachShader(glProgram, glFragment); glLinkProgram(glProgram); }
void PShader::setFragmentShader(std::string src) { fragSrc = src; compileFragmentShader(); glAttachShader(glProgram, glFragment); glLinkProgram(glProgram); }