整理自之前使用的163博客原创文章。
环境:系统 OS X 10.9.2 (13C64),工具 Xcode 5.1,图形卡 Intel Iris 1024 MB
1 MacPorts安装
$ sudo port -v selfupdate
---> Updating the ports tree
Synchronizing local ports tree from rsync://rsync.macports.org/release/tarballs/ports.tar
receiving file list ... done
ports.tar
使用control+z终止更新,终端输入
$ sudo vim /opt/local/etc/macports/sources.conf
打开配置文件,更改最后一行的默认下载地址(参考“解决安装macports,不能更新的问题”,参考下载网址列表),如可修改为韩国镜像:
#rsync://rsync.macports.org/release/tarballs/ports.tar [default]
#http://www.macports.org/files/ports.tar.gz [default]
rsync://cjj.kr.rsync.macports.org/macports/release/tarballs/ports.tar
改好后wq保存退出,再次运行
$ sudo port -v selfupdate
进行更新,更新成功后会显示如下信息:
---> MacPorts base is already the latest version
The ports tree has been updated. To upgrade your installed ports, you should run port upgrade outdated
安装过程中遇到其它问题请戳:
Mac OS X Lion安装MacPorts(让你在Mac的Shell下更加游刃有余)
2 GLEW库安装
glew库主要有如下三种安装方式:1)在Xcode中直接导入framework(下载地址);2)编译glew源代码进行安装(参考“Installing GLEW in Mac OS X (Leopard)”);3)通过MacPorts进行安装。
实测了三种方式,均可成功安装glew库,但前两种方式在Xcode中链接不成功,本文通过MacPorts进行安装(参考“GLEW: Apple Mach-O Linker (Id) Error”)。
终端输入
$ sudo port install glew
进行安装,安装成功后显示:
---> Installing glew @1.10.0_0
---> Activating glew @1.10.0_0
---> Cleaning glew
---> Updating database of binaries: 100.0%
---> Scanning binaries for linking errors: 100.0%
---> No broken files found.
安装过程中遇到其它问题请戳
cannot link glew under xcode4, macosx lion
3 Xcode中GLSL的配置
Mac系统中已内置openGL、GLUT和GLSL,无需安装。
启动Xcode,新建Cocoa Application工程,删除“TriangleAppDelegate.*”文件、“*main.m”文件和“MainMenu.xib”文件。右键单击"Supporting Files"添加新文件,选择"C++ File"。添加后,删除对应的头文件。右键单击"Frameworks",添加"OpenGL.framework"和"GLUT.framework"(参考“MAC OS X And Win7 vs2010 搭建OpenGL”,参考“在Xcode中使用GLUT开发OpenGL应用程序”)。
下面是添加对GLEW库的链接,在build setting中添加如下参数即可:
other linker flags: -lGLEW
header search paths: /opt/local/include/
library search paths: /opt/local/lib/
4 Hello World
在supporting files下添加basic.vert:
void main(){
gl_Position = ftransform();
}
在supporting files下添加basic.frag:
void main() {
gl_FragColor = vec4(0.4,0.4,0.8,1.0);
}
在supporting files下添加textfile.h:
#ifndef macGL_textfile_h
#define macGL_textfile_h #include <stdio.h>
#include <stdlib.h>
#include <string.h> char *textFileRead(const char *fn);
int textFileWrite(char *fn, char *s);
unsigned char *readDataFromFile(char *fn);
#endif
在supporting files下添加textfile.cpp:
#include "textfile.h" unsigned char * readDataFromFile(char *fn){
FILE *fp;
unsigned char *content = NULL;
int count=; if (fn != NULL) {
fp = fopen(fn,"rb");
if (fp != NULL) {
fseek(fp, , SEEK_END);
count = ftell(fp);
rewind(fp);
if (count > ) {
content = (unsigned char *)malloc(sizeof(unsigned char) * (count+));
count = fread(content,sizeof(unsigned char),count,fp);
content[count] = '\0';
}
fclose(fp);
}
} return content;
} char *textFileRead(const char *fn) {
FILE *fp;
char *content = NULL;
int count=; if (fn != NULL) {
fp = fopen(fn,"rt");
if (fp != NULL) {
fseek(fp, , SEEK_END);
count = ftell(fp);
rewind(fp);
if (count > ) {
content = (char *)malloc(sizeof(char) * (count+));
count = fread(content,sizeof(char),count,fp);
content[count] = '\0';
}
fclose(fp);
}
}
return content;
} int textFileWrite(char *fn, char *s) {
FILE *fp;
int status = ;
if (fn != NULL){
fp = fopen(fn,"w");
if (fp != NULL) {
if (fwrite(s,sizeof(char),strlen(s),fp) == strlen(s))
status = ;
fclose(fp);
}
}
return(status);
}
在supporting files下添加GLSLTest.cpp。其中display函数中的glutWireTeapot()是glut内置的画茶壶的函数。注意MAC下特有的glBindVertexArrayAPPLE,glBindVertexArrayAPPLE,glGenVertexArraysAPPLE:
//#pragma comment(lib,"libGLEW.a") #include <GL/glew.h> #include "textfile.h" #include <glut/glut.h>
#include <iostream>
using namespace std; GLuint vShader,fShader;//顶点着色器对象 //顶点位置数组
float positionData[] = {
-0.8f, -0.8f, 0.0f,
0.8f, -0.8f, 0.0f,
0.0f, 0.8f, 0.0f };
//颜色数组
float colorData[] = {
1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f }; GLuint vaoHandle;//vertex array object void initShader(const char *VShaderFile,const char *FShaderFile)
{
//1、查看GLSL和OpenGL的版本
const GLubyte *renderer = glGetString( GL_RENDERER );
const GLubyte *vendor = glGetString( GL_VENDOR );
const GLubyte *version = glGetString( GL_VERSION );
const GLubyte *glslVersion =
glGetString( GL_SHADING_LANGUAGE_VERSION );
GLint major, minor;
glGetIntegerv(GL_MAJOR_VERSION, &major);
glGetIntegerv(GL_MINOR_VERSION, &minor);
cout << "GL Vendor :" << vendor << endl;
cout << "GL Renderer : " << renderer << endl;
cout << "GL Version (string) : " << version << endl;
cout << "GL Version (integer) : " << major << "." << minor << endl;
cout << "GLSL Version : " << glslVersion << endl; //2、编译着色器
//创建着色器对象:顶点着色器
vShader = glCreateShader(GL_VERTEX_SHADER);
//错误检测
if ( == vShader)
{
cerr << "ERROR : Create vertex shader failed" << endl;
exit();
} //把着色器源代码和着色器对象相关联
const GLchar *vShaderCode = textFileRead(VShaderFile);
const GLchar *vCodeArray[] = {vShaderCode};
glShaderSource(vShader,,vCodeArray,NULL); //编译着色器对象
glCompileShader(vShader); //检查编译是否成功
GLint compileResult;
glGetShaderiv(vShader,GL_COMPILE_STATUS,&compileResult);
if (GL_FALSE == compileResult)
{
GLint logLen;
//得到编译日志长度
glGetShaderiv(vShader,GL_INFO_LOG_LENGTH,&logLen);
if (logLen > )
{
char *log = (char *)malloc(logLen);
GLsizei written;
//得到日志信息并输出
glGetShaderInfoLog(vShader,logLen,&written,log);
cerr << "vertex shader compile log : " << endl;
cerr << log << endl;
free(log);//释放空间
}
} //创建着色器对象:片断着色器
fShader = glCreateShader(GL_FRAGMENT_SHADER);
//错误检测
if ( == fShader)
{
cerr << "ERROR : Create fragment shader failed" << endl;
exit();
} //把着色器源代码和着色器对象相关联
const GLchar *fShaderCode = textFileRead(FShaderFile);
const GLchar *fCodeArray[] = {fShaderCode};
glShaderSource(fShader,,fCodeArray,NULL); //编译着色器对象
glCompileShader(fShader); //检查编译是否成功
glGetShaderiv(fShader,GL_COMPILE_STATUS,&compileResult);
if (GL_FALSE == compileResult)
{
GLint logLen;
//得到编译日志长度
glGetShaderiv(fShader,GL_INFO_LOG_LENGTH,&logLen);
if (logLen > )
{
char *log = (char *)malloc(logLen);
GLsizei written;
//得到日志信息并输出
glGetShaderInfoLog(fShader,logLen,&written,log);
cerr << "fragment shader compile log : " << endl;
cerr << log << endl;
free(log);//释放空间
}
} //3、链接着色器对象
//创建着色器程序
GLuint programHandle = glCreateProgram();
if (!programHandle)
{
cerr << "ERROR : create program failed" << endl;
exit();
}
//将着色器程序链接到所创建的程序中
glAttachShader(programHandle,vShader);
glAttachShader(programHandle,fShader);
//将这些对象链接成一个可执行程序
glLinkProgram(programHandle);
//查询链接的结果
GLint linkStatus;
glGetProgramiv(programHandle,GL_LINK_STATUS,&linkStatus);
if (GL_FALSE == linkStatus)
{
cerr << "ERROR : link shader program failed" << endl;
GLint logLen;
glGetProgramiv(programHandle,GL_INFO_LOG_LENGTH,
&logLen);
if (logLen > )
{
char *log = (char *)malloc(logLen);
GLsizei written;
glGetProgramInfoLog(programHandle,logLen,
&written,log);
cerr << "Program log : " << endl;
cerr << log << endl;
}
}
else//链接成功,在OpenGL管线中使用渲染程序
{
glUseProgram(programHandle);
}
} void initVBO()
{
// Create and populate the buffer objects
GLuint vboHandles[];
glGenBuffers(, vboHandles);
GLuint positionBufferHandle = vboHandles[];
GLuint colorBufferHandle = vboHandles[]; //绑定VBO以供使用
glBindBuffer(GL_ARRAY_BUFFER,positionBufferHandle);
//加载数据到VBO
glBufferData(GL_ARRAY_BUFFER, * sizeof(float),
positionData,GL_STATIC_DRAW); //绑定VBO以供使用
glBindBuffer(GL_ARRAY_BUFFER,colorBufferHandle);
//加载数据到VBO
glBufferData(GL_ARRAY_BUFFER, * sizeof(float),
colorData,GL_STATIC_DRAW); glGenVertexArraysAPPLE(,&vaoHandle);
glBindVertexArrayAPPLE(vaoHandle); glEnableVertexAttribArray();//顶点坐标
glEnableVertexAttribArray();//顶点颜色 //调用glVertexAttribPointer之前需要进行绑定操作
glBindBuffer(GL_ARRAY_BUFFER, positionBufferHandle);
glVertexAttribPointer( , , GL_FLOAT, GL_FALSE, , (GLubyte *)NULL ); glBindBuffer(GL_ARRAY_BUFFER, colorBufferHandle);
glVertexAttribPointer( , , GL_FLOAT, GL_FALSE, , (GLubyte *)NULL );
} void init()
{
//初始化glew扩展库
GLenum err = glewInit();
if( GLEW_OK != err )
{
cout <<"Error initializing GLEW: " << glewGetErrorString(err) << endl;
} initShader("basic.vert","basic.frag"); initVBO(); glClearColor(0.0,0.0,0.0,0.0);
//glShadeModel(GL_SMOOTH); } void display()
{
glClear(GL_COLOR_BUFFER_BIT); ////绘制一个三角形(使用普通方法)
//glBegin(GL_TRIANGLES);
//glColor3f(0.0f,1.0f,0.0f);
//glVertex3f(0.0f,1.0f,0.0f); //glColor3f(0.0f,1.0f,0.0f);
//glVertex3f(-1.0f,-1.0f,0.0f); //glColor3f(0.0f,0.0f,1.0f);
//glVertex3f(1.0f,-1.0f,0.0f);
//glEnd(); //使用VAO、VBO绘制
glBindVertexArrayAPPLE(vaoHandle);
//glDrawArrays(GL_TRIANGLES,0,3);
glutWireTeapot(0.5);
GLint tiz = glGetUniformLocation(vaoHandle, "iz");
glUniform1iARB(tiz, );
glBindVertexArrayAPPLE();
glutWireTeapot(1.0); glutSwapBuffers();
} void keyboard(unsigned char key,int x,int y)
{
switch(key)
{
case :
glDeleteShader(vShader);
glUseProgram();
break;
}
} int main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(,);
glutInitWindowPosition(,);
glutCreateWindow("GLSL Test : Draw a triangle");
init();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard); glutMainLoop();
return ;
}
运行成功后的效果图:
其它参考网址: