我目前正在学徒阶段,其中一位培训师说"Shaders是面向对象的“,作为面向对象编程的一个例子。对我来说,这意味着HLSL和GLSL是面向对象的语言。我从来没有想过着色器是面向对象的。
但是现在我来看看这个:(GLSL)
vec4 someVec;
someVec.x + someVec.y;我也看到了物体的方向,因为点。现在我很困惑。
我两年前就开始做OpenGL和GLSL了,我从来没有想到GLSL是面向对象的。所以我漏掉了一个要点。
我知道这些着色器语言HLSL/GLSL是从它们的汇编语言衍生而来的。
请有人说明GLSL是否确实是面向对象的。
发布于 2019-10-19 09:50:35
发布于 2019-10-19 10:49:57
我也看到了物体的方向,因为点。
这不是“面向对象”的意思。点仅仅是“成员访问操作符”,对于所有意图和目的来说,都是某种类型的“类型转换”、“指针取消引用”和“指针算法”的组合。所有的引号都是引号,因为在语言和编译器方面没有实际的指针,但是在硅层,它实际上是用来处理偏移量的。
面向对象意味着您可以从其他类、重载和覆盖方法等派生类。像这样(伪码)
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发布于 2021-10-23 18:46:11
GLSL不是一种面向对象的语言,但是可以使用带有重载方法的结构来模拟面向对象的类:
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);
}https://stackoverflow.com/questions/58461958
复制相似问题