1 /* 2 * Copyright (c) 2002-2008 LWJGL Project 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: 8 * 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * * Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * * Neither the name of 'LWJGL' nor the names of 17 * its contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 package org.lwjgl.test.glu.tessellation; 33 import org.lwjgl.LWJGLException; 34 import org.lwjgl.opengl.Display; 35 import org.lwjgl.opengl.DisplayMode; 36 import org.lwjgl.util.glu.GLUtessellator; 37 38 import static org.lwjgl.opengl.GL11.*; 39 import static org.lwjgl.util.glu.GLU.*; 40 41 public class TessellationTest { 42 private GLUtessellator tesselator; 43 init()44 void init() 45 { 46 // Create a new tessellation object 47 tesselator = gluNewTess(); 48 49 // Set callback functions 50 TessCallback callback = new TessCallback(); 51 tesselator.gluTessCallback(GLU_TESS_VERTEX, callback); 52 tesselator.gluTessCallback(GLU_TESS_BEGIN, callback); 53 tesselator.gluTessCallback(GLU_TESS_END, callback); 54 tesselator.gluTessCallback(GLU_TESS_COMBINE, callback); 55 } 56 setWindingRule(int windingRule)57 void setWindingRule(int windingRule) 58 { 59 // Set the winding rule 60 tesselator.gluTessProperty(GLU_TESS_WINDING_RULE, windingRule); 61 } 62 renderContour(double obj_data[][], int num_vertices)63 void renderContour(double obj_data[][], int num_vertices) 64 { 65 for (int x = 0; x < num_vertices; x++) //loop through the vertices 66 { 67 tesselator.gluTessVertex(obj_data[x], 0, new VertexData(obj_data[x])); //store the vertex 68 } 69 } 70 beginPolygon()71 void beginPolygon() 72 { 73 tesselator.gluTessBeginPolygon(null); 74 } 75 endPolygon()76 void endPolygon() 77 { 78 tesselator.gluTessEndPolygon(); 79 } 80 beginContour()81 void beginContour() 82 { 83 tesselator.gluTessBeginContour(); 84 } 85 endContour()86 void endContour() 87 { 88 tesselator.gluTessEndContour(); 89 } 90 end()91 void end() 92 { 93 tesselator.gluDeleteTess(); 94 } 95 createDisplay()96 private void createDisplay() throws LWJGLException { 97 int width = 300; 98 int height = 300; 99 100 Display.setDisplayMode(new DisplayMode(width,height)); 101 Display.create(); 102 Display.setVSyncEnabled(true); 103 104 glEnable(GL_TEXTURE_2D); 105 glShadeModel(GL_SMOOTH); 106 glDisable(GL_DEPTH_TEST); 107 glDisable(GL_LIGHTING); 108 109 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 110 glClearDepth(1); 111 112 glEnable(GL_BLEND); 113 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 114 115 glViewport(0,0,width,height); 116 glMatrixMode(GL_MODELVIEW); 117 118 glMatrixMode(GL_PROJECTION); 119 glLoadIdentity(); 120 glOrtho(0, width, height, 0, 1, -1); 121 glMatrixMode(GL_MODELVIEW); 122 } 123 loop()124 private void loop() { 125 while (true) { 126 render(); 127 Display.update(); 128 Display.sync(100); 129 130 if (Display.isCloseRequested()) { 131 System.exit(0); 132 } 133 } 134 } 135 render()136 private void render() { 137 glTranslatef(150,125,0); 138 139 glScalef(50,50,1); 140 // first polygon: a star-5 vertices and color information 141 double star[][] = { {0.6f, -0.1f, 0f, 1.0f, 1.0f, 1.0f}, 142 {1.35f, 1.4f, 0f, 1.0f, 1.0f, 1.0f}, 143 {2.1f, -0.1f, 0f, 1.0f, 1.0f, 1.0f}, 144 {0.6f, 0.9f, 0f, 1.0f, 1.0f, 1.0f}, 145 {2.1f, 0.9f, 0f, 1.0f, 1.0f, 1.0f} }; 146 147 //second polygon: a quad-4 vertices; first contour 148 double quad[][] = { {0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f}, 149 {1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f}, 150 {1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f}, 151 {0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f} }; 152 153 //second polygon: a triangle-3 vertices; second contour 154 double tri[][] = {{0.3f, 0.3f, 0.0f, 0.0f, 0.0f, 0.0f}, 155 {0.7f, 0.3f, 0.0f, 0.0f, 0.0f, 0.0f}, 156 {0.5f, 0.7f, 0.0f, 0.0f, 0.0f, 0.0f} }; 157 158 // render the first polygon: the textured star 159 160 // set winding rule to positive 161 setWindingRule(GLU_TESS_WINDING_POSITIVE); 162 beginPolygon(); 163 beginContour(); 164 renderContour(star, 5); 165 endContour(); 166 endPolygon(); 167 168 // render the second polygon: triangle cut out of a quad 169 170 glTranslatef(-2,0,0); 171 // set winding rule to odd 172 setWindingRule(GLU_TESS_WINDING_ODD); 173 // begin the new polygon 174 beginPolygon(); 175 beginContour(); 176 renderContour(quad, 4); 177 endContour(); 178 beginContour(); 179 renderContour(tri, 3); 180 endContour(); 181 endPolygon(); 182 // delete the tess object 183 end(); 184 } 185 start()186 private void start() throws LWJGLException { 187 createDisplay(); 188 init(); 189 loop(); 190 } 191 main(String[] argv)192 public static void main(String[] argv) throws LWJGLException { 193 TessellationTest test = new TessellationTest(); 194 test.start(); 195 } 196 } 197