首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >一般问题:阴影语言/着色器是否面向对象?

一般问题:阴影语言/着色器是否面向对象?
EN

Stack Overflow用户
提问于 2019-10-19 08:45:13
回答 3查看 1K关注 0票数 4

我目前正在学徒阶段,其中一位培训师说"Shaders是面向对象的“,作为面向对象编程的一个例子。对我来说,这意味着HLSL和GLSL是面向对象的语言。我从来没有想过着色器是面向对象的。

但是现在我来看看这个:(GLSL)

代码语言:javascript
复制
vec4 someVec;
someVec.x + someVec.y;

我也看到了物体的方向,因为点。现在我很困惑。

我两年前就开始做OpenGL和GLSL了,我从来没有想到GLSL是面向对象的。所以我漏掉了一个要点。

我知道这些着色器语言HLSL/GLSL是从它们的汇编语言衍生而来的。

请有人说明GLSL是否确实是面向对象的。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-10-19 09:50:35

不,OpenGL着色语言不是面向对象的。在glsl中没有任何方法(甚至遗传和多态)。

数据类型的行为更像C中的结构,而不是C++中的类。当然,构造函数和初始化还有一个额外的选项,即glsl数据类型,一些特殊的向量和矩阵运算和组件可以被晃动访问。

但这使得语言不适合面向对象语言,因为对象的概念需要包含在对象中的数据字段和过程(方法)。在glsl中,缺少方法的一般概念。

票数 3
EN

Stack Overflow用户

发布于 2019-10-19 10:49:57

我也看到了物体的方向,因为点。

这不是“面向对象”的意思。点仅仅是“成员访问操作符”,对于所有意图和目的来说,都是某种类型的“类型转换”、“指针取消引用”和“指针算法”的组合。所有的引号都是引号,因为在语言和编译器方面没有实际的指针,但是在硅层,它实际上是用来处理偏移量的。

面向对象意味着您可以从其他类、重载和覆盖方法等派生类。像这样(伪码)

代码语言:javascript
复制
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
票数 2
EN

Stack Overflow用户

发布于 2021-10-23 18:46:11

GLSL不是一种面向对象的语言,但是可以使用带有重载方法的结构来模拟面向对象的类:

代码语言:javascript
复制
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);
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58461958

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档