コード例 #1
0
ファイル: noise.c プロジェクト: Distrotech/mesa-demos
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);
}
コード例 #2
0
ファイル: identity.c プロジェクト: Distrotech/mesa-demos
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);
}
コード例 #3
0
ファイル: bitmap.c プロジェクト: MttDs/new-rexeno-tindpe
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();
}
コード例 #4
0
ファイル: shaderutil.c プロジェクト: toastpp/toastpp
/**
 * Read a shader from a file.
 */
GLuint
CompileShaderFile(GLenum shaderType, const char *filename)
{
   const int max = 100*1000;
   int n;
   char *buffer = (char*) malloc(max);
   GLuint shader;
   FILE *f;

   Init();


   f = fopen(filename, "r");
   if (!f) {
      fprintf(stderr, "Unable to open shader file %s\n", filename);
      return 0;
   }

   n = fread(buffer, 1, max, f);
   /*printf("read %d bytes from shader file %s\n", n, filename);*/
   if (n > 0) {
      buffer[n] = 0;
      shader = CompileShaderText(shaderType, buffer);
   }
   else {
      return 0;
   }

   fclose(f);
   free(buffer);

   return shader;
}
コード例 #5
0
ファイル: deriv.c プロジェクト: aosm/X11apps
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);
}
コード例 #6
0
static void
Init(void)
{
   static const char *fragShaderText =
      "uniform float StipplePattern[16]; \n"
      "varying float stippleCoord; \n"
      "void main() \n"
      "{ \n"
      "   // check the stipple pattern and discard if value is zero \n"
      "   // TODO: we should really undo the perspective interpolation here \n"
      "   // so that it's linear. \n"
      "   float stip = StipplePattern[int(fract(stippleCoord) * 16.0)]; \n"
      "   if (stip == 0.0) \n"
      "      discard; \n"
      "   gl_FragColor = gl_Color; \n"
      "} \n";
   static const char *vertShaderText =
      "void main() \n"
      "{ \n"
      "   gl_FrontColor = gl_Color; \n"
      "   gl_Position = ftransform(); \n"
      "} \n";
   static const char *geomShaderText =
      "#version 120 \n"
      "#extension GL_ARB_geometry_shader4: enable \n"
      "uniform vec2 ViewportSize; \n"
      "uniform float StippleFactor; \n"
      "varying float stippleCoord; \n"
      "void main() \n"
      "{ \n"
      "   vec4 pos0 = gl_PositionIn[0]; \n"
      "   vec4 pos1 = gl_PositionIn[1]; \n"
      "   // Convert eye coords to window coords \n"
      "   // Note: we're off by a factor of two here, make up for that below \n"
      "   vec2 p0 = pos0.xy / pos0.w * ViewportSize; \n"
      "   vec2 p1 = pos1.xy / pos1.w * ViewportSize; \n"
      "   float len = length(p0.xy - p1.xy); \n"
      "   // Emit first vertex \n"
      "   gl_FrontColor = gl_FrontColorIn[0]; \n"
      "   gl_Position = pos0; \n"
      "   stippleCoord = 0.0; \n"
      "   EmitVertex(); \n"
      "   // Emit second vertex \n"
      "   gl_FrontColor = gl_FrontColorIn[1]; \n"
      "   gl_Position = pos1; \n"
      "   stippleCoord = len / StippleFactor / 32.0; // Note: not 16, see above \n"
      "   EmitVertex(); \n"
      "} \n";

   if (!ShadersSupported())
      exit(1);

   if (!glutExtensionSupported("GL_ARB_geometry_shader4")) {
      fprintf(stderr, "Sorry, GL_ARB_geometry_shader4 is not supported.\n");
      exit(1);
   }

   VertShader = CompileShaderText(GL_VERTEX_SHADER, vertShaderText);
   FragShader = CompileShaderText(GL_FRAGMENT_SHADER, fragShaderText);
   GeomShader = CompileShaderText(GL_GEOMETRY_SHADER_ARB, geomShaderText);
   assert(GeomShader);

   Program = LinkShaders3(VertShader, GeomShader, FragShader);
   assert(Program);
   CheckError(__LINE__);

   /*
    * The geometry shader accepts lines and produces lines.
    */
   glProgramParameteriARB(Program, GL_GEOMETRY_INPUT_TYPE_ARB,
                          GL_LINES);
   glProgramParameteriARB(Program, GL_GEOMETRY_OUTPUT_TYPE_ARB,
                          GL_LINE_STRIP);
   glProgramParameteriARB(Program, GL_GEOMETRY_VERTICES_OUT_ARB, 4);
   CheckError(__LINE__);

   glLinkProgramARB(Program);

   /* check link */
   {
      GLint stat;
      GetProgramiv(Program, GL_LINK_STATUS, &stat);
      if (!stat) {
         GLchar log[1000];
         GLsizei len;
         GetProgramInfoLog(Program, 1000, &len, log);
         fprintf(stderr, "Shader link error:\n%s\n", log);
      }
   }

   glUseProgram(Program);

   uViewportSize = glGetUniformLocation(Program, "ViewportSize");
   uStippleFactor = glGetUniformLocation(Program, "StippleFactor");
   uStipplePattern = glGetUniformLocation(Program, "StipplePattern");

   glUniform1f(uStippleFactor, StippleFactor);

   glClearColor(0.3f, 0.3f, 0.3f, 0.0f);

   printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));

   assert(glIsProgram(Program));
   assert(glIsShader(FragShader));
   assert(glIsShader(VertShader));
   assert(glIsShader(GeomShader));


   glLineStipple(StippleFactor, StipplePattern);
   SetStippleUniform(StippleFactor, StipplePattern);

   MakePointsVBO();
}
コード例 #7
0
static void
Init(void)
{
   static const char *fragShaderText =
      "void main() \n"
      "{ \n"
      "   gl_FragColor = gl_Color; \n"
      "} \n";
   static const char *vertShaderText =
      "void main() \n"
      "{ \n"
      "   gl_FrontColor = gl_Color; \n"
      "   gl_Position = ftransform(); \n"
      "} \n";
   static const char *geomShaderText =
      "#version 120 \n"
      "#extension GL_ARB_geometry_shader4: enable \n"
      "uniform vec2 InverseViewportSize; \n"
      "uniform float LineWidth; \n"
      "void main() \n"
      "{ \n"
      "   vec4 pos0 = gl_PositionIn[0]; \n"
      "   vec4 pos1 = gl_PositionIn[1]; \n"
      "   vec4 dir = abs(pos1 - pos0); \n"
      "   vec2 d0 = vec2(LineWidth * pos0.w) * InverseViewportSize; \n"
      "   vec2 d1 = vec2(LineWidth * pos1.w) * InverseViewportSize; \n"
      "   // this conditional could be avoided \n"
      "   if (dir.x > dir.y) { \n"
      "      // X-major line \n"
      "      d0.x = 0.0; \n"
      "      d1.x = 0.0; \n"
      "   } \n"
      "   else { \n"
      "      // Y-major line \n"
      "      d0.y = 0.0; \n"
      "      d1.y = 0.0; \n"
      "   } \n"
      "   gl_FrontColor = gl_FrontColorIn[0]; \n"
      "   gl_TexCoord[0] = vec4(0, 0, 0, 1); \n"
      "   gl_Position = pos0 + vec4( d0.x, -d0.y, 0, 0); \n"
      "   EmitVertex(); \n"
      "   gl_FrontColor = gl_FrontColorIn[1]; \n"
      "   gl_TexCoord[0] = vec4(1, 0, 0, 1); \n"
      "   gl_Position = pos1 + vec4( d1.x, -d1.y, 0, 0); \n"
      "   EmitVertex(); \n"
      "   gl_FrontColor = gl_FrontColorIn[0]; \n"
      "   gl_TexCoord[0] = vec4(0, 1, 0, 1); \n"
      "   gl_Position = pos0 + vec4(-d0.x,  d0.y, 0, 0); \n"
      "   EmitVertex(); \n"
      "   gl_FrontColor = gl_FrontColorIn[1]; \n"
      "   gl_TexCoord[0] = vec4(1, 1, 0, 1); \n"
      "   gl_Position = pos1 + vec4(-d1.x,  d1.y, 0, 0); \n"
      "   EmitVertex(); \n"
      "} \n";

   if (!ShadersSupported())
      exit(1);

   if (!glutExtensionSupported("GL_ARB_geometry_shader4")) {
      fprintf(stderr, "Sorry, GL_ARB_geometry_shader4 is not supported.\n");
      exit(1);
   }

   VertShader = CompileShaderText(GL_VERTEX_SHADER, vertShaderText);
   FragShader = CompileShaderText(GL_FRAGMENT_SHADER, fragShaderText);
   GeomShader = CompileShaderText(GL_GEOMETRY_SHADER_ARB, geomShaderText);
   assert(GeomShader);

   Program = LinkShaders3(VertShader, GeomShader, FragShader);
   assert(Program);
   CheckError(__LINE__);

   /*
    * The geometry shader will convert incoming lines to quads (4-vertex
    * triangle strips).
    */
   glProgramParameteriARB(Program, GL_GEOMETRY_INPUT_TYPE_ARB,
                          GL_LINES);
   glProgramParameteriARB(Program, GL_GEOMETRY_OUTPUT_TYPE_ARB,
                          GL_TRIANGLE_STRIP);
   glProgramParameteriARB(Program,GL_GEOMETRY_VERTICES_OUT_ARB, 4);
   CheckError(__LINE__);

   glLinkProgramARB(Program);

   /* check link */
   {
      GLint stat;
      GetProgramiv(Program, GL_LINK_STATUS, &stat);
      if (!stat) {
         GLchar log[1000];
         GLsizei len;
         GetProgramInfoLog(Program, 1000, &len, log);
         fprintf(stderr, "Shader link error:\n%s\n", log);
      }
   }

   CheckError(__LINE__);

   glUseProgram(Program);
   CheckError(__LINE__);

   uInverseViewportSize = glGetUniformLocation(Program, "InverseViewportSize");
   uLineWidth = glGetUniformLocation(Program, "LineWidth");

   glClearColor(0.3f, 0.3f, 0.3f, 0.0f);

   printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));

   assert(glIsProgram(Program));
   assert(glIsShader(FragShader));
   assert(glIsShader(VertShader));
   assert(glIsShader(GeomShader));

   glEnable(GL_DEPTH_TEST);

   {
      GLfloat r[2];
      glGetFloatv(GL_LINE_WIDTH_RANGE, r);
      MaxLineWidth = r[1];
   }

   MakePointsVBO();
}
コード例 #8
0
ファイル: main.cpp プロジェクト: EfestoLab/OpenColorIO
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);
}
コード例 #9
0
static void
Init(void)
{
   const GLubyte *version;
   static const char *vertShaderText =
      "#version 150 \n"
      "uniform mat4 ModelViewProjection; \n"
      "in vec4 Vertex; \n"
      "void main() \n"
      "{ \n"
      "   gl_Position = ModelViewProjection * Vertex; \n"
      "} \n";
   static const char *geomShaderText =
      "#version 150 \n"
      "layout(triangles) in; \n"
      "layout(triangle_strip, max_vertices = 3) out; \n"
      "uniform vec2 ViewportSize; \n"
      "out vec2 Vert0, Vert1, Vert2; \n"
      "\n"
      "// Transform NDC coord to window coord \n"
      "vec2 vpxform(vec4 p) \n"
      "{ \n"
      "   return (p.xy / p.w + 1.0) * 0.5 * ViewportSize; \n"
      "} \n"
      "\n"
      "void main() \n"
      "{ \n"
      "   Vert0 = vpxform(gl_in[0].gl_Position); \n"
      "   Vert1 = vpxform(gl_in[1].gl_Position); \n"
      "   Vert2 = vpxform(gl_in[2].gl_Position); \n"
      "   gl_Position = gl_in[0].gl_Position; \n"
      "   EmitVertex(); \n"
      "   gl_Position = gl_in[1].gl_Position; \n"
      "   EmitVertex(); \n"
      "   gl_Position = gl_in[2].gl_Position; \n"
      "   EmitVertex(); \n"
      "} \n";
   static const char *fragShaderText =
      "#version 150 \n"
      "#define LINE_WIDTH 2.5 \n"
      "uniform vec4 Color; \n"
      "in vec2 Vert0, Vert1, Vert2; \n"
      "out vec4 FragColor; \n"
      "// Compute distance from a point to a line \n"
      "float point_line_dist(vec2 p, vec2 v1, vec2 v2) \n"
      "{ \n"
      "   float s = (v2.x - v1.x) * (v1.y - p.y) - (v1.x - p.x) * (v2.y - v1.y); \n"
      "   float t = length(v2 - v1); \n"
      "   return abs(s) / t; \n"
      "} \n"
      "\n"
      "void main() \n"
      "{ \n"
      "   float d0 = point_line_dist(gl_FragCoord.xy, Vert0, Vert1); \n"
      "   float d1 = point_line_dist(gl_FragCoord.xy, Vert1, Vert2); \n"
      "   float d2 = point_line_dist(gl_FragCoord.xy, Vert2, Vert0); \n"
      "   float m = min(d0, min(d1, d2)); \n"
      "   FragColor = Color * smoothstep(0.0, LINE_WIDTH, m); \n"
      "} \n";
   static const GLfloat verts[3][2] = {
      { -1, -1 },
      {  1, -1 },
      {  0,  1 }
   };

   if (!ShadersSupported())
      exit(1);

   if (!GLEW_VERSION_3_2) {
      fprintf(stderr, "Sorry, OpenGL 3.2 or later required.\n");
      exit(1);
   }

   VertShader = CompileShaderText(GL_VERTEX_SHADER, vertShaderText);
   FragShader = CompileShaderText(GL_FRAGMENT_SHADER, fragShaderText);
   GeomShader = CompileShaderText(GL_GEOMETRY_SHADER, geomShaderText);

   Program = LinkShaders3(VertShader, GeomShader, FragShader);
   assert(Program);
   CheckError(__LINE__);

   glBindAttribLocation(Program, 0, "Vertex");
   glBindFragDataLocation(Program, 0, "FragColor");

   /* relink */
   glLinkProgram(Program);

   assert(glIsProgram(Program));
   assert(glIsShader(FragShader));
   assert(glIsShader(VertShader));
   assert(glIsShader(GeomShader));

   glUseProgram(Program);

   uViewportSize = glGetUniformLocation(Program, "ViewportSize");
   uModelViewProj = glGetUniformLocation(Program, "ModelViewProjection");
   uColor = glGetUniformLocation(Program, "Color");

   glUniform4fv(uColor, 1, Orange);

   glGenBuffers(1, &vbo);
   glBindBuffer(GL_ARRAY_BUFFER, vbo);
   glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);

   glGenVertexArrays(1, &vao);
   glBindVertexArray(vao);

   glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);
   glEnableVertexAttribArray(0);

   glClearColor(0.3f, 0.3f, 0.3f, 0.0f);
   glEnable(GL_DEPTH_TEST);

   printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
}
コード例 #10
0
ファイル: glmdraw.c プロジェクト: JoakimSoderberg/mesademos
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);
}