OpenGL human
by Shinigami on Aug.15, 2012, under OpenGL
Hi,
started to learn OpenGL and decided to share my first work
.
It is written using FreeGLUT and C++. It can be rotated using a, d, w, s, q, e buttons. Light can be turned on and of using mouse left button. And it can be zoomed using mouse wheel (not guaranteed to work). It uses OpenGL 3.1 (it will not display human if 4.1 version used.
|
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 257 258 259 260 261 262 |
#include <iostream> #include <cstdlib> #include <cmath> #include <GL/freeglut.h> using namespace std; // Start struct blocks struct xyz { float x, y, z; }; // End struct blocks // Start functions headers void initRendering(); void drawScene(); void handleResize(int, int); void handleKeypress(unsigned char, int, int); void handleMouse(int, int, int, int); void spinDisplay(int, int); void lightBulb(); void human(float, float, float); void cube(xyz, float, float, float); // End function headers // Start global variables bool _light_switch = false; float _angle_x = 0.0f; float _angle_y = 0.0f; float _angle_z = 0.0f; float _distance = 6.0f; // End global variables int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); glutInitWindowPosition(100, 100); glutInitWindowSize(800, 800); glutInitContextVersion(3, 1); //glutInitContextFlags(GLUT_FORWARD_COMPATIBLE); glutCreateWindow("Human"); initRendering(); glutDisplayFunc(drawScene); glutReshapeFunc(handleResize); glutKeyboardFunc(handleKeypress); glutMouseFunc(handleMouse); glutMainLoop(); return 0; } void initRendering() { glClear(GL_COLOR_BUFFER_BIT); glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHTING); glEnable(GL_NORMALIZE); glEnable(GL_COLOR_MATERIAL); glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); glClearColor(0.7f, 0.8f, 1.0f, 1.0f); GLfloat ambientLight[] = {0.5f, 0.5f, 0.5f, 1.0f}; glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight); glShadeModel(GL_SMOOTH); } void drawScene() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0f, 0.0f, -20.0f); glRotatef(-_angle_x, 1.0, 0.0, 0.0f); glRotatef(-_angle_y, 0.0, 1.0, 0.0f); glRotatef(-_angle_z, 0.0, 0.0, 1.0f); human(-1.0, -3.0, _distance); glutSwapBuffers(); } void handleResize(int w, int h) { glViewport(0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0, (float)w / (float)h, 1.0, 200.0); } void handleKeypress(unsigned char key, int x, int y) { switch (key) { case 27: exit(0); break; //Esc case 97: spinDisplay(1, 1); break; // a case 65: spinDisplay(1, 1); break; // A case 100: spinDisplay(-1, 1); break; // d case 68: spinDisplay(-1, 1); break; // D case 83: spinDisplay(-1, 0); break; // S case 115: spinDisplay(-1, 0); break; // s case 87: spinDisplay(1, 0); break; // W case 119: spinDisplay(1, 0); break; // w case 81: spinDisplay(-1, 2); break; //Q case 113: spinDisplay(-1, 2); break; //q case 69: spinDisplay(1, 2); break; // E case 101: spinDisplay(1, 2); break; //e } } void handleMouse(int button, int state, int x, int y) { switch (button) { case GLUT_LEFT_BUTTON: if (state == GLUT_DOWN) { lightBulb(); } else if (state == GLUT_UP) { break; } break; case GLUT_MIDDLE_BUTTON: if (state == GLUT_DOWN) { lightBulb(); } break; case GLUT_RIGHT_BUTTON: break; case 3: _distance -= 0.1f; glutPostRedisplay(); break; case 4: _distance += 0.1f; glutPostRedisplay(); break; default: break; } } void spinDisplay(int in_value, int in_xyz) { switch (in_xyz) { case 0: _angle_x += in_value; if (_angle_x > 360) { _angle_x -= 360; } break; case 1: _angle_y += in_value; if (_angle_y > 360) { _angle_y -= 360; } break; case 2: _angle_z += in_value; if (_angle_z > 360) { _angle_z -= 360; } break; default: break; } glutPostRedisplay(); } void lightBulb() { if (_light_switch == false) { glEnable(GL_LIGHT0); GLfloat lightColorAmbient0[] = {0.2f, 0.2f, 0.2f, 1.0f}; GLfloat lightColorDIFFUSE0[] = {0.8f, 0.8f, 0.8f, 1.0f}; GLfloat lightColorSPECULAR0[] = {0.5f, 0.5f, 0.5f, 1.0f}; GLfloat lightPos[] = {0.0f, 5.0f, 0.0f, 1.0f}; glLightfv(GL_LIGHT0, GL_AMBIENT, lightColorAmbient0); glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColorDIFFUSE0); glLightfv(GL_LIGHT0, GL_SPECULAR, lightColorSPECULAR0); glLightfv(GL_LIGHT0, GL_POSITION, lightPos); _light_switch = true; } else { glDisable(GL_LIGHT0); _light_switch = false; } glutPostRedisplay(); return; } void human(float in_x, float in_y, float in_z) { float head_height = 1; float head_width = 0.8 * head_height; float head_length = 0.6 * head_height; xyz body = {in_x, in_y + 4 * head_height, in_z}; xyz neck = {body.x + 1.1 * head_width, body.y + 3 * head_height, body.z}; xyz head = {neck.x - 0.1 * head_width, neck.y + 0.5 * head_length, neck.z}; xyz left_leg_upper = {body.x, body.y - 2 * head_height, body.z}; xyz left_leg_down = {left_leg_upper.x, left_leg_upper.y - 2 * head_height, left_leg_upper.z}; xyz right_leg_upper = {body.x + 1.7 * head_width, body.y - 2 * head_height, body.z}; xyz right_leg_down = {right_leg_upper.x, right_leg_upper.y - 2 * head_height, right_leg_upper.z}; xyz left_hand_upper = {body.x - head_height, body.y + 2.48 * head_height, body.z}; xyz right_hand_upper = {body.x + 3 * head_width, body.y + 2.48 * head_height, body.z}; float specReflection[] = { 0.8f, 0.8f, 0.8f, 1.0f }; glMaterialfv(GL_FRONT, GL_SPECULAR, specReflection); glMateriali(GL_FRONT, GL_SHININESS, 96); glBegin(GL_QUADS); glColor3f(1.0f, 0.0f, 0.0f); cube(body, 3 * head_width, 3 * head_height, head_length); cube(neck, 0.8 * head_width, 0.5 * head_length, head_length); glColor3f(1.0f, 1.0f, 0.0f); cube(head, head_width, head_height, head_length); glColor3f(0.0f, 1.0f, 0.0f); cube(left_leg_upper, 1.3 * head_width, 2 * head_height, head_length); cube(left_leg_down, 1.3 * head_width, 2 * head_height, head_length); cube(right_leg_upper, 1.3 * head_width, 2 * head_height, head_length); cube(right_leg_down, 1.3 * head_width, 2 * head_height, head_length); glColor3f(0.0f, 0.0f, 1.0f); cube(left_hand_upper, head_height, 0.52 * head_height, head_length); cube(right_hand_upper, head_height, 0.52 * head_height, head_length); glEnd(); return; } void cube(xyz in_coords, float length_x, float length_y, float length_z) { length_x = in_coords.x + length_x; length_y = in_coords.y + length_y; length_z = in_coords.z - length_z; //Front glVertex3f(in_coords.x, in_coords.y, in_coords.z); //A glVertex3f(in_coords.x, length_y, in_coords.z); //B glVertex3f(length_x, length_y, in_coords.z); //C glVertex3f(length_x, in_coords.y, in_coords.z); //D //Back glVertex3f(in_coords.x, in_coords.y, length_z); //A1 glVertex3f(in_coords.x, length_y, length_z); //B1 glVertex3f(length_x, length_y, length_z); //C1 glVertex3f(length_x, in_coords.y, length_z); //D1 //Top glVertex3f(in_coords.x, in_coords.y, length_z); //A1 glVertex3f(in_coords.x, in_coords.y, in_coords.z); //A glVertex3f(length_x, in_coords.y, in_coords.z); //D glVertex3f(length_x, in_coords.y, length_z); //D1 //Bottom glVertex3f(in_coords.x, length_y, length_z); //B1 glVertex3f(in_coords.x, length_y, in_coords.z); //B glVertex3f(length_x, length_y, in_coords.z); //C glVertex3f(length_x, length_y, length_z); //C1 //Left glVertex3f(in_coords.x, in_coords.y, length_z); //A1 glVertex3f(in_coords.x, length_y, length_z); //B1 glVertex3f(in_coords.x, length_y, in_coords.z); //B glVertex3f(in_coords.x, in_coords.y, in_coords.z); //A //Right glVertex3f(length_x, in_coords.y, in_coords.z); //D glVertex3f(length_x, length_y, in_coords.z); //C glVertex3f(length_x, length_y, length_z); //C1 glVertex3f(length_x, in_coords.y, length_z); //D1 return; } |
And here are result.

August 30th, 2012 on 22:47
It’s not openGL 3 – you use deprecated techniques, which currently isn’t exist in the hardware and are emulated in the modern GPUs. For using modern openGL in linux you can learn some examples from this site: http://ogldev.atspace.co.uk
There are also pretty library for using openGL with C++ and without deprecated functionality, called oglplus: http://oglplus.org/
August 31st, 2012 on 21:14
Thanks, will look to it. And my code is old, becouse I use old tutorial to learn, in here http://www.videotutorialsrock.com/opengl_tutorial/get_opengl_setup/home.php