OpenGL环境搭建Windows+Mac+Linux
2014-11-22 18:12
495 查看
OpenGL环境搭建Windows+Mac+Linux
Mac平台下
下载列表:GLFW
cmake
下载的GLFW解压缩
然后安装cmake, 安装好cmake之后打开
1.browse source, 选择GLFW的源码根目录
2.browse build, 选择要生成的工程目录,最好是空文件夹
3.点击Configure
选择Xcode, 然后点击Done
然后会出来这个画面
不要惊慌,再点击一次Configure应该就没有那么红了(主要是上面那么框中没有红得就可以了,下面那个一直存在红色)
4.然后点击Generate, 就生成工程了
双击GLFW.xcodeproj, XCode就会打开了
然后继续下面操作
选择Simple吧,然后运行就可以看到一个窗口了
我们可以在上面画一个三角形
#include <GLFW/glfw3.h> int main(void) { GLFWwindow* window; /* Initialize the library */ if (!glfwInit()) return -1; /* Create a windowed mode window and its OpenGL context */ window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL); if (!window) { glfwTerminate(); return -1; } /* Make the window's context current */ glfwMakeContextCurrent(window); /* Loop until the user closes the window */ while (!glfwWindowShouldClose(window)) { /* Render here */ glClearColor(1, 1, 1, 1); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); static GLfloat vertexs[] = { -1, -1, 0, 1, -1, 0, 0, 1, 0 }; static GLubyte colors[] = { 255,0,0,0, 0,255,0,0, 0,0,255,0, }; glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_COLOR_ARRAY); glVertexPointer(3, GL_FLOAT, 0, vertexs); glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors); glDrawArrays(GL_TRIANGLES, 0, 3); /* Swap front and back buffers */ glfwSwapBuffers(window); /* Poll for and process events */ glfwPollEvents(); } glfwTerminate(); return 0; }
windows平台
直接在GLFW网站上下载 [Windows pre-compiled binaries], 下载32位版比较保险下载后解压看到如下文件:
将include\GLFW里.h文件加入C:\Program Files (x86)\Windows Kits\8.0\Include\um\gl
如果你用的是vs2012
将lib-msvc110里的glfw3.lib,glfw3dll.lib放到C:\Program Files (x86)\Windows Kits\8.0\Lib\win8\um\x86
将lib-msvc110里的glfw3.dll放到C:\windows\System32
好了环境配置完毕!下面实战吧 打开vs2012创建一个win32 console的工程
然后创建一个main.cpp, 以后每个工程都要加这两句代码
#pragma comment(lib,"opengl32.lib") #pragma comment(lib,"glfw3.lib")
下面我们绘制一个三角形吧!!
#pragma comment(lib,"opengl32.lib") #pragma comment(lib,"glfw3.lib")
#include <gl/glfw3.h>
#include <gl/GL.h>
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClearColor(1,1,1,1);
glClear(GL_COLOR_BUFFER_BIT);
static GLfloat pvertexs[] = {
-1, -1, 0,
1, -1, 0,
0, 1, 0
};
static GLfloat color[] = {
1,0,0,1,
0,1,0,1,
0,0,1,1
};
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, pvertexs);
glColorPointer(4, GL_FLOAT, 0, color);
glDrawArrays(GL_TRIANGLES, 0, 3);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
效果图:
Linux平台
在Linux上安装也比较简单,首先还是安装cmake,这里安装的命令行的cmake(到了linux下不用命令行不显逼格)然后安装 xorg-dev, libglu1-mesa-dev,基于debian的linux(ubuntu,mint...)可以使用如下命令安装
sudo apt-get install xorg-dev sudo apt-get install libglu1-mesa-dev
安装后将glfw解压缩到一个文件夹glfw中,然后选择一个你要生成文件的位置(空白文件夹)test中
1.打开命令行,cd 到你的test文件夹中
cd '/home/luwei/Downloads/test'
2.使用cmake命令指向glfw文件夹生成文件
cmake '/home/luwei/Downloads/glfw'
3.输入编译命令,编译源码
make
现在生成的可执行文件都在test文件夹中了,生成位置对应glfw的文件目录结构,输入命令运行程序
./examples/simple
我们自己写个自己的OpenGL的程序吧
找到glfw/examples/simple.c,使用你习惯的编辑器打开,然后写自己的OpenGL程序
这里我还是使用上面的那段程序啦(注意引入的头文件路径不同了)
写完之后,要重新make命令编译,在test文件夹下使用make命令编译,然后运行。
总结
哎!!!总算都出来了,OpenGL编程记住一大堆函数和它该用啥参数是让我很苦恼的事情,尤其是Linux下我用编辑器写的程序根本没有代码提示,没办法记性不好呀相关文章推荐
- LibGDX_1.2: Desktop环境搭建: 包括 Windows,MAC OS X,Linux
- Android一分钟环境搭建,包含Mac、Linux、Windows
- Android一分钟环境搭建,包含Mac、Linux、Windows
- Android一分钟环境搭建,包含Mac、Linux、Windows
- mac/windows/linux中eclipse搭建Android开发环境,无法访问dl-ssl.google.com
- golang及gopath环境搭建(windows&linux&mac )
- Node.js【2】开发环境搭建(Windows、Linux&Mac)
- Windows和Linux下搭建J2sdk的环境
- 在windows平台上搭建linux C 开发环境
- 在windows下开发面向Linux的php程序环境搭建
- 在Linux下搭建NIS服务器(类似于windows下的域环境)(1)
- Windows/Linux下使用Emacs+Slime+SBCL/SCHEME搭建LISP开发环境
- windows开发 linux部署 java [debian+resin+mysql+java] 之环境搭建
- vim+global+ctags搭建高效的编程环境(Windows/Linux)
- 用qemu搭建CentOS 6 for colinux虚拟系统——《Windows下搭建CentOS 6开发环境之一》
- Windows上Eclipse开发OpenGL的环境搭建
- Windows上Eclipse开发OpenGL的环境搭建
- GTK+学习:概述 、搭建环境(Windows,Linux)、开发
- GTK+学习:概述 、搭建环境(Windows,Linux)、开发 收藏
- 跨平台wxWidgets在windows及linux上的开发环境搭建