Author Topic: help installing Glut into code::blocks(for dummies)  (Read 35021 times)

Offline Need4sleep

  • Single posting newcomer
  • *
  • Posts: 5
help installing Glut into code::blocks(for dummies)
« on: July 08, 2012, 01:06:32 am »
I recently purchased a book on OpenGl to learn with c++. The only problem is i am absolutely terrible with all the crazy options that go along with code::blocks and installing lib's(installing SFML took about a week of headaches for me, thats how bad i am). Right now this is what im getting:
Code
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|1|error: GL/glut.h: No such file or directory|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp||In function 'int main(int, char**)':|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|13|error: 'glutInit' was not declared in this scope|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|14|error: 'GLUT_DOUBLE' was not declared in this scope|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|14|error: 'GLUT_RGB' was not declared in this scope|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|14|error: 'GLUT_DEPTH' was not declared in this scope|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|14|error: 'glutInitDisplayMode' was not declared in this scope|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|17|error: 'glutInitWindowSize' was not declared in this scope|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|18|error: 'glutInitWindowPosition' was not declared in this scope|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|19|error: 'glutCreateWindow' was not declared in this scope|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|24|error: 'glutDisplayFunc' was not declared in this scope|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|25|error: 'glutReshapeFunc' was not declared in this scope|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|26|error: 'glutMouseFunc' was not declared in this scope|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|27|error: 'glutKeyboardFunc' was not declared in this scope|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|28|error: 'glutIdleFunc' was not declared in this scope|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|30|error: 'glutMainLoop' was not declared in this scope|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp||In function 'void Initialize()':|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|36|error: 'glutCreateMenu' was not declared in this scope|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|38|error: 'glutSetMenu' was not declared in this scope|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|39|error: 'glutAddMenuEntry' was not declared in this scope|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|40|error: 'GLUT_RIGHT_BUTTON' was not declared in this scope|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|40|error: 'glutAttachMenu' was not declared in this scope|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|42|error: 'GL_DEPTH_TEST' was not declared in this scope|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|42|error: 'glEnable' was not declared in this scope|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp||In function 'void MouseHandler(int, int, int, int)':|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|48|error: 'GLUT_LEFT_BUTTON' was not declared in this scope|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|50|error: 'exit' was not declared in this scope|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|57|error: 'glutPostRedisplay' was not declared in this scope|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp||In function 'void MainMenuHandler(int)':|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|65|error: 'exit' was not declared in this scope|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|70|error: 'glutPostRedisplay' was not declared in this scope|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp||In function 'void Animate()':|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|74|error: 'glutPostRedisplay' was not declared in this scope|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp||In function 'void Reshape(int, int)':|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|81|error: 'GLsizei' was not declared in this scope|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|81|error: expected ')' before 'height'|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|81|error: 'glViewport' was not declared in this scope|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|82|error: 'GL_PROJECTION' was not declared in this scope|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|82|error: 'glMatrixMode' was not declared in this scope|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|83|error: 'glLoadIdentity' was not declared in this scope|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|84|error: 'gluPerspective' was not declared in this scope|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|86|error: 'GL_MODELVIEW' was not declared in this scope|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp||In function 'void Display()':|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|91|error: 'glLoadIdentity' was not declared in this scope|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|94|error: 'gluLookAt' was not declared in this scope|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|97|error: 'GL_COLOR_BUFFER_BIT' was not declared in this scope|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|97|error: 'GL_DEPTH_BUFFER_BIT' was not declared in this scope|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|97|error: 'glClear' was not declared in this scope|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|100|error: 'GL_TRIANGLES' was not declared in this scope|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|100|error: 'glBegin' was not declared in this scope|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|101|error: 'glColor3f' was not declared in this scope|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|102|error: 'glVertex3f' was not declared in this scope|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|107|error: 'glEnd' was not declared in this scope|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|110|error: 'GL_POLYGON' was not declared in this scope|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|124|error: 'glutSwapBuffers' was not declared in this scope|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp||In function 'void KeyboardHandler(unsigned char, int, int)':|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|132|error: 'exit' was not declared in this scope|
F:\OpenGl\GLUT_OpenGl_Simple\main.cpp|138|error: 'glutPostRedisplay' was not declared in this scope|
||=== Build finished: 50 errors, 0 warnings ===|

and my program:

Code
#include <GL/glut.h>
void Initialize();
void MouseHandler(int button, int state, int x, int y);
void MainMenuHandler(int option);
void Animate();
void Reshape(int width, int height);
void Display();
void KeyboardHandler(unsigned char key, int x, int y);

int main(int argc, char **argv)
{
    //set up basic GLUT
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

    //create window
    glutInitWindowSize(1024,768);
    glutInitWindowPosition(100,150);
    glutCreateWindow("BOGLGP CHAPTER 1");

    Initialize();

    //register even callback function
    glutDisplayFunc(Display);
    glutReshapeFunc(Reshape);
    glutMouseFunc(MouseHandler);
    glutKeyboardFunc(KeyboardHandler);
    glutIdleFunc(Animate);

    glutMainLoop();
}

void Initialize()
{
    int mainMenu;
    mainMenu = glutCreateMenu(MainMenuHandler);

    glutSetMenu(mainMenu);
    glutAddMenuEntry("Exit",0);
    glutAttachMenu(GLUT_RIGHT_BUTTON);

    glEnable(GL_DEPTH_TEST);
}
void MouseHandler(int button, int state, int x, int y)
{
   switch (button)
  {
  case GLUT_LEFT_BUTTON:
    {
      exit(0);
    } break;
  default:
    break;
  }

  // force a screen redraw
  glutPostRedisplay();
}
void MainMenuHandler(int option)
{
    switch(option)
  {
  case 0:
    {
      exit(0);
    } break;
  default:
    break;
  }
  glutPostRedisplay();
}
void Animate()
{
    glutPostRedisplay();
}
void Reshape(int width, int height)
{
    if (height == 0)
    return;

  glViewport(0, 0, (GLsizei) width, (GLsizei) height);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(90.0, width/height, 1.0, 100.0);

  glMatrixMode(GL_MODELVIEW);
}
void Display()
{
    // set up the camera
  glLoadIdentity();
  gluLookAt(0.0, 1.0, 6.0,
            0.0, 0.0, 0.0,
            0.0, 1.0, 0.0);

  // clear the screen
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  // draw a triangle
  glBegin(GL_TRIANGLES);
    glColor3f(1.0, 0.0, 0.0);
    glVertex3f(2.0, 2.5, -1.0);
    glColor3f(0.0, 1.0, 0.0);
    glVertex3f(-3.5, -2.5, -1.0);
    glColor3f(0.0, 0.0, 1.0);
    glVertex3f(2.0, -4.0, 0.0);
  glEnd();

  // draw a polygon
  glBegin(GL_POLYGON);
    glColor3f(1.0, 1.0, 1.0);
    glVertex3f(-1.0, 2.0, 0.0);
    glColor3f(1.0, 1.0, 0.0);
    glVertex3f(-3.0, -0.5, 0.0);
    glColor3f(0.0, 1.0, 1.0);
    glVertex3f(-1.5, -3.0, 0.0);
    glColor3f(0.0, 0.0, 0.0);
    glVertex3f(1.0, -2.0, 0.0);
    glColor3f(1.0, 0.0, 1.0);
    glVertex3f(1.0, 1.0, 0.0);
  glEnd();

  // draw everything and swap the display buffer
  glutSwapBuffers();
}
void KeyboardHandler(unsigned char key, int x, int y)
{
     switch (key)
  {
  case 'q':  // exit
    {
      exit(0);
    } break;
  default:
    {
    } break;
  }
  glutPostRedisplay();
}


Could someone please explain to me in a SIMPLE way what is wrong and what i need to do? keep in mind i dont know much at all about code blocks, so telling me to do simple tasks for you might sound like a different language to me. I would appreciate the help a TON, thanks!

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: help installing Glut into code::blocks(for dummies)
« Reply #1 on: July 08, 2012, 05:34:22 am »
At this point I usually recommend start at the command line first. The compiler cannot find a certain header file (GL/glut.h) that provides the interface definition for all the functions coming next in the error log (i.e. glutInit).

You need to understand that you are using a non-default C/C++ library, so you need to:
  • Install this library
  • adjust the command line for the compiler accordingly
  • adjust the command line for the linker accordingly

For this (of course) you need to understand how a compiler/linker works, which you learn BEST at the command line. Then, once you have understood what I am talking about, you can start using an IDE which basically helps you to organise all the compiler/linker flags and all the references with an UI. Then, you can have a look at the C::B manual (see my sig) to know, where you have to enter what. Basically the project settings and build options of your project are the UI front-end to the compiler.

Note that an IDE (like Code::Blocks is) just drives the compiler. You are responsible to provide the driving direction... so you should first learn that. As a hint (Windows only):
1.) the compiler command line under Windows should look like this:
mingw32-g++.exe -Wall -O2 -IC:/Path/To/GLUT/include -c main.cpp -o main.o
2.) the linker command line under Windows should look like this:
mingw32-g++.exe -LC:/Path/To/GLUT/lib -o GL.exe main.o -s -lglut32 -lopengl32 -lglu32 -lwinmm -lgdi32  -mwindows

...both command lines look different on another platform, like Linux. Again, here comes the IDE into play which helps you to organise your project by platform. If you understand what these flags mean you can easily set-up and extend a project in an IDE. Without that knowledge its going to be hard...
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline Need4sleep

  • Single posting newcomer
  • *
  • Posts: 5
Re: help installing Glut into code::blocks(for dummies)
« Reply #2 on: July 08, 2012, 06:59:05 am »
I appreciate the help, but im still completely lost as how to specifically install GLUT. I downloaded MC visual c++ and was able to install it without any pain using simple steps, but i enjoy the codeblocks IDE over the Microsoft, and would be heart broken to have to switch. Thanks again!


EDIT: so i guess i made some progress.. i tried installing freeglut into codeblocks(i have no idea how to install normal glut) and using the glut wizard to create a project using this tutorial: http://wiki.codeblocks.org/index.php?title=Using_FreeGlut_with_Code::Blocks

everything seems great until i try and run it, the command line does nothing, then returns a massive negative number. Any ideas on whats going wrong?
« Last Edit: July 08, 2012, 07:28:08 am by Need4sleep »

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: help installing Glut into code::blocks(for dummies)
« Reply #3 on: July 08, 2012, 12:54:08 pm »
i tried installing freeglut into codeblocks
Not sure why you did this, but FreeGLUT != GLUT, as the name implies.

Any ideas on whats going wrong?
You know Google, right? Searching for MinGW and GLUT I found this under the first 10 hits:
http://www.sci.brooklyn.cuny.edu/~goetz/codeblocks/glut/

I don't do OpenGL programming very often. But you also know that there is a wizard for an OpenGL project in Code::Blocks, right?
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline Radek

  • Multiple posting newcomer
  • *
  • Posts: 104
Re: help installing Glut into code::blocks(for dummies)
« Reply #4 on: July 08, 2012, 05:46:00 pm »
Freeglut is GLUT  :)  It's an open source equivalent to GLUT. I am using the freeglut library all the time without any problems. The problem of Need4sleep seems to be the header, it was not found for some reason.

(1) Check whether you have glut.h and whether it is on the search path. Note that you #include <GL/glut.h> so there must be a directory GL on the search path and this directory must contain glut.h .
(2) If you are using freeglut then check whether you have freeglut.h .
(3) Check the contents of glut.h . Freeglut has it's own header, freeglut.h, glut.h should simply #include freeglut.h when you are using freeglut. It can happen that your glut.h tries to #include freeglut.h with a wrong directory, it can happen that your glut.h is a remnant from some old installation of GLUT and do not relate to your current installation.
(4) If you do not insist on portability then switch from glut.h to freeglut.h: #include <GL/freeglut.h>. The GLUT code need not change.
(5) After successful compilation, you will link. Make sure that the freeglut library (or DLL) is available to the linker. Therefore, Build Options -> Linker -> Add Library. The library should be glut.lib, glut.dll, libglut.lib or something similar. If you have a port from Linux, then libglut.so (the DLL) or libglut.a (the LIB). In winblows, you should link with a LIB, even if you have a DLL. With a DLL, the LIB is an import library. Don't be surprised that it is small, it's only an interface between the DLL and your code.

Offline Need4sleep

  • Single posting newcomer
  • *
  • Posts: 5
Re: help installing Glut into code::blocks(for dummies)
« Reply #5 on: July 08, 2012, 05:53:33 pm »
I cannot thank you enough!!! i couldnt find a tutorial like that, but i got it working. A huge weight has been lifted off my shoulders from having to learn VC, THANK YOU!!!