Open GL Super Bible:Visual Effects:nBlending and Fog
To access the contents, click the chapter and section titles.
Open GL Super Bible
(Publisher: Macmillan Computer Publishing)
Author(s): Waite group Press
ISBN: 1571690735
Publication Date: 08/01/96
Previous
Table of Contents
Next
Listing 16-2 BLENDRAW.C: Paint program using glBlendFunc
/*
* Include necessary headers.
*/
#include <windows.h>
#include <GL/gl.h>
#include "blendraw.h"
#include <stdarg.h>
#include <math.h>
#ifndef M_PI
# define M_PI (double)3.14159265358979323846
#endif /* !M_PI */
/*
* Globals
*/
HWND BlendWindow; /* Blend window */
HPALETTE BlendPalette; /* Color palette (if necessary) */
HDC BlendDC; /* Drawing context */
HGLRC BlendRC; /* OpenGL rendering context */
unsigned char BlendBrush[7][16] =
{
{0xff,0x00,0xff,0x00,0xff,0x08,0xff,0x10,0xff,0x08,0xff,0x00,0xff,0x00},
{0xff,0x00,0xff,0x08,0xff,0x10,0xff,0x20,0xff,0x10,0xff,0x08,0xff,0x00},
{0xff,0x08,0xff,0x10,0xff,0x20,0xff,0x40,0xff,0x20,0xff,0x10,0xff,0x08},
{0xff,0x10,0xff,0x20,0xff,0x40,0xff,0x80,0xff,0x40,0xff,0x20,0xff,0x10},
{0xff,0x08,0xff,0x10,0xff,0x20,0xff,0x40,0xff,0x20,0xff,0x10,0xff,0x08},
{0xff,0x00,0xff,0x08,0xff,0x10,0xff,0x20,0xff,0x10,0xff,0x08,0xff,0x00},
{0xff,0x00,0xff,0x00,0xff,0x08,0xff,0x10,0xff,0x08,0xff,0x00,0xff,0x00},
};
GLboolean Drawing = GL_FALSE; /* GL_TRUE if drawing */
/*
* Local functions
*/
void DisplayErrorMessage(char *, );
void MakePalette(int);
LRESULT CALLBACK BlendProc(HWND, UINT, WPARAM, LPARAM);
void DrawXY(int, int);
void RepaintWindow(RECT *);
/*
* 'WinMain()' - Main entry
*/
int APIENTRY
WinMain(HINSTANCE hInst, /* I - Current process instance */
HINSTANCE hPrevInstance, /* I - Parent process instance */
LPSTR lpCmdLine, /* I - Command-line arguments */
int nCmdShow) /* I - Show window at startup? */
{
MSG msg; /* Window UI event */
WNDCLASS wc; /* Window class */
POINT pos; /* Current mouse pos */
/*
* Register main window
*/
wc.style = 0;
wc.lpfnWndProc = (WNDPROC)BlendProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInst;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = 0;
wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
wc.lpszClassName = "Blend Paint";
if (RegisterClass(&wc) == 0)
{
DisplayErrorMessage("Unable to register window class!");
return (FALSE);
};
/*
* Then create it
*/
BlendWindow = CreateWindow("Blend Paint", "Blend Paint",
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN |
WS_CLIPSIBLINGS,
32, 32, 400, 300,
NULL, NULL, hInst, NULL);
if (BlendWindow == NULL)
{
DisplayErrorMessage("Unable to create window!");
return (FALSE);
};
ShowWindow(BlendWindow, nCmdShow);
UpdateWindow(BlendWindow);
/*
* Loop on events until the user quits this application
*/
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
};
return (msg.wParam);
}
/*
* 'DisplayErrorMessage()' - Display an error message dialog.
*/
void
DisplayErrorMessage(char *format, /* I - printf() style format string */
...) /* I - Other arguments as necessary */
{
va_list ap; /* Argument pointer */
char s[1024]; /* Output string */
if (format == NULL)
return;
va_start(ap, format);
vsprintf(s, format, ap);
va_end(ap);
MessageBeep(MB_ICONEXCLAMATION);
MessageBox(NULL, s, "Error", MB_OK | MB_ICONEXCLAMATION);
}
/*
* 'MakePalette()' - Make a color palette for RGB colors if necessary.
*/
void
MakePalette(int pf) /* I - Pixel format ID */
{
PIXELFORMATDESCRIPTOR pfd; /* Pixel format information */
LOGPALETTE *pPal; /* Pointer to logical palette */
int nColors; /* Number of entries in palette */
int i, /* Color index */
rmax, /* Maximum red value */
gmax, /* Maximum green value */
bmax; /* Maximum blue value */
/*
* Find out if we need to define a color palette
*/
DescribePixelFormat(BlendDC, pf, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
if (!(pfd.dwFlags & PFD_NEED_PALETTE))
{
BlendPalette = NULL;
return;
};
/*
* Allocate memory for a color palette
*/
nColors = 1 << pfd.cColorBits;
pPal = (LOGPALETTE *)malloc(sizeof(LOGPALETTE) + nColors *
sizeof(PALETTEENTRY));
pPal->palVersion = 0x300;
pPal->palNumEntries = nColors;
/*
* Get the maximum values for red, green, and blue. Then build 'nColors'
* colors
*/
rmax = (1 << pfd.cRedBits) - 1;
gmax = (1 << pfd.cGreenBits) - 1;
bmax = (1 << pfd.cBlueBits) - 1;
for (i = 0; i < nColors; i ++)
{
pPal->palPalEntry[i].peRed = 255 * ((i >> pfd.cRedShift) & rmax) /
rmax;
pPal->palPalEntry[i].peGreen = 255 * ((i >> pfd.cGreenShift) & gmax) /
gmax;
pPal->palPalEntry[i].peBlue = 255 * ((i >> pfd.cBlueShift) & bmax) /
bmax;
pPal->palPalEntry[i].peFlags = 0;
};
/*
* Create, select, and realize the palette
*/
BlendPalette = CreatePalette(pPal);
SelectPalette(BlendDC, BlendPalette, FALSE);
RealizePalette(BlendDC);
free(pPal);
}
/*
* 'BlendProc()' - Handle window events in the viewing window.
*/
LRESULT CALLBACK
BlendProc(HWND hWnd, /* I - Window triggering this event */
UINT uMsg, /* I - Message type */
WPARAM wParam, /* I - 'word' parameter value */
LPARAM lParam) /* I - 'long' parameter value */
{
int pf; /* Pixel format ID */
PIXELFORMATDESCRIPTOR pfd; /* Pixel format information */
PAINTSTRUCT ps; /* WM_PAINT message info */
RECT rect; /* Current client area rectangle */
switch (uMsg)
{
case WM_CREATE :
/*
* 'Create' message. Get device and rendering contexts, and
* setup the client area for OpenGL drawing
*/
BlendDC = GetDC(hWnd);
pfd.nSize = sizeof(pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
/* Do OpenGL drawing */
pfd.dwLayerMask = PFD_MAIN_PLANE; /* Main drawing plane */
pfd.iPixelType = PFD_TYPE_RGBA; /* RGB color buffer */
pfd.cColorBits = 0; /* Best color buffer please */
pfd.cDepthBits = 0; /* Don't need a depth buffer */
pfd.cStencilBits = 0; /* No stencil buffer */
pfd.cAccumBits = 0; /* No accumulation buffer */
pf = ChoosePixelFormat(BlendDC, &pfd);
if (pf == 0)
DisplayErrorMessage("texscene was unable to choose a suitable
pixel format!");
else if (!SetPixelFormat(BlendDC, pf, &pfd))
DisplayErrorMessage("texscene was unable to set the pixel
format!");
MakePalette(pf);
BlendRC = wglCreateContext(BlendDC);
wglMakeCurrent(BlendDC, BlendRC);
break;
case WM_SIZE :
case WM_PAINT :
/*
* Repaint the client area with our bitmap
*/
BeginPaint(hWnd, &ps);
GetClientRect(hWnd, &rect);
RepaintWindow(&rect);
EndPaint(hWnd, &ps);
break;
case WM_COMMAND :
/*
* Handle menu selections
*/
switch (LOWORD(wParam))
{
case IDM_FILE_EXIT :
DestroyWindow(BlendWindow);
break;
};
break;
case WM_QUIT :
case WM_CLOSE :
/*
* Destroy the windows and bitmaps and exit
*/
DestroyWindow(BlendWindow);
exit(0);
break;
case WM_DESTROY :
/*
* Release and free the device context, rendering
* context, and color palette
*/
if (BlendRC)
wglDeleteContext(BlendRC);
if (BlendDC)
ReleaseDC(BlendWindow, BlendDC);
if (BlendPalette)
DeleteObject(BlendPalette);
PostQuitMessage(0);
break;
case WM_QUERYNEWPALETTE :
/*
* Realize the color palette if necessary
*/
if (BlendPalette)
{
SelectPalette(BlendDC, BlendPalette, FALSE);
RealizePalette(BlendDC);
InvalidateRect(hWnd, NULL, FALSE);
return (TRUE);
};
break;
case WM_PALETTECHANGED:
/*
* Reselect our color palette if necessary
*/
if (BlendPalette && (HWND)wParam != hWnd)
{
SelectPalette(BlendDC, BlendPalette, FALSE);
RealizePalette(BlendDC);
UpdateColors(BlendDC);
};
break;
case WM_LBUTTONDOWN : /* Left button = red */
Drawing = GL_TRUE;
glColorMask(GL_TRUE, GL_FALSE, GL_FALSE, GL_TRUE);
DrawXY(LOWORD(lParam), HIWORD(lParam));
break;
case WM_MBUTTONDOWN : /* Middle button = green */
Drawing = GL_TRUE;
glColorMask(GL_FALSE, GL_TRUE, GL_FALSE, GL_TRUE);
DrawXY(LOWORD(lParam), HIWORD(lParam));
break;
case WM_ RBUTTONDOWN : /* Right button = blue */
Drawing = GL_TRUE;
glColorMask(GL_FALSE, GL_FALSE, GL_TRUE, GL_TRUE);
DrawXY(LOWORD(lParam), HIWORD(lParam));
break;
case WM_MOUSEMOVE :
if (Drawing)
DrawXY(LOWORD(lParam), HIWORD(lParam));
break;
case WM_LBUTTONUP :
case WM_MBUTTONUP :
case WM_RBUTTONUP :
Drawing = GL_FALSE;
break;
default :
/*
* Pass all other messages through the default window
* procedure
*/
return (DefWindowProc(hWnd, uMsg, wParam, lParam));
};
return (FALSE);
}
/*
* 'DrawXY()' - Draw at the given mouse position.
*/
void
DrawXY(int mousex, /* I - Horizontal mouse position */
int mousey) /* I - Vertical mouse position */
{
glRasterPos2i(mousex, mousey);
glDrawPixels(7, 7, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, BlendBrush[0]);
glFinish();
}
/*
* 'RepaintWindow()' - Redraw the client area.
*/
void
RepaintWindow(RECT *rect) /* I - Client area rectangle */
{
/*
* Reset the viewport and clear the window to white
*/
glViewport(0, 0, rect->right, rect->bottom);
glOrtho(0.0, (float)rect->right, (float)rect->bottom, 0.0, -1.0, 1.0);
glClearColor(0.0, 0.0, 0.0, 1.0);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glFinish();
}
Previous
Table of Contents
Next
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.
Wyszukiwarka
Podobne podstrony:
516 519519 52304 (512)519,24,artykul507 512INDEX (519)512,17,artykul512,17,artykul519 (2)plants and the central nervous system pharm biochem behav 75 (2003) 501 512512 P3 N856więcej podobnych podstron