0
votes

I'm reading OpenGL Programming Guide: The Official Guide to Learning OpenGL. But I come across problems when running the first example.

#include <iostream>
#include "vgl.h"
#include "LoadShaders.h"

using namespace std;

enum VAO_IDs {Triangles, NumVAOs};
enum Buffer_IDs {ArrayBuffers, NumBuffers};
enum Attrib_IDs { vPosition = 0};

GLuint Buffers[NumBuffers];
GLuint VAOs[NumVAOs];

const GLuint NumVertices = 6;

void init(void)
{
    glGenVertexArrays(NumVAOs, VAOs);
    glBindVertexArray(VAOs[Triangles]);

    GLfloat vertices[NumVertices][2] = {
        {-0.9, -0.9},
        {0.85, -0.9},
        {-0.9, 0.85},
        {0.9, -0.85},
        {0.9, 0.9},
        {-0.85, 0.9}
    };

    glGenBuffers(NumBuffers, Buffers);
    glBindBuffer(GL_ARRAY_BUFFER, Buffers[ArrayBuffers]);
    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();
}

I've already add the path where vgl.h and LoadShaders.h are located to Header Search Paths. (I will post these two files at the end of the post.) But a error shows up:

enter image description here

Then I change the statement the included header "GL/gl.h" to "OpenGL/gl.h", another error shows up:

enter image description here

Could anyone tell me what's wrong? I would really appreciate it!

[Attachment]

vgl.h:

#ifndef __VGL_H__
#define __VGL_H__

// #define USE_GL3W

#ifdef USE_GL3W

#include <GL3/gl3.h>
#include <GL3/gl3w.h>

#else

#define GLEW_STATIC

#include <GL/glew.h>

#ifdef _MSC_VER
#  ifdef _DEBUG
#    if (_MSC_VER >= 1600)
#      pragma comment (lib, "glew_static_vs2010_d.lib")
#    else
#      pragma comment (lib, "glew_static_d.lib")
#    endif
#  else
#    if (_MSC_VER >= 1600)
#      pragma comment (lib, "glew_static_vs2010.lib")
#    else
#      pragma comment (lib, "glew_static.lib")
#    endif
#  endif
#endif

#endif

#define FREEGLUT_STATIC

#include <GL/freeglut.h>

#ifdef _MSC_VER
#  ifdef _DEBUG
#    if (_MSC_VER >= 1600)
#      pragma comment (lib, "freeglut_static_vs2010_d.lib")
#    else
#      pragma comment (lib, "freeglut_static_d.lib")
#    endif
#  else
#    if (_MSC_VER >= 1600)
#      pragma comment (lib, "freeglut_static_vs2010.lib")
#    else
#      pragma comment (lib, "freeglut_static.lib")
#    endif
#  endif
#endif

#define BUFFER_OFFSET(x)  ((const void*) (x))

#endif /* __VGL_H__ */

LoadeShaders.h:

//////////////////////////////////////////////////////////////////////////////
//
//  --- LoadShaders.h ---
//
//////////////////////////////////////////////////////////////////////////////

#ifndef __LOAD_SHADERS_H__
#define __LOAD_SHADERS_H__

#include <OpenGL/gl.h>

#ifdef __cplusplus
extern "C" {
#endif  // __cplusplus

//----------------------------------------------------------------------------
//
//  LoadShaders() takes an array of ShaderFile structures, each of which
//    contains the type of the shader, and a pointer a C-style character
//    string (i.e., a NULL-terminated array of characters) containing the
//    entire shader source.
//
//  The array of structures is terminated by a final Shader with the
//    "type" field set to GL_NONE.
//
//  LoadShaders() returns the shader program value (as returned by
//    glCreateProgram()) on success, or zero on failure. 
//

typedef struct {
    GLenum       type;
    const char*  filename;
    GLuint       shader;
} ShaderInfo;

GLuint LoadShaders( ShaderInfo* );

//----------------------------------------------------------------------------

#ifdef __cplusplus
};
#endif // __cplusplus

#endif // __LOAD_SHADERS_H__
1

1 Answers

1
votes

When I compile this example,I confront with the same problem as yours,i think you didn't understand the build process.

The build process consists of these three steps:

  1. Preprocessor – this takes care of including/importing header files into your source code, essentially pasting the content of the specified file right into your source code for building. This step also replaces all instances of #defined values with what you defined them to be.
  2. Compiler – this goes through all the .m files and builds an object file with .o extension for each. Each item that is not coming from the same .m file is marked as an external symbol.
  3. Linker – this merges all the object files into one big binary while at the same time linking the symbols with the actual implementations.

Header files do not actually contain executable code, but instead they tell the compiler which names and functions it should accept. It is the linker then that resolves these references. In short: If you want to use something which is implemented outside the current .m file than you need the appropriate import for that.

There are two kinds of imports:

  • with angle brackets – these are meant for system or “global” includes.
  • “path/header.h” with double quotes – these are meant for items that are limited in scope to your current project

you need to compile file that your main programme need to static library,these files can be found in directory "lib",after your library is built successfully,you should link your project with this library

besides,there are two function in main() that are functions in freeglut,you need to install freeglut on your mac otherwise linking errors will still show up

you can browse an article on http://www.cocoanetics.com/2011/12/sub-projects-in-xcode/,it explains how to deal with external library on Xcode