Shader. Original Rendering Pipeline Vertex Shader Vertex transformation Normal transformation & normalization Texture coordinate generation & transformation.

Slides:



Advertisements
Similar presentations
EECS 700: Computer Modeling, Simulation, and Visualization Dr. Shontz Chapter 2: Shader Fundamentals (continued)
Advertisements

GLSL Basics Discussion Lecture for CS 418 Spring 2015 TA: Zhicheng Yan, Sushma S Kini, Mary Pietrowicz.
Informationsteknologi Wednesday, December 12, 2007Computer Graphics - Class 171 Today’s class OpenGL Shading Language.
MAT 594CM S10Fundamentals of Spatial ComputingAngus Forbes Week 4 : GLSL Shaders Topics: Shader programs, vertex & fragment shaders, passing data into.
MAT 594CM S10Fundamentals of Spatial ComputingAngus Forbes Week 5 : GLSL Shaders Topics: Shader syntax, passing textures into shaders, per-pixel lighting,
GLSL II Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University of New.
Draw a Simple Object. Example 1/4 #include “ glut.h ” void display(); void reshape(GLsizei w, GLsizei h); void main(int argc, char** argv){ glutInit(&argc,
GLSL I May 28, 2007 (Adapted from Ed Angel’s lecture slides)
Hidden Surfaces and Shading CS BSP Tree T1T1 T2T2 E if (f 1 (E) < 0) then draw T 1 draw T 2 else draw T 2 draw T 1 f 1 (p) = 0 is the.
GLSL I Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University of New Mexico.
OpenGL and Projections
CH7 Buffers and Blending. Blending Example 1/6 #include #include "glut.h" GLfloat alpha = 0.0; GLfloat pos[4] = {0, 10, 10, 0}; GLfloat dif_l[4] = {1.0,
Mohan Sridharan Based on slides created by Edward Angel GLSL I 1 CS4395: Computer Graphics.
OpenGL Tutorial Jason Lawrence COS426 Precept Notes 2/16/04.
Lighting & Material. Example 1/5 #include ” glut.h ” void display(); void reshape(int, int); void lighting(); int main(int argc, char** argv) { glutInit(&argc,
Michael Robertson Yuta Takayama. WebGL is OpenGL on a web browser. OpenGL is a low-level 3D graphics API Basically, WebGL is a 3D graphics API that generates.
1 Graphics CSCI 343, Fall 2013 Lecture 20 Lighting and Shading III.
Programmable Pipelines. Objectives Introduce programmable pipelines ­Vertex shaders ­Fragment shaders Introduce shading languages ­Needed to describe.
Computing & Information Sciences Kansas State University Lecture 13 of 42CIS 636/736: (Introduction to) Computer Graphics Lecture 13 of 42 Wednesday, 20.
OpenGL Shading Language (Advanced Computer Graphics) Ernest Tatum.
ECSE-4750 Computer Graphics Fall 2004 Prof. Michael Wozny TA. Abhishek Gattani TA. Stephen
Foundations of Computer Graphics (Fall 2012) CS 184, Lecture 6: OpenGL 1
Real-Time High Quality Rendering CSE 291 [Winter 2015], Lecture 4 Brief Intro to Programmable Shaders
Lecture by: Martin Deschamps CSE 4431
CG1 Labs Wei Li. Back Face Culling // enable back-face culling glEnable( GL_CULL_FACE ); // orientation of front-facing polygons glFrontFace( GL_CCW );
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Shading I Ed Angel Professor of Computer Science, Electrical and Computer Engineering,
OpenGL Shading Language Jian Huang CS594, Spring 2005.
GRAPHICS PIPELINE & SHADERS SET09115 Intro to Graphics Programming.
Programmable Pipelines Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University.
OpenGL Shader Language Vertex and Fragment Shading Programs.
CH8 Frame Buffer Object 1. Introduction Name OpenGL :Frame Buffer Object DirectX:Render Target Usage Render to Texture 2.
OpenGL Lighting Jian-Liang Lin 2002 Hidden-Surface Removal -1 Original Code: while (1) { get_viewing_point_from_mouse_position(); glClear(GL_COLOR_BUFFER_BIT);
Draw a Simple Object. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending.
Shaders in OpenGL Marshall Hahn. Introduction to Shaders in OpenGL In this talk, the basics of OpenGL Shading Language will be covered. This includes.
CS418 Computer Graphics John C. Hart
Shading in OpenGL Ed Angel Professor Emeritus of Computer Science University of New Mexico 1 E. Angel and D. Shreiner: Interactive Computer Graphics 6E.
OpenGL Shading Language (GLSL)
CSE 381 – Advanced Game Programming GLSL. Rendering Revisited.
Viewing and Transformation. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture.
OpenGL Shading Language (GLSL)
Projections. Viewports Windows can have separate viewports void glViewport(GLint x, GLint y, GLsizei width, GLsizei height ) x, y - Specify the lower.
1 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Programming with OpenGL Part 2: Complete Programs Ed Angel Professor.
GLSL II.
GLSL II Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University of New.
CH7 Buffers and Blending. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending.
实验 2 设计一个真实感图形显示系统. 选题  60 分:有灯光、材质、纹理 (p203,p206) 的场景  80 分:+交互控制场景( figure11.1.c )  100 分:+读入多边形物体+ Bezier 曲面.
Week 3 Lecture 4: Part 2: GLSL I Based on Interactive Computer Graphics (Angel) - Chapter 9.
Programmable Pipelines. 2 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Black Box View Geometry Processor Frame Buffer Fragment Processor.
Programming with OpenGL Part 5: More GLSL Isaac Gang University of Mary Hardin-Baylor E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley.
 Learn some important functions and process in OpenGL ES  Draw some triangles on the screen  Do some transformation on each triangle in each frame.
What are shaders? In the field of computer graphics, a shader is a computer program that runs on the graphics processing unit(GPU) and is used to do shading.
OpenGL Shading Language
OpenGL Shading Language (GLSL)
Programming with OpenGL Part 3: Shaders Ed Angel Professor of Emeritus of Computer Science University of New Mexico 1 E. Angel and D. Shreiner: Interactive.
OpenGl Shaders Lighthouse3d.com.
GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global.
Shader.
GLSL II Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University of New.
GLSL I Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University of New Mexico.
Introduction to Computer Graphics with WebGL
Lighting and Shading Lab 8:.
CSC461: Lecture 24 Lighting and Shading in OpenGL
Lighting and Shading Lab 8:.
Programming with OpenGL Part 3: Shaders
Mickaël Sereno Shaders Mickaël Sereno 25/04/2019 Mickaël Sereno -
CIS 441/541: Introduction to Computer Graphics Lecture 15: shaders
CS 480/680 Computer Graphics GLSL Overview.
Computer Graphics Shading in OpenGL
Shading in OpenGL Ed Angel Professor Emeritus of Computer Science
CS 480/680 Fall 2011 Dr. Frederick C Harris, Jr. Computer Graphics
Presentation transcript:

Shader

Original Rendering Pipeline

Vertex Shader Vertex transformation Normal transformation & normalization Texture coordinate generation & transformation Per-vertex processing

Fragment (pixel) Shader Operations on interpolated values Texture access Texture application Color sum

Shading languages DirectX ’ s High Level Shading Language NVidia ’ s Cg ATI ’ s RenderMonkey OpenGL Shading Language (GLSL)

Introduction of GLSL Data Type Vertex Shader Fragment Shader

GLSL Language Definition Data Type Description intInteger floatFloating-point boolBoolean (true or false). vec2Vector with two floats. vec3Vector with three floats. vec4Vector with four floats. mat22x2 floating-point matrix. mat33x3 floating-point matrix. mat44x4 floating-point matrix.

Vector Vector is like a structure You can use following to access.r.g.b.a.x.y.z.w.s.t.p.q Example: vec4 color; color.rgb = vec3(1.0, 1.0, 0.0 ); color.a = 0.5 or color = vec4(1.0, 1.0, 0.0, 0.5); or color.xy = vec2(1.0, 1.0); color.zw =vec2(0.0, 0.5);

Addition data type : Texture Sampler sampler{1,2,3}D sampler{1,2}DShadow samplerCube sampler{1,2,3}D Texture unit to access the content of texture. sampler*DShadow The depth texture for shadow map. samplerCube The cube map.

Addition data type struct, array Similar to C. No union, enum, class

Qualifiers Used to management the input and output of shaders. attribute Communicate changing variables from the application to a vertex shader. uniform Communicate changing variables from the application to any shader. varying Communicate interpolated variables from a vertex shader to a fragment shader

Qualifiers Vertex Shader Fragment Shader OpenGL Application uniform attribute varying

Vertex Shader

Fragment Shader

Vertex Shader Code Example void main(void) { vec3 v3Normal; float fAngle; float fShininessFactor; v3Normal = gl_NormalMatrix * gl_Normal; v3Normal = normalize(v3Normal); fAngle = max(0.0, dot(v3Normal, vec3(gl_LightSource[0].halfVector))); fShininessFactor = pow(fAngle, gl_FrontMaterial.shininess); gl_FrontColor = gl_LightSource[0].ambient * gl_FrontMaterial.ambient + gl_LightSource[0].diffuse * gl_FrontMaterial.diffuse * fAngle + gl_LightSource[0].specular * gl_FrontMaterial.specular * fShininessFactor; gl_Position = ftransform(); }

Fragment Shader Code Example void main(void) { gl_FragColor = gl_Color; }

Result OpenGL Gouraud ShadingGLSL Phong Shading

Use GLSL in OpenGL You need those head and library files glew.h wglew.h glew32.lib glew32s.lib glew32.dll texture glaux.h glaux.lib glaux.dll

How to Use Shade? Steps to create and use shaders 1. Create shader objects. 2. Load the source code for each shader object. 3. Compile all the source code. 4. Create a program object. 5. Attach shader objects to the program object. 6. Link the program object. 7. Use the program. 8. Attach shader variables with application

Step 1 : Create shader objects GLhandleARB glCreateShaderObjectARB(GLenumshaderType) This function create an empty shader object. shaderType can be GL_VERTEX_SHADER_ARB or GL_FRAGMENT_SHADER_ARB, which specifies the shader type to be vertex shader or fragment shader. The handle of the shader is returned.

Step 2 : Load source code void glShaderSourceARB(GLhandleARB shader, GLuint nstrings, const GLcharARB **strings, Glint *lengths) To load the source code of a shader from the array of strings. nstrings is the number of strings in the array. strings is the array of strings. length is an array of integer as the length of each string.

Step 3 : Compile the source code To compile the source code. The status of the compilation can be queried by function glGetObjectParameterARB() with argument GL_OBJECT_COMPILE_STATUS_ARB and the error message can be obtained by function glGetInfoLogARB().

Step 4 : Create program object GLhandleARB glCreateProgramObjectARB(void); To create an empty program object and return its handle.

Step 5 : Attach shader objects void glAttachObjectARB(GLhandleARB program, GLhandleARB shader) To attach a shader object to a program object. A program object may contains several shader objects. Vertex shaders and fragment shaders can be attached with the same program object. OpenGL can execute only one program each time

Step 6 : Link the program object void glLinkProgramARB(GLhandleARB program) To link the program. the link status and the error message can be obtained in the same way as compilation.

Step 7 : Use the program Void glUseProgramObjectARB(GLhandle ARB program) Tells OpenGL to use the program to replace the fixed-function pipeline. You can call glUseProgramObjectARB( NULL ) to disable the shaders at any time.

Example Code : ShaderLoader.h 1/4 #include #include "glew.h" #include "wglew.h" #include "glut.h" bool ShaderLoad(GLhandleARB h_program, char* shader_file, GLenum shader_type) { FILE *fp; GLhandleARB h_shader; GLcharARB *shader_string; GLint str_length, maxLength; GLint isCompiled = GL_FALSE, isLinked = GL_FALSE; GLcharARB *pInfoLog;

ShaderLoader.h 2/4 // open the file of shader source code if((fp=fopen(shader_file, "r")) == NULL) { fprintf(stderr, "Error : Failed to read the OpenGL shader source \"%s\".\n", shader_file); return false; } // allocate memory for program string and load it. shader_string = (GLcharARB*) malloc(sizeof(GLcharARB) * 65536); str_length = (GLint) fread(shader_string, 1, 65536, fp); fclose(fp); // Create and load shader string. h_shader = glCreateShaderObjectARB(shader_type); //Step 1 if(h_shader == 0) { fprintf(stderr, "Error : Failed to create OpenGL shader object \"%s\".\n", shader_file); return false; } glShaderSourceARB(h_shader, 1, (const GLcharARB**)&shader_string, &str_length); //Step 2 free(shader_string);

ShaderLoader.h 3/4 // Compile the vertex shader, print out the compiler log message. glCompileShaderARB(h_shader); //Step 3 // get compile state information glGetObjectParameterivARB(h_shader, GL_OBJECT_COMPILE_STATUS_ARB, &isCompiled); if(!isCompiled) { fprintf(stderr, "Error : Failed to compile OpenGL shader source \"%s\".\n", shader_file); glGetObjectParameterivARB(h_shader, GL_OBJECT_INFO_LOG_LENGTH_ARB, &maxLength); pInfoLog = (GLcharARB *) malloc(maxLength * sizeof(GLcharARB)); glGetInfoLogARB(h_shader, maxLength, &str_length, pInfoLog); fprintf(stderr, "%s\n", pInfoLog); free(pInfoLog); return false; } glAttachObjectARB(h_program, h_shader); //Step 5

ShaderLoader.h 4/4 // delete the shader object, since we have attached it with the program object. glDeleteObjectARB(h_shader); // Link the program and print out the linker log message glLinkProgramARB(h_program); //Step 6 glGetObjectParameterivARB(h_program, GL_OBJECT_LINK_STATUS_ARB, &isLinked); if(!isLinked) { fprintf(stderr, "Error : Failed to link OpenGL shader \"%s\".\n", shader_file); glGetObjectParameterivARB(h_program, GL_OBJECT_INFO_LOG_LENGTH_ARB, &maxLength); pInfoLog = (GLcharARB *) malloc(maxLength * sizeof(GLcharARB)); glGetInfoLogARB(h_program, maxLength, &str_length, pInfoLog); fprintf(stderr, "%s\n", pInfoLog); free(pInfoLog); return false; } return true; }

Main.cpp 1/8 #include "ShaderLoader.h" #include "./mesh/mesh.h" mesh *object; GLhandleARB MyShader; int WinW,WinH; void LoadShaders(); void Light(); void Display(); void Reshape(GLsizei, GLsizei );

Main.cpp 2/8 int main(int argc, char** argv) { object = new mesh("Bunny.obj"); glutInit(&argc, argv); glutInitWindowSize(500, 500); glutInitWindowPosition(0, 0); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); glutCreateWindow("Shader Example"); GLenum glew_error; if((glew_error = glewInit()) != GLEW_OK)return -1; LoadShaders(); glutDisplayFunc(Display); glutReshapeFunc(Reshape); glutMainLoop(); return 0; }

Main.cpp 3/8 void LoadShaders() { MyShader = glCreateProgramObjectARB(); //Step 4 if(MyShader != 0) { ShaderLoad(MyShader, "../Shader/PhongShading.vs", GL_VERTEX_SHADER_ARB); ShaderLoad(MyShader, "../Shader/PhongShading.fs", GL_FRAGMENT_SHADER_ARB);\ }

Main.cpp 4/8 void Light() { GLfloat light_specular[] = {1.0, 1.0, 1.0, 1.0}; GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0}; GLfloat light_ambient[] = {0.0, 0.0, 0.0, 1.0}; GLfloat light_position[] = {15.0, 15.0, 15.0, 0.0}; GLfloat Ka[] = {0.2, 0.2, 0.2 }; GLfloat Kd[] = {0.6, 0.6, 0.6 }; GLfloat Ks[] = {0.6, 0.6, 0.6 }; GLfloat Ns = 160; glShadeModel(GL_SMOOTH); // z buffer enable glEnable(GL_DEPTH_TEST);

Main.cpp 5/8 // enable lighting glEnable(GL_LIGHTING); // set light property glEnable(GL_LIGHT0); glLightfv(GL_LIGHT0, GL_POSITION, light_position); glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); glMaterialfv(GL_FRONT,GL_AMBIENT,Ka); glMaterialfv(GL_FRONT,GL_DIFFUSE,Kd); glMaterialfv(GL_FRONT,GL_SPECULAR,Ks); glMaterialf(GL_FRONT,GL_SHININESS,Ns); }

Main.cpp 6/8 void Display() { // projection transformation glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0, (GLfloat)WinW/(GLfloat)WinH, 1.0, ); // viewing and modeling transformation glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(2.0, 0.5, 2.0, // eye 0.0, 0.0, 0.0, // center 0.0, 1.0, 0.0); // up

Main.cpp 7/8 Light(); // clear the buffer glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClearDepth(1.0f); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glUseProgramObjectARB(MyShader); //Step 7 int i,j; for (i=0;i fTotal;i++) { glBegin(GL_POLYGON); for (j=0;j<3;j++) { glNormal3fv(object->nList[object->faceList[i][j].n].ptr); glVertex3fv(object->vList[object->faceList[i][j].v].ptr); } glEnd(); } glutSwapBuffers(); glutPostRedisplay(); }

Main.cpp 8/8 void Reshape(GLsizei w, GLsizei h) { WinW = w; WinH = h; // viewport transformation glViewport(0, 0, WinW,WinH); }

Texture #define TEX_NUM 1 //the number of textures you use. Gluint texObject[TEX_NUM];//texture object glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, texObject[0]); GLint location = glGetUniformLocationARB( MyShader, "colorTexture"); glMultiTexCoord2fv(GL_TEXTURE0_ARB,object->tList[object->faceList[i][j].t].ptr);

LoadTexture void LoadTexture(int textureIndex,char* filename) { AUX_RGBImageRec* img; img = auxDIBImageLoadA(filename); glBindTexture(GL_TEXTURE_2D, texobject[textureIndex]); gluBuild2DMipmaps(GL_TEXTURE_2D, 4, img->sizeX, img->sizeY,GL_RGB, GL_UNSIGNED_BYTE, img->data); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_MODULATE); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP _LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP _LINEAR); }

Texture Vertex shader void main() { gl_TexCoord[0].xy = gl_MultiTexCoord0.xy; gl_Position = ftransform(); } Fragment shader uniform sampler2D colorTexture; void main (void) { gl_FragColor = texture2D(colorTexture, gl_TexCoord[0].xy).rgba; }

Texture

vec3 v3Normal = gl_NormalMatrix * gl_Normal; vec3 vVertex = vec3(gl_ModelViewMatrix * gl_Vertex); lightDir = vec3(gl_LightSource[0].position.xyz - vVertex); eyeDir = -vVertex; gl_ModelViewMatrix : 4x4 Matrix representing the m-v matrix. gl_NormalMatrix : 3x3 Matrix representing the inverse transpose m-v matrix.This matrix is used for normal transformation.

uniform sampler2D colorTexture; vec2 i = vec2 ( gl_TexCoord[1].x,gl_TexCoord[1].y ); // 將 texture 座標點存入 i vec4 i_color = texture2D(colorTexture,i); // 將 i 座標點的顏色存入 i_color gl_FrontMaterial.ambient gl_LightSource[0].ambient gl_FrontMaterial.diffuse gl_LightSource[0].diffuse gl_FrontMaterial.specular gl_LightSource[0].specular // 對應程式中的各種 light 參數

HW2 : Shader Programming Write a shader program with Phong Shading. (50%) Write a shader program with Bump Mapping. (50%) Due day : 5/21( 四 ) 5/25 HW3

Phong ShadingGouraud Shading Bump Mapping