4

I am currently in an apprenticeship and one of the trainers said "Shaders are object-oriented" as an example for object-orientated programming. To me it means HLSL & GLSL are object-oriented languages. I never thought of shaders as object-oriented.

But now when I look at this: https://www.khronos.org/opengl/wiki/Data_Type_(GLSL)

vec4 someVec;
someVec.x + someVec.y;

I also see object-orientation, because of the dot. Now I am confused.

I started doing OpenGL and GLSL 2 years ago, it never came to my mind that GLSL is object-oriented. So I kind of missed out a major point.

I know that these shader-languages HLSL/GLSL derive from their assembly-predecessors.

Can somebody please state if GLSL is indeed object-oriented.

Zehke
  • 129
  • 1
  • 1
  • 11

3 Answers3

3

No, OpenGL Shading Language is not object orientated. There are no methods (or even inheritance and polymorphism) in glsl.
The data types behave more like a struct in C, than a class in C++. But of course there a additional options to Constructors and initialize the glsl data types respectively some special Vector and Matrix Operations and components can be accessed by Swizzling.
But that makes the language not to an Object-oriented language, because the concept of objects requires fields of data and procedures (methods) contained in an object. In glsl the general concept of methods is missing.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
2

I also see object-orientation, because of the dot.

That's not what "object orientation" means. The dot is merely the "member access operator", and for all intents and purposes is a combination of some sort of "typecast", "pointer dereferencing" and "pointer arithmetic". All in quotes, because there are not actual pointers involved as far as language and compiler are concerned, but on the silicon level it really boils down to address offsets.

Object orientation means that you can derive classes from other classes, overload and overwrite methods, and so on. Like this (pseudocode)

class A begin
    var foo
    var bar
    method spam()
endclass

class B inherits A begin
    var eggs
    var bacon
    var mixture

    method spam() begin
       eggs -= bacon
       A::spam() 
    end

    method mix(seasoning) begin
        mixture = eggs * bacon + seasoning
        spam()
    end
endclass
datenwolf
  • 159,371
  • 13
  • 185
  • 298
2

GLSL is not an object-oriented language, but it is possible to mimic object-oriented classes using structs with overloaded methods:

struct Rectangle { //define a "base class"
    float width;
    float height;
};
void new(inout Rectangle self,float width,float height){ //a "constructor"
    self.width = width;
    self.height = height;
}
float area(Rectangle r){ //an "instance method"
    return r.width * r.height;
}
float perimeter(Rectangle r){ //an "instance method"
    return (r.width + r.height)*2.;
}

struct Square{
    Rectangle super; //Square is a "subclass" of Rectangle
    float width;
    float height;
};
void new(inout Square self,float width){ //constructor for Square
    self.width = width;
    self.height = width;
}
void super(inout Square self){ // copy instance variables to superclass
    self.super.width = self.width;
    self.super.height = self.height;
}
float area(Square self){ //"inherit" this method from the superclass
    super(self);
    return area(self.super);
}
float perimeter(Square self){ //"inherit" this method from the superclass
    super(self);
    return perimeter(self.super);
}

void example(){
    Rectangle r;
    new(r,3.,4.); //initialize an instance of Rectangle
    float rectangle_area = area(r); //call an instance method
    float rectangle_perimeter = perimeter(r);

    Square s; 
    new(s,3.); //initialize an instance of Square
    float square_area = area(s); //call an instance method
    float square_perimeter = perimeter(s);
}
Anderson Green
  • 30,230
  • 67
  • 195
  • 328