-1

I am learning OpenGL and trying to run my first program. I have included all the files in include, lib, and bin folders. I have tried to add opengl32.lib;glut32.lib;glu32.lib; in Configuration properties -> linker -> input, but it did't work too.

I am using Visual Studio 2012.

///////////////////////////////////////////////////////////////////////
//
// 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;

//---------------------------------------------------------------------
//
// init
//

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

    GLfloat vertices[NumVertices][2] = {
        { -0.90, -0.90 }, // Triangle 1
        { 0.85, -0.90 },
        { -0.90, 0.85 },
        { 0.90, -0.85 }, // Triangle 2
        { 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);
}

//---------------------------------------------------------------------
//
// display
//

void
display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);

    glBindVertexArray(VAOs[Triangles]);
    glDrawArrays(GL_TRIANGLES, 0, NumVertices);

    glFlush();
}

//---------------------------------------------------------------------
//
// main
//

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();
}
Lucas
  • 31
  • 1
  • 2

3 Answers3

6

You're missing the lib (or cpp) file for that LoadShaders() thing unless it's header only. Without knowing its contents we can't say that for sure.


Update:

The zip file with the book's source code includes everything you need.

You should do the following:

  • Extract the zip file to an easy to remember location, e.g. "c:\openglbook".
  • Open or create your project in Visual Studio 2012.
  • Open the menu "PROJECT" and pick "[your project] Properties..." (or hit Alt+F7).
  • Repeat the following steps for each of your configurations or select "All Configurations" on the top left.
  • In the tree on the left open "Configuration Properties" and select "VC++ Directories".
  • Add c:\openglbook\include to "Include Directories".
  • Add c:\openglbook\lib to "Library Directories".
  • Close the project properties.
  • Copy the file "c:\openglbook\lib\LoadShaders.cpp" to your project files and add it as another source file.

Once this is done your project should build. If you're still lacking dependencies, like GLUT functions, add the apropriate library from the "lib" sub directory to the linker libraries list you already know.

Mario
  • 35,726
  • 5
  • 62
  • 78
  • This is a library and the directory is: C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include – Lucas Jun 04 '13 at 19:33
  • You'll have to put the library file into the `lib` sub directory or add it as an additional directory to look into. If you don't have such file, what files have you got? List them all in your question. – Mario Jun 05 '13 at 08:47
  • Where is your "LoadShaders.h" file? And if possible to link, where did you get it from? – Mario Jun 05 '13 at 22:28
  • The "Loadshaders.h" file is one folder before GL. I uploaded another picture in the same folder if you wanna see. The website where I got it is: http://www.opengl-redbook.com/ – Lucas Jun 06 '13 at 01:51
  • Updated my answer. You're indeed missing the corret cpp file belonging to those headers. – Mario Jun 06 '13 at 10:01
  • I did exactly as you said, but got different errors:[Link](https://www.dropbox.com/s/usxunevotzs040o/ERROR.png) – Lucas Jun 06 '13 at 23:22
  • Obviously some conflict between different runtime versions. Remove the entry you added to "Library Directories" and try again. – Mario Jun 07 '13 at 08:31
  • Remove the include directory as well, then just add the LoadShaders.cpp file to your project. – Mario Jun 08 '13 at 08:54
  • I had already done that and the problem persisted. Actually, the conflict is here: Project + Properties, C/C++, Code Generation, Runtime Library /MT and /MD – Lucas Jun 08 '13 at 19:52
  • Yes, that's fixing it from the other end. The conflicting libs have to origin somewhere (default ones should never conflict with each other no matter the configuration). – Mario Jun 09 '13 at 09:00
  • Currently, I am using only glut, and it's working. When I am obliged to use shaders I will come back to that. I do appreciate your help! – Lucas Jun 11 '13 at 19:54
1

The problem is from your LoadShaders function not having a body (presumably from your LoadShaders.h). Either locate the .cpp for LoadShaders, or link it's lib.

If LoadShaders has a lib, since you're using VS 2012, you can use a preprocessor directive to link your lib:

#pragma comment( lib, "yourlibfilename.lib" )

Make sure your lib is actually where you say it is. By default Visual Studio will assume the lib resides in the same folder as the default folder for the source files.

bobobobo
  • 64,917
  • 62
  • 258
  • 363
  • What's special about VS2012? You've been able to do that forever – David Heffernan Jun 02 '13 at 18:55
  • Orly? I was pretty sure it was an MSVC extension. Can never be sure with `#pragma`'s anyway. – bobobobo Jun 02 '13 at 18:57
  • It is MSVC only, but it was available in earlier versions. Your text reads as though this is new with VS2012. – David Heffernan Jun 02 '13 at 19:03
  • Note that this only works if you specify the directory of the lib in under "Additional Library Directories". If it's only one lib, you might as well just specify the full path without the pragma. – Andreas Haferburg Jun 02 '13 at 19:04
  • @AndreasHaferburg No no, any directory in the VC++ "project path" (including the folder where your src files are) will work too. – bobobobo Jun 02 '13 at 21:24
  • I don't have a file called LoadShaders.lib. But, anyway, I tried and got: error LNK1104: cannot open file 'LoadShaders.lib' – Lucas Jun 04 '13 at 19:44
0

Only adding the libs in Linker->Input is not enough. The Linker will still not find them because your libs are in some arbitrary directory. Add the directory they are in to the setting Linker->General->additional library directories.

typ1232
  • 5,535
  • 6
  • 35
  • 51