opengl 画骨架

来源:互联网 发布:saber软件百度云 编辑:程序博客网 时间:2024/05/23 16:59

rt, 本程序展示如何用opengl画人体骨架, 并用鼠标旋转。


头文件 reparse.h

#ifndef REPARSE_H_#define REPARSE_H_#include <stdlib.h>#include <stdio.h>#include <iostream>using namespace std;#include <vector>typedef vector<float> mypoint;typedef vector<mypoint> myskeleton;typedef vector<myskeleton> myskeletons;myskeleton reshape_mk(myskeleton mk);void get_mk_box(myskeleton mk, float *box);myskeleton readsingle(string name){FILE *fp = fopen(name.c_str(), "r");int num = 1;int jo = 15;float x = 0, y = 0, z = 0;myskeleton mk;mk.resize(jo);for (int j = 0; j< jo; j++){mypoint mp;mp.resize(3);fscanf(fp, "%f %f %f\n", &x, &y, &z);mp[0] = x;mp[1] = y;mp[2] = z;mk[j] = mp;}return reshape_mk(mk);}myskeleton reshape_mk(myskeleton mk){int jo = 15;float box[4];get_mk_box(mk, box);float len = box[3] - box[2];for (int j = 0; j< jo; j++){mk[j][0] *= 100/len;mk[j][1] *= 100/len;mk[j][2] *= 100/len;}mypoint mp = mk[8];for (int j = 0; j< jo; j++){mk[j][0] -= mp[0];mk[j][1] -= mp[1];mk[j][2] -= mp[2];}return mk;}void get_mk_box(myskeleton mk, float *box){//xbox[0] = 100000000;box[1] = -100000000;//ybox[2] = 100000000;box[3] = -100000000;for (int j = 0; j< mk.size(); j++){mypoint mp = mk[j];// xif (box[0] > mp[0]){box[0] = mp[0];}if (box[1] < mp[0]){box[1] = mp[0];}// yif (box[2] > mp[1]){box[2] = mp[1];}if (box[3] < mp[1]){box[3] = mp[1];}}}#endif


opengl显示文件 glshow.cpp

#include <Windows.h>#include <gl/glut.h>#include <opencv2/core/core.hpp>#include <opencv2/highgui/highgui.hpp>#include <opencv2/imgproc/imgproc.hpp>using namespace cv;#include <iostream>using namespace std;#include "reparse.h"myskeleton mk;float box[6];// mousebool mouseLeftDown =false;bool mouseRightDown = false;float mouseX = 0;float mouseY = 0;float cameraDistance = 0;;float cameraAngleX = 0;float cameraAngleY = 0;// keyboadrdfloat xRot = 0;float yRot = 0;float zRot = 0;string ind;int height = 0;int width = 0;void savepic(string name){GLint viewPort[4] = {0};glGetIntegerv(GL_VIEWPORT, viewPort);int winrows = viewPort[3];int wincols = viewPort[2];GLbyte *colorArr = new GLbyte[winrows*wincols*3];glReadPixels(viewPort[0], viewPort[1], viewPort[2], viewPort[3], GL_RGB, GL_UNSIGNED_BYTE, colorArr);for(int i=0; i<winrows * wincols * 3; i ++) {if(colorArr[i] == -1) { colorArr[i] = 255; }}Mat show = Mat(winrows, wincols, CV_8UC3, Scalar::all(0));for (int i = 0; i< winrows; i++){for (int j = 0; j< wincols; j++){show.at<Vec3b>(winrows-i-1, j) = Vec3b(colorArr[(i*wincols+j)*3+2], colorArr[(i*wincols+j)*3+1], colorArr[(i*wincols+j)*3]);}}delete colorArr;imwrite(name, show);// imshow("show", show);// waitKey(33);}void RenderBone(float x0, float y0, float z0, float x1, float y1, float z1){GLdouble  dir_x = x1 - x0;GLdouble  dir_y = y1 - y0;GLdouble  dir_z = z1 - z0;GLdouble  bone_length = sqrt( dir_x*dir_x + dir_y*dir_y + dir_z*dir_z );static GLUquadricObj *  quad_obj = NULL;if ( quad_obj == NULL )quad_obj = gluNewQuadric();gluQuadricDrawStyle( quad_obj, GLU_FILL );gluQuadricNormals( quad_obj, GLU_SMOOTH );// glPushMatrix();glTranslated( x0, y0, z0 );double  length;length = sqrt( dir_x*dir_x + dir_y*dir_y + dir_z*dir_z );if ( length < 0.0001 ) { dir_x = 0.0; dir_y = 0.0; dir_z = 1.0;  length = 1.0;}dir_x /= length;  dir_y /= length;  dir_z /= length;GLdouble  up_x, up_y, up_z;up_x = 0.0;up_y = 1.0;up_z = 0.0;double  side_x, side_y, side_z;side_x = up_y * dir_z - up_z * dir_y;side_y = up_z * dir_x - up_x * dir_z;side_z = up_x * dir_y - up_y * dir_x;length = sqrt( side_x*side_x + side_y*side_y + side_z*side_z );if ( length < 0.0001 ) {side_x = 1.0; side_y = 0.0; side_z = 0.0;  length = 1.0;}side_x /= length;  side_y /= length;  side_z /= length;up_x = dir_y * side_z - dir_z * side_y;up_y = dir_z * side_x - dir_x * side_z;up_z = dir_x * side_y - dir_y * side_x;GLdouble  m[16] = { side_x, side_y, side_z, 0.0,up_x,   up_y,   up_z,   0.0,dir_x,  dir_y,  dir_z,  0.0,0.0,    0.0,    0.0,    1.0 };glMultMatrixd( m );GLdouble radius= 1;GLdouble slices = 10.0;GLdouble stack = 10.0;gluCylinder( quad_obj, radius, radius, bone_length, slices, stack );GLdouble  m2[16] = { side_x, up_x, dir_x, 0.0,side_y,   up_y,  dir_y,   0.0,side_z,  up_z,  dir_z,  0.0,0.0,    0.0,    0.0,    1.0 };glMultMatrixd( m2 );glTranslated( -x0, -y0, -z0 );// glPopMatrix();}void myDisplay(void){glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);glPushMatrix();glRotatef(xRot, 1, 0, 0);glRotatef(yRot, 0, 1, 0);glRotatef(zRot, 0, 0, 1);glTranslatef(mk[8][0], mk[8][1], mk[8][2]);glRotatef(cameraAngleX, 1, 0, 0);glRotatef(cameraAngleY, 0, 1, 0);glTranslatef(-mk[8][0], -mk[8][1], -mk[8][2]);glColor3f(1.0, 0.0, 0.0);for (int i = 0; i< 15; i++){mypoint mp = mk[i];glTranslatef(mp[0], mp[1], mp[2]);glutSolidSphere(2, 10, 10);glTranslatef(-mp[0], -mp[1], -mp[2]);}glColor3f(1, 1, 0);float ind[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};float ind2[] = {0, 1, 2, 3, 1, 5, 6, 1, 8, 9, 10, 8, 12, 13};// float ind[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};// float ind2[] = {0, 1, 2, 3, 1, 5, 6, 1, 8, 9, 10, 11, 8, 13, 14, 15};for (int i = 0; i< 14; i++){int in1 = ind[i];int in2 = ind2[i];mypoint mp = mk[in1];mypoint mp2 = mk[in2];// glVertex3f(mp[0], mp[1], mp[2]);// glVertex3f(mp2[0], mp2[1], mp2[2]);if ( i == 0 || i == 7){glColor3f(1, 1, 1);}if ( i == 1 || i == 2 ||i == 3 || i == 8 ||i == 9 || i == 10 ){glColor3f(0, 0.5, 0.7);}if ( i == 4 || i == 5 ||i == 6 || i == 11 ||i == 12 || i == 13 ){glColor3f(0, 1, 0);}RenderBone(mp[0], mp[1], mp[2],  mp2[0], mp2[1], mp2[2]);}glPopMatrix();glutSwapBuffers();}void SetupRC(void){glClearColor (1, 1, 1, 1);glClearDepth(1);glEnable(GL_DEPTH_TEST);glShadeModel(GL_SMOOTH);GLfloat _ambient[]={1.0,1.0,1.0,1.0};GLfloat _diffuse[]={1.0,1.0,1.0,1.0};GLfloat _specular[]={1.0,1.0,1.0,1.0};GLfloat _position[]={-0,200,200,0};glLightfv(GL_LIGHT0,GL_AMBIENT,_ambient);glLightfv(GL_LIGHT0,GL_DIFFUSE,_diffuse);glLightfv(GL_LIGHT0,GL_SPECULAR,_specular);glLightfv(GL_LIGHT0,GL_POSITION,_position);glEnable(GL_LIGHT0);glEnable(GL_LIGHTING);glColorMaterial(GL_FRONT, GL_DIFFUSE); glEnable(GL_COLOR_MATERIAL);//启用颜色追踪 // best qualityglHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);}void reshape(int w,int h){float xlen = box[1] - box[0];float ylen = box[3] - box[2];int base = 50;GLfloat x = -base*xlen/ylen;GLfloat y = -base;GLfloat z = -10*base;GLfloat x2 = x + 2*base*xlen/ylen;GLfloat y2 = y + 2*base;GLfloat z2 = z + 10*2*base;h = (h/4+1)*4;w = (w/4+1)*4;glViewport (0, 0, (GLsizei) w, (GLsizei) h);glMatrixMode(GL_PROJECTION);glLoadIdentity();glOrtho(box[0] - 10, box[1]+10, box[2]-10, box[3]+10, z,  z2);glMatrixMode(GL_MODELVIEW);glLoadIdentity();}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;}elsemouseLeftDown = false;}}void mouseMotionCB(int x, int y){if (mouseLeftDown){cameraAngleY += (x - mouseX);cameraAngleX += (y - mouseY);mouseX = x;mouseY = y;glutPostRedisplay();}}void keyboard(unsigned char key, int x, int y)  {  if (key == 'a'){savepic(ind + ".jpg");glutPostRedisplay();}if (key == 'z'){zRot += 5;}if (key == 'x'){zRot -= 5;}glutPostRedisplay();}void keyboard2(int key, int x, int y)  {  if (key == GLUT_KEY_UP){xRot -= 5;}if (key == GLUT_KEY_DOWN){xRot += 5;}if (key == GLUT_KEY_LEFT){yRot -= 5;}if (key == GLUT_KEY_RIGHT){yRot += 5;}glutPostRedisplay();}void frameDisplay(void){glutPostRedisplay();}int main(int argc, char *argv[]){/*if (argc != 2){return -1;}*/string filename = "3.txt";mk = readsingle(filename);get_mk_box(mk, box);float xlen = box[1] - box[0];float ylen = box[3] - box[2];height = 800;width = 800*xlen/ylen;width = (width/4+1)*4;for (int i = 0; i< 15; i++){mk[i][0] *= 1;mk[i][1] *= 1;}int a = filename.find_last_of('\\');int b = filename.find_first_of('.');ind = filename.substr(a+1, b-a-1);glutInit(&argc, argv);  glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH); glutInitWindowSize(width, height);    //显示框大小glutInitWindowPosition(100,100); //确定显示框左上角的位置glutCreateWindow("last window");SetupRC();glutDisplayFunc(myDisplay);glutReshapeFunc(reshape);// glutIdleFunc(frameDisplay);glutKeyboardFunc(keyboard);glutSpecialFunc(keyboard2);glutMouseFunc(mouseCB);glutMotionFunc(mouseMotionCB);glutMainLoop();return 0;}


输入: 15x3的骨架坐标

-231.897729 253.621091 -201.321746-236.962886 234.089934 -217.952419-218.731232 227.771542 -224.344949-211.455765 202.000900 -229.395108-220.457684 184.508665 -211.269169-255.276888 232.137040 -212.173848-261.020481 207.339936 -221.904073-253.934963 181.556302 -222.653852-243.250000 190.770000 -246.930000-233.681006 189.507187 -247.926585-216.917963 149.907593 -251.121406-204.210428 112.041004 -267.368696-252.884553 189.665609 -246.599485-254.062059 147.814377 -236.283369-254.472998 104.712990 -237.481678



输出:



over。




原创粉丝点击