How to run examples in the OpenGL Mac Programming Guide using Xcode

I am learning OpenGL programming using the OpenGL Programming Guide. But I cannot run examples in a book using my Macbook. There are always dozens of mistakes when working even with the first example of triangles. I wonder what I should do to run examples in the Red Book using Xcode.

[Platform Information]
Macbook Air, OS X 10.10, Xcode 6.1

[What I tried]
1. I uninstalled AppDelegate. * And main.m, and then I create the cpp triangles.cpp file and copy the source code into it (the source code will be attached at the end)
2. I added OpenGL.framework.
3. I manually installed glew and added the appropriate paths in the header search path and library search path. And added -lGLEW to other linker flags.
4. I manually installed freeglut, added the appropriate paths in the header search paths and library search paths and added -lGLUT to other linker flags, only according to the Lazy Foo OpenGL instruction. 5. I added the source code directory, attached to the Red Book, in the header search path and library search path, so Xcode can find "vgl.h" and "LoadShaders.h". And LoadShaders.cpp has been added to the project.

The reason I did steps 3 and 4 is to make the compilation successful, otherwise I get a lot of errors, such as:

Undefined symbols for architecture x86_64: "_glutInitContextProfile", referenced from: _main in main.o 

So the compilation was fine, but when I started, I found an error:

 X Error of failed request: BadRequest (invalid request code or no such operation) Major opcode of failed request: 34 (X_UngrabKey) Serial number of failed request: 29 Current serial number in output stream: 29 Program ended with exit code: 1 

I'm really wondering what the right way to run examples on Red Book is on Mac OS X!

triangles.cpp:

 #include <iostream> using namespace std; #include "vgl.h" #include "LoadShaders.h" enum VAO_IDs { Triangles, NumVAOs }; enum Buffer_IDs { ArrayBuffer, NumBuffers}; enum Attrib_IDs { vPosition = 0}; GLuint VAOs[NumVAOs]; GLuint Buffers[NumBuffers]; const GLuint NumVertices = 6; void init(void) { glGenVertexArrays(NumVAOs, VAOs); glBindVertexArray(VAOs[Triangles]); GLfloat vertices[NumVertices][2] = { {-0.90, -0.90}, {0.85, -0.90}, {-0.90, 0.85}, {0.90, -0.85}, {0.90, 0.90}, {-0.85, 0.90} }; glGenBuffers(NumBuffers, Buffers); glBindBuffer(GL_ARRAY_BUFFER, Buffers[ArrayBuffer]); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); ShaderInfo shaders[] = { {GL_VERTEX_SHADER, "triangles.vert"}, {GL_FRAGMENT_SHADER, "triangles.frag"}, {GL_NONE, NULL} }; GLuint program = LoadShaders(shaders); glUseProgram(program); glVertexAttribPointer(vPosition, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0)); glEnableVertexAttribArray(vPosition); } void display(void) { glClear(GL_COLOR_BUFFER_BIT); glBindVertexArray(VAOs[Triangles]); glDrawArrays(GL_TRIANGLES, 0, NumVertices); glFlush(); } int main(int argc, char ** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGBA); glutInitWindowSize(512, 512); glutInitContextVersion(4, 3); glutInitContextProfile(GLUT_CORE_PROFILE); glutCreateWindow(argv[0]); if(glewInit()) { cerr << "Unable to initialize GLEW ... exiting" << endl; exit(EXIT_FAILURE); } init(); glutDisplayFunc(display); glutMainLoop(); } 
+6
source share
1 answer

If you don't mind working in C #, check out OpenTK , this is the OpenGL shell for Mono and .Net. I have not tried this with Xcode, but it works great in Xamarin Studio.

If you are following the Xamarin route, there are two options:

1) Download the latest version of OpenTK and the OpenTK.dll link
2) Use the OpenTK included in Xamarin, instructions here .

I would recommend downloading the external version, there are many useful examples.

Something to keep in mind is that on MacOS, the GameWindow constructor uses OpenGL 2.1 by default (not for Windows), so if you want to use modern features, you must explicitly specify the version number of OpenGL in the constructor.

 public Game() : base(800, 600, new GraphicsMode(new ColorFormat(8), 3, 3, 4), "Welcome To Hell", GameWindowFlags.Default, DisplayDevice.Default, 3, 0, GraphicsContextFlags.Default) { } 

The Red Book examples will not run directly in OpenTK, but the GL calls are the same, the syntax just needs to be modified. I found the Neo Kabuto blog best for OpenTK tutorials, and quite a bit of time, you can rewrite the Red Book code in TK.

Perhaps this was not the answer you were looking for, but how I solved the problem.

0
source

Source: https://habr.com/ru/post/980691/


All Articles