-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
244 lines (188 loc) · 4.98 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//#include <glut.h>
#include <assert.h>
#include "Stroke.h"
#include "noise.h"
#include "GL/glut.h"
//#include "polyfill.h"
Stroke curve;
int windowWidth = 512, windowHeight = 512;
bool opaque = true;
bool controlPolygon = true;
GLenum checkForError(char *loc);
GLubyte * texture;
int texWidth,texHeight;
void Redraw(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
// curve.drawTexture();
if (opaque)
glColor3ub(0, 64, 16);
else
glColor4ub(0, 64, 16, 128);
curve.render();
if (controlPolygon)
{
glColor3ub(255,0,0);
curve.drawControl();
}
glutSwapBuffers();
checkForError("swap");
}
void Button(int button, int state, int x, int y)
{
if (state != GLUT_UP)
return;
switch (button)
{
case GLUT_LEFT_BUTTON:
curve.add(x,windowHeight-y);
glutPostRedisplay();
break;
case GLUT_RIGHT_BUTTON:
curve.clear();
glutPostRedisplay();
break;
}
}
void Reshape(int width, int height)
{
windowWidth = width; windowHeight = height;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f, (GLfloat) windowWidth, 0.0f,
(GLfloat) windowHeight, -1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0, 0, windowWidth, windowHeight);
}
void menuSelect(int value)
{
switch(value)
{
case 0:
curve.curveType = CUBIC_BSPLINE;
curve.forceRecompute();
glutPostRedisplay();
break;
case 1:
curve.curveType = FOUR_POINT;
curve.forceRecompute();
glutPostRedisplay();
break;
case 2:
controlPolygon = !controlPolygon;
glutPostRedisplay();
break;
case 3:
opaque = !opaque;
glutPostRedisplay();
break;
case 4:
curve.radius += 5;
glutPostRedisplay();
break;
case 5:
curve.radius -= 5;
if (curve.radius < 1)
curve.radius = 1;
glutPostRedisplay();
break;
case 8:
exit(0);
//////////////////////// tapering ///////////////////////////
case 6:
curve.taperBrush = !curve.taperBrush;
glutPostRedisplay();
break;
//////////////////////// tapering ///////////////////////////
case 7:
curve.useTexture = !curve.useTexture;
glutPostRedisplay();
break;
}
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
// glutInitDisplayMode(GLUT_RGB|GLUT_DEPTH);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glutInitWindowSize(windowWidth, windowHeight);
glutCreateWindow("Square");
// set up world space to screen mapping
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f, (GLfloat) windowWidth, 0.0f,
(GLfloat) windowHeight, -1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0, 0, windowWidth, windowHeight);
glClearColor(1.0,.95,.85,0);
glutDisplayFunc(Redraw);
glutReshapeFunc(Reshape);
glutMouseFunc(Button);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
curve . add(100,100);
curve . add(200,150);
curve . add(300,100);
curve . add(400,300);
curve . add(100,100);
curve.radius = 10;
curve.curveType = CUBIC_BSPLINE;
// curve.curveType = FOUR_POINT;
// make a texture map
texWidth = texHeight = 512;
texture = new GLubyte[4*texWidth*texHeight];
for(int x=0;x<texWidth;x++)
for(int y=0;y<texHeight;y++)
{
float n = noise2(x/100.,y/100.);
GLubyte v = n*255;
// printf("%f(%d) ",n,v);
texture[4*(x + y*texWidth)] = v;
texture[4*(x + y*texWidth)+1] = v;
texture[4*(x + y*texWidth)+2] = v;
texture[4*(x + y*texWidth)+3] = v;
}
// initialize texturing
glTexImage2D(GL_TEXTURE_2D,0,4,texHeight,texWidth,0,GL_RGBA,
GL_UNSIGNED_BYTE,texture);
// glTexImage2D(GL_TEXTURE_2D,0,1,texHeight,texWidth,0,GL_LUMINANCE,
// GL_UNSIGNED_BYTE,texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// filtering
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glutCreateMenu(menuSelect);
glutAddMenuEntry("Cubic B-Spline",0);
glutAddMenuEntry("Four Point",1);
glutAddMenuEntry("Control Polygon",2);
glutAddMenuEntry("Opaque/Transparent",3);
glutAddMenuEntry("Thicker",4);
glutAddMenuEntry("Thinner",5);
glutAddMenuEntry("Toggle Tapering",6);
glutAddMenuEntry("Toggle Texture",7);
glutAddMenuEntry("Quit",8);
glutAttachMenu(GLUT_MIDDLE_BUTTON);
glutMainLoop();
return 0;
}
GLenum checkForError(char *loc)
{
GLenum errCode;
const GLubyte *errString;
if ((errCode = glGetError()) != GL_NO_ERROR)
{
errString = gluErrorString(errCode);
printf("OpenGL error: %s",errString);
if (loc != NULL)
printf("(%s)",loc);
printf("\n");
}
return errCode;
}