PageRenderTime 56ms CodeModel.GetById 46ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/opengles/redbook/alpha.c

http://ftk.googlecode.com/
C | 141 lines | 80 code | 13 blank | 48 comment | 5 complexity | ff7ee83d5c75e38f0d1f4c3452a574f0 MD5 | raw file
  1/*
  2 * License Applicability. Except to the extent portions of this file are
  3 * made subject to an alternative license as permitted in the SGI Free
  4 * Software License B, Version 1.1 (the "License"), the contents of this
  5 * file are subject only to the provisions of the License. You may not use
  6 * this file except in compliance with the License. You may obtain a copy
  7 * of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
  8 * Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
  9 * 
 10 * http://oss.sgi.com/projects/FreeB
 11 * 
 12 * Note that, as provided in the License, the Software is distributed on an
 13 * "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
 14 * DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
 15 * CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
 16 * PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
 17 * 
 18 * Original Code. The Original Code is: OpenGL Sample Implementation,
 19 * Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
 20 * Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
 21 * Copyright in any portions created by third parties is as indicated
 22 * elsewhere herein. All Rights Reserved.
 23 * 
 24 * Additional Notice Provisions: The application programming interfaces
 25 * established by SGI in conjunction with the Original Code are The
 26 * OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
 27 * April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
 28 * 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
 29 * Window System(R) (Version 1.3), released October 19, 1998. This software
 30 * was created using the OpenGL(R) version 1.2.1 Sample Implementation
 31 * published by SGI, but has not been independently verified as being
 32 * compliant with the OpenGL(R) version 1.2.1 Specification.
 33 *
 34 */
 35
 36/*
 37 *  alpha.c
 38 *  This program draws several overlapping filled polygons
 39 *  to demonstrate the effect order has on alpha blending results.
 40 *  Use the 't' key to toggle the order of drawing polygons.
 41 */
 42#include <stdlib.h>
 43#include "ug.h"
 44
 45static int leftFirst = GL_TRUE;
 46
 47/*  Initialize alpha blending function.
 48 */
 49static void init(void)
 50{
 51   static const GLfloat v[] = {
 52      0.1, 0.9, 0.0,
 53      0.1, 0.1, 0.0,
 54      0.7, 0.5, 0.0,
 55      0.9, 0.9, 0.0,
 56      0.3, 0.5, 0.0,
 57      0.9, 0.1, 0.0,
 58   };
 59   glEnable (GL_BLEND);
 60   glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 61   glShadeModel (GL_FLAT);
 62   glClearColor (0.0, 0.0, 0.0, 0.0);
 63   glVertexPointer(3, GL_FLOAT, 0, v);
 64   glEnableClientState(GL_VERTEX_ARRAY);
 65}
 66
 67static void drawLeftTriangle(void)
 68{
 69   /* draw yellow triangle on LHS of screen */
 70
 71   glColor4f(1.0, 1.0, 0.0, 0.75);
 72   glDrawArrays(GL_TRIANGLES, 0, 3);
 73}
 74
 75static void drawRightTriangle(void)
 76{
 77   /* draw cyan triangle on RHS of screen */
 78
 79   glColor4f(0.0, 1.0, 1.0, 0.75);
 80   glDrawArrays(GL_TRIANGLES, 3, 3);
 81}
 82
 83void display(UGWindow uwin)
 84{
 85   glClear(GL_COLOR_BUFFER_BIT);
 86
 87   if (leftFirst) {
 88      drawLeftTriangle();
 89      drawRightTriangle();
 90   }
 91   else {
 92      drawRightTriangle();
 93      drawLeftTriangle();
 94   }
 95
 96   glFlush();
 97   ugSwapBuffers(uwin);
 98}
 99
100void reshape(UGWindow uwin, int w, int h)
101{
102   glViewport(0, 0, (GLsizei) w, (GLsizei) h);
103   glMatrixMode(GL_PROJECTION);
104   glLoadIdentity();
105   if (w <= h) 
106      glOrthof(0.0, 1.0, 0.0, 1.0*(GLfloat)h/(GLfloat)w, -1.0, 1.0);
107   else 
108      glOrthof(0.0, 1.0*(GLfloat)w/(GLfloat)h, 0.0, 1.0, -1.0, 1.0);
109}
110
111void keyboard(UGWindow uwin, int key, int x, int y)
112{
113   switch (key) {
114      case 't':
115      case 'T':
116         leftFirst = !leftFirst;
117         ugPostRedisplay(uwin);	
118         break;
119      case 27:  /*  Escape key  */
120         exit(0);
121         break;
122      default:
123         break;
124   }
125}
126
127/*  Main Loop
128 *  Open window with initial window size, title bar, 
129 *  RGBA display mode, and handle input events.
130 */
131int main(int argc, char** argv)
132{
133   UGCtx ug = ugInit();
134   UGWindow uwin = ugCreateWindow (ug, "", argv[0], 200, 200, 100, 100);
135   init ();
136   ugDisplayFunc(uwin, display); 
137   ugReshapeFunc(uwin, reshape);
138   ugKeyboardFunc(uwin, keyboard);
139   ugMainLoop(ug);
140   return 0;
141}