xcode运行OpenGL程序
运行环境的搭建
我是使用的xcode7.3.1,最近入门图形学,所以必须在电脑上跑一跑程序,网上写了一些关于xcode运行OpenGL程序的例子,都是相互抄袭的,还是xcode4版本,这里我贴出如何配置运行环境的图,其实只需要添加两个包,但是在Products下面那个黑框框里面添加,点击那个黑框框,就很清晰了,需要添加GLUT和OpenGL两个包。
我的第一个OpenGL程序
第一个OpenGL程序是按照书上抄的,不是显示的老掉牙的Hello World,是显示了一个窗口,画了一条线,毕竟OpenGL就是研究图形图像么,肯定先从线开始,废话不多说,上代码(这里需要注意,初学者代码一行都不可以少,因为有些代码你不懂意思,删了是无法显示的)。
//
// main.cpp
// firstCode
//
// Created by 李林 on 16/7/27.
// Copyright © 2016年 李林. All rights reserved.
//
#include <iostream>
#include <OpenGl/gl.h>
#include <GLUT/GLUT.h>
void init(){
// red, green, blue, alpha
glClearColor(1.0, 1.0, 1.0, 0.0);
// project to the screen and set the location
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 200.0, 0.0, 150.0);
}
void lineSegment(){
//clear display window
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINES);
glVertex2i(180, 15);
glVertex2i(10, 145);
glEnd();
// Process all OpenGl routines as quickly as possible
// 强制清空缓存来处理OpenGL函数
glFlush();
}
int main(int argc, char** argv){
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);// Set display mode
glutInitWindowPosition(500, 100);
glutInitWindowSize(400, 300);
glutCreateWindow("An Example OpenGL Program");
init();
glutDisplayFunc(lineSegment);// Note:function have no ()
glutMainLoop(); // Display everything and wait
return 0;
}