PDA

View Full Version : Trouble compiling code



Tom2002
01-13-2013, 10:30 AM
Hi, Im new to OpenGL and am trying to compile and run a code I got from a textbook (Interactive Computer Graphics: A Top-Down Approach with Shader-Based OpenGL (6th Edition)). I use Xcode 3.2.6 and OpenGl 3.0. The code i want to compile is:


// Two-Dimensional Sierpinski Gasket
// Generated using randomly selected vertices and bisection
#include "Angel.h"
const int NumPoints = 5000;
//----------------------------------------------------------------------------

void
init( void )
{
vec2 points[NumPoints];

// Specifiy the vertices for a triangle
vec2 vertices[3] = {
vec2( -1.0, -1.0 ), vec2( 0.0, 1.0 ), vec2( 1.0, -1.0 )
};

// Select an arbitrary initial point inside of the triangle
points[0] = vec2( 0.25, 0.50 );

// compute and store N-1 new points
for ( int i = 1; i < NumPoints; ++i ) {
int j = rand() % 3; // pick a vertex at random

// Compute the point halfway between the selected vertex
// and the previous point
points[i] = ( points[i - 1] + vertices[j] ) / 2.0;
}

// Create a vertex array object
GLuint vao[1];
glGenVertexArraysAPPLE( 1, vao );
glBindVertexArrayAPPLE( vao[0] );


// Create and initialize a buffer object
GLuint buffer;
glGenBuffers( 1, &buffer );
glBindBuffer( GL_ARRAY_BUFFER, buffer );
glBufferData( GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW );

// Load shaders and use the resulting shader program
GLuint program = InitShader( "vshader_a2.glsl", "fshader_a2.glsl" );
glUseProgram( program );

// Initialize the vertex position attribute from the vertex shader
GLuint loc = glGetAttribLocation( program, "vPosition" );
glEnableVertexAttribArray( loc );
glVertexAttribPointer( loc, 2, GL_FLOAT, GL_FALSE, 0,
BUFFER_OFFSET(0) );

glClearColor( 1.0, 1.0, 1.0, 1.0 ); // white background
}

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

void
display( void )
{
glClear( GL_COLOR_BUFFER_BIT ); // clear the window
glDrawArrays( GL_POINTS, 0, NumPoints ); // draw the points
glFlush();
}

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

void
keyboard( unsigned char key, int x, int y )
{
switch ( key ) {
case 033:
exit( EXIT_SUCCESS );
break;
}
}

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

int
main( int argc, char **argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA );
glutInitWindowSize( 512, 512 );

glutCreateWindow( "Sierpinski Gasket" );

init();

glutDisplayFunc( display );
glutKeyboardFunc( keyboard );

glutMainLoop();
return 0;
}

and the header file Angle:


//////////////////////////////////////////////////////////////////////////////
//
// --- Angel.h ---
//
// The main header file for all examples from Angel 6th Edition
//
//////////////////////////////////////////////////////////////////////////////

#ifndef __ANGEL_H__
#define __ANGEL_H__

//----------------------------------------------------------------------------
//
// --- Include system headers ---
//

#include <cmath>
#include <iostream>

// Define M_PI in the case it's not defined in the math header file
#ifndef M_PI
# define M_PI 3.14159265358979323846
#endif

//----------------------------------------------------------------------------
//
// --- Include OpenGL header files and helpers ---
//
// The location of these files vary by operating system. We've included
// copies of open-soruce project headers in the "GL" directory local
// this this "include" directory.
//

// Rowan: Alas, OS X GLUT won't work with GLSL 1.50 anyway.

#ifdef __APPLE__ // include Mac OS X verions of headers
# include <OpenGL/OpenGL.h>
# include <GLUT/glut.h>
#else // non-Mac OS X operating systems

// Without glew, define glewInit to do nothing
//# include <GL/glew.h>
# define glewInit(x)

# define GL_GLEXT_PROTOTYPES
# include <GL/gl.h>
# include <GL/glext.h>
# include <GL/freeglut.h>
# include <GL/freeglut_ext.h>

#endif // __APPLE__



// Define a helpful macro for handling offsets into buffer objects
#define BUFFER_OFFSET( offset ) ((GLvoid*) (offset))

//----------------------------------------------------------------------------
//
// --- Include our class libraries and constants ---
//

namespace Angel {

// Helper function to load vertex and fragment shader files
GLuint InitShader( const char* vertexShaderFile,
const char* fragmentShaderFile );

// Defined constant for when numbers are too small to be used in the
// denominator of a division operation. This is only used if the
// DEBUG macro is defined.
const GLfloat DivideByZeroTolerance = GLfloat(1.0e-07);

// Degrees-to-radians constant
const GLfloat DegreesToRadians = M_PI / 180.0;

} // namespace Angel

#include "vec.h"
#include "mat.h"
#include "CheckError.h"

#define Print(x) do { std::cerr << #x " = " << (x) << std::endl; } while(0)

// Globally use our namespace in our example programs.
using namespace Angel;

#endif // __ANGEL_H__


I get the following error:
Undefined symbols:
"Angel::InitShader(char const*, char const*)", referenced from:
init() in main.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

I dont know how I can fix this problem so any help would be really appreciated.

tonyo_au
01-13-2013, 04:27 PM
I would say you have not included the Angel library in your link


Also note the Angle code is in a namespace



namespace Angel


It would be safer to do one of the below

either include this code below you includes (probably not the best option)


using namespace Angel;


or change this


GLuint program = InitShader( "vshader_a2.glsl", "fshader_a2.glsl" );

to


GLuint program = Angel::InitShader(( "vshader_a2.glsl", "fshader_a2.glsl" );