Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between opengl and GLSL?

Tags:

opengl

glsl

I recently started programming with openGL. I've done code creating basic primitives and have used shaders in webGL. I've googled the subject extensively but it's still not that clear to me. Basically, here's what I want to know. Is there anything that can be done in GLSL that can't be done in plain openGL, or does GLSL just do things more efficiently?

like image 296
user1631075 Avatar asked May 19 '13 06:05

user1631075


People also ask

What is GLSL used for?

About GLSL These shading languages are used to program shaders (i.e. more or less small programs) that are executed on a GPU (graphics processing unit), i.e. the processor of the graphics system of a computer – as opposed to the CPU (central processing unit) of a computer.

Is GLSL an OOP?

No, OpenGL Shading Language is not object orientated. There are no methods (or even inheritance and polymorphism) in glsl.

Is GLSL a programming language?

OpenGL Shading Language (GLSL) is a high-level shading language with a syntax based on the C programming language.

What is the difference between OpenGL and WebGL?

OpenGL is a desktopcomputer-centric API (like Direct3D). WebGL is derived from OpenGL ES 2.0 (intended for mobile devices) which has less capabilities and is simpler to use. WebGL is also designed to run in a browser, and has therefore a few limitations more then OpenGL ES 2.0.


2 Answers

The short version is: OpenGL is an API for rendering graphics, while GLSL (which stands for GL shading language) is a language that gives programmers the ability to modify pipeline shaders. To put it another way, GLSL is a (small) part of the overall OpenGL framework.

To understand where GLSL fits into the big picture, consider a very simplified graphics pipeline.

Vertexes specified ---(vertex shader)---> transformed vertexes ---(primitive assembly)---> primitives ---(rasterization)---> fragments ---(fragment shader)---> output pixels

The shaders (here, just the vertex and fragment shaders) are programmable. You can do all sorts of things with them. You could just swap the red and green channels, or you could implement a bump mapping to make your surfaces appear much more detailed. Writing these shaders is an important part of graphics programming. Here's a link with some nice examples that should help you see what you can accomplish with custom shaders: http://docs.unity3d.com/Documentation/Components/SL-SurfaceShaderExamples.html.

In the not-too-distant past, the only way to program them was to use GPU assembler. In OpenGL's case, the language is known as ARB assembler. Because of the difficulty of this, the OpenGL folks gave us GLSL. GLSL is a higher-level language that can be compiled and run on graphics hardware. So to sum it all up, programmable shaders are an integral part of the OpenGL framework (or any modern graphics API), and GLSL makes it vastly easier to program them.

like image 57
mattsills Avatar answered Oct 19 '22 17:10

mattsills


As also covered by Mattsills answer GL Shader Language or GLSL is a part of OpenGL that enables the creation of algorithms called shaders in/for OpenGL. Shaders run on the GPU.

Shaders make decisions about factors such as the color of parts of surfaces, and the way surfaces share information such as reflected light. Vertex Shaders, Geometry Shaders, Tesselation Shaders and Pixel Shaders are types of shader that can be written in GLSL.


Q1:

Is there anything that can be done in GLSL that can't be done in plain OpenGL?

A:

You may be able to use just OpenGL without the GLSL parts, but if you want your own surface properties you'll probably want a shader make this reasonably simple and performant, created in something like GLSL. Here are some examples:

enter image description here

enter image description here

enter image description here

enter image description here

Q2:

Or does GLSL just do things more efficiently?

A:

Pixel shaders specifically are very parallel, calculating values independently for every cell of a 2D grid, while also containing significant caveats, like not being unable to handle "if" statement like conditions very performantly, so it's a case of using different kinds of shaders to there strengths, on surfaces described and dealt with in the rest of OpenGL.



Q3:

I suspect you want to know if just using GLSL is an option, and I can only answer this with my knowledge of one kind of shader, Pixel Shaders. The rest of this answer covers "just" using GLSL as a possible option:

A:

While GLSL is a part of OpenGL, you can use the rest of OpenGL to set up the enviroment and write your program almost entirly as a pixel shader, where each element of the pixel shader colours a pixel of the whole screen.


For example:

(Note that WebGL has a tendency to hog CPU to the point of stalling the whole system, and Windows 8.1 lets it do so, Chrome seems better at viewing these links than Firefox.)

No, this is not a video clip of real water:

https://www.shadertoy.com/view/Ms2SD1

The only external resources fed to this snail some easily generatable textures:

https://www.shadertoy.com/view/ld3Gz2

Rendering using a noisy fractal clouds of points:

https://www.shadertoy.com/view/Xtc3RS

https://www.shadertoy.com/view/MsdGzl

A perfect sphere: 1 polygon, 1 surface, no edges or vertices:

https://www.shadertoy.com/view/ldS3DW

A particle system like simulation with cars on a racetrack, using a 2nd narrow but long pixel shader as table of data about car positions:

https://www.shadertoy.com/view/Md3Szj

Random values are fairly straightforward:

fract(sin(p)*10000.)

I've found the language in some respects to be hard to work with and it may or may not be particularly practical to use GLSL in this way for a large project such as a game or simulation, however as these demos show, a computer game does not have to look like a computer game and this sort of approach should be an option, perhaps used with generated content and/or external data.

As I understand it to perform reasonably Pixel Shaders in OpenGL:

  • Have to be loaded into a small peice of memory.
  • Do not support:
    • "if" statement like conditions.
    • recursion or while loop like flow control.
  • Are restricted to a small pool of valid instructions and data types.
    • Including "sin", mod, vector multiplication, floats and half precision floats.
  • Lack high level features like objects or lambdas.
  • And effectively calculate values all at once in parallel.

A consequence of all this is that code looks more like lines of closed form equations and lacks algorythms or higher level structures, using modular arithmetic for something akin to conditions.

like image 7
alan2here Avatar answered Oct 19 '22 19:10

alan2here