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
|
#include <GL/glut.h>
void mouseCB(int button, int stat, int x, int y);
void mouseMotionCB(int x, int y);
bool mouseLeftDown;
bool mouseRightDown;
float mouseX, mouseY;
float cameraAngleX = 0;
float cameraAngleY = 0;
float cameraDistance;
bool dlUsed;
GLdouble vertex[][3] = { //Manually copied from Dodecahedron.obj file
{-0.57735,-0.57735, 0.57735},
{0.934172, 0.356822, 0},
{0.934172, -0.356822, 0},
{-0.934172, 0.356822, 0},
{-0.934172, -0.356822, 0},
{0, 0.934172, 0.356822},
{0,0.934172, -0.356822},
{0.356822, 0, -0.934172},
{-0.356822, 0, -0.934172},
{0,-0.934172, -0.356822},
{0,-0.934172, 0.356822},
{0.356822, 0, 0.934172},
{-0.356822, 0, 0.934172},
{0.57735, 0.57735, -0.57735},
{0.57735, 0.57735, 0.57735},
{-0.57735, 0.57735, -0.57735},
{-0.57735, 0.57735, 0.57735},
{0.57735, -0.57735, -0.57735},
{0.57735, -0.57735, 0.57735},
{-0.57735, -0.57735, -0.57735}
};
int face[][4] = { //Manually copied from Dodecahedron.obj file but subtracted 1 to each
{18, 2, 1}, //values because of index concerns...
{11, 18,1},
{14,11, 1},
{ 7, 13, 1 },
{17, 7, 1 },
{2, 17, 1 },
{ 19, 4, 3},
{ 8, 19, 3},
{ 15, 8, 3},
{ 12, 16, 3},
{ 0, 12, 3},
{ 4, 0, 3},
{ 6, 15, 3},
{ 5, 6, 3},
{ 16, 5, 3},
{ 5, 14, 1},
{ 6, 5, 1},
{ 13, 6, 1},
{ 9, 17, 2},
{ 10, 9, 2},
{ 18, 10, 2},
{ 10, 0, 4},
{ 9, 10, 4},
{ 19, 9, 4},
{ 19, 8, 7},
{ 9, 19, 7},
{ 17, 9, 7},
{ 8, 15, 6},
{ 7, 8, 6},
{ 13, 7, 6},
{ 11, 14, 5},
{ 12, 11, 5},
{ 16, 12, 5},
{ 12, 0, 10},
{ 11, 12, 10},
{ 18, 11, 10}
};
void idle()
{
glutPostRedisplay();
}
void display(void)
{
int i;
int j;
//static int r = 0; /*rotation angle */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glLoadIdentity();
/*Viewpoint position and line of sight direction */
gluLookAt(3.0, 4.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
/*Rotation of figure */
//glRotated((double)r, 0.0, 1.0, 0.0);
//insert from teapot
// tramsform camera ; this helps to activate the the mouseCB and mouseMotionCB :)
glTranslatef(0, 0, cameraDistance);
glRotatef(cameraAngleX, 1, 0, 0); // pitch
glRotatef(cameraAngleY, 0, 1, 0); // heading
//
/*Drawing figures */
glColor3d(0.0, 1.0, 0.0);
glBegin(GL_TRIANGLES); //originial GL_QUADS
for (j = 0; j <36; ++j) { //original j<6
for (i = 0; i <3; ++i) { //original i<4
glVertex3dv(vertex[face[j][i]]);
}
}
glEnd();
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINE_LOOP);
for (j = 0; j <36; ++j) { //original j<6
for (i = 0; i <3; ++i) { //original i<4
glVertex3dv(vertex[face[j][i]]);
}
}
glEnd();
glutSwapBuffers();
/*Return the rotation angle to 0 when turning around */
//if (++r >= 360) r = 0;
}
void resize(int w, int h)
{
glViewport(0, 0, w, h);
/*Setting perspective transformation matrix */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(30.0, (double)w / (double)h, 1.0, 100.0);
/*Model view transformation matrix setting */
glMatrixMode(GL_MODELVIEW);
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 'q':
case 'Q':
case '\033': /*'\ 033' is the ASCII code of ESC */
exit(0);
default:
break;
}
}
void init(void)
{
glClearColor(1.0, 1.0, 1.0, 1.0);
}
void mouseCB(int button, int state, int x, int y)
{
mouseX = x;
mouseY = y;
if (button == GLUT_LEFT_BUTTON)
{
if (state == GLUT_DOWN)
{
mouseLeftDown = true;
}
else if (state == GLUT_UP)
mouseLeftDown = false;
}
else if (button == GLUT_RIGHT_BUTTON)
{
if (state == GLUT_DOWN)
{
mouseRightDown = true;
}
else if (state == GLUT_UP)
mouseRightDown = false;
}
}
void mouseMotionCB(int x, int y)
{
if (mouseLeftDown)
{
cameraAngleY += (x - mouseX);
cameraAngleX += (y - mouseY);
mouseX = x;
mouseY = y;
}
if (mouseRightDown)
{
cameraDistance += (y - mouseY) * 0.2f;
mouseY = y;
}
glutPostRedisplay();
}
///////////////////////////////////////////////////////////////////////////////
// initialize global variables
///////////////////////////////////////////////////////////////////////////////
bool initSharedMem()
{
mouseLeftDown = mouseRightDown = false;
dlUsed = true;
return true;
}
int main(int argc, char * argv[])
{
initSharedMem();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE|GLUT_DEPTH);
glutInitWindowSize(1000, 1000);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_DEPTH_TEST);
glutDisplayFunc(display);
glutReshapeFunc(resize);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouseCB);
glutMotionFunc(mouseMotionCB);
init();
glutMainLoop();
return 0;
}
|