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 244 245 246 247 248 249 250 251 252 253 254 255 256
|
#include <iostream>
#include <fstream>
#include <cmath>
#include "CIsoSurface.h"
#include <iomanip>
#include <GL/glut.h>
#include "importOBJ.h"
using namespace std;
int lines_faces = 0, lines_faces1 = 0;
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[35000000][3] = { //for random 35000000
{},
};
int face[35000000][3] = { //for random
{},
};
void idle()
{
glutPostRedisplay();
}
void display(void)
{
//Start Importing values from any OBJ Files...
vector<Point> vertices;
vector<Triangle> faces;
string filename = "C:/Users/user/Downloads/bunny/bunny.obj"; //string filename = "file.OBJ";
getFacesAndVertices(filename, vertices, faces);
cout << "Vertices:\n";
int point = 0;
for (Point p : vertices)
{
vertex[point][0] = p.x;
vertex[point][1] = p.y;
vertex[point][2] = p.z;
//cout << vertex[point][0] << " " << vertex[point][1] << " " << vertex[point][2] << '\n';
point++;
}
cout << "\n\n";
int triangles = 0;
cout << "Triangles\n";
for (Triangle t : faces) {
face[triangles][0] = t.i-1;
face[triangles][1] = t.j-1;
face[triangles][2] = t.k-1;
//cout << face[triangles][0] << " " << face[triangles][1] << " " << face[triangles][2] << '\n';
triangles++;
lines_faces1++;
}
// lighting
// setup lighting 0 ; STARTS HERE!!!!
glEnable(GL_LIGHT0);
const GLfloat LightAmbient[4] = { 0.1f, 0.1f, 0.1f, 1.0f };
const GLfloat LightDiffuse[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat LightSpecular[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmbient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, LightDiffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, LightSpecular);
const GLfloat diffuseCoeff[] = { 0.2, 0.4, 0.9, 1.0 };
const GLfloat specularCoeff[] = { 1.0,1.0,1.0,1.0 };
//Color material of object when light is enabled
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, diffuseCoeff);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specularCoeff);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 25.0);
const GLfloat light_x = 1.0f;
const GLfloat light_y = 1.0f;
const GLfloat light_z = 1.0f;
const GLfloat LightPosition[4] = { light_x, light_y, light_z, 1 };
glLightfv(GL_LIGHT0, GL_POSITION, LightPosition);
//glEnable(GL_LIGHTING); //This enables lighting
// setup lighting 0 ; ENDS HERE!!!!
int i;
int j;
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
glColor3d(0.0, 1.0, 0.0);
glBegin(GL_TRIANGLES); //original GL_QUADS
for (j = 0; j <lines_faces1; ++j) { //original j<6
for (i = 0; i <3; ++i) { //original i<4
glVertex3dv(vertex[face[j][i]]);
}
}
glEnd();
glEnable(GL_LINE_SMOOTH);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
//TO SHOW LINES IN MARCHING CUBES!
//glColor3f(0.0, 0.0, 0.0);
//glBegin(GL_LINE_LOOP);
//for (j = 0; j <lines_faces1; ++j) { //original j<6
// for (i = 0; i <3; ++i) { //original i<4
// glVertex3dv(vertex[face[j][i]]);
// }
//}
//glEnd();
lines_faces1 = 0;
point = 0, triangles = 0;
glutSwapBuffers();
}
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();
system("pause");
return 0;
}
| |