944,188 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 8786
  • C++ RSS
Feb 15th, 2005
0

Randomize number with OpenGL

Expand Post »
I am taking a Computer Graphics course where we are doing some c++ programming using the OpenGL API. I'm writing a C++ program in Visual Studio.NET, where I've installed GLUT (Open GL Utility Toolkit). OpenGL, itself, apparently comes with VS.

The only include that I have in my one-file program is:
#include <GL/glut.h>


The program works fine up until the point where I need to randomize a number. When I try including stdlib.h or cstdlib in my file, I get an error message about redefinition and my program won't compile.

In the textbook the class is using, it shows rand() being used without having to include anything extra. I'm assuming it's somehow built into glut.h? However, even when I copy code directly out of a textbook example, I get the error message "unresolved external symbol referenced in function" and then "fatal error: 1 unresolved externals"

When I remove the line or two referencing rand() in my program, once again it compiles and runs fine. Please, how can I generate a random number from within an OpenGL program?
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 163
The Queen of DaniWeb
cscgal is offline Offline
13,646 posts
since Feb 2002
Feb 15th, 2005
0

Re: Randomize number with OpenGL

Oh boy,
Make sure you include the .h files in your project - add them to your project.
Copy "glut32.dll" to your C:\WINDOWS\system\
Copy "glut32.lib" to your C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\Lib
Create a directory called "gl" under
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\Include\

and copy glut32.h to it, ie under
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\Include\gl\
i copied all the GLUT .h files there anyway.

When you create your project, click the project name then choose properties, From the Configuration list box, select All Configurations, In the left pane, select the Linker subtree and then the Input option. Add the given code to the Additional Dependencies text box in the right pane. This tells Visual Studio where to find GLUT. (Copy and Paste: opengl32.lib glu32.lib glut32.lib )

Good luck.
Intel
Reputation Points: 10
Solved Threads: 1
Light Poster
Intel is offline Offline
30 posts
since Feb 2005
Feb 15th, 2005
0

Re: Randomize number with OpenGL

Thank you, Intel. However, all of that was done already And it works. I was able to create my first two OpenGL programs just fine ... this is the third project. The only time the program stops working is when I try to use rand(). It works other than that darn randomize!

Any other ideas?
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 163
The Queen of DaniWeb
cscgal is offline Offline
13,646 posts
since Feb 2002
Feb 16th, 2005
0

Re: Randomize number with OpenGL

In C++, rand() is defined in
C++ Syntax (Toggle Plain Text)
  1. #include <stdlib.h>
I used rand() and all i included is
C++ Syntax (Toggle Plain Text)
  1. #include <GL/glut.h>
Reputation Points: 10
Solved Threads: 1
Light Poster
Intel is offline Offline
30 posts
since Feb 2005
Feb 16th, 2005
0

Re: Randomize number with OpenGL

Try
C++ Syntax (Toggle Plain Text)
  1. std::rand();
Here is how i used it
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <GL/gl.h>
  4. #include <GL/glu.h>
  5. .....
  6. double .... = std::rand();
Reputation Points: 10
Solved Threads: 1
Light Poster
Intel is offline Offline
30 posts
since Feb 2005
Feb 16th, 2005
0

Re: Randomize number with OpenGL

This might sound silly silly, but when I did
C++ Syntax (Toggle Plain Text)
  1. #include <GL/glut.h>
  2. #include <cstdlib>
I got a compile-time error message. But when I did
C++ Syntax (Toggle Plain Text)
  1. #include <cstdlib>
  2. #include <GL/glut.h>
everything compiled and ran just fine. Either way, it all works now. Thank you for your help and support.
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 163
The Queen of DaniWeb
cscgal is offline Offline
13,646 posts
since Feb 2002
Feb 16th, 2005
0

Re: Randomize number with OpenGL

#include <cstdlib> is declared in glut.h so when u declare it first it wont be redefined in glut.h , [#ifndef ... #def ... #endif]
Reputation Points: 10
Solved Threads: 1
Light Poster
Intel is offline Offline
30 posts
since Feb 2005
Feb 16th, 2005
0

Re: Randomize number with OpenGL

Yes, but the problem is that when I only included glut.h, and not cstdlib or stdlib.h at all, I got a compiling error when I used rand() !!
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 163
The Queen of DaniWeb
cscgal is offline Offline
13,646 posts
since Feb 2002
Feb 16th, 2005
0

Re: Randomize number with OpenGL

My guess then is that rand is defined in the file space - glut.h, check it out and you'll see ....
Reputation Points: 10
Solved Threads: 1
Light Poster
Intel is offline Offline
30 posts
since Feb 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: help getting !true bool to break out of program
Next Thread in C++ Forum Timeline: arrays???





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC