第1关 简单实体构建
#include <GL/freeglut.h>
#include<stdio.h>
#include<iostream>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
using namespace std;
GLfloat points1[4][3] = {{0 , 1.7 , 0},{-1, 0 , 1},{1 , 0 , 1},{0 , 0 , -0.7}};
GLfloat Colors1[4][3] = {{0 , 0, 1}, {1 , 0, 0},{1 , 1, 0},{0 , 1, 0} };
int vertice1[4][3] = {{0,1,2},{0,2,3},{0,1,3},{1,2,3}};
void InitGL(GLvoid)
{
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glEnable(GL_COLOR_MATERIAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}
void Create()
{
glBegin(GL_TRIANGLES);
for (int i = 0; i < 4; i++)
{
glColor3fv(Colors1[i]);
for (int j = 0; j < 3; j++)
{
int VtxId = vertice1[i][j];
glVertex3fv(points1[VtxId]);
}
}
glEnd();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glPushMatrix();
glTranslatef(0.0f,0.2f,-3.0f);
glRotatef(95.0f,1.0f,0.0f, 0.0f);
Create();
glPopMatrix();
glutSwapBuffers();
}
void reshape(int width, int height)
{
if (height == 0)
height = 1;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char *argv[])
{
GLubyte* pPixelData = (GLubyte*)malloc(400 * 400 * 3);
GLint viewport[4] = {0};
glutInit(&argc, argv);
glutInitWindowPosition(100, 100);
glutInitWindowSize(400, 400);
glutCreateWindow("几何变换示例");
InitGL();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoopEvent();
glReadBuffer(GL_FRONT);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
glGetIntegerv(GL_VIEWPORT, viewport);
glReadPixels(viewport[0], viewport[1], viewport[2], viewport[3], GL_RGB, GL_UNSIGNED_BYTE, pPixelData);
cv::Mat img;
std::vector<cv::Mat> imgPlanes;
img.create(400, 400, CV_8UC3);
cv::split(img, imgPlanes);
for (int i = 0; i < 400; i++) {
unsigned char* plane0Ptr = imgPlanes[0].ptr<unsigned char>(i);
unsigned char* plane1Ptr = imgPlanes[1].ptr<unsigned char>(i);
unsigned char* plane2Ptr = imgPlanes[2].ptr<unsigned char>(i);
for (int j = 0; j < 400; j++) {
int k = 3 * (i * 400 + j);
plane2Ptr[j] = pPixelData[k];
plane1Ptr[j] = pPixelData[k + 1];
plane0Ptr[j] = pPixelData[k + 2];
}
}
cv::merge(imgPlanes, img);
cv::flip(img, img, 0);
cv::namedWindow("openglGrab");
cv::imshow("openglGrab", img);
cv::imwrite("../img_step1/test.jpg", img);
return 0;
}
第2关 简单机器人构建
#include <GL/freeglut.h>
#include<stdio.h>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
GLint winWidth = 500, winHeight =500 ;
GLfloat x0 = 2.0, yy = 2.0, z0 = 5.0;
GLfloat xref = 0.0, yref = 0.0, zref = 0.0;
GLfloat Vx = 0.0, Vy = 1.0, Vz = 0.0;
GLfloat xwMin = -1.0, ywMin = -1.0, xwMax = 1.0, ywMax = 1.0;
GLfloat dnear = 1.5, dfar = 20.0;
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
gluLookAt(x0, yy, z0, xref, yref, zref, Vx, Vy, Vz);
glPushMatrix();
glColor3f(1.0f,1.0f,1.0f);
glTranslatef(0.0f,1.4f,0.0f);
glScalef(0.5,0.7,0.5);
glutSolidCube(0.5);
glPopMatrix();
glPushMatrix();
glColor3f(1.0f,0.5f,0.2f);
glTranslatef(0.0f,1.9f,0.0f);
glScalef(1.5,1.5,0.5);
glutSolidCube(0.5);
glPopMatrix();
glPushMatrix();
glColor3f(1.0f,0.0f,0.0f);
glTranslatef(0.0f,0.25f,0.0f);
glScalef(4.0f,4.0f,0.5f);
glutSolidCube(0.5);
glPopMatrix();
glPushMatrix();
glColor3f(1.0f,1.0f,0.0f);
glTranslatef(-1.25f,0.5f,0.0f);
glScalef(1.0,3.0,0.5);
glutSolidCube(0.5);
glPopMatrix();
glPushMatrix();
glColor3f(1.0f,1.0f,0.0f);
glTranslatef(1.25f,0.5f,0.0f);
glScalef(1.0,3.0,0.5);
glutSolidCube(0.5);
glPopMatrix();
glPushMatrix();
glColor3f(0.5f,0.5f,1.5f);
glTranslatef(-0.5f,-1.5f,0.0f);
glScalef(1.0,3.0,0.5);
glutSolidCube(0.5);
glPopMatrix();
glPushMatrix();
glColor3f(0.5f,0.5f,1.5f);
glTranslatef(0.5f,-1.5f,0.0f);
glScalef(1.0,3.0,0.5);
glutSolidCube(0.5);
glPopMatrix();
GLUquadricObj *sphere;
sphere=gluNewQuadric();
glPushMatrix();
glColor3f(1.0, 0.5, 0.2);
glTranslatef(-1.25f,-0.5f,0.0f);
glScalef(1,1.5,1);
gluSphere(sphere,0.25,50,50);
glPopMatrix();
glPushMatrix();
glColor3f(1.0, 0.5, 0.2);
glTranslatef(1.25f,-0.5f,0.0f);
glScalef(1,1.5,1);
gluSphere(sphere,0.25,50,50);
glPopMatrix();
glFlush();
}
void reshape(GLint newWidth, GLint newHeight)
{
glViewport(0, 0, newWidth, newHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(xwMin, xwMax, ywMin, ywMax, dnear, dfar);
glMatrixMode(GL_MODELVIEW);
winWidth = newWidth;
winHeight = newHeight;
}
int main(int argc, char* argv[])
{
GLubyte* pPixelData = (GLubyte*)malloc(500 * 500 * 3);
GLint viewport[4] = { 0 };
glutInit(&argc, argv);
glutInitWindowPosition(100, 100);
glutInitWindowSize( 500 , 500 );
glutCreateWindow("三维观察");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoopEvent();
glReadBuffer(GL_FRONT);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
glGetIntegerv(GL_VIEWPORT, viewport);
glReadPixels(viewport[0], viewport[1], viewport[2], viewport[3], GL_RGB, GL_UNSIGNED_BYTE, pPixelData);
cv::Mat img;
std::vector<cv::Mat> imgPlanes;
img.create(500, 500, CV_8UC3);
cv::split(img, imgPlanes);
for (int i = 0; i < 500; i++) {
unsigned char* plane0Ptr = imgPlanes[0].ptr<unsigned char>(i);
unsigned char* plane1Ptr = imgPlanes[1].ptr<unsigned char>(i);
unsigned char* plane2Ptr = imgPlanes[2].ptr<unsigned char>(i);
for (int j = 0; j < 500; j++) {
int k = 3 * (i * 500 + j);
plane2Ptr[j] = pPixelData[k];
plane1Ptr[j] = pPixelData[k + 1];
plane0Ptr[j] = pPixelData[k + 2];
}
}
cv::merge(imgPlanes, img);
cv::flip(img, img, 0);
cv::namedWindow("openglGrab");
cv::imshow("openglGrab", img);
cv::imwrite("../img_step2/test.jpg", img);
return 0;
}
第3关 Bezier曲线和B样条曲线
#include <GL/freeglut.h>
#include<stdio.h>
#include <stdlib.h>
#include <vector>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
using namespace std;
struct Point
{
int x, y;
};
Point bz[11], bspt[11];
int nInput;
Point pt[4] = { { 50, 100}, { 140,300},{250, 320}, {290, 120} };
Point pt2[4]={ { 450, 100}, { 540,300},{650, 320}, {690, 120} };
void CalcBZPoints()
{
float a0, a1, a2, a3, b0, b1, b2, b3;
a0 = pt[0].x;
a1 = -3 * pt[0].x + 3 * pt[1].x;
a2 = 3 * pt[0].x - 6 * pt[1].x + 3 * pt[2].x;
a3 = -pt[0].x + 3 * pt[1].x - 3 * pt[2].x + pt[3].x;
b0 = pt[0].y;
b1 = -3 * pt[0].y + 3 * pt[1].y;
b2 = 3 * pt[0].y - 6 * pt[1].y + 3 * pt[2].y;
b3 = -pt[0].y + 3 * pt[1].y - 3 * pt[2].y + pt[3].y;
float t = 0;
float dt = 0.01;
for (int i = 0; t < 1.1; t += 0.1, i++)
{
bz[i].x = a0 + a1 * t + a2 * t * t + a3 * t * t * t;
bz[i].y = b0 + b1 * t + b2 * t * t + b3 * t * t * t;
}
}
void CalcBSPoints()
{
float a0, a1, a2, a3, b0, b1, b2, b3;
a0 = pt2[0].x + 4 * pt2[1].x + pt2[2].x;
a1 = -3 * pt2[0].x + 3 * pt2[2].x;
a2 = 3 * pt2[0].x - 6 * pt2[1].x + 3 * pt2[2].x;
a3 = -pt2[0].x + 3 * pt2[1].x - 3 * pt2[2].x + pt2[3].x;
b0 = pt2[0].y + 4 * pt2[1].y + pt2[2].y;
b1 = -3 * pt2[0].y + 3 * pt2[2].y;
b2 = 3 * pt2[0].y - 6 * pt2[1].y + 3 * pt2[2].y;
b3 = -pt2[0].y + 3 * pt2[1].y - 3 * pt2[2].y + pt2[3].y;
float t = 0;
float dt = 0.01;
for (int i = 0; t < 1.1; t += 0.1, i++)
{
bspt[i].x = ( a0 + a1 * t + a2 * t * t + a3 * t * t * t ) / 6;
bspt[i].y = ( b0 + b1 * t + b2 * t * t + b3 * t * t * t ) / 6;
}
}
void ControlPoint()
{
glPointSize(2);
for (int i = 0; i < 4; i++)
{
glBegin(GL_POINTS);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2i(pt[i].x, pt[i].y);
glEnd();
}
}
void PolylineGL(Point* pt, int num)
{
glBegin(GL_LINE_STRIP);
for (int i = 0; i < num; i++)
{
glColor3f(0.0f, 1.0f, 0.0f);
glVertex2i(pt[i].x, pt[i].y);
}
glEnd();
}
void PolylineGL1(Point* pt, int num)
{
glBegin(GL_LINE_STRIP);
for (int i = 0; i < num; i++)
{
glColor3f(1.0f, 1.0f, 1.0f);
glVertex2i(pt[i].x, pt[i].y);
}
glEnd();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 1.0f, 1.0f);
ControlPoint();
PolylineGL1(pt, 4);
CalcBZPoints();
PolylineGL(bz, 11);
PolylineGL1(pt2, 4);
CalcBSPoints();
PolylineGL(bspt, 11);
glFlush();
}
void init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)w, 0.0, (GLdouble)h);
}
int main(int argc, char* argv[])
{
GLubyte* pPixelData = (GLubyte*)malloc(800 * 400 * 3);
GLint viewport[4] = { 0 };
glutInit(&argc, argv);
glutInitWindowPosition(100, 100);
glutInitWindowSize( 800 , 400 );
glutCreateWindow("三维观察");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoopEvent();
glReadBuffer(GL_FRONT);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
glGetIntegerv(GL_VIEWPORT, viewport);
glReadPixels(viewport[0], viewport[1], viewport[2], viewport[3], GL_RGB, GL_UNSIGNED_BYTE, pPixelData);
cv::Mat img;
std::vector<cv::Mat> imgPlanes;
img.create(400, 800, CV_8UC3);
cv::split(img, imgPlanes);
for (int i = 0; i < 400; i++) {
unsigned char* plane0Ptr = imgPlanes[0].ptr<unsigned char>(i);
unsigned char* plane1Ptr = imgPlanes[1].ptr<unsigned char>(i);
unsigned char* plane2Ptr = imgPlanes[2].ptr<unsigned char>(i);
for (int j = 0; j < 800; j++) {
int k = 3 * (i * 800 + j);
plane2Ptr[j] = pPixelData[k];
plane1Ptr[j] = pPixelData[k + 1];
plane0Ptr[j] = pPixelData[k + 2];
}
}
cv::merge(imgPlanes, img);
cv::flip(img, img, 0);
cv::namedWindow("openglGrab");
cv::imshow("openglGrab", img);
cv::imwrite("../img_step3/test.jpg", img);
return 0;
}