欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

openGL安装配置+三维点坐标显示+64位电脑调试

程序员文章站 2022-07-04 09:09:22
...

http://blog.csdn.net/Jacketinsysu/article/details/49563139(正确)

备注:只需要下载glut文件(包含5个),不需要安装opengl;不需要在vs和系统环境下配置;只需要加入头文件即可


1.下载(glut)

http://blog.csdn.net/Jacketinsysu/article/details/49563139

解压得到5个文件:glut.h,glut.dll,glut32.dll,glut.lib,glut32.lib。

2.安装

安装glut库。假设你的vs的安装路径为MY_VS_ROOT,那么在MY_VS_ROOT/VC/include/下新建一个文件夹GL,然后复制glut.h到这个文件夹下,比如我的就是D:\soft\vs2015\VC\include\GL\glut.h。 
然后复制glut.lib和glut32.lib到MY_VS_ROOT/VC/lib/下,最后复制glut.dll和glut32.dll到系统的dll目录下:C:\Windows\system32文件夹内(32位系统)或‪C:\Windows\SysWOW64(64位系统)。

3.测试

备注:由于vs新建的GL文件夹,因此在写头文件的时候

#include<GL/glut.h>

#include <glut.h>//出错,头文件地址不对
#include<GL/glut.h>
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )
void dis(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glRectf(-0.5f, -0.5f, 0.5f, 0.5f);
    glFlush();
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(400, 400);
    glutCreateWindow("the first openGL");
    glutDisplayFunc(&dis);
    glutMainLoop();
    return 0;
}

4.一堆离散点,画出三维结构,最后拟合曲面

拟合:http://blog.csdn.net/bcbobo21cn/article/details/51073246

http://blog.csdn.net/nick_wang94/article/details/51946702

三维点:http://blog.csdn.net/Carnnation/article/details/44197665

http://blog.csdn.net/zju_fish1996/article/details/51425476

opengl 画兔子: 找原始例子,教程

https://pan.baidu.com/share/home?uk=406211347#category/type=0

5.三维显示------顶点数组


5.1 c++读取txt三维点坐标

#include<fstream>
	ifstream file("result2.txt");
	//	file.open("result2.txt");
	if (!file){
		return 0;
	}
	float x, y, z;
	while (file){
		file >> x >> y >> z;
	}

#include <iostream>  
#include <stdlib.h>  
#include <cv.h>  
#include <cxcore.h>  
#include <highgui.h>  
#include <math.h>  
#include <gl/glut.h>  
#include <fstream>
using namespace std;

float imgdata[500][500];
int w = 0;
int h = 0;
float scalar = 50;//scalar of converting pixel color to float coordinates  

#define pi 3.1415926  
bool mouseisdown = false;
bool loopr = false;
int mx, my;
int ry = 10;
int rx = 10;
int rz = 10;

void timer(int p)
{
	ry -= 5;
	//marks the current window as needing to be redisplayed.  
	glutPostRedisplay();
	if (loopr)
		glutTimerFunc(200, timer, 0);
}


void mouse(int button, int state, int x, int y)
{
	if (button == GLUT_LEFT_BUTTON)
	{
		if (state == GLUT_DOWN)
		{
			mouseisdown = true;
			loopr = false;
		}
		else
		{
			mouseisdown = false;
		}
	}

	if (button == GLUT_RIGHT_BUTTON)
	if (state == GLUT_DOWN)
	{
		loopr = true; glutTimerFunc(200, timer, 0);
	}
}

void motion(int x, int y)
{
	if (mouseisdown == true)
	{
		ry += x - mx;
		rx += y - my;
		mx = x;
		my = y;
		glutPostRedisplay();
	}
}

void special(int key, int x, int y)
{
	switch (key)
	{
	case GLUT_KEY_LEFT:
		ry -= 5;
		glutPostRedisplay();
		break;
	case GLUT_KEY_RIGHT:
		ry += 5;
		glutPostRedisplay();
		break;
	case GLUT_KEY_UP:
		rx += 5;
		glutPostRedisplay();
		break;
	case GLUT_KEY_DOWN:
		rx -= 5;
		glutPostRedisplay();
		break;
	}
}
void renderScene(void) {

	glClear(GL_COLOR_BUFFER_BIT);
	glLoadIdentity();// Reset the coordinate system before modifying  
	gluLookAt(0.0, 0.0, 7.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0);//相机位置,眼睛看物体位置,头顶朝向位置
	//to invert the image  
	glRotatef(ry, 0, 1, 0);
//	glRotatef(rx - 180, 1, 0, 0);
	glRotatef(rx + 90 , 1, 0, 0);
	glRotatef(rz + 90, 0, 0, 1);

	//float imageCenterX = w*.5;
	//float imageCenterY = h*.5;
	float x, y, z;

	glPointSize(1.0);
	glBegin(GL_POINTS);//GL_POINTS  
	ifstream file("result2.txt");
	while (file){
		file >> x >> y >> z;
		x = (x-512)/200;
		y = (y-484)/200;
		z = (255 - z) / 300;
		glColor3f(1,0,0);
		//glVertex3f(0.5, 0.5,0.2); glVertex3f(0.5, 0.8, 0.3); glVertex3f(0.3, 0.5, 4); glVertex3f(0.1, 0.5, 3);
		glVertex3f(x, y, z);//参数的绝对值只能小于1,大于1就没法显示
	}

	//for (int i = 0; i<h; i++){
	//	for (int j = 0; j<w; j++){
	//		// color interpolation  
	//		glColor3f(1 - imgdata[i][j] / 255, imgdata[i][j] / 255, imgdata[i][j] / 255);//red,green,blue  
	//		x = ((float)j - imageCenterX) / scalar;
	//		y = ((float)i - imageCenterY) / scalar;
	//		z = (255 - imgdata[i][j]) / scalar;
	//		glVertex3f(x, y, z);
	//	}
	//}
	glEnd();
	glFlush();
}

void reshape(int w, int h) {
	glViewport(0, 0, (GLsizei)w, (GLsizei)h);//set viewpoint  
	glMatrixMode(GL_PROJECTION);//specify which matrix is the current matrix  
	glLoadIdentity();//replace the current matrix with the identity matrix  
	gluPerspective(60, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
	glMatrixMode(GL_MODELVIEW);
}

void displayDisparity(IplImage* disparity){
	//double xyscale=100;  
	int j = 0;
	int i = 0;
	CvScalar s;

	//accessing the image pixels  
	for (i = 0; i<h; i++){
		for (j = 0; j<w; j++){
			s = cvGet2D(disparity, i, j);//像素点的灰度值
			imgdata[i][j] = s.val[0];//for disparity is a grey image. 
			//其中i代表y轴(第i行),即height;j代表x轴(第j列),即width。
			//cout << imgdata[i][j]<<endl;  
		}
	}
}


int main(int argc, char *argv[])
{
	//cout << "OpenCV and OpenGL works together!" << endl;
	//char* filename = "22.jpg";
	//IplImage* imgGrey = cvLoadImage(filename, 0); //read image as a grey one  

	//ifstream file("result2.txt");
	////	file.open("result2.txt");
	//if (!file){
	//	return 1;
	//}
	//float x, y, z;
	//while (file){
	//	file >> x >> y >> z;
	//}


	//w = imgGrey->width;
	//h = imgGrey->height;

//	displayDisparity(imgGrey);  //二维图像显示
	//cvNamedWindow("original", CV_WINDOW_AUTOSIZE);
	//cvShowImage("original", imgGrey);

	//------------------OpenGL-------------------------  
	glutInit(&argc, argv);//initialize the GLUT library  
	glutInitDisplayMode(GLUT_DEPTH | GLUT_SINGLE | GLUT_RGBA);//sets the initial display mode  
	glutInitWindowPosition(100, 100);
	glutInitWindowSize(500, 500);
//	glutInitWindowSize(1024,968);
	glutCreateWindow("3D disparity image");
	glutDisplayFunc(renderScene);

	glutReshapeFunc(reshape);
	glutMouseFunc(mouse);
	glutMotionFunc(motion);
	glutSpecialFunc(special);
	glutMainLoop();


	cvWaitKey(0);
	//release opencv stuff.  
//	cvReleaseImage(&imgGrey);
	cvDestroyWindow("Original");

	return 0;
}

将一幅图进行三维显示,z深度值是像素值

#include <iostream>  
#include <stdlib.h>  
#include <cv.h>  
#include <cxcore.h>  
#include <highgui.h>  
#include <math.h>  
#include <gl/glut.h>  
using namespace std;

float imgdata[500][500];
int w = 0;
int h = 0;
float scalar = 50;//scalar of converting pixel color to float coordinates  

#define pi 3.1415926  
bool mouseisdown = false;
bool loopr = false;
int mx, my;
int ry = 10;
int rx = 10;

void timer(int p)
{
	ry -= 5;
	//marks the current window as needing to be redisplayed.  
	glutPostRedisplay();
	if (loopr)
		glutTimerFunc(200, timer, 0);
}


void mouse(int button, int state, int x, int y)
{
	if (button == GLUT_LEFT_BUTTON)
	{
		if (state == GLUT_DOWN)
		{
			mouseisdown = true;
			loopr = false;
		}
		else
		{
			mouseisdown = false;
		}
	}

	if (button == GLUT_RIGHT_BUTTON)
	if (state == GLUT_DOWN)
	{
		loopr = true; glutTimerFunc(200, timer, 0);
	}
}

void motion(int x, int y)
{
	if (mouseisdown == true)
	{
		ry += x - mx;
		rx += y - my;
		mx = x;
		my = y;
		glutPostRedisplay();
	}
}

void special(int key, int x, int y)
{
	switch (key)
	{
	case GLUT_KEY_LEFT:
		ry -= 5;
		glutPostRedisplay();
		break;
	case GLUT_KEY_RIGHT:
		ry += 5;
		glutPostRedisplay();
		break;
	case GLUT_KEY_UP:
		rx += 5;
		glutPostRedisplay();
		break;
	case GLUT_KEY_DOWN:
		rx -= 5;
		glutPostRedisplay();
		break;
	}
}
void renderScene(void) {

	glClear(GL_COLOR_BUFFER_BIT);
	glLoadIdentity();// Reset the coordinate system before modifying  
	gluLookAt(0.0, 0.0, 7.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0);
	//to invert the image  
	glRotatef(ry, 0, 1, 0);
	glRotatef(rx - 180, 1, 0, 0);

	float imageCenterX = w*.5;
	float imageCenterY = h*.5;
	float x, y, z;

	glPointSize(1.0);
	glBegin(GL_POINTS);//GL_POINTS  
	for (int i = 0; i<h; i++){
		for (int j = 0; j<w; j++){
			// color interpolation  
			glColor3f(1 - imgdata[i][j] / 255, imgdata[i][j] / 255, imgdata[i][j] / 255);//red,green,blue  
			x = ((float)j - imageCenterX) / scalar;
			y = ((float)i - imageCenterY) / scalar;
			z = (255 - imgdata[i][j]) / scalar;
			glVertex3f(x, y, z);
		}
	}
	glEnd();
	glFlush();
}

void reshape(int w, int h) {
	glViewport(0, 0, (GLsizei)w, (GLsizei)h);//set viewpoint  
	glMatrixMode(GL_PROJECTION);//specify which matrix is the current matrix  
	glLoadIdentity();//replace the current matrix with the identity matrix  
	gluPerspective(60, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
	glMatrixMode(GL_MODELVIEW);
}

void displayDisparity(IplImage* disparity){
	//double xyscale=100;  
	int j = 0;
	int i = 0;
	CvScalar s;

	//accessing the image pixels  
	for (i = 0; i<h; i++){
		for (j = 0; j<w; j++){
			s = cvGet2D(disparity, i, j);
			imgdata[i][j] = s.val[0];//for disparity is a grey image.  
			//cout << imgdata[i][j]<<endl;  
		}
	}
}


int main(int argc, char *argv[])
{
	cout << "OpenCV and OpenGL works together!" << endl;
	char* filename = "22.jpg";
	IplImage* imgGrey = cvLoadImage(filename, 0); //read image as a grey one  
	if (imgGrey == NULL){
		cout << "No valid image input." << endl;
		return 1;
	}
	w = imgGrey->width;
	h = imgGrey->height;

	displayDisparity(imgGrey);
	cvNamedWindow("original", CV_WINDOW_AUTOSIZE);
	cvShowImage("original", imgGrey);

	//------------------OpenGL-------------------------  
	glutInit(&argc, argv);//initialize the GLUT library  
	glutInitDisplayMode(GLUT_DEPTH | GLUT_SINGLE | GLUT_RGBA);//sets the initial display mode  
	glutInitWindowPosition(100, 100);
	glutInitWindowSize(500, 500);
	glutCreateWindow("3D disparity image");
	glutDisplayFunc(renderScene);

	glutReshapeFunc(reshape);
	glutMouseFunc(mouse);
	glutMotionFunc(motion);
	glutSpecialFunc(special);
	glutMainLoop();


	cvWaitKey(0);
	//release opencv stuff.  
	cvReleaseImage(&imgGrey);
	cvDestroyWindow("Original");

	return 0;
}

6. 以上均是在32位系统调试,64位需要参考

http://blog.sina.com.cn/s/blog_ba9d7d990102wd42.html

dll存放的路径:E:\代码\chair3D\x64\Debug