📄 openglcdt.c
字号:
/********************************************************************************* simpleCDT.C: a simple OpenGL test program for constructing CDTs.**** Copyright (C) 1995 by Dani Lischinski **** This program is free software; you can redistribute it and/or modify** it under the terms of the GNU General Public License as published by** the Free Software Foundation; either version 2 of the License, or** (at your option) any later version.**** This program is distributed in the hope that it will be useful,** but WITHOUT ANY WARRANTY; without even the implied warranty of** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the** GNU General Public License for more details.**** You should have received a copy of the GNU General Public License** along with this program; if not, write to the Free Software** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.********************************************************************************/#include <unistd.h>#include <string.h>extern "C" {#include <GL/glut.h>}#include "quadedge.h"#include "cdt.h"char *program;void getArguments(int, char**, int&);void InsertPoints(Mesh&, int);void display(Mesh&, Real, Real, Real, Real);double points[100] = {0, 0, 0.3, 0, 0.8, 0, 1, 0, 1, 0.1, 1, 0.7, 1, 1, 0.5, 1, 0, 1};main(int argc, char** argv){ int num = 20; getArguments(argc, argv, num); Mesh mesh(9, points); InsertPoints(mesh, num); display(mesh, 0, 0, 1, 1);}static void usage(){ cerr << "usage: " << program << " [ options ]\n" << "options:\n" << " -n num insert num initial points\n" << "an odd number will create a regular Delaunay triangulation\n" << "an even number will create a CDT (each pair defines a segment)\n";}static void errmsg(char *msg){ cerr << program << ": " << msg << endl; usage(); exit(1);}void getArguments(int argc, char** argv, int& num){ program = argv[0]; if(argc == 2 && strcmp(argv[1], "-h") == 0) { usage(); exit(0); } for (int i = 1; i < argc && argv[i][0] == '-'; i++) if (strcmp(argv[i], "-n") == 0) { if(++i < argc) num = atoi(argv[i]); else errmsg("option ``-n'': missing parameter"); } else errmsg("unknown option"); if(argc - i > 0) errmsg("too many parameters");}extern "C" double drand48(void);void InsertPoints(Mesh& mesh, int num){ if (num % 2) { for (int i = 0; i < num; i++) { double u = drand48(); double v = drand48(); mesh.InsertSite(Point2d(u,v)); } } else { for (int i = 0; i < num/2; i += 2) { mesh.InsertEdge(Point2d(drand48(),drand48()), Point2d(drand48(),drand48())); } }}/***************************************************************************/static Mesh *meshPtr;static Boolean outputPS = FALSE;void printEdge(char *str, Edge *e, Boolean c){ cerr << str << "from " << e->Org2d() << " to " << e->Dest2d(); cerr << ((c) ? " constrained" : " free") << endl;}void printVertex(char *str, Point2d *p){ cerr << str << *p << endl;}void setView(float cx, float cy, float xsize, float ysize){ float dx = 0.5 * xsize; float dy = 0.5 * ysize; glMatrixMode (GL_PROJECTION); glLoadIdentity (); glOrtho(cx - dx, cx + dx, cy - dy, cy + dy, -1.0, 1.0); glMatrixMode (GL_MODELVIEW); /* back to modelview matrix */}void printHelp(){ cerr << " <h> print this message\n" << " <a> toggle anti-aliasing\n" << " <d> dump encapsulated PostScript to standrad output\n" << " <p> print out edges and vertices to standard error\n" << " <q> quit program\n" << " <left> insert a new point\n" << " <right> insert a constrained edge\n";}static Point2d point(0.5, 0.5);void meshDraw(){ glClear(GL_COLOR_BUFFER_BIT); glColor3f(1, 1, 1); draw_mesh(meshPtr, outputPS); glutSwapBuffers();}void toggleAntiAl(){ static Boolean antiAliased = FALSE; antiAliased = !antiAliased; if (antiAliased) { glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); glEnable(GL_LINE_SMOOTH); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glLineWidth(1.5); } else { glDisable(GL_LINE_SMOOTH); glDisable(GL_BLEND); glLineWidth(1.0); } meshDraw();}Point2d window2uv(int x, int y){ GLdouble ox, oy, oz; GLint viewport[4]; GLdouble modelM[16], projM[16]; glGetDoublev(GL_MODELVIEW_MATRIX, modelM); glGetDoublev(GL_PROJECTION_MATRIX, projM); glGetIntegerv(GL_VIEWPORT, viewport); gluUnProject((GLdouble)x, (GLdouble)y, (GLdouble)0, modelM, projM, viewport, &ox, &oy, &oz); ox = (ox < 0) ? 0 : (ox > 1) ? 1 : ox; oy = (oy < 0) ? 1 : (oy > 1) ? 0 : 1 - oy; return Point2d(ox, oy);}void addPoint(int x, int y){ point = window2uv(x, y); meshPtr->InsertSite(point); meshDraw();}void addEdge(int x, int y){ meshPtr->InsertEdge(point, window2uv(x, y)); point = window2uv(x, y); meshDraw();}void processKey(unsigned char key, int, int){ switch (key) { case 'h': printHelp(); break; case 'a': toggleAntiAl(); break; case 'd': outputPS = TRUE; meshDraw(); outputPS = FALSE; break; case 'p': meshPtr->ApplyVertices(printVertex, "vertex: "); meshPtr->ApplyEdges(printEdge, "edge: "); cerr << "Total number of edges: " << meshPtr->numEdges() << endl; break; case 'q': glutDestroyWindow(glutGetWindow()); exit(0); break; default: break; }}void processMouse(int btn, int state, int x, int y){ if (state != GLUT_DOWN) return; switch (btn) { case GLUT_LEFT_BUTTON: addPoint(x, y); break; case GLUT_RIGHT_BUTTON: addEdge(x, y); break; }}void createWindow(char *title){ glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); glutInitWindowPosition(-1, -1); glutInitWindowSize(500, 500); glutCreateWindow(title); glutDisplayFunc(meshDraw); glutKeyboardFunc(processKey); glutMouseFunc(processMouse); glClearColor(0.0, 0.0, 0.0, 0.0);}void display(Mesh& mesh, Real left, Real bottom, Real right, Real top){ meshPtr = &mesh; createWindow(program); float cx = 0.5 * (left + right); float cy = 0.5 * (bottom + top); float xsize = 1.1 * (right - left); float ysize = 1.1 * (top - bottom); setView(cx, cy, xsize, ysize); toggleAntiAl(); glutMainLoop();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -