AnimSprite::AnimSprite(const char* a_cpTexture, GLFWwindow* window) { GameWindow = window; m_dElapsedTime = 0; m_v3Position = Vector3(g_gl_width/2, g_gl_height/2, 0); LoadVertShader("./resources/VertexShader.glsl"); LoadFragShader("./resources/FragmentShader.glsl"); LinkShaders(); GLint uvAttrib = glGetAttribLocation(m_ShaderProgram, "texcoord"); glEnableVertexAttribArray(uvAttrib); matrix_location = glGetUniformLocation(m_ShaderProgram, "matrix"); LoadSlices(a_cpTexture); LoadAnimations(m_atlas.sAnimations.c_str()); LoadTexture(m_atlas.sSheet.c_str()); m_dFrameDuration = (1.0/5.0); m_sCurrentAnimation = "move N"; m_sCurrentSlice = "move N 0"; iCurrentFrame = 0; iLoopMarker = 0; currentCycle = LOOP; m_texSize = m_atlas.v2Size; SetSlice(); UVSetup(); }
GameEntity::GameEntity( const char* a_pSpriteSheet, GLFWwindow * window) { GameWindow = window; elapsedTime = 0; LoadVertShader("./Resources/LoadVertexShader.glsl"); LoadFragShader("./Resources/LoadFragmentShader.glsl"); LinkShaders(); GLint uvAttrib = glGetAttribLocation(m_ShaderProgram,"texcoord"); glEnableVertexAttribArray(uvAttrib); matrix_location = glGetUniformLocation (m_ShaderProgram, "matrix"); LoadSprites(a_pSpriteSheet); LoadAnimations(atlas.sAnimations.c_str()); LoadTexture(atlas.sSheet.c_str()); m_dFrames = (1.0/15.0); currentAnimation = "Idle"; currentSprite = "Idle_01"; currentFrame = 0; currentPlayType = SINGLE; m_uvScale.m_fX = atlas.v2Size.m_fY; m_uvScale.m_fY = atlas.v2Size.m_fX; SetSprite(); SetUVData(); iFacing = "Right"; //flag for facing }
Sprite::Sprite(const char* a_pTexture, int a_iWidth, int a_iHeight, Vector4 a_v4Color, GLFWwindow* window) { //load in the shaders using the new functions in quad GameWindow = window; LoadVertShader("../resources/VertexShader.glsl"); LoadFragShader("../resources/FragmentShader.glsl"); LinkShaders(); GLuint uvAttrib = glGetAttribLocation(m_ShaderProgram, "texcoord"); glEnableVertexAttribArray(uvAttrib); matrix_location = glGetUniformLocation(m_ShaderProgram, "matrix"); LoadTexture(a_pTexture); m_v4SpriteColor = a_v4Color; m_aoVerts[0].Color = m_v4SpriteColor; m_aoVerts[1].Color = m_v4SpriteColor; m_aoVerts[2].Color = m_v4SpriteColor; m_aoVerts[3].Color = m_v4SpriteColor; m_v2Scale = Vector2::Vector2(a_iWidth, a_iHeight); m_v3Position = Vector3::Vector3(g_gl_width / 2, g_gl_height / 2, 0.0f); }
uint GLShader::SetupShader( const char* VertexShader, const char* GeometryShader, const char* FragmentShader) { String ShaderPath = FindShaderPath(FragmentShader); uint VertShaderID=0, GeomShaderID=0, FragShaderID=0, ID=0; if(VertexShader) { String VertShaderPath = ShaderPath + String(VertexShader) + ".vert"; VertShaderID = LoadShader(GL_VERTEX_SHADER, VertShaderPath.Val()); } if(GeometryShader) { String GeomShaderPath = ShaderPath + String(GeometryShader) + ".geom"; GeomShaderID = LoadShader(GL_GEOMETRY_SHADER, GeomShaderPath.Val()); } if(FragmentShader) { String FragShaderPath = ShaderPath + String(FragmentShader) + ".frag"; FragShaderID = LoadShader(GL_FRAGMENT_SHADER, FragShaderPath.Val()); } ProgramID[NrShaders++] = ID = LinkShaders(VertShaderID, GeomShaderID, FragShaderID); return ID; }
static void Init(void) { static const char *fragShaderText = "void main() {\n" " gl_FragColor = vec4(1.0,0.0,0.0,1.0);\n" "}\n"; static const char *vertShaderText = "void main() {\n" " gl_Position = gl_Vertex;\n" "}\n"; if (!ShadersSupported()) exit(1); fragShader = CompileShaderText(GL_FRAGMENT_SHADER, fragShaderText); vertShader = CompileShaderText(GL_VERTEX_SHADER, vertShaderText); program = LinkShaders(vertShader, fragShader); glUseProgram(program); assert(glGetError() == 0); glClearColor(0.3f, 0.3f, 0.3f, 1.0f); printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER)); assert(glIsProgram(program)); assert(glIsShader(fragShader)); assert(glIsShader(vertShader)); glColor3f(1, 0, 0); }
static void Init(void) { if (!ShadersSupported()) exit(1); vertShader = CompileShaderText(GL_VERTEX_SHADER, VertShaderText); fragShader = CompileShaderText(GL_FRAGMENT_SHADER, FragShaderText); program = LinkShaders(vertShader, fragShader); glUseProgram(program); SetUniformValues(program, Uniforms); PrintUniforms(Uniforms); assert(glGetError() == 0); glClearColor(0.4f, 0.4f, 0.8f, 0.0f); printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER)); assert(glIsProgram(program)); assert(glIsShader(fragShader)); assert(glIsShader(vertShader)); glColor3f(1, 0, 0); }
void Graphics::LoadShaders() { mVertexShader = glCreateShader(GL_VERTEX_SHADER); mFragmentShader = glCreateShader(GL_FRAGMENT_SHADER); mBasicProgram = glCreateProgram(); //TODO: add delete commands in case these fail if( !CompileShader("vertexshader.vert", GL_VERTEX_SHADER, mVertexShader, mBasicProgram) ) return; //crash inc if( !CompileShader("fragmentshader.frag", GL_FRAGMENT_SHADER, mFragmentShader, mBasicProgram) ) return; //crash inc if( !LinkShaders( mBasicProgram ) ) return; //crash inc //mFragmentShader = glCreateShader(GL_FRAGMENT_SHADER); //const char* `VertexShader = CompileShader("vertexshader.vert", mVertexShader); //const char* FragmentShader = CompileShader("fragmentshader.frag", mFragmentShader); }
GLuint LoadShaderProgram(const char *pszFilename, std::string &infoLog) { infoLog.clear(); GLuint program = 0; std::string buffer; // Read the text file containing the GLSL shader program. // This file contains 1 vertex shader and 1 fragment shader. ReadTextFile(pszFilename, buffer); // Compile and link the vertex and fragment shaders. if (buffer.length() > 0) { const GLchar *pSource = 0; GLint length = 0; GLuint vertShader = 0; GLuint fragShader = 0; std::string::size_type vertOffset = buffer.find("[vert]"); std::string::size_type fragOffset = buffer.find("[frag]"); try { // Get the vertex shader source and compile it. // The source is between the [vert] and [frag] tags. if (vertOffset != std::string::npos) { vertOffset += 6; // skip over the [vert] tag pSource = reinterpret_cast<const GLchar *>(&buffer[vertOffset]); length = static_cast<GLint>(fragOffset - vertOffset); vertShader = CompileShader(GL_VERTEX_SHADER, pSource, length); } // Get the fragment shader source and compile it. // The source is between the [frag] tag and the end of the file. if (fragOffset != std::string::npos) { fragOffset += 6; // skip over the [frag] tag pSource = reinterpret_cast<const GLchar *>(&buffer[fragOffset]); length = static_cast<GLint>(buffer.length() - fragOffset - 1); fragShader = CompileShader(GL_FRAGMENT_SHADER, pSource, length); } // Now link the vertex and fragment shaders into a shader program. program = LinkShaders(vertShader, fragShader); } catch (const std::string &errors) { infoLog = errors; } } return program; }
void ShaderProgram::Init( StrongHandle<VertexShader> a_vp, StrongHandle<FragShader> a_fp ) { ASSERT( a_vp != nullptr ); ASSERT( a_fp != nullptr ); m_vp = a_vp; m_fp = a_fp; LinkShaders(); ShaderManager::Ref().RegisterShaderProgram(this, a_vp->GetFileName().c_str(), a_fp->GetFileName().c_str() ); }
static void Init(void) { /* Fragment shader: modulate raster color by texture, discard fragments * with alpha < 1.0 */ static const char *fragShaderText = "uniform sampler2D tex2d; \n" "void main() {\n" " vec4 c = texture2D(tex2d, gl_TexCoord[0].xy); \n" " if (c.w < 1.0) \n" " discard; \n" " gl_FragColor = c * gl_Color; \n" "}\n"; /* Vertex shader: compute new vertex position based on incoming vertex pos, * texcoords and special scale factor. */ static const char *vertShaderText = "uniform vec2 scale; \n" "void main() {\n" " vec4 p = gl_ModelViewProjectionMatrix * gl_Vertex;\n" " gl_Position.xy = p.xy + gl_MultiTexCoord0.xy * scale * p.w; \n" " gl_Position.zw = p.zw; \n" " gl_TexCoord[0] = gl_MultiTexCoord0; \n" " gl_FrontColor = gl_Color; \n" "}\n"; if (!ShadersSupported()) exit(1); VertShader = CompileShaderText(GL_VERTEX_SHADER, vertShaderText); FragShader = CompileShaderText(GL_FRAGMENT_SHADER, fragShaderText); Program = LinkShaders(VertShader, FragShader); glUseProgram(Program); uScale = glGetUniformLocation(Program, "scale"); uTex = glGetUniformLocation(Program, "tex2d"); if (uTex != -1) { glUniform1i(uTex, 0); /* tex unit 0 */ } glUseProgram(0); glClearColor(0.3f, 0.3f, 0.3f, 0.0f); glEnable(GL_DEPTH_TEST); glEnable(GL_NORMALIZE); glEnable(GL_LIGHT0); MakeBitmapTextures(); }
void ShaderProgram::Init( const char * a_filename_vp, const char* a_filename_fp ) { ASSERT( a_filename_vp ); ASSERT( a_filename_fp ); m_vp = new VertexShader(); m_vp->Init(a_filename_vp); m_fp = new FragShader(); m_fp->Init(a_filename_fp); LinkShaders(); ShaderManager::Ref().RegisterShaderProgram(this, a_filename_vp, a_filename_fp ); }
//Create a vertex and fragment shader void Shader::CreateShader(char *vertexPath, char *fragmentPath) { //Create a handle for the program m_ProgramHandle = glCreateProgram(); //Add vertex shader to the program AddShader(vertexPath, VERTEX_SHADER); //Add fragment shader to the program AddShader(fragmentPath, FRAGMENT_SHADER); //Link the shaders in the program and check for errors LinkShaders(); }
bool CGLComputeTechnique::Load(const CResourceObject& resource_obj) { Renderer::ShaderType t = resource_obj.m_ShaderType; CGLSLCompiler compiler; StGLSLCompilerOptions option; option.m_InputFile = resource_obj.m_Path; compiler.Initialize(option); m_Shaders[0] = compiler.Process(); LinkShaders(&m_Shaders[0], m_Shaders.size()); return true; }
void FSlateOpenGLElementProgram::CreateProgram( const FSlateOpenGLVS& VertexShader, const FSlateOpenGLPS& PixelShader ) { // Link the vertex and pixel shader for this program LinkShaders( VertexShader, PixelShader ); // Set up uniform parameters ViewProjectionMatrixParam = glGetUniformLocation( ProgramID, "ViewProjectionMatrix" ); VertexShaderParam = glGetUniformLocation( ProgramID, "VertexShaderParams" ); TextureParam = glGetUniformLocation( ProgramID, "ElementTexture" ); EffectsDisabledParam = glGetUniformLocation( ProgramID, "EffectsDisabled" ); IgnoreTextureAlphaParam = glGetUniformLocation( ProgramID, "IgnoreTextureAlpha" ); ShaderTypeParam = glGetUniformLocation( ProgramID, "ShaderType" ); MarginUVsParam = glGetUniformLocation( ProgramID, "MarginUVs" ); CHECK_GL_ERRORS; }
static void Init(void) { static const char *fragShaderText = "uniform ivec2 WinSize; \n" "void main() {\n" " vec2 d = dFdy(gl_TexCoord[0].xy) * vec2(WinSize); \n" " gl_FragColor = vec4(d.x, d.y, 0.0, 1.0);\n" " // gl_FragColor = gl_TexCoord[0];\n" "}\n"; static const char *vertShaderText = "void main() {\n" " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n" " gl_TexCoord[0] = gl_MultiTexCoord0;\n" "}\n"; if (!ShadersSupported()) exit(1); vertShader = CompileShaderText(GL_VERTEX_SHADER, vertShaderText); fragShader = CompileShaderText(GL_FRAGMENT_SHADER, fragShaderText); program = LinkShaders(vertShader, fragShader); glUseProgram(program); WinSizeUniform = glGetUniformLocation(program, "WinSize"); /*assert(glGetError() == 0);*/ glClearColor(0.3f, 0.3f, 0.3f, 0.0f); glEnable(GL_DEPTH_TEST); MakeSphere(); MakeRect(); CurList = SphereList; printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER)); assert(glIsProgram(program)); assert(glIsShader(fragShader)); assert(glIsShader(vertShader)); glColor3f(1, 0, 0); }
bool CGLRenderTechnique::Load( const CResourceObject& resource_obj ) { Renderer::ShaderType t = resource_obj.m_ShaderType; CGLSLCompiler compiler; StGLSLCompilerOptions option; option.m_InputFile = resource_obj.m_Path; compiler.Initialize(option); m_Shaders[t] = compiler.Process(); // PixelShader will be loaded at the last one, so perform linking step here if(t == Renderer::SHA_PIXEL_SHADER) { return LinkShaders(&m_Shaders[0], m_Shaders.size()); } return true; }
// -------------------------------------------------------------------------------------------------------------------- GLuint CreateProgram(const std::string& _vsFilename, const std::string& _psFilename, const std::string& _shaderPrefix) { GLuint vs = CompileShaderFromFile(GL_VERTEX_SHADER, _vsFilename, _shaderPrefix), fs = CompileShaderFromFile(GL_FRAGMENT_SHADER, _psFilename, _shaderPrefix); // If any are 0, dump out early. if ((vs * fs) == 0) { return 0; } GLuint retProgram = LinkShaders(vs, fs); // Flag these now, they're either attached (linked in) and will be cleaned up with the link, or the // link failed and we're about to lose track of them anyways. glDeleteShader(fs); glDeleteShader(vs); return retProgram; }
AnimatedSprite::AnimatedSprite(const char* a_pSpriteSheet, GLFWwindow * window) { GameWindow = window; m_dElapsedTime = 0; LoadVertShader("../resources/VertexShader.glsl"); LoadFragShader("../resources/FragShader.glsl"); LinkShaders(); GLint uvAttrib = glGetAttribLocation(m_ShaderProgram, "texcoord"); glEnableVertexAttribArray(uvAttrib); matrix_location = glGetUniformLocation(m_ShaderProgram, "matrix"); LoadSprites(a_pSpriteSheet); LoadAnimations(atlas.sAnimations.c_str()); m_dFrames = (1.0 / 15.0); sCurrentAnimation = ""; sCurrentSprite ="idle 0"; CurrentPlayType = SINGLE; m_uvScale.m_fX = atlas.v2Size.m_fY; m_uvScale.m_fY = atlas.v2Size.m_fX; SetSprite(); SetUVData(); }
Sprite::Sprite() { srand(time(NULL)); LoadVertShader("../resources/VertexShader.glsl"); LoadFragShader("../resources/FragmentShader.glsl"); LinkShaders(); m_v4SpriteColor = Vector4::Vector4(1.0f, 1.0f, 1.0f, 1.0f); m_aoVerts[0].Color = m_v4SpriteColor; m_aoVerts[1].Color = m_v4SpriteColor; m_aoVerts[2].Color = m_v4SpriteColor; m_aoVerts[3].Color = m_v4SpriteColor; m_aoVerts[0].UV = Vector2::Vector2(1.0f, 0.0f); m_aoVerts[1].UV = Vector2::Vector2(0.0f, 1.0f); m_aoVerts[2].UV = Vector2::Vector2(1.0f, 0.0f); m_aoVerts[3].UV = Vector2::Vector2(1.0f, 1.0f); m_v2Scale = Vector2::Vector2(1.0f, 1.0f); m_v3Position = Vector3::Vector3(g_gl_width / 2, g_gl_height / 2, 0.0f); }
AnimatedSprite::AnimatedSprite( const char* a_pSpriteSheet, GLFWwindow * window) { //Make GameWindow equal window GameWindow = window; //Make elapsedTime equal 0 elapsedTime = 0; //Load the Shaders LoadVertShader("resources/VertexShader.glsl"); LoadFragShader("resources/FragmentShader.glsl"); //Link the Shaders LinkShaders(); //Make a GLint and equal it to glGetAttribLocation GLint uvAttrib = glGetAttribLocation(m_ShaderProgram,"texcoord"); //Call glEnableVertexAttribArray with the required data glEnableVertexAttribArray(uvAttrib); //Make matrix_location equal to glGetUniformLocation matrix_location = glGetUniformLocation (m_ShaderProgram, "matrix"); //Load the Sprites,Animations, and Texture LoadSprites(a_pSpriteSheet); // LoadTexture(atlas.sSheet.c_str()); //Make m_dFrames equal to 1 divided by 2 m_dFrames = (1.0/2.0); //Make currentAnimation equal nothing currentAnimation = ""; //Make the currentSprite equal idle //currentSprite = "idle"; //Make the currentFrame equal 0 currentFrame = 0; //Make uvScale equale the atlas size m_uvScale.m_fX = atlas.v2Size.m_fY; m_uvScale.m_fY = atlas.v2Size.m_fX; //Call SetSprite SetSprite(); //call SetUVData SetUVData(); }
static void Init(void) { if (!ShadersSupported()) exit(1); vertShader = CompileShaderFile(GL_VERTEX_SHADER, VertProgFile); fragShader = CompileShaderFile(GL_FRAGMENT_SHADER, FragProgFile); program = LinkShaders(vertShader, fragShader); glUseProgram(program); SetUniformValues(program, Uniforms); PrintUniforms(Uniforms); assert(glGetError() == 0); glClearColor(0.4f, 0.4f, 0.8f, 0.0f); glEnable(GL_DEPTH_TEST); glColor3f(1, 0, 0); }
Sprite::Sprite( const char* a_pTexture, int a_iWidth, int a_iHeight, Vector4 a_v4Color , GLFWwindow * window) { GameWindow = window; //Default Shaders for Default constructor /* const char * VertexShader = // Vertex Shaders deal with objects in 3D space "#version 330\n" "in vec3 position;" "in vec4 color;" "in vec2 texcoord;" "out vec2 UV;" "out vec4 vColor;" "uniform mat4 matrix;" // our matrix "void main() {" " UV = texcoord;" " gl_Position = matrix * vec4 (position, 1.0);" " vColor = color;" "}"; const char * FragmentShader = // Fragment Shaders dela with pixel data "#version 330\n" "in vec4 vColor;" "in vec2 UV;" "out vec4 outColour;" "uniform sampler2D diffuseTexture;" "void main () {" " outColour = texture2D(diffuseTexture, UV) * vColor;" "}"; // Compile Vertex Shader m_VertexShader = glCreateShader(GL_VERTEX_SHADER); glShaderSource(m_VertexShader, 1, &VertexShader, NULL); glCompileShader(m_VertexShader); printShaderInfoLog(m_VertexShader); // Compile Fragment Shader m_FragmentShader = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(m_FragmentShader, 1, &FragmentShader, NULL); glCompileShader(m_FragmentShader); printShaderInfoLog(m_FragmentShader); */ LoadVertShader("../resources/exampleVert.glsl"); LoadFragShader("../resources/exampleFrag.glsl"); LinkShaders(); // Link Shaders into Shader Program m_ShaderProgram = glCreateProgram(); glAttachShader(m_ShaderProgram, m_FragmentShader); glAttachShader(m_ShaderProgram, m_VertexShader); glLinkProgram(m_ShaderProgram); printProgramInfoLog(m_ShaderProgram); glUseProgram(m_ShaderProgram); m_v4SpriteColor = a_v4Color; m_aoVerts[0].Pos = Vector3( -0.5f, 0.5f, 0.0f); m_aoVerts[1].Pos = Vector3( 0.5f, 0.5f, 0.0f); m_aoVerts[2].Pos = Vector3( -0.5f, -0.5f, 0.0f); m_aoVerts[3].Pos = Vector3( 0.5f, -0.5f, 0.0f); m_aoVerts[0].Color = m_v4SpriteColor; m_aoVerts[1].Color = m_v4SpriteColor; m_aoVerts[2].Color = m_v4SpriteColor; m_aoVerts[3].Color = m_v4SpriteColor; m_aoVerts[0].UV = Vector2(0.0f, 0.0f); m_aoVerts[1].UV = Vector2(0.0f, 1.0f); m_aoVerts[2].UV = Vector2(1.0f, 0.0f); m_aoVerts[3].UV = Vector2(1.0f, 1.0f); GLuint elements[] = { 0,1,2,3 }; //Gen Buffers glGenBuffers(1,&m_VBO); glGenBuffers(1,&m_EBO); glGenVertexArrays(1, &m_VAO); //Bind Buffers glBindVertexArray(m_VAO); glBindBuffer(GL_ARRAY_BUFFER, m_VBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_EBO); //Put Data into buffers glBufferData(GL_ARRAY_BUFFER, 4* sizeof(Vertex), m_aoVerts, GL_STATIC_DRAW); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW); //Enable some attributes GLint posAttrib = glGetAttribLocation(m_ShaderProgram,"position"); glEnableVertexAttribArray(posAttrib); GLint colAttrib = glGetAttribLocation(m_ShaderProgram,"color"); glEnableVertexAttribArray(colAttrib); GLint uvAttrib = glGetAttribLocation(m_ShaderProgram,"texcoord"); glEnableVertexAttribArray(uvAttrib); glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0); glVertexAttribPointer(colAttrib, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(3 * sizeof(float))); glVertexAttribPointer(uvAttrib, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(7 * sizeof(float))); glBindVertexArray(0); m_v3Position = Vector3(0,0,0); modelMatrix =new float[16]; float temp[]= { 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1 }; memcpy(modelMatrix,temp,16*sizeof(float)); //not sure of a better way matrix_location = glGetUniformLocation (m_ShaderProgram, "matrix"); m_uiTexture = 0; m_minUVCoords = Vector2( 0.f, 0.f ); m_maxUVCoords = Vector2( 1.f, 1.f ); m_uvScale = Vector2( 1.f, 1.f ); m_uvOffset = Vector2( 0.f, 0.f ); m_uSourceBlendMode = GL_SRC_ALPHA; m_uDestinationBlendMode = GL_ONE_MINUS_SRC_ALPHA; glGenTextures(1, &m_uiTexture); glActiveTexture (GL_TEXTURE0); int width, height; unsigned char* image = SOIL_load_image(a_pTexture, &width, &height, 0, SOIL_LOAD_RGBA); glBindTexture( GL_TEXTURE_2D,m_uiTexture); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image); SOIL_free_image_data(image); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); tex_loc = glGetUniformLocation (m_ShaderProgram, "diffuseTexture"); glUseProgram (m_ShaderProgram); glUniform1i (tex_loc, 0); // use active texture 0 }
void UpdateOCIOGLState() { // Step 0: Get the processor using any of the pipelines mentioned above. OCIO::ConstConfigRcPtr config = OCIO::GetCurrentConfig(); OCIO::DisplayTransformRcPtr transform = OCIO::DisplayTransform::Create(); transform->setInputColorSpaceName( g_inputColorSpace.c_str() ); transform->setDisplay( g_display.c_str() ); transform->setView( g_transformName.c_str() ); // Add optional transforms to create a full-featured, "canonical" display pipeline // Fstop exposure control (in SCENE_LINEAR) { float gain = powf(2.0f, g_exposure_fstop); const float slope4f[] = { gain, gain, gain, gain }; float m44[16]; float offset4[4]; OCIO::MatrixTransform::Scale(m44, offset4, slope4f); OCIO::MatrixTransformRcPtr mtx = OCIO::MatrixTransform::Create(); mtx->setValue(m44, offset4); transform->setLinearCC(mtx); } // Channel swizzling { float lumacoef[3]; config->getDefaultLumaCoefs(lumacoef); float m44[16]; float offset[4]; OCIO::MatrixTransform::View(m44, offset, g_channelHot, lumacoef); OCIO::MatrixTransformRcPtr swizzle = OCIO::MatrixTransform::Create(); swizzle->setValue(m44, offset); transform->setChannelView(swizzle); } // Post-display transform gamma { float exponent = 1.0f/std::max(1e-6f, static_cast<float>(g_display_gamma)); const float exponent4f[] = { exponent, exponent, exponent, exponent }; OCIO::ExponentTransformRcPtr expTransform = OCIO::ExponentTransform::Create(); expTransform->setValue(exponent4f); transform->setDisplayCC(expTransform); } OCIO::ConstProcessorRcPtr processor; try { processor = config->getProcessor(transform); } catch(OCIO::Exception & e) { std::cerr << e.what() << std::endl; return; } catch(...) { return; } // Step 1: Create a GPU Shader Description OCIO::GpuShaderDesc shaderDesc; shaderDesc.setLanguage(OCIO::GPU_LANGUAGE_GLSL_1_0); shaderDesc.setFunctionName("OCIODisplay"); shaderDesc.setLut3DEdgeLen(LUT3D_EDGE_SIZE); // Step 2: Compute the 3D LUT std::string lut3dCacheID = processor->getGpuLut3DCacheID(shaderDesc); if(lut3dCacheID != g_lut3dcacheid) { //std::cerr << "Computing 3DLut " << g_lut3dcacheid << std::endl; g_lut3dcacheid = lut3dCacheID; processor->getGpuLut3D(&g_lut3d[0], shaderDesc); glBindTexture(GL_TEXTURE_3D, g_lut3dTexID); glTexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, 0, LUT3D_EDGE_SIZE, LUT3D_EDGE_SIZE, LUT3D_EDGE_SIZE, GL_RGB,GL_FLOAT, &g_lut3d[0]); } // Step 3: Compute the Shader std::string shaderCacheID = processor->getGpuShaderTextCacheID(shaderDesc); if(g_program == 0 || shaderCacheID != g_shadercacheid) { //std::cerr << "Computing Shader " << g_shadercacheid << std::endl; g_shadercacheid = shaderCacheID; std::ostringstream os; os << processor->getGpuShaderText(shaderDesc) << "\n"; os << g_fragShaderText; //std::cerr << os.str() << std::endl; if(g_fragShader) glDeleteShader(g_fragShader); g_fragShader = CompileShaderText(GL_FRAGMENT_SHADER, os.str().c_str()); if(g_program) glDeleteProgram(g_program); g_program = LinkShaders(g_fragShader); } glUseProgram(g_program); glUniform1i(glGetUniformLocation(g_program, "tex1"), 1); glUniform1i(glGetUniformLocation(g_program, "tex2"), 2); }
void glmShaderMaterial(GLMmaterial *mat) { static const float ambientLight[4] = { 0.1, 0.1, 0.1, 0.0 }; static const float diffuseLight[4] = { 0.75, 0.75, 0.75, 1.0 }; static const float specularLight[4] = { 1.0, 1.0, 1.0, 0.0 }; if (!mat->prog) { /* make shader now */ char newShader[10000]; GLuint vs, fs; const char *diffuseTex = mat->texture_kd ? "true" : "false"; const char *specularTex = mat->texture_ks ? "true" : "false"; GLint uAmbientLight, uDiffuseLight, uSpecularLight; /* replace %d with 0 or 1 */ sprintf(newShader, TexFragmentShader, diffuseTex, specularTex); if (0) printf("===== new shader =====\n%s\n============\n", newShader); vs = CompileShaderText(GL_VERTEX_SHADER, VertexShader); fs = CompileShaderText(GL_FRAGMENT_SHADER, newShader); mat->prog = LinkShaders(vs, fs); assert(mat->prog); glUseProgram(mat->prog); mat->uAmbient = glGetUniformLocation(mat->prog, "ambient"); mat->uDiffuse = glGetUniformLocation(mat->prog, "diffuse"); mat->uSpecular = glGetUniformLocation(mat->prog, "specular"); mat->uShininess = glGetUniformLocation(mat->prog, "shininess"); mat->uDiffTex = glGetUniformLocation(mat->prog, "diffTex"); mat->uSpecTex = glGetUniformLocation(mat->prog, "specTex"); uAmbientLight = glGetUniformLocation(mat->prog, "ambientLight"); uDiffuseLight = glGetUniformLocation(mat->prog, "diffuseLight"); uSpecularLight = glGetUniformLocation(mat->prog, "specularLight"); glUniform4fv(mat->uAmbient, 1, mat->ambient); glUniform4fv(mat->uDiffuse, 1, mat->diffuse); glUniform4fv(mat->uSpecular, 1, mat->specular); glUniform1f(mat->uShininess, mat->shininess); glUniform1i(mat->uDiffTex, 0); glUniform1i(mat->uSpecTex, 1); glUniform4fv(uAmbientLight, 1, ambientLight); glUniform4fv(uDiffuseLight, 1, diffuseLight); glUniform4fv(uSpecularLight, 1, specularLight); } glActiveTexture(GL_TEXTURE1); if (mat->texture_ks) glBindTexture(GL_TEXTURE_CUBE_MAP, mat->texture_ks); else glBindTexture(GL_TEXTURE_CUBE_MAP, 0); glActiveTexture(GL_TEXTURE0); if (mat->texture_kd) glBindTexture(GL_TEXTURE_2D, mat->texture_kd); else glBindTexture(GL_TEXTURE_2D, 0); if (mat->diffuse[3] < 1.0) { glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); } else { glDisable(GL_BLEND); } glUseProgram(mat->prog); }
Sprite::Sprite( const char* a_pTexture, int a_iWidth, int a_iHeight, tbyte::Vector4 a_v4Color ,GLFWwindow * window) { GameWindow = window; LoadVertShader("../resources/exampleVert.glsl"); LoadFragShader("../resources/exampleFrag.glsl"); LinkShaders(); m_v4SpriteColor = a_v4Color; m_aoVerts[0].Pos = tbyte::Vector3( -0.5f, 0.5f, 0.0f); m_aoVerts[1].Pos = tbyte::Vector3( 0.5f, 0.5f, 0.0f); m_aoVerts[2].Pos = tbyte::Vector3( -0.5f, -0.5f, 0.0f); m_aoVerts[3].Pos = tbyte::Vector3( 0.5f, -0.5f, 0.0f); /* m_aoVerts[0].Color = tbyte::Vector4(1.0f, 0.0f, 0.0f, 1.0f); m_aoVerts[1].Color = tbyte::Vector4(0.0f, 1.0f, 0.0f, 1.0f); m_aoVerts[2].Color = tbyte::Vector4(0.0f, 0.0f, 1.0f, 1.0f); m_aoVerts[3].Color = tbyte::Vector4(1.0f, 1.0f, 1.0f, 1.0f); */ m_aoVerts[0].Color = m_v4SpriteColor; m_aoVerts[1].Color = m_v4SpriteColor; m_aoVerts[2].Color = m_v4SpriteColor; m_aoVerts[3].Color = m_v4SpriteColor; m_aoVerts[0].UV = tbyte::Vector2(0.0f, 0.0f); m_aoVerts[1].UV = tbyte::Vector2(0.0f, 1.0f); m_aoVerts[2].UV = tbyte::Vector2(1.0f, 0.0f); m_aoVerts[3].UV = tbyte::Vector2(1.0f, 1.0f); GLuint elements[] = { 0,1,2,3 }; //Gen Buffers glGenBuffers(1,&m_VBO); glGenBuffers(1,&m_EBO); glGenVertexArrays(1, &m_VAO); //Bind Buffers glBindVertexArray(m_VAO); glBindBuffer(GL_ARRAY_BUFFER, m_VBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_EBO); //Put Data into buffers glBufferData(GL_ARRAY_BUFFER, 4* sizeof(Vertex), m_aoVerts, GL_STATIC_DRAW); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW); //Enable some attributes GLint posAttrib = glGetAttribLocation(m_ShaderProgram,"position"); glEnableVertexAttribArray(posAttrib); GLint colAttrib = glGetAttribLocation(m_ShaderProgram,"color"); glEnableVertexAttribArray(colAttrib); GLint uvAttrib = glGetAttribLocation(m_ShaderProgram,"texcoord"); glEnableVertexAttribArray(uvAttrib); glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0); glVertexAttribPointer(colAttrib, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(3 * sizeof(float))); glVertexAttribPointer(uvAttrib, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(7 * sizeof(float))); glBindVertexArray(0); m_v2Scale = tbyte::Vector2(a_iWidth,a_iHeight); m_v3Position = tbyte::Vector3(1024/2,768/2,0); modelMatrix = new tbyte::Matrix4(); viewMatrix = new tbyte::Matrix4(); *modelMatrix = *viewMatrix = Matrix4::IdentityMatrix(); ViewLookAt(tbyte::Vector4(0,0,0,0),tbyte::Vector4(0,0,.5,0),tbyte::Vector4(0,1,0,0), viewMatrix); modelMatrix->m_afArray[12] = m_v3Position.m_fX; modelMatrix->m_afArray[13] = m_v3Position.m_fY; modelMatrix->m_afArray[14] = m_v3Position.m_fZ; matrix_location = glGetUniformLocation (m_ShaderProgram, "matrix"); m_uiTexture = 0; m_minUVCoords = tbyte::Vector2( 0.f, 0.f ); m_maxUVCoords = tbyte::Vector2( 1.f, 1.f ); m_uvScale = tbyte::Vector2( 1.f, 1.f ); m_uvOffset = tbyte::Vector2( 0.f, 0.f ); m_uSourceBlendMode = GL_SRC_ALPHA; m_uDestinationBlendMode = GL_ONE_MINUS_SRC_ALPHA; glGenTextures(1, &m_uiTexture); glActiveTexture (GL_TEXTURE0); int width, height; unsigned char* image = SOIL_load_image(a_pTexture, &width, &height, 0, SOIL_LOAD_RGBA); glBindTexture( GL_TEXTURE_2D,m_uiTexture); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image); SOIL_free_image_data(image); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); tex_location = glGetUniformLocation (m_ShaderProgram, "diffuseTexture"); proj_location = glGetUniformLocation (m_ShaderProgram, "projection"); view_location = glGetUniformLocation (m_ShaderProgram, "view"); }