Thread: changing color in OpenGL

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    31

    changing color in OpenGL

    Hi, I want to design a function so if the user press "c" at any time during my program, this function will display random color on the object from green, yellow, blue and red. How do I do it? thanks in advance.
    'The bigger they are, the harder they fall' ~Yang

  2. #2
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    Okay, so you want to change the red green and blue color components. You've got to generate a floating point between 0 and 1 if you're using glColor3f, which I have to assume that you are because you did not tell me otherwise.


    if(GetKeyState('C') & 0x80)
    {
    red = (float)rand() / (float)RAND_MAX;
    green = (float)rand() / (float)RAND_MAX;
    blue = (float)rand() / (float)RAND_MAX;
    }

    rand() produces a maximum value of rand_max, which is why rand()/ rand_max must produce a float between 0 and 1

    make sure that you call:
    srand(GetTickCount());
    at the start of the program. this 'initializes' the random number generator. include windows.h to get it to work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM
  2. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  3. OpenGL .dll vs video card dll
    By Silvercord in forum Game Programming
    Replies: 14
    Last Post: 02-12-2003, 07:57 PM
  4. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM
  5. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM