What is the difference between opengl and GLSL?

前端 未结 2 506
青春惊慌失措
青春惊慌失措 2021-01-31 06:36

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 t

2条回答
  •  忘了有多久
    2021-01-31 07:04

    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:

    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.

提交回复
热议问题