Skip to content

Instantly share code, notes, and snippets.

@dlivingstone
Created November 1, 2010 07:17
Show Gist options
  • Save dlivingstone/657768 to your computer and use it in GitHub Desktop.
Save dlivingstone/657768 to your computer and use it in GitHub Desktop.
This sample code will load and display a rotating 3D model. Uses OpenGL and the GLTools library from the OpenGL SuperBible. More details on the UWSM model format used (and sample model data) available at http://3dgamedev.wordpress.com
uwsm0_2
378
48.4251968503937 0 0 0 -38.18897637795276 0 0 0 0 48.4251968503937 -38.18897637795276 0 0 0 11.81102362204724 0 -38.18897637795276 0 0 -38.18897637795276 11.81102362204724 0 0 0 0 0 11.81102362204724 48.4251968503937 0 0 0 0 0 48.4251968503937 0 11.81102362204724 48.4251968503937 0 0 48.4251968503937 -38.18897637795276 11.81102362204724 48.4251968503937 -38.18897637795276 0 48.4251968503937 -26.57597781267043 11.81102362204724 48.4251968503937 0 11.81102362204724 48.4251968503937 -38.18897637795276 11.81102362204724 0 -38.18897637795276 0 48.4251968503937 -38.18897637795276 0 0 -38.18897637795276 11.81102362204724 40.11978340741202 -38.18897637795276 11.81102362204724 31.92395467371481 -38.18897637795276 11.81102362204724 0 -38.18897637795276 11.81102362204724 27.25139183695274 -35.34645501628111 11.81102362204724 0 0 11.81102362204724 29.82020513564828 -37.31757478744946 11.81102362204724 31.92395467371481 -38.18897637795276 11.81102362204724 25.28027206578439 -32.77764171758558 11.81102362204724 24.04117437711892 -29.78619527250425 11.81102362204724 23.61854123073313 -26.57597781267043 11.81102362204724 27.25139183695273 -17.80550060905975 11.81102362204724 48.4251968503937 0 11.81102362204724 25.28027206578438 -20.37431390775529 11.81102362204724 24.04117437711891 -23.36576035283662 11.81102362204724 29.82020513564827 -15.8343808378914 11.81102362204724 32.81165158072959 -14.59528314922593 11.81102362204724 36.02186904056342 -14.17265000284014 11.81102362204724 39.23208650039722 -14.59528314922593 11.81102362204724 42.22353294547855 -15.8343808378914 11.81102362204724 44.7923462441741 -17.80550060905974 11.81102362204724 46.76346601534245 -20.37431390775528 11.81102362204724 48.00256370400791 -23.36576035283661 11.81102362204724 48.4251968503937 -26.57597781267043 11.81102362204724 48.4251968503937 -38.18897637795276 11.81102362204724 42.22353294547856 -37.31757478744946 11.81102362204724 40.11978340741202 -38.18897637795276 11.81102362204724 44.7923462441741 -35.34645501628111 11.81102362204724 46.76346601534245 -32.77764171758557 11.81102362204724 48.00256370400792 -29.78619527250424 11.81102362204724 40.11978340741202 -38.18897637795276 31.88976377952757 31.92395467371481 -38.18897637795276 31.88976377952757 31.92395467371481 -38.18897637795276 31.88976377952757 29.82020513564828 -37.31757478744946 11.81102362204724 31.92395467371481 -38.18897637795276 11.81102362204724 29.82020513564828 -37.31757478744946 31.88976377952757 27.25139183695274 -35.34645501628111 11.81102362204724 27.25139183695274 -35.34645501628111 31.88976377952757 25.28027206578439 -32.77764171758558 31.88976377952757 25.28027206578439 -32.77764171758558 11.81102362204724 24.04117437711892 -29.78619527250425 31.88976377952757 24.04117437711892 -29.78619527250425 11.81102362204724 23.61854123073313 -26.57597781267043 31.88976377952757 23.61854123073313 -26.57597781267043 11.81102362204724 24.04117437711891 -23.36576035283662 31.88976377952757 24.04117437711891 -23.36576035283662 11.81102362204724 25.28027206578438 -20.37431390775529 31.88976377952757 25.28027206578438 -20.37431390775529 11.81102362204724 27.25139183695273 -17.80550060905975 31.88976377952757 27.25139183695273 -17.80550060905975 11.81102362204724 29.82020513564827 -15.8343808378914 11.81102362204724 29.82020513564827 -15.8343808378914 31.88976377952757 32.81165158072959 -14.59528314922593 11.81102362204724 32.81165158072959 -14.59528314922593 31.88976377952757 36.02186904056342 -14.17265000284014 11.81102362204724 36.02186904056342 -14.17265000284014 31.88976377952757 39.23208650039722 -14.59528314922593 11.81102362204724 39.23208650039722 -14.59528314922593 31.88976377952757 42.22353294547855 -15.8343808378914 11.81102362204724 42.22353294547855 -15.8343808378914 31.88976377952757 44.7923462441741 -17.80550060905974 11.81102362204724 44.7923462441741 -17.80550060905974 31.88976377952757 46.76346601534245 -20.37431390775528 31.88976377952757 46.76346601534245 -20.37431390775528 11.81102362204724 48.00256370400791 -23.36576035283661 31.88976377952757 48.00256370400791 -23.36576035283661 11.81102362204724 48.4251968503937 -26.57597781267043 31.88976377952757 48.4251968503937 -26.57597781267043 11.81102362204724 48.4251968503937 -26.57597781267043 11.81102362204724 48.00256370400792 -29.78619527250424 31.88976377952757 48.00256370400792 -29.78619527250424 11.81102362204724 48.4251968503937 -26.57597781267043 31.88976377952757 46.76346601534245 -32.77764171758557 31.88976377952757 46.76346601534245 -32.77764171758557 11.81102362204724 44.7923462441741 -35.34645501628111 31.88976377952757 44.7923462441741 -35.34645501628111 11.81102362204724 42.22353294547856 -37.31757478744946 11.81102362204724 42.22353294547856 -37.31757478744946 31.88976377952757 40.11978340741202 -38.18897637795276 11.81102362204724 40.11978340741202 -38.18897637795276 31.88976377952757 32.8116515807296 -38.55667247611493 11.81102362204724 36.02186904056342 -38.97930562250072 11.81102362204724 39.23208650039724 -38.55667247611493 11.81102362204724 24.04117437711892 -29.78619527250425 31.88976377952757 24.04117437711891 -23.36576035283662 31.88976377952757 23.61854123073313 -26.57597781267043 31.88976377952757 25.28027206578438 -20.37431390775529 31.88976377952757 25.28027206578439 -32.77764171758558 31.88976377952757 27.25139183695273 -17.80550060905975 31.88976377952757 27.25139183695274 -35.34645501628111 31.88976377952757 29.82020513564827 -15.8343808378914 31.88976377952757 29.82020513564828 -37.31757478744946 31.88976377952757 32.81165158072959 -14.59528314922593 31.88976377952757 31.92395467371481 -38.18897637795276 31.88976377952757 40.11978340741202 -38.18897637795276 31.88976377952757 36.02186904056342 -14.17265000284014 31.88976377952757 39.23208650039722 -14.59528314922593 31.88976377952757 42.22353294547855 -15.8343808378914 31.88976377952757 42.22353294547856 -37.31757478744946 31.88976377952757 44.7923462441741 -17.80550060905974 31.88976377952757 44.7923462441741 -35.34645501628111 31.88976377952757 46.76346601534245 -32.77764171758557 31.88976377952757 46.76346601534245 -20.37431390775528 31.88976377952757 48.00256370400792 -29.78619527250424 31.88976377952757 48.00256370400791 -23.36576035283661 31.88976377952757 48.4251968503937 -26.57597781267043 31.88976377952757
378
0 0 -1 0 0 -1 0 0 -1 0 0 -1 -1 0 0 -1 0 0 -1 0 0 -1 0 0 -0 1 0 -0 1 0 -0 1 0 -0 1 0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 0 -1 0 0 -1 0 0 -1 0 0 -1 0 0 -1 0 0 -1 0 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 -0 -0 1 0 -1 0 0 -1 0 -0.3826834323650915 -0.9238795325112862 -0 -0.5000000000000007 -0.8660254037844383 0 -0.3826834323650915 -0.9238795325112862 -0 -0.5000000000000007 -0.8660254037844383 0 -0.7071067811865468 -0.7071067811865482 0 -0.7071067811865468 -0.7071067811865482 0 -0.8660254037844382 -0.5000000000000008 0 -0.8660254037844382 -0.5000000000000008 0 -0.9659258262890682 -0.2588190451025208 0 -0.9659258262890682 -0.2588190451025208 0 -1 -3.920888354084638e-016 0 -1 -3.920888354084638e-016 0 -0.9659258262890684 0.2588190451025202 0 -0.9659258262890684 0.2588190451025202 0 -0.8660254037844388 0.4999999999999997 0 -0.8660254037844388 0.4999999999999997 0 -0.7071067811865477 0.7071067811865474 0 -0.7071067811865477 0.7071067811865474 0 -0.5000000000000006 0.8660254037844383 0 -0.5000000000000006 0.8660254037844383 0 -0.2588190451025213 0.9659258262890682 0 -0.2588190451025213 0.9659258262890682 0 -1.158444286434098e-016 1 0 -1.158444286434098e-016 1 0 0.2588190451025203 0.9659258262890684 0 0.2588190451025203 0.9659258262890684 0 0.4999999999999992 0.8660254037844392 0 0.4999999999999992 0.8660254037844392 0 0.7071067811865471 0.707106781186548 0 0.7071067811865471 0.707106781186548 0 0.8660254037844386 0.5000000000000001 0 0.8660254037844386 0.5000000000000001 0 0.9659258262890682 0.2588190451025212 0 0.9659258262890682 0.2588190451025212 0 0.9914448613738103 0.1305261922200525 0 0.9914448613738103 0.1305261922200525 0 0.9914448613738106 -0.1305261922200508 0 0.9659258262890681 -0.2588190451025211 0 0.9659258262890681 -0.2588190451025211 0 0.9914448613738106 -0.1305261922200508 0 0.866025403784438 -0.5000000000000009 0 0.866025403784438 -0.5000000000000009 0 0.7071067811865476 -0.7071067811865476 0 0.7071067811865476 -0.7071067811865476 0 0.5000000000000002 -0.8660254037844385 0 0.5000000000000002 -0.8660254037844385 0 0.3826834323650897 -0.9238795325112867 0 0.3826834323650897 -0.9238795325112867 0 -0 -0 1 -0 -0 1 -0 -0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1
264
-0.615 0 0 -0.7275 0 0 -0.615 -0.7275 0 0.225 0.485 0 0.485 0.225 -0.615 0.225 -0.485 0.225 -0.485 0 -0.3375149182209145 0.225 0.615 0.225 0.615 0 0.5095212492741326 0.225 0.4054342243561781 0.225 0.3460926763292997 -0.6733499680601552 0.3787166052227331 -0.7108997997009122 0.4054342243561781 -0.7275 0.3210594552354617 -0.6244140747200053 0.3053229145894102 -0.5674270199412059 0.2999554736303107 -0.5062723773313718 0.3460926763292996 -0.3391947866025882 0.3210594552354616 -0.3881306799427383 0.3053229145894102 -0.4451177347215377 0.378716605222733 -0.3016449549618313 0.4167079750752658 -0.2780401439927541 0.4574777368151554 -0.2699889825541048 0.4982474985550447 -0.278040143992754 0.5362388684075776 -0.3016449549618311 0.568862797301011 -0.3391947866025881 0.5938960183948491 -0.3881306799427381 0.6096325590409004 -0.4451177347215374 0.615 -0.5062723773313718 0.615 -0.7275 0.5362388684075777 -0.7108997997009122 0.5095212492741326 -0.7275 0.568862797301011 -0.6733499680601552 0.5938960183948491 -0.6244140747200052 0.6096325590409005 -0.5674270199412059 0.5095212492741326 0.6075000000000002 0.4054342243561781 0.6075000000000002 0.5601738463593311 0.6075000000000002 0.5312549037989074 0.225 0.5601738463593311 0.225 0.5312549037989074 0.6075000000000002 0.5889683357425061 0.6075000000000002 0.5478467733357009 0.225 0.5889683357425061 0.225 0.5478467733357009 0.6075000000000002 0.5257026073685526 0.6075000000000002 0.5668241697753577 0.225 0.5668241697753577 0.6075000000000002 0.5257026073685526 0.225 0.466331494213158 0.6075000000000002 0.5074530566199633 0.225 0.5074530566199633 0.6075000000000002 0.466331494213158 0.225 0.3737794771156541 0.6075000000000002 0.4149010395224593 0.225 0.4149010395224593 0.6075000000000002 0.3737794771156541 0.225 0.2543538230917961 0.6075000000000002 0.2954753854986012 0.225 0.2954753854986012 0.6075000000000002 0.2543538230917961 0.225 0.1161931931030845 0.6075000000000002 0.1573147555098897 0.225 0.1573147555098897 0.6075000000000002 0.1161931931030845 0.225 -0.03128699423798634 0.6075000000000002 0.009834568168818864 0.225 0.009834568168818864 0.6075000000000002 -0.03128699423798634 0.225 -0.1369146454798498 0.6075000000000002 -0.178036207886655 0.225 -0.1369146454798498 0.225 -0.178036207886655 0.6075000000000002 -0.2729321690405022 0.6075000000000002 -0.3140537314473073 0.225 -0.2729321690405022 0.225 -0.3140537314473073 0.6075000000000002 -0.3889486330620655 0.6075000000000002 -0.4300701954688708 0.225 -0.3889486330620655 0.225 -0.4300701954688708 0.6075000000000002 -0.4770577072477418 0.6075000000000002 -0.5181792696545469 0.225 -0.4770577072477418 0.225 -0.5181792696545469 0.6075000000000002 -0.5312549037989067 0.6075000000000002 -0.5723764662057119 0.225 -0.5312549037989067 0.225 -0.5723764662057119 0.6075000000000002 -0.5478467733357009 0.6075000000000002 -0.5889683357425062 0.225 -0.5478467733357009 0.225 -0.5889683357425062 0.6075000000000002 -0.5257026073685525 0.225 -0.5668241697753577 0.6075000000000002 -0.5668241697753577 0.225 -0.5257026073685525 0.6075000000000002 -0.4663314942131577 0.225 -0.5074530566199628 0.6075000000000002 -0.5074530566199628 0.225 -0.4663314942131577 0.6075000000000002 -0.3737794771156545 0.225 -0.4149010395224598 0.6075000000000002 -0.4149010395224598 0.225 -0.3737794771156545 0.6075000000000002 -0.2543538230917963 0.225 -0.2954753854986015 0.6075000000000002 -0.2954753854986015 0.225 -0.2543538230917963 0.6075000000000002 -0.1161931931030831 0.225 -0.1573147555098883 0.6075000000000002 -0.1573147555098883 0.225 -0.1161931931030831 0.6075000000000002 0.03128699423798684 0.225 -0.0098345681688183 0.6075000000000002 -0.0098345681688183 0.225 0.03128699423798684 0.6075000000000002 0.1780362078866549 0.6075000000000002 0.1369146454798498 0.225 0.1780362078866549 0.225 0.1369146454798498 0.6075000000000002 0.3140537314473077 0.6075000000000002 0.2851347888868839 0.225 0.3140537314473077 0.225 0.2851347888868839 0.6075000000000002 0.4167079750752659 -0.7345046106699895 0.4574777368151554 -0.7425557721086387 0.4982474985550449 -0.7345046106699894
107
0 0 1 1 2 2 1 1 0 0 3 3 4 4 5 5 6 6 5 5 4 4 7 2 8 4 9 0 10 2 9 0 8 4 11 7 12 2 13 8 14 9 13 8 12 2 15 10 15 10 12 2 16 4 17 11 18 2 19 12 18 2 17 11 20 4 20 4 17 11 21 13 20 4 21 13 22 14 23 1 24 15 25 2 24 15 23 1 26 16 26 16 23 1 27 17 25 2 24 15 28 18 25 2 28 18 29 19 25 2 29 19 30 20 25 2 31 21 32 12 31 21 25 2 33 22 33 22 25 2 34 23 34 23 25 2 30 20 32 12 31 21 35 24 32 12 35 24 36 25 32 12 36 25 37 26 32 12 37 26 38 27 32 12 38 27 39 28 32 12 39 28 40 29 32 12 40 29 41 30 32 12 41 30 42 31 32 12 42 31 43 32 44 33 45 34 46 35 45 34 44 33 47 36 47 36 44 33 48 37 48 37 44 33 49 38 49 38 44 33 43 32 50 39 22 14 21 13 22 14 50 39 51 40 52 41 53 42 54 43 53 42 52 41 55 44 55 45 56 46 53 47 56 46 55 45 57 48 58 49 56 50 57 51 56 50 58 49 59 52 60 53 59 54 58 55 59 54 60 53 61 56 62 57 61 58 60 59 61 58 62 57 63 60 64 61 63 62 62 63 63 62 64 61 65 64 66 65 65 66 64 67 65 66 66 65 67 68 68 69 67 70 66 71 67 70 68 69 69 72 68 73 70 74 69 75 70 74 68 73 71 76 71 77 72 78 70 79 72 78 71 77 73 80 73 81 74 82 72 83 74 82 73 81 75 84 75 85 76 86 74 87 76 86 75 85 77 88 77 89 78 90 76 91 78 90 77 89 79 92 79 93 80 94 78 95 80 94 79 93 81 96 80 97 82 98 83 99 82 98 80 97 81 100 83 101 84 102 85 103 84 102 83 101 82 104 85 105 86 106 87 107 86 106 85 105 84 108 88 109 89 110 90 111 89 110 88 109 91 112 90 113 92 114 93 115 92 114 90 113 89 116 93 117 94 118 95 119 94 118 93 117 92 120 94 121 96 122 95 123 96 122 94 121 97 124 97 125 98 126 96 127 98 126 97 125 99 128 100 129 46 35 27 17 46 35 100 129 101 130 46 35 101 130 102 131 103 19 104 23 105 20 104 23 103 19 106 22 106 22 103 19 107 18 106 22 107 18 108 21 108 21 107 18 109 15 108 21 109 15 110 24 110 24 109 15 111 16 110 24 111 16 112 25 112 25 111 16 113 17 112 25 113 17 114 35 112 25 114 35 115 26 115 26 114 35 116 27 116 27 114 35 117 28 117 28 114 35 118 34 117 28 118 34 119 29 119 29 118 34 120 36 119 29 120 36 121 37 119 29 121 37 122 30 122 30 121 37 123 38 122 30 123 38 124 31 124 31 123 38 125 32
// texModelGLTools
// A simple app that loads model data from a custom data format
// the format is a (relatively) simple one based on how an individual
// mesh may be saved in a Collada file - currently the easiest way
// to make models, is with Google Sketchup, export to Collada, then
// manually hack the data and save.
// Daniel Livingstone, Oct 2010
// On Windows, the following define can remove some compiler problems,
// but remove the line if compiling for other platforms!
#define WIN32
#include <GLTools.h> // OpenGL toolkit
#include <GLMatrixStack.h>
#include <GLFrustum.h>
#include <GLBatch.h>
#include <GLMatrixStack.h>
#include <GLGeometryTransform.h>
#include <iostream>
#include <fstream>
#include <sstream>
#define FREEGLUT_STATIC
#include <GL/glut.h>
using namespace std;
// * Global Variables *
GLMatrixStack projectionMatrix;
GLFrustum viewFrustum;
GLShaderManager shaderManager;
GLTriangleBatch myObject;
GLMatrixStack modelViewMatrix;
GLGeometryTransform transformPipeline;
GLfloat angle = 0.0f; // going to animate the object
GLuint textures[1]; // only using one texture just now
GLuint loadTexture(char *fname, GLuint *texID)
{
// generate texture ID
glGenTextures(1, texID);
// texture dimensions and data buffer
GLint width, height, components;
GLenum format;
GLbyte *pBytes;
// load file - using GLTools library
pBytes = gltReadTGABits(fname, &width, &height, &components, &format);
if (pBytes == NULL)
{
cerr << "Something went wrong loading texture " << fname << endl;
}
// bind texture and set parameters
glBindTexture(GL_TEXTURE_2D, *texID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexImage2D(GL_TEXTURE_2D,0,components,width, height, 0,
format, GL_UNSIGNED_BYTE, pBytes);
glGenerateMipmap(GL_TEXTURE_2D);
// texture loaded, free the temporary buffer
free(pBytes);
return *texID; // return value of texure ID - redundant
}
// loadUWSM function
// This function *assumes* that the data matches the particular
// format expected, with minimal checks, and will blow-up if it does not
void loadUWSM(char* fname)
{
ifstream::pos_type size;
char * memblock;
ifstream file (fname, ios::in|ios::binary|ios::ate);
if (file.is_open())
{
size = file.tellg();
memblock = new char [size];
file.seekg (0, ios::beg);
file.read (memblock, size);
file.close();
cout << "file " << fname << " loaded" << endl;
}
else
{ // Creates empty triangle mesh if file fails to load
cerr << "Unable to open file " << fname << endl;
myObject.BeginMesh(0);
myObject.End();
return;
}
stringstream strstream(memblock); // Copy file into a stringstream
string magicnumber; // Basic check: is this a uwsm file?
strstream >> magicnumber;
if (magicnumber.compare(0,4,"uwsm")!=0)
{ // could also use the version number, but not doing that just now
cerr << "File " << fname << " is not a uwsm file." << endl;
myObject.BeginMesh(0);
myObject.End();
return;
}
// Read geometry
int i, count; //count is number of floats, ie verts x 3
strstream >> count;
M3DVector3f *vertices = new M3DVector3f[count/3];
for (i=0;i<count/3;i++)
strstream >> vertices[i][0] >> vertices[i][1] >> vertices[i][2];
// Read normals
strstream >> count; // how many normals? normally will be same as vertices
M3DVector3f *normals = new M3DVector3f[count/3];
for (i=0;i<count/3;i++)
strstream >> normals[i][0] >> normals[i][1] >> normals[i][2];
// Read texture coordinates
strstream >> count; // how many texture coords? need not be same as vertices
M3DVector2f *uv = new M3DVector2f[count/2];
if (count > 0) // model has UV coordinates
for (i=0;i<count/2;i++)
strstream >> uv[i][0] >> uv[i][1];
int triangles;
strstream >> triangles; // how many *triangles* in mesh?
// Add data to GLTools TriangleBatch object
// GLTriangleBatch will remove identical vertices, and free any unused memory
myObject.BeginMesh(triangles*3);
int v0, v1, v2, t0, t1, t2;
M3DVector3f triangleVerts[3], triangleNorms[3];
M3DVector2f triangleTex[3];
for (i=0;i<triangles;i++)
{
// vertex & texture indices for one triangle
strstream >> v0 >> t0 >> v1 >> t1 >> v2 >> t2;
m3dCopyVector3(triangleVerts[0],vertices[v0]);
m3dCopyVector3(triangleVerts[1],vertices[v1]);
m3dCopyVector3(triangleVerts[2],vertices[v2]);
m3dCopyVector3(triangleNorms[0],normals[v0]);
m3dCopyVector3(triangleNorms[1],normals[v1]);
m3dCopyVector3(triangleNorms[2],normals[v2]);
m3dCopyVector2(triangleTex[0],uv[t0]);
m3dCopyVector2(triangleTex[1],uv[t1]);
m3dCopyVector2(triangleTex[2],uv[t2]);
myObject.AddTriangle(triangleVerts,triangleNorms,triangleTex);
}
myObject.End();
delete [] memblock;
delete [] vertices;
delete [] normals;
delete [] uv;
}
void reshape(GLint w, GLint h)
{
glViewport(0, 0, w, h);
viewFrustum.SetPerspective(60.0f, float(w) / float(h), 0.5f, 500.0f);
projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix());
}
void display(void)
{
GLfloat vColour[] = { 1.0f, 0.0f, 0.8f, 1.0f};
GLfloat vLightPos[] = {-4.0f, 3.0f, -3.0f};
// clear the screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
// set up the projection matrix
projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix());
modelViewMatrix.LoadIdentity();
modelViewMatrix.Translate(0.0f,0.0f,-10.0f);
modelViewMatrix.Rotate(-45.0f,1.0f,0.0f,0.0f);
modelViewMatrix.Rotate(angle,0.0f,0.0f,1.0f);
modelViewMatrix.Scale(0.1f,0.1f,0.1f);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,textures[0]);
shaderManager.UseStockShader(GLT_SHADER_TEXTURE_POINT_LIGHT_DIFF,
modelViewMatrix.GetMatrix(), projectionMatrix.GetMatrix(), vLightPos, vColour, 0);
myObject.Draw();
angle += 1.0;
glutPostRedisplay();
glutSwapBuffers();
}
void init(void)
{
glClearColor(1.0f, 1.0f, 1.0f, 1.0f ); // white background
glEnable(GL_DEPTH_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
shaderManager.InitializeStockShaders();
loadUWSM("blocks.uwsm");
textures[0] = loadTexture("wall.tga",&textures[0]);
transformPipeline.SetMatrixStacks(modelViewMatrix, projectionMatrix);
modelViewMatrix.LoadIdentity();
modelViewMatrix.Translate(0.0f,0.0f,-5.0f);
}
// Main function - program entry point
int main(int argc, char* argv[])
{
gltSetWorkingDirectory(argv[0]);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutCreateWindow("simple3Dapp");
GLenum err = glewInit();
if (GLEW_OK != err)
{
// Problem: glewInit failed, something is seriously wrong.
cerr << "Error: " << glewGetErrorString(err) << endl;
return 1;
}
glutReshapeFunc(reshape);
glutDisplayFunc(display);
init();
glutPostRedisplay(); // draw the scene once
glutMainLoop();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment