Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8738349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:42:52+00:00 2026-06-13T10:42:52+00:00

I have written a color picker in C++ using OpenGL but I cannot figure

  • 0

I have written a color picker in C++ using OpenGL but I cannot figure out how to color a polygon (or anything else for that matter) with the colors I have stored. Do I need to put the draw function in some kind of loop or is there a better way to do this?

#ifdef __APPLE__
#  include <OpenGL/gl.h>
#  include <OpenGL/glu.h>
#  include <GLUT/glut.h>
#else
#  include <GL/gl.h>
#  include <GL/glu.h>
#  include <GL/glut.h>
#endif

#include <iostream>
#include <cmath>
using namespace std;

#define WIDTH 750
#define HEIGHT 750

int i=0,mousex, mousey;

float pick[3];

bool mouseleftdown = false;



void hex(void){
glBegin(GL_POLYGON);
    glColor3f(1,0,0);           //red
    glVertex2f(0, 2);           //top
    glColor3f(1,.38,.01);       //orange
    glVertex2f(2, 1);           //top right
    glColor3f(1,1,0);           //yellow
    glVertex2f(2, -1);          //bottom right
    glColor3f(0,1,0);           //green
    glVertex2f(0, -2);          //bottom
    glColor3f(0,0,1);           //blue
    glVertex2f(-2, -1);         //bottom left
    glColor3f(.8,0,.8);         //purple
    glVertex2f(-2, 1);          //top left
glEnd();

glEnd();
}

void square(void){
glBegin(GL_POLYGON);
glVertex2i(1, -1);
glVertex2i(1, 1);
glVertex2i(-1, 1);
glVertex2i(-1, -1);
glEnd();
}

void mouse(int button, int state, int x, int y){
// Save the left button state
/*if (button == GLUT_LEFT_BUTTON)
{
   if (state == GLUT_DOWN)
    glutPostRedisplay();  // Left button has changed; redisplay!
}*/


// Save the mouse position
mousex = x;
mousey = y;

glReadPixels(mousex , mousey , 1 , 1 , GL_RGB , GL_FLOAT , &pick);
cout << pick[0] <<"pick";
cout << " mouse x " << mousex << "\n";
cout << " mouse y " << mousey << "\n";
fflush(stdout);

cout << "pick R: " << pick[1] << "\n";
cout << "pick G: " << pick[0] << "\n";
cout << "pick B: " << pick[2] << "\n";
}



void draw(void){
glClear(GL_COLOR_BUFFER_BIT);

glPushMatrix();
glScalef(20,20,1);
    hex();
glPopMatrix();

glPushMatrix();
glTranslatef(100,100,0);
glColor3f(pick[1],pick[0],pick[2]);
glScalef(20,20,1);
square();
glPopMatrix();

glFlush();

}

void my_init(void){
//glClearColor(0, 0, 0, 1);               //sets clear color
glLineWidth(4);
gluOrtho2D(-100, 100, -100, 100);       //sets origin
}

int main(int argc, char* argv[ ]){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutInitWindowSize(WIDTH, HEIGHT);
glutCreateWindow("Color Picker");
glutDisplayFunc(draw);
glutMouseFunc(mouse);

my_init();

glClearColor(1, 1, 1, 0);

glutMainLoop();                         //listens for events

return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:42:53+00:00Added an answer on June 13, 2026 at 10:42 am

    You have to simply add the line:

    glutPostRedisplay();
    

    at the end of your mouse callback function.

    This is because you always have to explicitly tell glut when to refresh the window.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written code that changes the color of a mark the user places
I have written a code to change the CSS (color font) of tr, but
Okay, I have written a function that should convert HSL color value to RGB.
I have written a code in java using swing.So that i will have a
I have written a code in java using swing, so that I will have
I have written a very basic jquery script that is true theorically but not
I have written a function that sorts a big scale of data. To test
I have written a function that extract the domain from hostname. e.g. www.domain.com ->
I have written a very simple CTE expression that retrieves a list of all
I have written a basic website that calculates SHA1 hashes. I know, there are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.