1. Select CommandLineTool corresponding engineering works under MacOS, Next
2. Fill in the project name, and select ++ language C, the Next
3. Click the project name, select Build Phases, select Link Binary Libraries, add OpenGL.framework and GLUT.framework
4. Select the search under the Build Setting Macro label, add precompiled macros subsequent removal of the warning OpenGL functions
5. environmental four steps above it. Main.cpp then modify the code as follows:
#include <iostream>
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>
void myDisplay(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("第一个OpenGL程序");
glutDisplayFunc(&myDisplay);
const GLubyte* vendorName = glGetString(GL_VENDOR); //返回负责GL实现的厂商名字
const GLubyte* renderName = glGetString(GL_RENDERER); //返回渲染器名称,通常是个硬件平台
const GLubyte* OpenGLVersion =glGetString(GL_VERSION); //返回OpenGL版本号
const GLubyte* gluVersion= gluGetString(GLU_VERSION); //返回GLU工具库版本
printf("GL实现的厂商名字:%s\n", vendorName);
printf("渲染器名称:%s\n", renderName);
printf("OpenGL版本号:%s\n",OpenGLVersion );
printf("GLU工具库版本:%s\n", gluVersion);
glutMainLoop();
return 0;
}
6. Run View results draw a rectangle:
7. You can also use the library development glfw
need to remember to install the library glfw
#include <iostream>
#include <GL/glew.h>
#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, "Show Me Window", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
glfwMakeContextCurrent(window);
const GLubyte* OpenGLVersion =glGetString(GL_VERSION); //返回OpenGL版本号
printf("OpenGL版本号:%s\n",OpenGLVersion );
/* Initialize the library */
GLenum err = glewInit();
if (GLEW_OK != err) {
fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
}
if (GLEW_VERSION_4_1) {
std::cout << "Yay! OpenGL 4.1 is supported!" << std::endl;
}
while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
8. Normally the pushbutton support are required to test, the following correction process setting key is glfw
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
// 设置按键
if( key == GLFW_KEY_ESCAPE && action == GLFW_PRESS )
glfwSetWindowShouldClose(window, GL_TRUE);
else if(key == GLFW_KEY_A && action == GLFW_RELEASE)
printf("key A is released");
}
glfwSetKeyCallback(window, key_callback);