1 / 15

GL Shading Language (GLSL)

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

erelah
Download Presentation

GL Shading Language (GLSL)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Shaders - programs on the GPU GL Shading Language (GLSL)

  2. 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).

  3. 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.

  4. Vertex shader

  5. Fragment Shader

  6. 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.

  7. 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.

  8. 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

  9. 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).

  10. 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

  11. 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.

  12. Struct/Class to store 3D Model

  13. Read file into char* (Read Shader From File)

  14. 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.

  15. 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.

More Related