170 likes | 363 Views
Shaders - programs on the GPU. GL Shading Language (GLSL). 3 rd Party Libraries . GLEW OpenGL Extension Wrangles Cross-platform library Painless access to OpenGL functions (GPU) Not using GLEW requires a lot more work (more or less writing the code that GLEW takes care of). GLM
E N D
Shaders - programs on the GPU GL Shading Language (GLSL)
3rd Party Libraries • GLEW • OpenGL Extension Wrangles • Cross-platform library • Painless access to OpenGL functions (GPU) • Not using GLEW requires a lot more work (more or less writing the code that GLEW takes care of). • GLM • Powerful, cross-platform math library that works with GLSL (Use this instead of deprecated OpenGL function calls).
Shader s • A shader is simply a program that runs on the GPU. • It uses a “C” like language. Although it much more restricted than C. • Shaders: • Vertex Shader – run for each vertex given to GPU • Fragment Shader – process a Fragment (collection of values from Rasterizer– i.e. color). • Write these in files that your program will read.
Make Shader Program (GLSL) • GluintglCreateShader(GlenumshaderType); • Creates a shader object. • void glShaderSource(Gluint, shader, Glsizei count, const Glchar** string, cont GLuint* length); • Gets source from shader files. • void glCompileShader(Gluint shader); • Compiles shader object.
Make Shader Program (GLSL) • GluintglCreateProgram(); • Create the shader program object. • void glAttachShader(Gluint program, Gluint shader); • Attaches shader to program object. • void glLinkProgram(Gluint program); • Links program object. • void glUseProgram(Gluint program); • Install program object to current rendering state.
Loading Buffers (GLSL) • void glGenVertexArrays(Glsizei n, Gluint* arrays); //generate vertex array object(VAO) • void glBindVertexArrays(Gluint array); //bind VAO • void glGenBuffers(Glsizei n, Gluint* buffers);//generate buffer object names • voidglBindBuffer(Glenum target, Gluint buffer);//bind named buffer • void glBufferData(Glenum target, Glsizeiptr size, const GLvoid* data, Glenum usage);//creates and initializes buffer object’s data store
Loading Buffers (GLSL) • void glVertexAttribPointer(…); //Overloaded function with different paramaters. Define array of generic vertex data • void glEnableVertexAttribArray(Gluint index);//enable/disable generic vertex array • void glGetUniformLocation(Glint program, const Glchar* name); //returns location of uniform variable (from shader).
Rendering Buffer Data • void glUseProgram(Gluint program); //installs program and part of current rendering • void glUniformMatrix4fv(Glint location, Glsizei count, Glboolean transpose, const Glfloat* value); // specify value of uniform variable. (GLM is very good to use here). • void glDrawElements(Glenum mode, Glsizei count, Glenum type, const Glvoid* indices); // render to scene
OBJ File Format • First character on line lets you know what kind of data it is. • v x y z • Vertex value (x, y, z) coordinate. • vt x y • Texture value (x, y) coordinate in image. • vn x y z • Vertex normal (x, y, z). • f v1/v1/v1 v2/v2/v2 v3/v3/v3 • Face (triangle). Each one is a vertex. They are an index into the vertex data. Some other obj files change this format. Double check file to make sure it is same. If it is different it will still be similar.
Notes • All functions mentioned can be used to create 3d model viewer. • Do a web search for the function name. • Almost always this link will be first hit: http://www.opengl.org/sdk/docs/man4/xhtml • One of best references for finding what each function does. • There are a lot of tutorials about GLSL, GLEW, and GLM. They will show how to put these functions together to create viewer.
Notes • If you are having problems with GLEW working, make sure you have the right type. • 32-bit vs. 64-bit. • You can download binaries for Windows, and the source if you need to compile on another OS. http://glew.sourceforge.net/ • Download GLM (headers only needed):http://glm.g-truc.net/ • Skeleton has GLEW and GLM setup for Windows.