标签:一个 ring spec erro space 场景 坐标 play exit
分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!http://www.captainbed.net
#include "windows.h"
#include <GL/glut.h>
#include <GL/GLAUX.H>
#include <iostream>
using namespace std;
//
typedef GLbyte* bytePt;
int winWidth = 400;
int winHeight = 400;
int arrLen = winWidth * winHeight * 3;
GLbyte* colorArr = new GLbyte[ arrLen ];
void saveColorData(bytePt& _pt, string& _str) {
FILE* pFile = NULL;
pFile = fopen(_str.c_str(), "wt");
if(!pFile) { fprintf(stderr, "error \n"); exit(-1); }
for(int i=0; i<winWidth * winHeight * 3; i ++) {
if(colorArr[i] == -1) { colorArr[i] = 255; }
}
for(int i=0; i<winWidth * winHeight * 3; i ++) {
fprintf(pFile, "%d\n", colorArr[i]);
}
fclose(pFile);
printf("color data saved! \n");
}
void init() {
glClearColor(0.5, 0.5, 0.5, 0.0);
glShadeModel(GL_SMOOTH);
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, 100.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, 1.0, 0.1, 500.0);
glMatrixMode(GL_MODELVIEW);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_TRIANGLES);
glVertex3f(0.0, 25.0, 0.0);
glVertex3f(-25.0, -25.0, 0.0);
glVertex3f(25.0, -25.0, 0.0);
glEnd();
glFlush();
}
void keyboard(unsigned char key, int x , int y) {
GLint viewPort[4] = {0};
switch(key) {
case ‘c‘:
case ‘C‘:
glGetIntegerv(GL_VIEWPORT, viewPort);
glReadPixels(viewPort[0], viewPort[1], viewPort[2], viewPort[3], GL_RGB, GL_UNSIGNED_BYTE, colorArr);
printf("color data read !\n");
saveColorData(colorArr, (string)"tmpcolor.txt");
default:
break;
}
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(200, 200);
glutInitWindowSize(400, 400);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
delete [] colorArr;
return 0;
}
function test
a = load(‘tmpcolor.txt‘);
pos = find(a == -1);
a(pos) = 255;
r = a(1:3:end);
g = a(2:3:end);
b = a(3:3:end);
img = zeros(400,400,3);
img(:,:,1) = reshape(r,[400,400]);
img(:,:,2) = reshape(g,[400,400]);
img(:,:,3) = reshape(b,[400,400]);
img = uint8(img);
tmpR = zeros(400,400); tmpR = img(:,:,1);
tmpG = zeros(400,400); tmpG = img(:,:,2);
tmpB = zeros(400,400); tmpB = img(:,:,3);
for i=1:1:400
img(i,:,1) = tmpR(:,400-i+1);
img(i,:,2) = tmpG(:,400-i+1);
img(i,:,3) = tmpB(:,400-i+1);
end
figure;
imshow(img);
end
void saveColorData2img(bytePt& _pt, string& _str) {
cv::Mat img;
vector<cv::Mat> imgPlanes;
img.create(winHeight, winWidth, CV_8UC3);
cv::split(img, imgPlanes);
for(int i = 0; i < winHeight; i ++) {
UCHAR* plane0Ptr = imgPlanes[0].ptr<UCHAR>(i);
UCHAR* plane1Ptr = imgPlanes[1].ptr<UCHAR>(i);
UCHAR* plane2Ptr = imgPlanes[2].ptr<UCHAR>(i);
for(int j = 0; j < winWidth; j ++) {
int k = 3 * (i * winWidth + j);
plane2Ptr[j] = _pt[k];
plane1Ptr[j] = _pt[k+1];
plane0Ptr[j] = _pt[k+2];
}
}
cv::merge(imgPlanes, img);
cv::flip(img, img ,0); // !!!
cv::imwrite(_str.c_str(), img);
printf("opencv save opengl img done! \n");
}
class glGrabber {
public:
glGrabber();
~glGrabber();
void glGrab();
void saveColorData2Img(string& _str);
private:
GLbyte* colorArr;
GLint viewPort[4];
int winWidth;
int winHeight;
};
//
glGrabber::glGrabber() {
colorArr = NULL;
}
//
glGrabber::~glGrabber() {
if(colorArr!=NULL) { delete [] colorArr; colorArr = NULL; }
}
//
void glGrabber::glGrab() {
glGetIntegerv(GL_VIEWPORT, viewPort);
if(colorArr != NULL) { delete [] colorArr; colorArr = NULL; }
winWidth = viewPort[2];
winHeight = viewPort[3];
colorArr = new GLbyte[ winWidth * winHeight * 4 ]; // MFC的像素格式只支持RGBA
glReadPixels(viewPort[0], viewPort[1], viewPort[2], viewPort[3], GL_RGBA, GL_UNSIGNED_BYTE, colorArr); // RGBA
printf("x: %d, y: %d, window width: %d, window height: %d \n", viewPort[0], viewPort[1], viewPort[2], viewPort[3]);
printf("color data read! \n");
}
//
void glGrabber::saveColorData2Img(string& _str) {
cv::Mat img;
vector<cv::Mat> imgPlanes;
img.create(winHeight, winWidth, CV_8UC3);
cv::split(img, imgPlanes);
for(int i = 0; i < winHeight; i ++) {
UCHAR* plane0Ptr = imgPlanes[0].ptr<UCHAR>(i);
UCHAR* plane1Ptr = imgPlanes[1].ptr<UCHAR>(i);
UCHAR* plane2Ptr = imgPlanes[2].ptr<UCHAR>(i);
for(int j = 0; j < winWidth; j ++) {
int k = 4 * (i * winWidth + j); // RGBA
plane2Ptr[j] = colorArr[k];
plane1Ptr[j] = colorArr[k+1];
plane0Ptr[j] = colorArr[k+2];
}
}
cv::merge(imgPlanes, img);
cv::flip(img, img ,0); // !!!
cv::namedWindow("openglGrab");
cv::imshow("openglGrab", img);
cv::waitKey();
//cv::imwrite(_str.c_str(), img);
printf("opencv save opengl img done! \n");
}
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!http://www.captainbed.net
标签:一个 ring spec erro space 场景 坐标 play exit
原文地址:https://www.cnblogs.com/sownchz/p/10500021.html