PDA

View Full Version : co-ordinate problem in glut



onlyhuman23
07-22-2002, 12:42 AM
I am trying to get the co-ordinates in 2D to get some button like interactive mouse function while using glut. For simplicity I am trying to draw a square where ever a mouse click is made, but the problem is that I cannot get the co-ordinates right; especially when I maximize the window the co-ordinates sort of corrupt. Secondly is there a way to keep the window size constant in glut, as I have gone through the glut specs but I cant seem to find any.
I know about the transformation from world co-ordinates to viewport co-ordinates, and I declare but the functions but to no avail. Below is the code that I am using please see it and help me if someone can.
I am trying to make a program where a few stacks and queues will be displayed and the processes will be transffered from one stack to the other after scheduling. so apart from showing rectangular component containing stacks I have to have buttons for scheduling and start and stop etc. I am compiling in VC6 but am not using windows specific code.
////////////////////////////////////////////
#include <windows.h>
#include <math.h> // Header File For Windows Math Library
#include <stdio.h> // Header File For Standard Input/Output
#include <stdarg.h> // Header File For Variable Argument Routines
//////////////////////////////////////////////////////////////////////////
#include <gl/GL.h>
#include <gl/glu.h>
#include <gl/glut.h>

const int screenWidth = 800; //width of the screen in pixels
const int screenHeight = 600; //height of the screen in pixels
GLdouble A,B,C,D; //values used for shifting and scaling
int var=0;

class GLintPoint
{
public:
GLint x,y;
};

//GLdouble one=0, two=0;
//myInit()

void myInit(void)
{

glClearColor(0.0,0.0,0.0,0.0); //Background color is white
glColor3f(0.0f,1.0f,0.0f); //Drawing color is black
glPointSize(2.0); //A dot is 2*2 pixels
glMatrixMode(GL_PROJECTION); //Camera shape
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)screenWidth, 0.0, (GLdouble) screenHeight);//This function sets the world co-ordinates
glViewport(0,0,screenWidth,screenHeight); //This function sets the viewport co-ordinates
/* A = screenWidth /4.0; //Set values used for scaling and shifting
B = 0.0;
C = D = screenHeight / 2.0;
*/
}
//myDisplay
void myDisplay(void)
{

// glutFullScreen();
glClear(GL_COLOR_BUFFER_BIT); //clear the screen
// glTranslatef(0.0f,0.0f,-1.0f); // Move One Unit Into The Screen
/* glBegin(GL_LINE_LOOP);
for(GLdouble x = 0; x < 4.0 ; x += 0.005)
{
GLdouble func =exp(-x) * cos(2 * 3.14159265 * x);

//glVertex2d(one,two);
glVertex2d(A * x + B, C * func + D );

//one = A * x + B;
//two = C * func + D;
}
glEnd();*/
glColor3f(0.0f,1.0f,0.0f);
glRecti(150,500,430,340);
glColor3f(0.50f,0.50f,.50f);
glRecti(180,440,220,390);
glRecti(240,440,280,390);
glRecti(300,440,340,390);
glRecti(360,440,400,390);
glColor3f(0.20f,0.30f,0.40f);
// glBegin(GL_LINE_LOOP);
// glutSolidTeapot( 20);
// glEnd();
// glutStrokeCharacter(GLUT_STROKE_ROMAN, 33);
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'N');//I am not able to position this character

glFlush(); //send all output to display
}
void myMouse(int button,int state, int x, int y)
{
if(button== GLUT_LEFT_BUTTON && state == GLUT_DOWN)
// &&
// (glutGet(GLUT_WINDOW_X)>299 && glutGet(GLUT_WINDOW_X)<399) && (glutGet(screenHeight-GLUT_WINDOW_Y)>199 && glutGet(screenHeight-GLUT_WINDOW_Y)<249))
{
glRecti(x,screenHeight-y,x+50,(screenHeight-y)+50);
// glutBitmapCharacter(GLUT_BITMAP_9_BY_15,'y');

glFlush();

}
if(button== GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
// glutDestroyWindow(var);
{
glutPositionWindow(100, 150);
glutReshapeWindow(640,480);
}
}

//Main
void main(int argc, char** argv)
{
glutInit(&argc, argv); //initialize the toolkit
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); //set display mode
glutInitWindowSize(screenWidth, screenHeight); //set window size
glutInitWindowPosition(100, 150); //set window position on screen
var=glutCreateWindow("Dot plot of a function"); //open the screen window
glutFullScreen();
glutMouseFunc(myMouse);
glutDisplayFunc(myDisplay); //register redraw function
myInit();
glutMainLoop(); //go into a perpetual loop
}