
GraphicsTool subclass: #OpenGLPen
  instanceVariableNames: 
    ' renderingContext displayLists availableLightIdentifiers coatingStack currentCoating '
  classVariableNames: ''
  poolDictionaries: ''   !

OpenGLPen class instanceVariableNames: ' default current ' !


!OpenGLPen class methods !
   
current
    ^current!
  
current: anOpenGLPen
    current := anOpenGLPen!
   
default
    ^default!
  
default: anOpenGLPen
    default := anOpenGLPen.!
  
initialize
    self default: (self forDC: Display pen handle medium: Display).!

uninitialize
    self default notNil ifTrue: [self default destroy].! !



!OpenGLPen methods !

activate
    "Make the receiver's renderingContext current."
    self class current == self ifFalse: [
        OpenGLLibrary makeCurrent: deviceContext renderingContext: renderingContext.
        self class current: self].!
 
aspectRatio
    ^(self width / self height) asFloat!
   
createDisplayListWithKey: key using: aBlock
    "Create a display list if one doesn't exist."
    (self displayListWithKey: key) isNil ifTrue: [
        displayLists at: key put: (OpenGLLibrary createDisplayListFor: aBlock)].!
   
currentCoating
    ^currentCoating!

currentCoating: aCoating
    currentCoating := aCoating!
   
deactivate
    "Replace the current context by the default."
    self == self class default
        ifTrue: [
            OpenGLLibrary makeCurrent: 0 renderingContext: 0.
            self class default: nil]
        ifFalse: [self class default activate].!
 
destroy
    "Delete the receiver and free all resources associated with the receiver."
   displayLists do: [:displayList |
            OpenGLLibrary deleteDisplayList: displayList].
   (renderingContext notNil) ifTrue: [
        self deactivate.
        OpenGLLibrary deleteContext: renderingContext].
    renderingContext := nil.
    super release.!
  
displayListWithKey: key
    ^displayLists at: key ifAbsent: [nil]!
 
font: aFont
    "Sent by initGraphics from SubPane"!
   
initialize
    super initialize.
    self reinitialize!
   
nextAvailableLightIdentifier
    ^availableLightIdentifiers removeFirst!
   
popCoating
    ^coatingStack removeLast!
   
pushCoating: aCoating
    coatingStack addLast: aCoating!
  
reinitialize
   displayLists notNil ifTrue: [
        displayLists do: [:displayList |
            OpenGLLibrary deleteDisplayList: displayList]].
    displayLists := Dictionary new.
    availableLightIdentifiers := OpenGLLibrary lightIdentifiers asOrderedCollection.
    coatingStack := OrderedCollection new.
    self currentCoating: (PaintCoating new color: #(1.0 1.0 1.0)).
    self pushCoating: self currentCoating.!
   
release
        "For effeiciency reasons, only free the device context when the receiver is destroyed"
    ^self!
 
renderDisplayListWithKey: key
    OpenGLLibrary renderDisplayList: (self displayListWithKey: key)!
 
setHandle: aDeviceContext
    "Set the receiver's device context, get the openGL rendering context
    and reset its attributes to the original ones."
    self handle: aDeviceContext.
    deviceContext isNil ifFalse: [
        renderingContext := OpenGLLibrary
            setPixelFormatFor: deviceContext;
            createContext: deviceContext.
        self
            activate;
            resetAttributes]!
 
topCoating
    ^coatingStack last! !
