Changeset 8966:e8dda7644ae8 in orange


Ignore:
Timestamp:
09/14/11 14:31:51 (3 years ago)
Author:
matejd <matejd@…>
Branch:
default
Convert:
135df169e22131e11771af0bd21e84f3eaa54135
Message:

OpenGLRenderer documentation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • orange/OrangeWidgets/plot/owopenglrenderer.py

    r8965 r8966  
    2727class VertexBuffer: 
    2828    ''' 
    29     An abstraction simplifying the usage of Vertex Buffer Objects (VBO). Warning: understanding what this class does 
     29    A thin abstraction simplifying the usage of Vertex Buffer Objects (VBO). Warning: understanding what this code does 
    3030    requires basic knowledge of OpenGL. 
    3131 
     
    5454    def __init__(self, data, format_description, usage=GL_STATIC_DRAW): 
    5555        ''' 
    56         Constructs VBO and prepares vertex attribute bindings. 
     56        Constructs VBO and prepares vertex attribute bindings. OpenGL context must be up already (initializeGL 
     57        has been called). 
    5758 
    5859        :param data: Data array (vertices) to be sent to the GPU. 
     
    111112        :type int 
    112113 
    113         :param count: The number of indices to be rendered. 
     114        :param count: The number of indices (not primitives) to be rendered. 
    114115        :type int 
    115116        ''' 
     
    137138 
    138139class OWOpenGLRenderer: 
    139     '''OpenGL 3 deprecated a lot of old (1.x) functions, particulary, it removed 
    140        immediate mode (glBegin, glEnd, glVertex paradigm). Vertex buffer objects and similar 
    141        (through glDrawArrays for example) should be used instead. This class simplifies 
    142        the usage of that functionality by providing methods which resemble immediate mode.''' 
    143  
    144     # TODO: research performance optimizations (maybe store primitives and all of them just once at the end?) 
     140    ''' 
     141    OpenGL 3 deprecated a lot of old (1.x) functions, particulary, it removed 
     142    immediate mode (glBegin, glEnd, glVertex paradigm). Vertex buffer objects and similar 
     143    (through glDrawArrays for example) should be used instead. This class simplifies 
     144    the usage of that functionality by providing methods which resemble immediate mode. 
     145 
     146    Example usage:: 
     147 
     148        renderer = OWOpenGLRenderer() 
     149        # Setup vanilla transforms 
     150        projection = QMatrix4x4() 
     151        projection.perspective(45., 1., 1., 50.) 
     152        model = view = QMatrix4x4() 
     153        renderer.set_transform(model, view, projection) 
     154        renderer.draw_line(QVector3D(0, 0, 0), QVector3D(1, 1, 1), color=QColor(0, 0, 255)) 
     155 
     156    Note that usually you don't have to create an instance of the renderer, since it's available in Plot3D: ``OWPlot3D.renderer``. 
     157    Also, current projection and modelview transforms can be accessed (``OWPlot3D.projection`` and ``OWPlot3D.modelview``) in 
     158    OWPlot3D callbacks. 
     159 
     160    **2D drawing** 
     161 
     162        The API of this class is general enough to support drawing 2D shapes. An example setup:: 
     163 
     164            projection = QMatrix4x4() 
     165            # Use ortho projection, specify width and height of the window 
     166            projection.ortho(0, self.width(), self.height(), 0, -1, 1) 
     167            model = view = QMatrix4x4() 
     168            renderer.set_transform(model, view, projection) 
     169            # Red triangle. Z-value is not important. 
     170            renderer.draw_triangle(QVector3D(1, 2, 0), 
     171                                   QVector3D(3, 2, 0), 
     172                                   QVector3D(2, 3, 0), 
     173                                   color=QColor(255, 0, 0)) 
     174    ''' 
    145175 
    146176    def __init__(self): 
    147177        self._projection = QMatrix4x4() 
    148         self._modelview = QMatrix4x4() 
     178        self._model = QMatrix4x4() 
     179        self._view = QMatrix4x4() 
    149180 
    150181        ## Shader used to draw primitives. Position and color of vertices specified through uniforms. Nothing fancy. 
     
    156187            uniform vec4 colors[6]; 
    157188 
    158             uniform mat4 projection; 
    159             uniform mat4 modelview; 
     189            uniform mat4 projection, model, view; 
    160190 
    161191            void main(void) 
    162192            { 
    163193                int i = int(index); 
    164                 gl_Position = projection * modelview * vec4(positions[i], 1.); 
     194                gl_Position = projection * view * model * vec4(positions[i], 1.); 
    165195                color = colors[i]; 
    166196            } 
     
    188218        self._vertex_buffer = VertexBuffer(indices, [(1, GL_FLOAT)]) 
    189219 
    190     def set_transform(self, projection, modelview, viewport=None): 
    191         self._projection = projection 
    192         self._modelview = modelview 
     220    def set_transform(self, model, view=None, projection=None, viewport=None): 
     221        ''' 
     222        Sets current projection, model, view and viewport transforms. Only model parameter is required 
     223        since it's the one most likely to change. Passing None as a parameter value keeps the internal value 
     224        unmodified. 
     225 
     226        :param model: Model transform (usually translation, rotation, scale or a combination of these). 
     227        :type QMatrix4x4 
     228 
     229        :param view: View transform (camera transformation). 
     230        :type QMatrix4x4 
     231 
     232        :param projection: Projection transform (e.g. ortho, perspective). 
     233        :type QMatrix4x4 
     234 
     235        :param viewport: Viewport transform. A list of 4 ints specifying viewport rectangle (lower left corner + width and height). 
     236        :type list of 4 int 
     237        ''' 
     238        if model != None: 
     239            self._model = model 
     240        if view != None: 
     241            self._view = view 
     242        if projection != None: 
     243            self._projection = projection 
    193244        if viewport: 
    194245            glViewport(*viewport) 
    195246        self._shader.bind() 
    196         self._shader.setUniformValue('projection', projection) 
    197         self._shader.setUniformValue('modelview', modelview) 
     247        self._shader.setUniformValue('projection', self._projection) 
     248        self._shader.setUniformValue('model', self._model) 
     249        self._shader.setUniformValue('view', self._view) 
    198250        self._shader.release() 
    199251 
    200252    def draw_line(self, position0, position1, color0=QColor(0, 0, 0), color1=QColor(0, 0 ,0), color=None): 
    201         '''Draws a line. position0 and position1 must be instances of QVector3D. colors are specified with QColor''' 
     253        ''' 
     254        Draws a line using current transform.  
     255 
     256        :param position0: Beginning of line. 
     257        :type QVector3D 
     258 
     259        :param position1: End of line. 
     260        :type QVector3D 
     261 
     262        :param color0: Color of the beginning of the line. 
     263        :type QColor 
     264 
     265        :param color1: Color of the end of the line. 
     266        :type QColor 
     267 
     268        :param color: A convenience parameter. Overrides ``color0`` and ``color1``. 
     269        :type QColor 
     270        ''' 
    202271 
    203272        if color: 
     
    218287    def draw_rectangle(self, position0, position1, position2, position3, 
    219288                       color0=QColor(0, 0, 0), color1=QColor(0, 0, 0), color2=QColor(0, 0, 0), color3=QColor(0, 0, 0), color=None): 
     289        ''' 
     290        Draws a rectangle using current transform. Vertices must specified in clockwise or counter-clockwise order (otherwise nothing 
     291        might be drawn). 
     292 
     293        :param position0: First vertex position. 
     294        :type QVector3D 
     295 
     296        :param position1: Second vertex position. 
     297        :type QVector3D 
     298 
     299        :param position2: Third vertex position. 
     300        :type QVector3D 
     301 
     302        :param position3: Fourth vertex position. 
     303        :type QVector3D 
     304 
     305        :param color0: First vertex color. 
     306        :type QColor 
     307 
     308        :param color1: Second vertex color. 
     309        :type QColor 
     310 
     311        :param color2: Third vertex color. 
     312        :type QColor 
     313 
     314        :param color3: Fourth vertex color. 
     315        :type QColor 
     316 
     317        :param color: A convenience parameter: Overrides color values. 
     318        :type QColor 
     319        ''' 
    220320        if color: 
    221321            colors = [color.redF(), color.greenF(), color.blueF(), color.alphaF()] * 6 
     
    245345    def draw_triangle(self, position0, position1, position2, 
    246346                       color0=QColor(0, 0, 0), color1=QColor(0, 0, 0), color2=QColor(0, 0, 0), color=None): 
     347        ''' 
     348        Draws a triangle using current transform. 
     349 
     350        :param position0: First vertex position. 
     351        :type QVector3D 
     352 
     353        :param position1: Second vertex position. 
     354        :type QVector3D 
     355 
     356        :param position2: Third vertex position. 
     357        :type QVector3D 
     358 
     359        :param color0: First vertex color. 
     360        :type QColor 
     361 
     362        :param color1: Second vertex color. 
     363        :type QColor 
     364 
     365        :param color2: Third vertex color. 
     366        :type QColor 
     367 
     368        :param color: A convenience parameter: Overrides color values. 
     369        :type QColor 
     370        ''' 
    247371        if color: 
    248372            colors = [color.redF(), color.greenF(), color.blueF(), color.alphaF()] * 3 
Note: See TracChangeset for help on using the changeset viewer.