这是在mac上运行的第一个openGL程序,代码来自《计算机图形学(第四版)Computer Graphics with OpenGL》第三章,接口已经过时了,但刚接触,只能将就着用了:
//// main.cpp// OpenGLTest//// Created by Rick on 2017/3/6.// Copyright © 2017年 Rick. All rights reserved.//#include <iostream>#include <GLUT/GLUT.h>#include <stdio.h>#include <stdlib.h>#include <math.h>void init(void){ glClearColor(1.0, 1.0, 1.0, 1.0); glMatrixMode(GL_PROJECTION); gluOrtho2D(0.0, 200.0, 0.0, 150.0);}void lineSegment(void){ glClear(GL_COLOR_BUFFER_BIT); glColor3f(0.0, 0.4, 0.2); glBegin(GL_LINES); glVertex2i(180, 15); glVertex2i(10, 145); glEnd(); glFlush();}int main(int argc, char ** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowPosition(50, 100); glutInitWindowSize(400, 300); glutCreateWindow("An Example"); init(); glutDisplayFunc(lineSegment); glutMainLoop();}期间遇到的问题:xcode自动生成的c++代码int main(int argc, char ** argv)
中argv前加了const,会报错 no matching function to call “glutInit”, 把const去掉,让glutInit能修改argv就行。之后编译仍旧会出现问题,报了是个错误,这个是因为没有链接到GLUT的framework,加入相关frameWord就行了: 最终结果:
新闻热点
疑难解答