glviewport(0, 0, w, h); h = 1;

Similar documents
CSE 4431/ M Advanced Topics in 3D Computer Graphics. TA: Margarita Vinnikov

Waves, Displacement, Reflections. Arun Rao CS 594

GLSL Introduction. Fu-Chung Huang. Thanks for materials from many other people

Precept 2 Aleksey Boyko February 18, 2011

Philip Calderon CSE 520 Lab 3 Color Shader

2a. The triangles scale increases (expands) when the 'e' key is pressed and decreases (contracts) when the 'c' key is pressed.

Erik Anchondo cse 520 lab 4

Tuesday, 23 March OpenGL Shading Language

GLSL. OpenGL Shading Language. OpenGL 1.5 Logical Diagram. OpenGL 2.0 Logical Diagram

1)Write a shader program that renders a regular pentagon with textured image like the one shown below.

CSE 4431/ M Advanced Topics in 3D Computer Graphics. TA: Margarita Vinnikov

Information Coding / Computer Graphics, ISY, LiTH GLSL. OpenGL Shading Language. Language with syntax similar to C

An Introduction to 3D Computer Graphics, Stereoscopic Image, and Animation in OpenGL and C/C++ Fore June

Programmable shader. Hanyang University

1. Write a shader program that animates the morphing of Figure 'A' to Figure 'B' and back.

Some advantages come from the limited environment! No classes. Stranight ans simple code. Remarkably. Avoids most of the bad things with C/C++.

1) Here is the various stages of the tetrahedron:

GLUT Tutorial. Keyboard

GLyphy. Behdad Esfahbod linux.conf.au, 8 January 2014, Perth, Australia. glyphy.org behdad.org

Information Coding / Computer Graphics, ISY, LiTH GLSL. OpenGL Shading Language. Language with syntax similar to C

Shader Programs. Lecture 30 Subsections 2.8.2, Robb T. Koether. Hampden-Sydney College. Wed, Nov 16, 2011

CSE 167: Introduction to Computer Graphics Lecture #13: GLSL. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2015

VTU EDUSAT PROGRAMME. Computer Graphics & Visualization

Duc Nguyen CSE 420 Computer Graphics 10/10/2018 Homework 1

C OMPUTER G RAPHICS Thursday

Supplement to Lecture 22

Introduction to Computer Graphics. Hardware Acceleration Review

Using OpenGL with CUDA

Programmable Graphics Hardware

Today. Rendering - III. Outline. Texturing: The 10,000m View. Texture Coordinates. Specifying Texture Coordinates in GL

FAKULTI TEKNOLOGI MAKLUMAT DAN KOMUNIKASI BITM INTERACTIVE COMPUTER GRAPHICS LAB SESSION 4. C++ - OpenGL

20 GLuint objects; 36 Scale += 0.1; 37 break; 38 case GLUT_KEY_DOWN:

Introduction to Shaders for Visualization. The Basic Computer Graphics Pipeline

To Do. Computer Graphics (Fall 2008) Course Outline. Course Outline. Methodology for Lecture. Demo: Surreal (HW 3)

compatibility mode core mode

Lecture 3. Understanding of OPenGL programming

12.2 Programmable Graphics Hardware

CSE 167: Lecture #8: GLSL. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2012

Objectives. Open GL Shading Language (GLSL)

Introduction to the OpenGL Shading Language (GLSL)

Programming shaders & GPUs Christian Miller CS Fall 2011

Computer Graphics (CS 543) Lecture 3b: Shader Setup & GLSL Introduction

// double buffering and RGB glutinitdisplaymode(glut_double GLUT_RGBA); // your own initializations

The Basic Computer Graphics Pipeline, OpenGL-style. Introduction to the OpenGL Shading Language (GLSL)

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 3: Shaders

To Do. Demo for mytest3. Methodology for Lecture. Importance of Lighting. Brief primer on Color. Foundations of Computer Graphics (Spring 2012)

Programming Graphics Hardware

3D computer graphics: geometric modeling of objects in the computer and rendering them


Programmable GPUs. Real Time Graphics 11/13/2013. Nalu 2004 (NVIDIA Corporation) GeForce 6. Virtua Fighter 1995 (SEGA Corporation) NV1

The Transition from RenderMan to the OpenGL Shading Language (GLSL)

Lecture 09: Shaders (Part 1)

CMPS160 Shader-based OpenGL Programming. All slides originally from Prabath Gunawardane, et al. unless noted otherwise

GLSL Introduction. Fu-Chung Huang. Thanks for materials from many other people

CS559: Computer Graphics. Lecture 12: OpenGL Transformation Li Zhang Spring 2008

Computer Graphics (4731) Lecture 4: 2D Graphics Systems (Drawing Polylines, tiling, & Aspect Ratio)

OpenGL refresher. Advanced Computer Graphics 2012

SHADER PROGRAMMING. Based on Jian Huang s lecture on Shader Programming

UNIT 7 LIGHTING AND SHADING. 1. Explain phong lighting model. Indicate the advantages and disadvantages. (Jun2012) 10M

Lab 9 - Metal and Glass

2 Transformations and Homogeneous Coordinates

GL_COLOR_BUFFER_BIT, GL_PROJECTION, GL_MODELVIEW

Shader Programming. Daniel Wesslén, Stefan Seipel, Examples

Teacher Assistant : Tamir Grossinger Reception hours: by - Building 37 / office -102 Assignments: 4 programing using

INTRODUCTION TO OPENGL PIPELINE

Outline. Other Graphics Technology. OpenGL Background and History. Platform Specifics. The Drawing Process

Graphics and Computation Introduction to OpenGL

To Do. Demo: Surreal (HW 3) This Lecture. Introduction to OpenGL. Outline. Foundations of Computer Graphics (Spring 2012)

Today. Texture mapping in OpenGL. Texture mapping. Basic shaders for texturing. Today. Computergrafik

OpenGL pipeline Evolution and OpenGL Shading Language (GLSL) Part 2/3 Vertex and Fragment Shaders

1 of 5 9/10/ :11 PM

CMSC 425: Lecture 4 More about OpenGL and GLUT Tuesday, Feb 5, 2013

Computer Graphics (CS 4731) Lecture 11: Implementing Transformations. Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

Books, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, PointClouds, and G3D

GLUT Tutorial. John Tsiombikas (Nuclear / The Lab Demos) May 6, Introduction to GLUT (OpenGL Utility Toolkit)

Levy: Constraint Texture Mapping, SIGGRAPH, CS 148, Summer 2012 Introduction to Computer Graphics and Imaging Justin Solomon

Order of Transformations

Graphics Processing Units (GPUs) V1.2 (January 2010) Outline. High-Level Pipeline. 1. GPU Pipeline:

CS559: Computer Graphics. Lecture 12: OpenGL Li Zhang Spring 2008

DONALD HOUSE: CPSC 8170, FALL 2018 DESIGN OF A REAL-TIME ANIMATION PROGRAM

Visualizing Molecular Dynamics

1 /* 4 C:\opencv\build\include. 6 C:\opencv\build\x86\vc10\lib

Yazhuo Liu Homework 3

Programmable Graphics Hardware

The Graphics Pipeline and OpenGL III: OpenGL Shading Language (GLSL 1.10)!

This Lecture. Why OpenGL? Introduction to OpenGL. Programmer s View

Lectures OpenGL Introduction

Objectives. Coupling GLSL to Applications Example applications

Computer Graphics. Transformations. CSC 470 Computer Graphics 1

Computergrafik. Matthias Zwicker Universität Bern Herbst 2016

Intro to GPU Programming (OpenGL Shading Language) Cliff Lindsay Ph.D. Student CS WPI

Programming with OpenGL Part 2: Complete Programs Computer Graphics I, Fall

CMPS160 Shader-based OpenGL Programming

Rendering Pipeline/ OpenGL

GLSL v1.20. Scott MacHaffie Schrödinger, Inc.

Programming with OpenGL Part 3: Three Dimensions

Computer Graphics (CS 4731) Lecture 11: Implementing Transformations. Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

Introduction to OpenGL

Ulf Assarsson Department of Computer Engineering Chalmers University of Technology

Transformation, Input and Interaction. Hanyang University

Transcription:

/* hello1.txt Simple Demo for GLSL www.lighthouse3d.com */ #include <stdio.h> #include <stdlib.h> #include <GL/glew.h> #include <GL/glut.h> #include "textfile.h" GLhandleARB v,f,f2,p; float lpos[4] = 1,0.5,1,0; void changesize(int w, int h) Prevent a divide by zero, when window is too short (you cant make a window of zero width). if(h == 0) h = 1; float ratio = 1.0* w / h; Reset the coordinate system before modifying glmatrixmode(gl_projection); glloadidentity(); Set the viewport to be the entire window glviewport(0, 0, w, h); Set the correct perspective. gluperspective(45,ratio,1,1000); glmatrixmode(gl_modelview); float a = 0; void renderscene(void) glclear(gl_color_buffer_bit GL_DEPTH_BUFFER_BIT); glloadidentity(); glulookat(0.0,0.0,5.0, 0.0,0.0,-1.0, 0.0f,1.0f,0.0f); gllightfv(gl_light0, GL_POSITION, lpos); glrotatef(a,0,1,1); glutsolidteapot(1); a+=0.1; hello1.txt glutswapbuffers(); void processnormalkeys(unsigned char key, int x, int y) if (key == 27) exit(0); #define printopenglerror() printoglerror( FILE, LINE ) int printoglerror(char *file, int line) Returns 1 if an OpenGL error occurred, 0 otherwise. GLenum glerr; int retcode = 0; glerr = glgeterror(); while (glerr!= GL_NO_ERROR) printf("glerror in file %s @ line %d: %s\n", file, line, gluerrorstring(glerr)); retcode = 1; glerr = glgeterror(); return retcode; void printinfolog(glhandlearb obj) int infologlength = 0; int charswritten = 0; char *infolog; glgetobjectparameterivarb(obj, GL_OBJECT_INFO_LOG_LENGTH_ARB, &infologlength); if (infologlength > 0) infolog = (char *)malloc(infologlength); glgetinfologarb(obj, infologlength, &charswritten, infolog); printf("%s\n",infolog); free(infolog); void setshaders() Page 2

hello1.txt char *vs = NULL,*fs = NULL,*fs2 = NULL; v = glcreateshaderobjectarb(gl_vertex_shader_arb); f = glcreateshaderobjectarb(gl_fragment_shader_arb); f2 = glcreateshaderobjectarb(gl_fragment_shader_arb); vs = textfileread("minimal.vert"); fs = textfileread("minimal.frag"); const char * vv = vs; const char * ff = fs; glshadersourcearb(v, 1, &vv,null); glshadersourcearb(f, 1, &ff,null); free(vs);free(fs); glcompileshaderarb(v); glcompileshaderarb(f); printinfolog(v); printinfolog(f); printinfolog(f2); p = glcreateprogramobjectarb(); glattachobjectarb(p,v); glattachobjectarb(p,f); gllinkprogramarb(p); printinfolog(p); gluseprogramobjectarb(p); int main(int argc, char **argv) glutinit(&argc, argv); glutinitdisplaymode(glut_depth GLUT_DOUBLE GLUT_RGBA); glutinitwindowposition(100,100); glutinitwindowsize(320,320); glutcreatewindow("mm 2004-05"); glutdisplayfunc(renderscene); glutidlefunc(renderscene); glutreshapefunc(changesize); glutkeyboardfunc(processnormalkeys); glenable(gl_depth_test); Page 3 hello1.txt glclearcolor(1.0,1.0,1.0,1.0); glenable(gl_cull_face); glewinit(); if (GLEW_ARB_vertex_shader && GLEW_ARB_fragment_shader) printf("ready for GLSL\n"); else printf("no GLSL support\n"); exit(1); setshaders(); glutmainloop(); return 0; minimal vertex shader www.lighthouse3d.com the following three lines provide the same result gl_position = gl_projectionmatrix * gl_modelviewmatrix * gl_vertex; gl_position = gl_modelviewprojectionmatrix * gl_vertex; minimal fragment shader www.lighthouse3d.com gl_fragcolor = vec4(0.4,0.4,0.8,1.0); gl_fragcolor = vec4(0.9,0.,0.,1.0); Page 4

/* Toon_Shader_3.txt Simple Demo for GLSL www.lighthouse3d.com */ #include <GL/glew.h> #include <GL/glut.h> #include <stdio.h> #include <stdlib.h> #include "textfile.h" GLint loc; GLhandleARB v,f,f2,p; float lpos[4] = 1.0,0.0,1.0,0.0; void changesize(int w, int h) Prevent a divide by zero, when window is too short (you cant make a window of zero width). if(h == 0) h = 1; float ratio = 1.0* w / h; Reset the coordinate system before modifying glmatrixmode(gl_projection); glloadidentity(); Set the viewport to be the entire window glviewport(0, 0, w, h); Set the correct perspective. gluperspective(45,ratio,1,100); glmatrixmode(gl_modelview); float a = 0; void renderscene(void) glclear(gl_color_buffer_bit GL_DEPTH_BUFFER_BIT); glloadidentity(); glulookat(0.0,5.0,5.0, 0.0,0.0,0.0, 0.0f,1.0f,0.0f); Toon_Shader_3.txt gllightfv(gl_light0, GL_POSITION, lpos); glrotatef(a,0,1,0); glutsolidteapot(1); a+=0.01; glutswapbuffers(); void processnormalkeys(unsigned char key, int x, int y) if (key == 27) exit(0); #define printopenglerror() printoglerror( FILE, LINE ) int printoglerror(char *file, int line) Returns 1 if an OpenGL error occurred, 0 otherwise. GLenum glerr; int retcode = 0; glerr = glgeterror(); while (glerr!= GL_NO_ERROR) printf("glerror in file %s @ line %d: %s\n", file, line, gluerrorstring(glerr)); retcode = 1; glerr = glgeterror(); return retcode; void printinfolog(glhandlearb obj) int infologlength = 0; int charswritten = 0; char *infolog; glgetobjectparameterivarb(obj, GL_OBJECT_INFO_LOG_LENGTH_ARB, &infologlength); if (infologlength > 0) infolog = (char *)malloc(infologlength); glgetinfologarb(obj, infologlength, &charswritten, infolog); printf("%s\n",infolog); free(infolog); Page 2

Toon_Shader_3.txt void setshaders() char *vs = NULL,*fs = NULL,*fs2 = NULL; v = glcreateshaderobjectarb(gl_vertex_shader_arb); f = glcreateshaderobjectarb(gl_fragment_shader_arb); f2 = glcreateshaderobjectarb(gl_fragment_shader_arb); vs = textfileread("toonf2.vert"); fs = textfileread("toonf2.frag"); const char * vv = vs; const char * ff = fs; glshadersourcearb(v, 1, &vv,null); glshadersourcearb(f, 1, &ff,null); free(vs);free(fs); glcompileshaderarb(v); glcompileshaderarb(f); printinfolog(v); printinfolog(f); printinfolog(f2); p = glcreateprogramobjectarb(); glattachobjectarb(p,v); glattachobjectarb(p,f); gllinkprogramarb(p); printinfolog(p); gluseprogramobjectarb(p); loc = glgetuniformlocationarb(p,"time"); int main(int argc, char **argv) glutinit(&argc, argv); glutinitdisplaymode(glut_depth GLUT_DOUBLE GLUT_RGBA); glutinitwindowposition(100,100); glutinitwindowsize(320,320); glutcreatewindow("lighthouse 3D"); glutdisplayfunc(renderscene); Page 3 Toon_Shader_3.txt glutidlefunc(renderscene); glutreshapefunc(changesize); glutkeyboardfunc(processnormalkeys); glenable(gl_depth_test); glclearcolor(1.0,1.0,1.0,1.0); glenable(gl_cull_face); glewinit(); if (GLEW_ARB_vertex_shader && GLEW_ARB_fragment_shader) printf("ready for GLSL\n"); else printf("no GLSL support\n"); exit(1); setshaders(); glutmainloop(); return 0; Vertex shader varying vec3 lightdir,normal; lightdir = normalize(vec3(gl_lightsource[0].position)); normal = gl_normalmatrix * gl_normal; gl_normalmatrix permet une transformation un objet dans le repère de la caméra. Pourquoi appliquer ici cette trransformation, c'est parce que la position de la source est automatiquement transformée dans le repère de la caméra. Frag shader varying vec3 lightdir,normal; float intensity; vec4 color; normalizing the lights position to be on the safe side Page 4

Toon_Shader_3.txt vec3 n = normalize(normal); intensity = dot(lightdir,n); if (intensity > 0.95) color = vec4(1.0,0.5,0.5,1.0); else if (intensity > 0.5) color = vec4(0.6,0.3,0.3,1.0); else if (intensity > 0.25) color = vec4(0.4,0.2,0.2,1.0); else color = vec4(0.2,0.1,0.1,1.0); gl_fragcolor = color; textfile.h: interface for reading and writing text files www.lighthouse3d.com You may use these functions freely. they are provided as is, and no warranties, either implicit, or explicit are given char *textfileread(char *fn); int textfilewrite(char *fn, char *s); texfile.cpp textfile.cpp simple reading and writing for text files www.lighthouse3d.com You may use these functions freely. they are provided as is, and no warranties, either implicit, or explicit are given #include <stdio.h> #include <stdlib.h> #include <string.h> char *textfileread(char *fn) FILE *fp; char *content = NULL; Page 5 int count=0; Toon_Shader_3.txt if (fn!= NULL) fp = fopen(fn,"rt"); if (fp!= NULL) fseek(fp, 0, SEEK_END); count = ftell(fp); rewind(fp); if (count > 0) content = (char *)malloc(sizeof(char) * (count+1)); count = fread(content,sizeof(char),count,fp); content[count] = '\0'; fclose(fp); return content; int textfilewrite(char *fn, char *s) FILE *fp; int status = 0; if (fn!= NULL) fp = fopen(fn,"w"); if (fp!= NULL) strlen(s)) return(status); if (fwrite(s,sizeof(char),strlen(s),fp) == status = 1; fclose(fp); Page 6

Vertex shader /* ------------------------------------------------------- This shader implements a directional light per vertex using the diffuse, specular, and ambient terms acoording to "Mathematics of Lighthing" as found in the book "OpenGL Programming Guide" (aka the Red Book) António Ramires Fernandes --------------------------------------------------------- */ vec3 normal, lightdir, viewvector, halfvector; vec4 diffuse, ambient, globalambient, specular = vec4(0.0); float NdotL,NdotHV; /* first transform the normal into eye space and normalize the result */ normal = normalize(gl_normalmatrix * gl_normal); /* now normalize the light's direction. Note that according to the OpenGL specification, the light is stored in eye space. Also since we're talking about a directional light, the position field is actually direction */ lightdir = normalize(vec3(gl_lightsource[0].position)); /* compute the cos of the angle between the normal and lights direction. The light is directional so the direction is constant for every vertex. Since these two are normalized the cosine is the dot product. We also need to clamp the result to the [0,1] range. */ NdotL = max(dot(normal, lightdir), 0.0); /* Compute the diffuse, ambient and globalambient terms */ diffuse = gl_frontmaterial.diffuse * gl_lightsource[0].diffuse; ambient = gl_frontmaterial.ambient * gl_lightsource[0].ambient; globalambient = gl_lightmodel.ambient * gl_frontmaterial.ambient; /* compute the specular term if NdotL is larger than zero */ if (NdotL > 0.0) NdotHV = max(dot(normal, normalize(gl_lightsource[0].halfvector.xyz)),0.0); specular = gl_frontmaterial.specular * gl_lightsource[0].specular * pow(ndothv,gl_frontmaterial.shininess); gl_frontcolor = globalambient + NdotL * diffuse + ambient + specular; file:/c /Akadi/Partition_E/COURS/OpenGL/Exemples_GL_SL/liste_exples_pdf/dirLightAmbDiffSpec.txt[03/03/2010 16:25:42] Frag shader gl_fragcolor = gl_color; file:/c /Akadi/Partition_E/COURS/OpenGL/Exemples_GL_SL/liste_exples_pdf/dirLightAmbDiffSpec.txt[03/03/2010 16:25:42]

Vertex shader pointlight.txt /* ------------------------------------------------------- This shader implements a point light per pixel using the diffuse, specular, and ambient terms acoording to "Mathematics of Lighthing" as found in the book "OpenGL Programming Guide" (aka the Red Book) António Ramires Fernandes --------------------------------------------------------- */ varying vec4 diffuse,ambientglobal,ambient; varying vec3 normal,lightdir,halfvector; varying float dist; vec4 ecpos; vec3 aux; /* first transform the normal into eye space and normalize the result */ normal = normalize(gl_normalmatrix * gl_normal); /* now normalize the light's direction. Note that according to the OpenGL specification, the light is stored in eye space. Also since we're talking about a directional light, the position field is actually direction */ ecpos = gl_modelviewmatrix * gl_vertex; aux = vec3(gl_lightsource[0].position-ecpos); lightdir = normalize(aux); /* compute the distance to the light source to a varying variable*/ dist = length(aux); /* Normalize the halfvector to pass it to the fragment shader */ halfvector = normalize(gl_lightsource[0].halfvector.xyz); /* Compute the diffuse, ambient and globalambient terms */ diffuse = gl_frontmaterial.diffuse * gl_lightsource[0].diffuse; ambient = gl_frontmaterial.ambient * gl_lightsource[0].ambient; ambientglobal = gl_lightmodel.ambient * gl_frontmaterial.ambient; pointlight.txt Frag shader /* ------------------------------------------------------- This shader implements a point light per pixel using the diffuse, specular, and ambient terms acoording to "Mathematics of Lighthing" as found in the book "OpenGL Programming Guide" (aka the Red Book) António Ramires Fernandes --------------------------------------------------------- */ varying vec4 diffuse,ambientglobal, ambient; varying vec3 normal,lightdir,halfvector; varying float dist; vec3 n,halfv,viewv,ldir; float NdotL,NdotHV; vec4 color = ambientglobal; float att; /* a fragment shader can't write a verying variable, hence we need a new variable to store the normalized interpolated normal */ n = normalize(normal); /* compute the dot product between normal and ldir */ NdotL = max(dot(n,normalize(lightdir)),0.0); if (NdotL > 0.0) + att = 1.0 / (gl_lightsource[0].constantattenuation gl_lightsource[0].linearattenuation * dist + gl_lightsource[0].quadraticattenuation * dist * dist); color += att * (diffuse * NdotL + ambient); halfv = normalize(halfvector); NdotHV = max(dot(n,halfv),0.0); color += att * gl_frontmaterial.specular * gl_lightsource[0].specular * pow(ndothv,gl_frontmaterial.shininess); Page 2

pointlight.txt gl_fragcolor = color; Page 3 Vertex shader spotlight.txt /* ------------------------------------------------------- This shader implements a spotlight per pixel using the diffuse, specular, and ambient terms acoording to "Mathematics of Lighthing" as found in the book "OpenGL Programming Guide" (aka the Red Book) Antonio Ramires Fernandes --------------------------------------------------------- */ varying vec4 diffuse,ambientglobal, ambient; varying vec3 normal,lightdir,halfvector; varying float dist; vec4 ecpos; vec3 aux; /* first transform the normal into eye space and normalize the result */ normal = normalize(gl_normalmatrix * gl_normal); /* now normalize the light's direction. Note that according to the OpenGL specification, the light is stored in eye space.*/ ecpos = gl_modelviewmatrix * gl_vertex; aux = vec3(gl_lightsource[0].position-ecpos); lightdir = normalize(aux); /* compute the distance to the light source to a varying variable*/ dist = length(aux); /* Normalize the halfvector to pass it to the fragment shader */ halfvector = normalize(gl_lightsource[0].halfvector.xyz); /* Compute the diffuse, ambient and globalambient terms */ diffuse = gl_frontmaterial.diffuse * gl_lightsource[0].diffuse; ambient = gl_frontmaterial.ambient * gl_lightsource[0].ambient; ambientglobal = gl_lightmodel.ambient * gl_frontmaterial.ambient;

spotlight.txt Frag shader /* ------------------------------------------------------- This shader implements a spotlight per pixel using the diffuse, specular, and ambient terms acoording to "Mathematics of Lighthing" as found in the book "OpenGL Programming Guide" (aka the Red Book) Antonio Ramires Fernandes --------------------------------------------------------- */ varying vec4 diffuse,ambientglobal, ambient; varying vec3 normal,lightdir,halfvector; varying float dist; vec3 n,halfv; float NdotL,NdotHV; vec4 color = ambientglobal; float att,spoteffect; /* a fragment shader can't write a verying variable, hence we need a new variable to store the normalized interpolated normal */ n = normalize(normal); /* compute the dot product between normal and ldir */ NdotL = max(dot(n,normalize(lightdir)),0.0); if (NdotL > 0.0) spoteffect = dot(normalize(gl_lightsource[0].spotdirection), normalize(-lightdir)); if (spoteffect > gl_lightsource[0].spotcoscutoff) spoteffect = pow(spoteffect, gl_lightsource[0].spotexponent); att = spoteffect / (gl_lightsource[0].constantattenuation + gl_lightsource[0].linearattenuation * dist + gl_lightsource[0].quadraticattenuation * dist * dist); ambient); color += att * (diffuse * NdotL + Page 2 spotlight.txt halfv = normalize(halfvector); NdotHV = max(dot(n,halfv),0.0); color += att * gl_frontmaterial.specular * gl_lightsource[0].specular * pow(ndothv,gl_frontmaterial.shininess); gl_fragcolor = color; Page 3

texturesimple.txt Vertex shader gl_texcoord[0] = gl_multitexcoord0; Frag shader uniform sampler2d tex; vec4 color = texture2d(tex,gl_texcoord[0].st); gl_fragcolor = color; texturecomb.txt varying vec3 lightdir,normal; normal = normalize(gl_normalmatrix * gl_normal); lightdir = normalize(vec3(gl_lightsource[0].position)); gl_texcoord[0] = gl_multitexcoord0; Frag shader uniform sampler2d tex; vec4 color = texture2d(tex,gl_texcoord[0].st); gl_fragcolor = color;

Vertex shader texturemulti.txt varying vec3 lightdir,normal; normal = normalize(gl_normalmatrix * gl_normal); lightdir = normalize(vec3(gl_lightsource[0].position)); gl_texcoord[0] = gl_multitexcoord0; Frag shader varying vec3 lightdir,normal; uniform sampler2d tex,l3d; vec3 ct,cf,c; vec4 texel; float intensity,at,af,a; intensity = max(dot(lightdir,normalize(normal)),0.0); cf = intensity * (gl_frontmaterial.diffuse).rgb + gl_frontmaterial.ambient.rgb; af = gl_frontmaterial.diffuse.a; texel = texture2d(tex,gl_texcoord[0].st); ct = texel.rgb; at = texel.a; c = cf * ct; a = af * at; float coef = smoothstep(1.0,0.2,intensity); c += coef * vec3(texture2d(l3d,gl_texcoord[0].st)); gl_fragcolor = vec4(c, a);