//- // ========================================================================== // Copyright (C) 2005 ATI Technologies Inc. All rights reserved. // // Copyright (C) 1995 - 2006 Autodesk, Inc. and/or its licensors. All // rights reserved. // // The coded instructions, statements, computer programs, and/or related // material (collectively the "Data") in these files contain unpublished // information proprietary to Autodesk, Inc. ("Autodesk") and/or its // licensors, which is protected by U.S. and Canadian federal copyright // law and by international treaties. // // The Data is provided for use exclusively by You. You have the right // to use, modify, and incorporate this Data into other products for // purposes authorized by the Autodesk software license agreement, // without fee. // // The copyright notices in the Software and this entire statement, // including the above license grant, this restriction and the // following disclaimer, must be included in all copies of the // Software, in whole or in part, and all derivative works of // the Software, unless such copies or derivative works are solely // in the form of machine-executable object code generated by a // source language processor. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND. // AUTODESK DOES NOT MAKE AND HEREBY DISCLAIMS ANY EXPRESS OR IMPLIED // WARRANTIES INCLUDING, BUT NOT LIMITED TO, THE WARRANTIES OF // NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR // PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE, OR // TRADE PRACTICE. IN NO EVENT WILL AUTODESK AND/OR ITS LICENSORS // BE LIABLE FOR ANY LOST REVENUES, DATA, OR PROFITS, OR SPECIAL, // DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES, EVEN IF AUTODESK // AND/OR ITS LICENSORS HAS BEEN ADVISED OF THE POSSIBILITY // OR PROBABILITY OF SUCH DAMAGES. // // ========================================================================== //+ #include "DefaultShader.h" // // Simple default GLSL shader that computes a directional light on a checker // pattern. The front and back colors are different, to help identify the surface. // //////////////////////////////////////////////////////////////////////////////// const char defaultShader::vShader[] = "varying vec3 eyeNormal;\n" "\n" "void main() {\n" " gl_Position = ftransform();\n" " eyeNormal = normalize(gl_NormalMatrix*gl_Normal);\n" " gl_FrontColor = vec4(0.0, 0.0, 1.0, 1.0);\n" " gl_BackColor = vec4(1.0, 0.0, 0.0, 1.0);\n" " gl_TexCoord[0] = gl_MultiTexCoord0*4.0;\n" "}\n\0"; const char defaultShader::fShader[] = "varying vec3 eyeNormal;\n" "\n" "void main(){ \n" "\n" "vec3 lightDirection = vec3( 0.5, 0.5, 0.5 );\n" "vec3 halfVector = normalize( lightDirection + vec3( 0.0, 0.0, 1.0 ));\n" "vec4 col = step(0.5,mod(gl_TexCoord[0],1.0));\n" "vec3 color = (abs(1.0-(col.r+col.g))==0.0) ? gl_Color.xyz : vec3(1.0);\n" "\n" "vec3 normal = normalize(eyeNormal);\n" "float dif = abs(dot(normal, lightDirection));\n" "dif = min( dif+0.2, 1.0);\n" "float spec = clamp( abs( dot( normal, halfVector) ), 0.0, 1.0);\n" "spec = pow(spec, 24.0);\n" "\n" "gl_FragColor = vec4( color*dif+spec, 1.0);\n" "}\n\0"; // // defaultShader constructor // // This constructor actually compiles and links the GLSL shader, // so it must be called with a current context active. //////////////////////////////////////////////////////////////////////////////// defaultShader::defaultShader() : m_program(0), m_fShader(0), m_vShader(0) { if (glShadingLanguageSupport) { m_program = glCreateProgramObject(); m_vShader = glCreateShaderObject( GL_VERTEX_SHADER); m_fShader = glCreateShaderObject( GL_FRAGMENT_SHADER); glAttachObject( m_program, m_vShader); glAttachObject( m_program, m_fShader); const char *temp = vShader; glShaderSource( m_vShader, 1, &temp, NULL); temp = fShader; glShaderSource( m_fShader, 1, &temp, NULL); GLint success; glCompileShader( m_vShader); glGetObjectParameteriv( m_vShader, GL_COMPILE_STATUS, &success); if (! success) { //figure out why we didn't compile char log[256]; //printf("Failed to compile vertex shader\n"); glGetInfoLog( m_vShader, 256, NULL, log); //printf(" Infolog: %s\n", log); } glCompileShader( m_fShader); glGetObjectParameteriv( m_fShader, GL_COMPILE_STATUS, &success); if (! success) { //figure out why we didn't compile char log[256]; //printf("Failed to compile fragment shader\n"); glGetInfoLog( m_fShader, 256, NULL, log); //printf(" Infolog: %s\n", log); } glLinkProgram( m_program); glGetObjectParameteriv( m_program, GL_LINK_STATUS, &success); if (! success) { //figure out why we didn't link char log[256]; //printf("Failed to link\n"); glGetInfoLog( m_program, 256, NULL, log); //printf(" Infolog: %s\n", log); } } } // // //////////////////////////////////////////////////////////////////////////////// void defaultShader::bind() { if (glShadingLanguageSupport) glUseProgramObject(m_program); } //////////////////////////////////////////////////////////////////////////////// void defaultShader::setShapeDependentState() { } // // //////////////////////////////////////////////////////////////////////////////// void defaultShader::unbind() { if (glShadingLanguageSupport) glUseProgramObject(0); }