Search code examples
c++openglfreeglut

C++ Compiler throwing error with GLUT


The code was working fine but after I added the void projection(float), it's giving me unqaulified-id error inside the projection function. I'm using CodeBlocks IDE, MinGW. Please help me.

This is the error I'm getting:

||=== Build: Debug in L3 (compiler: GNU GCC Compiler) ===|
C:\Users\sos\Documents\Code Blocks c++\L3\src\CallbackFunctions.cpp||In function 'void projection(float)':|
C:\Users\sos\Documents\Code Blocks c++\L3\src\CallbackFunctions.cpp|56|error: expected unqualified-id before '=' token|
C:\Users\sos\Documents\Code Blocks c++\L3\src\CallbackFunctions.cpp|57|error: expected primary-expression before ',' token|
C:\Users\sos\Documents\Code Blocks c++\L3\src\CallbackFunctions.cpp| 57|error: expected primary-expression before ')' token|
C:\Users\sos\Documents\Code Blocks c++\L3\src\CallbackFunctions.cpp||In function 'void specialKeyboard(int, int, int)':|

||=== Build failed: 3 error(s), 15 warning(s) (0 minute(s), 0 second(s)) ===|

Moreover, if I comment everything inside projection, I get this error:

 obj\Debug\src\Display.o||In function `glutInit_ATEXIT_HACK':| 
 C:\Program Files\CodeBlocks\MinGW\include\GL\freeglut_std.h|623|multiple  definition of `keyboardKey'|  
C:\Program Files\CodeBlocks\MinGW\include\GL\freeglut_std.h|623|multiple definition of `mouseX'| 
 C:\Program Files\CodeBlocks\MinGW\include\GL\freeglut_std.h|623|multiple definition of `mouseY'|  
C:\Program Files\CodeBlocks\MinGW\include\GL\freeglut_std.h|623|multiple definition of `toggleProjection'|

CallbackFunctions.h

#ifndef CALLBACKFUNCTIONS_H
#define CALLBACKFUNCTIONS_H

#include "GL/freeglut.h"


unsigned char keyboardKey;
char *specialKeyboardKey ="";
char *modifier="";

char *mouseButtonPressed="";
char *mouseState="";
int mouseX, mouseY;

int toggleProjection=0;

char *view="";




/* Hardware Interrupts callback functions */
void display();                                     //called every single frame
void reshape(int width, int height);                //called whenever the dimensions of the window are changed
void idle();                                        //when there is no interaction with the window
void keyboard(unsigned char key, int x, int y);     //whenever a keyboard key is pressed/interrupt occurs
void specialKeyboard(int button, int x, int y);     //captures special keys like: SHIFT ALT CTRL F1.....
void mouse(int button, int state, int x, int y);    //captures the mouse clicks and their state
void mouseMotion(int x, int y);                     //captures the motion of the mouse pointer in the window


void projection(float asp);

#endif // CALLBACKFUNCTIONS_H

CallbackFunctions.cpp

#include "../include/CallbackFunctions.h"
#include "../include/Text.h"

extern int dim;



void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST);

    //Drawing stuff

    glFlush();
    glutSwapBuffers();
}

void idle()
{
    display();
}

/* This callback function is called whenever the window is resized or reshaped */
void reshape(int width, int height)
{
    //Width To Height Ratio
    float w2h = (height>0)? (float)width/height : 1;
    glViewport(0, 0, width, height);
    projection(w2h);
}


void projection(float asp)
{
    //Setting up the Viewing Matrix
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    if(toggleProjection==0)
    {
    glOrtho(-dim*asp, +dim*asp, -dim, +dim, -dim, +dim);
    }
    else
    {
        float fov = 55.0, near = asp/5, far = asp*5;
        gluPerspective(fov, asp, near, far);
        //To do
    }
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

/* This callback function is called when there is no activity in the window */
void keyboard(unsigned char key, int x, int y)
{
    switch(key)
    {
    case 27:
        exit(0);
        break;
    }
    keyboardKey = key;
    glutPostRedisplay();
}


/* This callback function is called whenever a keyboard interrupt is encountered */
void specialKeyboard(int key, int state, int a)
{

    int currentModifier = glutGetModifiers();
    switch(currentModifier)
    {
    case GLUT_ACTIVE_SHIFT:
        modifier = "SHIFT";
        break;
    case GLUT_ACTIVE_CTRL:
        modifier = "CTRL";
        break;
    case GLUT_ACTIVE_ALT:
        modifier = "ALT";
        break;
    }
    switch(key)
    {
    case GLUT_KEY_F1:
        if(toggleProjection=0)
        {
            toggleProjection = 1;
            view = "perspective";
            break;
        }
        else
        {
            toggleProjection = 0;
            view = "orthographic";
            break;
        }
    }
    glutPostRedisplay();
}


void mouse(int button, int state, int x, int y)
{
    if(button == GLUT_LEFT_BUTTON)
        mouseButtonPressed = "Left";
    else if(button == GLUT_RIGHT_BUTTON)
        mouseButtonPressed = "Right";

    if(state == GLUT_DOWN)
        mouseState = "down";
    else if(state == GLUT_UP)
        mouseState = "up";
}

void mouseMotion(int x, int y)
{
    mouseX = x;
    mouseY = y;
}



/*void info()
{
    displayTextAt(-130, -40, 0, "mouseButton: %s", mouseButtonPressed);
    displayTextAt(-130, -50, 0, "mouseButtonState: %s", mouseState);
    displayTextAt(-130, -60, 0, "mouseX: %d", mouseX);
    displayTextAt(-130, -70, 0, "mouseY: %d", mouseY);
    displayTextAt(-130, -80, 0, "keyboard key: %c", keyboardKey);
    displayTextAt(-130, -90, 0, "modifier: %s", modifier);
    displayTextAt(70, -90, 0, view);
}*/

Solution

  • This is really a global variable declaration in a header problem. I've included the pastebin code (with fixes) for posterity.

    /*This is the CallbackFunctions.h ------------------ */
    #ifndef CALLBACKFUNCTIONS_H
    #define CALLBACKFUNCTIONS_H
    #include <GL/freeglut.h>
    
    extern unsigned char keyboardKey;
    
    
    /* Hardware Interrupts callback functions */
    void display();                                     //called every single frame
    void keyboard(unsigned char key, int x, int y);     //whenever a keyboard key is pressed/interrupt occurs
    #endif // CALLBACKFUNCTIONS_H
    
    /*This is the CallbackFunctions.cpp ------------------ */
    
    #include "CallbackFunctions.h"
    
    
    
    unsigned char keyboardKey = 0;
    
    void display()
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    }
    
    void keyboard(unsigned char key, int x, int y)
    {
        keyboardKey = key;
    }
    
    
    
    /* --------------- This is the Main.cpp ---------------- */
    
    #include <GL/freeglut.h>
    #include "CallbackFunctions.h"
    
    
    
    #define WIDTH 700
    #define HEIGHT 400
    
    void setupGlutWindow(int *argc, char **argv, int width, int height);
    void setupCallbackFunctions(); 
    
    int main(int argc, char** argv)
    {
        setupGlutWindow(&argc, argv, WIDTH, HEIGHT);
        setupCallbackFunctions();
        glutMainLoop();
    }
    
    void setupGlutWindow(int *argc, char **argv, int width, int height)
    {
        glutInit(argc, argv);
        glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
        glutInitWindowSize(700, 500);
        glutCreateWindow("Project L3");
        glViewport(0, 0, width, height);
    }
    
    void setupCallbackFunctions()
    {
        glutDisplayFunc(display);
        glutIdleFunc(display);
        glutKeyboardFunc(keyboard);
    }