Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Matthew02

glutMouseFunc and global variables...

This topic is 5648 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Could someone please explain to me why my glutMouseFunc functions cannot modify my global variables? I have three different windows each with a different mouse function and I am trying to clear one window to a color determined by clicking in another window. I have a global variable which should contain the last color selected. The variable is updated within the mouse function, but when the mouse function returns, the variable returns to it's prior state. Please look at my code and tell me if you can figure out what the problem is. You can assume that everything else in the code works correctly. Thanks for your help.
      
#include "stdinc.h"

#include "image.h"

#include "fileppm.h"

#include "mask.h"


void DisplaySource(void);
void DisplayFinal(void);
void DisplayComposite(void);
void SourceImageMouse(int button, int state, int x, int y);
void FinalImageMouse(int button, int state, int x, int y);
void ControlPanelMouse(int button, int state, int x, int y);


ImageRGBb SourceImage;
ImageRGBAb MaskImage;
ImageRGBAb FinalImage;
PixelRGBb ClearColor;


int main(int argc, char** argv)
{
	FilePPM PFile("flag.ppm");

	MaskImage = PFile.Extract(SourceImage);


	Mask FlagMask1(5.0f, 5.0f, 0.0f, 0.2f, 0.2f);
	FlagMask1.Apply(MaskImage);

	Mask FlagMask2(5.0f, 5.0f, 230.0f, 0.2f, 0.2f);
	FlagMask2.Apply(MaskImage);

	Mask FlagMask3(25.0f, 5.0f, 210.0f, 0.2f, 0.2f);
	FlagMask3.Apply(MaskImage);

//	Mask CarouselMask(40.0f, 5.0f, 240.0f, 0.2f, 0.2f);

//	CarouselMask.Apply(MaskImage);


	MaskImage.DiscardColor();

	FinalImage = SourceImage + MaskImage;
	
	PFile.ConstructP6(FinalImage);
	PFile.Save("test.ppm");

	PFile.ConstructP5(MaskImage);
	PFile.Save("test.pgm.ppm");

	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);	// set up alpha blending mode


	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
	glutInitWindowSize(SourceImage.GetWidth(), SourceImage.GetHeight());
	glutInitWindowPosition(10, 30);
	glutCreateWindow("Source Image");
	glutDisplayFunc(DisplaySource);
	glutMouseFunc(SourceImageMouse);

	glutInitWindowSize(FinalImage.GetWidth(), FinalImage.GetHeight());
	glutInitWindowPosition(20 + SourceImage.GetWidth(), 30);
	glutCreateWindow("Final Image");
	glutDisplayFunc(DisplayFinal);
	glutMouseFunc(FinalImageMouse);

	glutInitWindowSize(250, 375);
	glutInitWindowPosition(30 + SourceImage.GetWidth() + FinalImage.GetWidth(), 30);
	glutCreateWindow("Control Panel");
	glutDisplayFunc(DisplayControlPanel);
	glutMouseFunc(ControlPanelMouse);

	glutMainLoop();

	return 0;
}


void SourceImageMouse(int button, int state, int x, int y)
{
	ClearColor = SourceImage(x, y);
}


void FinalImageMouse(int button, int state, int x, int y)
{
}


void ControlPanelMouse(int button, int state, int x, int y)
{
}


void DisplaySource(void)
{
	glClearColor(255.0f, 255.0f, 255.0f, 255.0f);
	glClear(GL_COLOR_BUFFER_BIT);

	int Width = SourceImage.GetWidth();
	int Height = SourceImage.GetHeight();

	PixelRGBAb Pixel;
	GLubyte * Framebuffer = new GLubyte[Width * Height * 4];

	int k = 0;
	for (int i=Height-1; i>=0; i--)
	{
		for (int j=0; j<Width; j++)
		{
			Pixel = SourceImage(j, i);
			Framebuffer[k++] = Pixel.r;
			Framebuffer[k++] = Pixel.g;
			Framebuffer[k++] = Pixel.b;
			Framebuffer[k++] = Pixel.a;
		}
	}

	glEnable(GL_BLEND);
	glDrawPixels(Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, Framebuffer);
    glFlush();
	glDisable(GL_BLEND);
}


void DisplayFinal(void)
{
	glClearColor(255.0f, 255.0f, 255.0f, 255.0f);
	glClear(GL_COLOR_BUFFER_BIT);

	int Width = FinalImage.GetWidth();
	int Height = FinalImage.GetHeight();

	PixelRGBAb Pixel;
	GLubyte * Framebuffer = new GLubyte[Width * Height * 4];

	int k = 0;
	for (int i=Height-1; i>=0; i--)
	{
		for (int j=0; j<Width; j++)
		{
			Pixel = FinalImage(j, i);
			Framebuffer[k++] = Pixel.r;
			Framebuffer[k++] = Pixel.g;
			Framebuffer[k++] = Pixel.b;
			Framebuffer[k++] = Pixel.a;
		}
	}

	glEnable(GL_BLEND);
	glDrawPixels(Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, Framebuffer);
    glFlush();
	glDisable(GL_BLEND);
}


void DisplayControlPanel(void)
{
	glClearColor(ClearColor.r, ClearColor.g, ClearColor.b, 255.0f);
	glClear(GL_COLOR_BUFFER_BIT);
    glFlush();
}
  
[edited by - matthew02 on December 5, 2002 1:48:57 AM]

Share this post


Link to post
Share on other sites
Advertisement
You too? I thought I was the only one with this problem. Haven''t found the proper solution, I''m currently skirting by it by having mousefunc call other functions that change the values, same with keyboardfunc.
For instance:
My keyboard function goes through a switch case, and call a global function (move, or rotate) passing different values depending on the input. Not as clean as it could be, but it works. I do hope to find out why exactly this happens though.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!