Creating a simple menu in OpenGL GLUT

I’m quite new at this OpenGL programming so i just need a little push in the right direction :slight_smile:

I’m just trying to create a simple pop-up menu to change the color of two shapes that i have in my program. But it’s not working right. The shapes do change colors but only every other time when i click in the menu and sometimes it doesn’t change at all. The shapes are supposed to be red fron the beginning and then I want to be able to change them to blue and then change it back by choosing the other option.

So, here’s the code anyways;

#include <GLUT/glut.h>
#include <stdlib.h>

GLfloat angle = 0.0;	/*Rotationsvinkel*/

void createMenu(void);
void menu(int value);
void disp(void);

static int win;
static int menyid;
static int animeringsmeny;
static int springmeny;
static int val = 0;
static int fyrkantRoed = 1;
static int fyrkantGroen = 0;
static int fyrkantBla = 0;
static int trekantRoed = 1;
static int trekantGroen = 0;
static int trekantBla = 0;

int main(int argc, char **argv){
	// normal initialisation
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE);
	win = glutCreateWindow("GLUT MENU");
	// put all the menu functions in one nice procedure
	// set the clearcolor and the callback
	// enter the main loop
void createMenu(void){
	// MENU //
	// Create a submenu, this has to be done first.
	animeringsmeny = glutCreateMenu(menu);
	// Add sub menu entry
	glutAddMenuEntry("På", 1);
	glutAddMenuEntry("Av", 2);
	springmeny = glutCreateMenu(menu);
	glutAddMenuEntry("Gå", 3);
	glutAddMenuEntry("Galopp", 4);
	// Create the menu, this menu becomes the current menu
	menyid = glutCreateMenu(menu);
	// Create an entry
	glutAddSubMenu("Animering", animeringsmeny);
	glutAddSubMenu("Löpstil", springmeny);
	// Create an entry
	glutAddMenuEntry("Avluta", 0);
	// Let the menu respond on the right mouse button

void disp(void){
	// Just clean the screen
	glColor3f(fyrkantRoed, fyrkantGroen, fyrkantBla);
    glVertex2f(-1.0f, -1.0f);
    glVertex2f(-0.75f, -1.0f);
    glVertex2f(-0.75f, -0.75f);
    glVertex2f (-1.0f, -0.75f);
	glColor3f(trekantRoed, trekantGroen, trekantBla);
    glVertex2f(-0.6f, -1.0f);
    glVertex2f(-0.4f, -1.0f);
    glVertex2f(-0.6f, -0.6f);
	// ändra färg beroende på menyval
	if(val == 1){
		trekantBla = 1;
		trekantRoed = 0;
		trekantGroen = 0;
	else if(val == 2){
		trekantBla = 0;
		trekantRoed = 1;
		trekantGroen = 0;
	else if(val == 3){
		fyrkantRoed = 0;
		fyrkantGroen = 0;
		fyrkantBla = 1;
	else if(val == 4){
		fyrkantRoed = 1;
		fyrkantGroen = 0;	
		fyrkantBla = 0;

void menu(int value){
	if(value == 0){
	// you would want to redraw now

Perhaps there’s an easier way to do this so any ideas are welcome :slight_smile:

Thank you!

Every time You clich Menu item, menu function is fired (val is set to correct value ), and then You post redraw - that’s correct,
but in your disp function colors are set AFTER drawing shapes.
Either move this part of Your code:

to menu func, or execute it before drawing.