132001f49Smrg/*
232001f49Smrg * Copyright (C) 2008  Brian Paul   All Rights Reserved.
332001f49Smrg *
432001f49Smrg * Permission is hereby granted, free of charge, to any person obtaining a
532001f49Smrg * copy of this software and associated documentation files (the "Software"),
632001f49Smrg * to deal in the Software without restriction, including without limitation
732001f49Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
832001f49Smrg * and/or sell copies of the Software, and to permit persons to whom the
932001f49Smrg * Software is furnished to do so, subject to the following conditions:
1032001f49Smrg *
1132001f49Smrg * The above copyright notice and this permission notice shall be included
1232001f49Smrg * in all copies or substantial portions of the Software.
1332001f49Smrg *
1432001f49Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
1532001f49Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1632001f49Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1732001f49Smrg * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
1832001f49Smrg * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
1932001f49Smrg * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2032001f49Smrg */
2132001f49Smrg
2232001f49Smrg/*
2332001f49Smrg * Draw a triangle with X/EGL.
2432001f49Smrg * Brian Paul
2532001f49Smrg * 3 June 2008
2632001f49Smrg */
2732001f49Smrg
2832001f49Smrg
2932001f49Smrg#include <math.h>
3032001f49Smrg#include <stdlib.h>
3132001f49Smrg#include <string.h>
3232001f49Smrg#include "gl_wrap.h"
3332001f49Smrg
3432001f49Smrg#include "eglut.h"
3532001f49Smrg
3632001f49Smrg
3732001f49Smrgstatic GLfloat view_rotx = 0.0, view_roty = 0.0, view_rotz = 0.0;
3832001f49Smrg
3932001f49Smrg
4032001f49Smrgstatic void
4132001f49Smrgdraw(void)
4232001f49Smrg{
4332001f49Smrg   static const GLfloat verts[3][2] = {
4432001f49Smrg      { -1, -1 },
4532001f49Smrg      {  1, -1 },
4632001f49Smrg      {  0,  1 }
4732001f49Smrg   };
4832001f49Smrg   static const GLfloat colors[3][3] = {
4932001f49Smrg      { 1, 0, 0 },
5032001f49Smrg      { 0, 1, 0 },
5132001f49Smrg      { 0, 0, 1 }
5232001f49Smrg   };
5332001f49Smrg
5432001f49Smrg   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
5532001f49Smrg
5632001f49Smrg   glPushMatrix();
5732001f49Smrg   glRotatef(view_rotx, 1, 0, 0);
5832001f49Smrg   glRotatef(view_roty, 0, 1, 0);
5932001f49Smrg   glRotatef(view_rotz, 0, 0, 1);
6032001f49Smrg
6132001f49Smrg   {
6232001f49Smrg      glVertexPointer(2, GL_FLOAT, 0, verts);
6332001f49Smrg      glColorPointer(3, GL_FLOAT, 0, colors);
6432001f49Smrg      glEnableClientState(GL_VERTEX_ARRAY);
6532001f49Smrg      glEnableClientState(GL_COLOR_ARRAY);
6632001f49Smrg
6732001f49Smrg      glDrawArrays(GL_TRIANGLES, 0, 3);
6832001f49Smrg
6932001f49Smrg      glDisableClientState(GL_VERTEX_ARRAY);
7032001f49Smrg      glDisableClientState(GL_COLOR_ARRAY);
7132001f49Smrg   }
7232001f49Smrg
7332001f49Smrg   glPopMatrix();
7432001f49Smrg}
7532001f49Smrg
7632001f49Smrg
7732001f49Smrg/* new window size or exposure */
7832001f49Smrgstatic void
7932001f49Smrgreshape(int width, int height)
8032001f49Smrg{
8132001f49Smrg   GLfloat ar = (GLfloat) width / (GLfloat) height;
8232001f49Smrg
8332001f49Smrg   glViewport(0, 0, (GLint) width, (GLint) height);
8432001f49Smrg
8532001f49Smrg   glMatrixMode(GL_PROJECTION);
8632001f49Smrg   glLoadIdentity();
8732001f49Smrg   glFrustum(-ar, ar, -1, 1, 5.0, 60.0);
8832001f49Smrg
8932001f49Smrg   glMatrixMode(GL_MODELVIEW);
9032001f49Smrg   glLoadIdentity();
9132001f49Smrg   glTranslatef(0.0, 0.0, -10.0);
9232001f49Smrg}
9332001f49Smrg
9432001f49Smrg
9532001f49Smrgstatic void
9632001f49Smrginit(void)
9732001f49Smrg{
9832001f49Smrg   glClearColor(0.4, 0.4, 0.4, 0.0);
9932001f49Smrg}
10032001f49Smrg
10132001f49Smrg
10232001f49Smrgstatic void
10332001f49Smrgspecial_key(int special)
10432001f49Smrg{
10532001f49Smrg   switch (special) {
10632001f49Smrg   case EGLUT_KEY_LEFT:
10732001f49Smrg      view_roty += 5.0;
10832001f49Smrg      break;
10932001f49Smrg   case EGLUT_KEY_RIGHT:
11032001f49Smrg      view_roty -= 5.0;
11132001f49Smrg      break;
11232001f49Smrg   case EGLUT_KEY_UP:
11332001f49Smrg      view_rotx += 5.0;
11432001f49Smrg      break;
11532001f49Smrg   case EGLUT_KEY_DOWN:
11632001f49Smrg      view_rotx -= 5.0;
11732001f49Smrg      break;
11832001f49Smrg   default:
11932001f49Smrg      break;
12032001f49Smrg   }
12132001f49Smrg}
12232001f49Smrg
12332001f49Smrgint
12432001f49Smrgmain(int argc, char *argv[])
12532001f49Smrg{
12632001f49Smrg   eglutInitWindowSize(300, 300);
12732001f49Smrg   eglutInitAPIMask(EGLUT_OPENGL_BIT);
12832001f49Smrg   eglutInit(argc, argv);
12932001f49Smrg
13032001f49Smrg   eglutCreateWindow("egltri");
13132001f49Smrg
13232001f49Smrg   eglutReshapeFunc(reshape);
13332001f49Smrg   eglutDisplayFunc(draw);
13432001f49Smrg   eglutSpecialFunc(special_key);
13532001f49Smrg
13632001f49Smrg   init();
13732001f49Smrg
13832001f49Smrg   eglutMainLoop();
13932001f49Smrg
14032001f49Smrg   return 0;
14132001f49Smrg}
142