Color, Line Attributes, OpenGL Frame Buffers PowerPoint PPT Presentation

presentation player overlay
1 / 18
About This Presentation
Transcript and Presenter's Notes

Title: Color, Line Attributes, OpenGL Frame Buffers


1
Color, Line Attributes, OpenGL Frame Buffers
  • Soon Tee Teoh
  • CS 116A

2
Science of Light
  • Visible light part of electromagnetic spectrum.
  • All electromagnetic waves travel with the same
    speed.
  • Speed wavelength X frequency.

Radio waves Microwaves Infrared Visible
Ultraviolet X-Rays
106
1014
1020
3
Science of Light
  • White light has energy distribution over the
    entire range of visible frequencies.
  • Red light source has dominant frequency around
    the red region.

4
Color and grayscale
  • Grayscale means from black to white (only vary in
    shade)
  • Color means deviation from gray scale.
  • In OpenGL, color is specified in RGB.

glColor3f(r,g,b) where r, g and b are floating
point numbers between 0.0 and 1.0, for
example glColor3f(0.5,0.1,0.9) This tells
display to emit 0.5 intensity red light together
with 0.1 intensity green light together with 0.9
intensity blue light. Note For grayscale, rgb.
5
OpenGL RGB Colors
Color Component Color Common Name R
G B 0 0 0
Black 0 0 1 Blue 0
1 0 Green 0 1
1 Cyan 1 0 0
Red 1 0 1
Magenta 1 1 0
Yellow 1 1 1 White
6
Color Look-up Table
  • Frame buffer
  • 2-D matrix
  • One entry for each pixel
  • Each entry stores the RGB color
  • Example
  • For 512 X 512 image, and 8 bits per color
    component, total storage requirement 512 X 512
    X 3 X 8 6.2M
  • Reduce storage space Use look-up table
  • Example
  • Only 256 distinct colors are shown at one time
  • Each entry in frame buffer is 8 bits (28 256)
  • Look-up table has 256 entries, each entry is
    24-bit color
  • Size of look-up table 256 X 24 0.006M
  • Size of frame buffer 512 X 512 X 8 2M

7
OpenGL RGB and RGBA modes
  • A stands for alpha, refers to transparency.
  • Alpha 1.0 Fully opaque
  • Alpha 0.0 Fully transparent
  • In RGB mode, alpha is assumed to be 1.0.
  • Example

glColor3f(0.5,1.0,0.6) // RGB mode, fully
opaque glColor4f(0.5,1.0,0.6,0.3) // RGBA
mode, alpha set to 0.3
8
OpenGL Color Blending
  • To enable blending glEnable(GL_BLEND)
  • To disable blending glDisable(GL_BLEND)
  • Must set blending function, e.g.

glEnable(GL_BLEND) glBlendFunc (GL_SRC_ALPHA,
GL_ONE_MINUS_SRC_ALPHA) //
source_factor, destination_factor glColor4f(1.0,0.
0,0.0,0.9) // almost opaque glLineWidth(5) glBeg
in(GL_LINES) glVertex3f(0.0,0.0,0.0)
glVertex3f(1.0,1.0,0.0) glEnd() glDisable(GL_BL
END)
Final_color s_factor object_color d_factor
frame_buffer_color
9
Transparency Example
glClear( GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT
) glEnable(GL_DEPTH_TEST) glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA)
glColor4f(1.0,0.0,0.0,1.0) glBegin(GL_QUADS) g
lVertex3f(0.0,0.0,0.0) glVertex3f(200.0,0.0,0.0)
glVertex3f(200.0,200.0,0.0) glVertex3f(0.0,200.0
,0.0) glEnd() glColor4f(1.0,1.0,0.0,0.8) glBeg
in(GL_QUADS) glVertex3f(100.0,100.0,0.1) glVerte
x3f(300.0,100.0,0.1) glVertex3f(300.0,300.0,0.1)
glVertex3f(100.0,300.0,0.1) glEnd()
10
Color Models RGB
  • Additive color
  • Used in display screen. Pixels emit three kinds
    of light Red, Green and Blue
  • We choose Red, Green and Blue to be our primary
    colors.
  • No set of 3 primary colors can generate all
    possible colors.
  • But, Red, Green and Blue are close enough.

11
Color Theory Human Retina
  • According tri-stimulus theory of vision
  • Eyes perceive color through stimulation of three
    visual pigments in cones of retina.
  • One pigment has peak sensitivity to red, the
    other green, the other blue.
  • Hence the RGB model
  • Below shows the intensity of the combination of
    pure R, G and B light that can appear to a
    human like each wavelength/frequency of actual
    light.

intensity
400 500 600 700
wavelength (nm)
12
Color Models CMY
  • Subtractive color
  • Used in printing
  • Printers usually have Cyan, Magenta and Yellow
    ink, plus Black ink (called CMYK)

13
Color Models HSV
  • Think of color in terms of Hue, Saturation and
    Value.
  • Hue The frequency
  • Saturation Is it washed out (whitish) ?
  • Value Bright or dark

The hues at full saturation (all the colors of
the rainbow)
14
Line Attributes
  • In OpenGL
  • Width
  • Color
  • Stipple
  • Smooth interpolation of color

16-bit pattern e.g. 0x00ff
Exercise Try code in Page 192
glEnable(GL_LINE_STIPPLE) glLineStipple(
repeatFactor, pattern)
Exercise Try the following code
glShadeModel(GL_SMOOTH) glBegin(GL_LINES)
glColor3f(1.0,0.0,0.0) glVertex3f(0.0,0.0,0.0)
glColor3f(1.0,1.0,0.0) glVertex3f(1.0,1.0,0.
0) glEnd()
15
Stipple Example
glEnable(GL_LINE_STIPPLE) for (i0ilt10i)
glLineWidth(2) glLineStipple(i,0x00ff)
glBegin(GL_LINES) glColor3f(0.01(float)(rand()
100),0.01(float)(rand()100),0.01(float)(rand()
100)) glVertex3f((float)(sidewidthi)/10.0,(fl
oat)sideheight,(float)i/10.0)
glVertex3f((float)(sidewidthi)/10.0,0.0,(float)i/
10.0) glEnd() glDisable(GL_LINE_STIPPLE)
16 x 9 pixels
16
OpenGL Frame Buffers
Frame buffer is just a part of memory, that the
video controller reads
Display Processor Memory
Frame Buffer
Video Controller
Monitor
CPU
Display Processor
System Memory
System Bus
From Figure 2-28, page 53
17
Double Buffering
  • There are two color buffers for display, the
    front buffer and the back buffer.
  • Double buffering enabled with
  • glutInitDisplayMode( GLUT_RGB GLUT_DOUBLE
    GLUT_DEPTH )
  • Display one buffer while drawing the other buffer
  • Once done drawing, swap the buffers with
    glutSwapBuffers()
  • Before drawing anything, we may want to clear the
    buffers with
  • glClear( GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT
    )

18
glDrawPixels()
  • Remember glBitmap()?
  • In glBitmap(), each pixel is either colored or
    not colored.
  • glDrawPixels() is more general.
  • Each pixel can be set to any color.

GLubyte colorShape64643 int i, j for
(i0ilt64i) for (j0jlt64j)
colorShapeij0 i4 colorShapeij1
j4 colorShapeij2 0
glRasterPos2i(50,50) glDrawPixels(64,64,GL_R
GB,GL_UNSIGNED_BYTE, colorShape)
64 50
50 64
Write a Comment
User Comments (0)
About PowerShow.com