CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Sep 2011
    Posts
    25

    Need opengl help.

    I made a program with opengl and I keep getting this error:


    Code:
    1>------ Build started: Project: Aiedoo, Configuration: Debug Win32 ------
    1>  RenderShape.cpp
    1>  Main.cpp
    1>  Generating Code...
    1>RenderShape.obj : error LNK2005: "void __cdecl Render2dBox(float,float,float,float)" (?Render2dBox@@YAXMMMM@Z) already defined in Main.obj
    1>RenderShape.obj : error LNK2005: "void __cdecl Render2dfilledbox(float,float,float,float)" (?Render2dfilledbox@@YAXMMMM@Z) already defined in Main.obj
    1>RenderShape.obj : error LNK2005: "void __cdecl RenderTriStrip(float,float,float,float)" (?RenderTriStrip@@YAXMMMM@Z) already defined in Main.obj
    1>C:\Program Files\Aiedoo\Debug\Aiedoo.exe : fatal error LNK1169: one or more multiply defined symbols found
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    Main.cpp:

    Code:
    #include <RenderShape.cpp>
    #include <GL/glew.h>
    #include <GL/glut.h>
    
    bool* keyStates = new bool[256]; // Create an array of boolean values of length 256 (0-255) 
    bool* keySpecialStates = new bool[246]; // Create an array of boolean values of length 256 (0-255)  
    
    void keyOperations (void) 
    {
    
    } 
    
    void keySpecialOperations (void) 
    {   
    
    }  
    
    void keyPressed (unsigned char key, int x, int y) 
    {
    keyStates[key] = true; // Set the state of the current key to pressed
    } 
    
    void keyUp (unsigned char key, int x, int y) 
    {
    keyStates[key] = false; // Set the state of the current key to not pressed  
    
    } 
    
    void keySpecial (int key, int x, int y) 
    {
    keyStates[key] = true; // Set the state of the current key to pressed 
    }
    
    void keySpecialUp (int key, int x, int y) 
    {   
    keyStates[key] = false; // Set the state of the current key to not pressed  
    }  
    
    
    void display (void) 
    {   
    keyOperations();
    keySpecialOperations();
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Clear the background of our window to red   
    glClear(GL_COLOR_BUFFER_BIT); //Clear the colour buffer (more buffers later on)   
    glLoadIdentity(); // Load the Identity Matrix to reset our drawing locations
    glTranslatef(0.0f, 0.0f, -5.0f);
    Render2dBox(1.0, 2.0, 0.0, 1.0);
    glFlush(); // Flush the OpenGL buffers to the window   
    }   
     
    void reshape (int width, int height) 
    {   
    glViewport(0, 0, (GLsizei)width, (GLsizei)height); // Set our viewport to the size of our window   
    glMatrixMode(GL_PROJECTION); // Switch to the projection matrix so that we can manipulate how our scene is viewed   
    glLoadIdentity(); // Reset the projection matrix to the identity matrix so that we don't get any artifacts (cleaning up)   
    gluPerspective(60, (GLfloat)width / (GLfloat)height, 1.0, 100.0); // Set the Field of view angle (in degrees), the aspect ratio of our window, and the new and far planes   
    glMatrixMode(GL_MODELVIEW); // Switch back to the model view matrix, so that we can start drawing shapes correctly   
    }
    
    int main (int argc, char **argv) 
    {     	
    glutInit(&argc, argv); // Initialize GLUT
    glutSpecialFunc(keySpecial); // Tell GLUT to use the method "keySpecial" for special key presses   
    glutSpecialUpFunc(keySpecialUp); // Tell GLUT to use the method "keySpecialUp" for special up key events
    glutKeyboardFunc(keyPressed); // Tell GLUT to use the method "keyPressed" for key presses  
    glutKeyboardUpFunc(keyUp); // Tell GLUT to use the method "keyUp" for key up events  
    glutInitDisplayMode (GLUT_SINGLE); // Set up a basic display buffer (only single buffered for now)   
    glutInitWindowSize (800, 600); // Set the width and height of the window   
    glutInitWindowPosition (100, 100); // Set the position of the window   
    glutCreateWindow ("Biolife"); // Set the title for the window   
    glutDisplayFunc(display); // Tell GLUT to use the method "display" for rendering   
    glutReshapeFunc(reshape); // Tell GLUT to use the method "reshape" for rendering   
    glutMainLoop(); // Enter GLUT's main loop   
    }
    RenderShape.cpp:

    Code:
    #include <GL/glew.h>
    #include <GL/glut.h>
    
    void Render2dBox(float r, float g, float b, float a)
    {
    	glColor4f (r, g, b, a);
    	glBegin(GL_QUADS);
    	glVertex3f(-1.0f, -1.0f, 0.0f); // The bottom left corner   
    	glVertex3f(-1.0f, 1.0f, 0.0f); // The top left corner   
    	glVertex3f(1.0f, 1.0f, 0.0f); // The top right corner   
    	glVertex3f(1.0f, -1.0f, 0.0f); // The bottom right corner  
    	glEnd(); 
    }
    
    void Render2dfilledbox(float r, float g, float b, float a)
    {
    glColor4f (r, g, b, a);
    glPointSize(20.0f);   
    glBegin(GL_POINTS); // Start drawing a point primitive   
    glVertex3f(-1.0f, -1.0f, 0.0f); // The bottom left corner   
    glVertex3f(-1.0f, 1.0f, 0.0f); // The top left corner   
    glVertex3f(1.0f, 1.0f, 0.0f); // The top right corner   
    glVertex3f(1.0f, -1.0f, 0.0f); // The bottom right corner   
    glEnd();   
    }
    
    void RenderTriStrip(float r, float g, float b, float a)
    {
    glColor4f (r, g, b, a);
    glBegin(GL_TRIANGLE_STRIP); 
    glVertex3f(-1.0f, -1.0f, 0.0f);
    glVertex3f(-1.0f, 1.0f, 0.0f);
    glVertex3f(1.0f, 1.0f, 0.0f);    
    glVertex3f(1.0f, -1.0f, 0.0f);   
    glVertex3f(-1.0f, -1.0f, 0.0f);   
    glEnd();   
    }
    Does anyone know whats wrong with this? All it was meant to do is draw something by calling the function in Display() , Render2dBox specificly. I am building a game engine so this is part of the testing.

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Need opengl help.

    Quote Originally Posted by HavingPhun View Post
    #include <RenderShape.cpp>
    You shouldn't include the cpp file.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    Join Date
    Sep 2011
    Posts
    25

    Re: Need opengl help.

    you mean the .cpp part just #include <RenderShape> ?

  4. #4
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Need opengl help.

    Usually you create a RenderShape.h that declares all things that are public from RenderShape and include that where needed. Now you compile both main.cpp (that includes RenderShape.cpp) and RenderShape.cpp and then try to link main.obj and RenderShape.obj (or maybe .o if you run in Linux). I.e. the RenderShape functions appear both in main.obj and in RenderShape.obj thus causing a linker collision.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  5. #5
    Join Date
    Sep 2011
    Posts
    25

    Re: Need opengl help.

    Ok thanks would I just make a Rendershape class with the public functions in the header and then include that in RenderShape.cpp?

  6. #6
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Need opengl help.

    Yes you include it in RenderShape.cpp and also in main.cpp. Tthen you compile them both and link them together to make the executable. In visual studio all you have to do to make this happen is to have both cpp files added to the project and build it.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  7. #7
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Need opengl help.

    One more thing now that I see it. Why do you create the globals keyStates and keySpecialStates as pointers like that? They could just as well be declared as
    Code:
    bool keyStates[256]; // Create an array of boolean values of length 256 (0-255) 
    bool keySpecialStatesbool[246]; // Create an array of boolean values of length 256 (0-255)
    As it is now you have a memory leak since you never delete them. I admit that it's not a critical memory leak but you shouldn't get in the habit of allocating yourself when it's not needed.

    An even better option is to use stl containers like std::vector instead of arrays (and of course not having them as globals but that's another issue). This page has a good stl documentation http://cplusplus.com/
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  8. #8
    Join Date
    Sep 2011
    Posts
    25

    Re: Need opengl help.

    So they vector would be
    Code:
     std::vector <bool> keystates[256];
    Also how do I let a function with a header take arguments because if i do this.

    Reshape.h:

    Code:
    class Reshape
    {
    public:
     Reshape(int height, int width);
     ~Reshape()
    
    private:
    
    };

    Reshape.cpp:

    Code:
    #include <Reshape.h>
    
    Reshape(int height, int width)
    {
    // Action
    }
    Thats just an example but I only get errors if I have it pass an argument. Can you help me?

  9. #9
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Need opengl help.

    A vector is capable of storing any number of element (limited by the amount of memory) so you don't declare it like that. Anyway, it might be that talking about stl was a little hasty. You're probably better off by using the C-array (without new:ing it)

    What errors do you get? You don't call the Reshape constructor as a regular function do you?
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  10. #10
    Join Date
    Sep 2011
    Posts
    25

    Re: Need opengl help.

    Well i am making a game engine and this is one of its utilities it has. So yes it would be called by other things as regular function.
    I changed the code a little:

    UtilWindow.h:

    Code:
    #ifndef __UTILWINDOW_H
    #define __UTILWINDOW_H
    
    class UtilWindow
    {
    public:
    	Reshape(int width, int height);
    	~Reshape();
    
    private:
    
    };
    
    
    #endif

    UtilWindow.cpp:

    Code:
    #include <UtilWindow.h>
    #include <GL\glew.h>
    #include <GL\glut.h>
    
    
    UtilWindow::Reshape (int width, int height) 
    {   
    glViewport(0, 0, (GLsizei)width, (GLsizei)height);  
    glMatrixMode(GL_PROJECTION);  
    glLoadIdentity();   
    gluPerspective(60, (GLfloat)width / (GLfloat)height, 1.0, 100.0);   
    glMatrixMode(GL_MODELVIEW);  
    }

    The errors:

    Code:
    1>------ Build started: Project: Aiedoo, Configuration: Debug Win32 ------
    1>  UtilWindow.cpp
    1>c:\program files\aiedoo\aiedoo\utility\utilwindow.h(7): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\program files\aiedoo\aiedoo\utility\utilwindow.h(7): warning C4183: 'Reshape': missing return type; assumed to be a member function returning 'int'
    1>c:\program files\aiedoo\aiedoo\utility\utilwindow.h(8): error C2523: 'UtilWindow::~Reshape' : destructor tag mismatch
    1>c:\program files\aiedoo\aiedoo\utility\utilwindow.cpp(7): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

  11. #11
    Join Date
    Sep 2011
    Posts
    25

    Re: Need opengl help.

    Quote Originally Posted by S_M_A View Post
    What errors do you get? You don't call the Reshape constructor as a regular function do you?
    Why would that require that I declare it differently?

  12. #12
    Join Date
    Jun 2008
    Posts
    592

    Re: Need opengl help.

    Code:
    class UtilWindow
    {
    public:
        Reshape(int width, int height);
        ~Reshape();
    
    private:
    
    };
    You should look at some tutorials about classes and see how they declare a constructor and destructor and compare yours.

    http://www.learn-programming.za.net/...p_learn10.html
    http://www.cplusplus.com/doc/tutorial/classes/
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured