PDA

View Full Version : Simply drawing lines; Adjusting drawing to window



Zaemz
04-11-2012, 01:11 PM
Hello people!

First, I have done some research, but I'm unfortunately unable to apply what I've seen to the problem I'm currently having.

A GLUT window will open, and the user can click and draw some simple lines, but if the window is stretched or moved, the coordinates for drawing the lines becomes skewed. I know that I have to adjust the window and the coordinates to that of the window being moved or resized, but I'm unsure how.

The following is my code:



//#include <windows.h>

#define _USE_MATH_DEFINES //provides access to math constants like M_PI
#include <math.h>
#include <iostream>
#include <string.h>
#include <vector>
#include "glut.h" //glut.h includes gl.h and glu.h

using namespace std;

/******************************************

GLUT Bitmap Fonts

0 GLUT_BITMAP_8_BY_13
1 GLUT_BITMAP_9_BY_15
3 GLUT_BITMAP_TIMES_ROMAN_10
4 GLUT_BITMAP_TIMES_ROMAN_24
5 GLUT_BITMAP_HELVETICA_10
6 GLUT_BITMAP_HELVETICA_12
7 GLUT_BITMAP_HELVETICA_18

GLUT Basic Color RGB Values

0 black 0.0, 0.0, 0.0
1 light grey 0.75, 0.75, 0.75
2 dark grey 0.5, 0.5, 0.5
3 maroon 0.5, 0.0, 0.0
4 red 1.0, 0.0, 0.0
5 purple 0.5, 0.0, 0.5
6 fuchsia 1.0, 0.0, 1.0
7 green 0.0, 0.5, 0.0
8 lime 0.0, 1.0, 0.0
9 olive 0.5, 0.5, 0.0
10 yellow 1.0, 1.0, 0.0
11 navy 0.0, 0.0, 0.5
12 blue 0.0, 0.0, 1.0
13 teal 0.0, 0.5, 0.5
14 aqua 0.0, 1.0, 1.0
15 white 1.0, 1.0, 1.0

******************************************/

const int screenWidth = 600; // width of screen window in pixels
const int screenHeight = 600; // height of screen window in pixels
const int screenLeft = 200; // left edge of screen window in pixels
const int screenTop = 50; // top edge of screen window in pixels

struct point {
int x;
int y;
};

// program specific variables
vector<point> points;
vector<vector <point>> lines;
bool mouseClicked;

//World coordinates
int leftSide = - screenWidth/2, rightSide = screenWidth/2;
int bottomSide = - screenHeight/2, topSide = screenHeight/2;
int nearSide = (screenWidth < screenHeight) ? - screenWidth/2 : -screenHeight/2;
int farSide = (screenWidth < screenHeight) ? screenWidth/2 : screenHeight/2;

//Bitmap font print
void bitmap_text(float x, float y, char *string, void *font)
{
int len, i;

glRasterPos2f(x, y);
len = (int) strlen(string);
for (i = 0; i < len; i++)
{
glutBitmapCharacter(font, string[i]);
}
}

//Initialize window
void init(void)
{
//Clear color
glClearColor(0.0, 0.0, 0.0, 0.0);
//Specify Projection view transformation matrix
glMatrixMode(GL_PROJECTION);
//Load identity matrix
glLoadIdentity();
//Viewing volume
//2D version
gluOrtho2D(leftSide, rightSide, bottomSide, topSide);
//3D version
//glOrtho(leftSide, rightSide, bottomSide, topSide, nearSide, farSide);
}

//Display callback function
void display(void)
{
//Clear window
glClear(GL_COLOR_BUFFER_BIT);
//Specify Model view transfromation matrix
glMatrixMode( GL_MODELVIEW );
//Load identity matrix
glLoadIdentity();

//Flush GL buffer = force OpenGL commands to execute
glFlush();
}

void idleDisplay(void)
{
//Clear window
glClear(GL_COLOR_BUFFER_BIT);
//Specify Model view transfromation matrix
glMatrixMode( GL_MODELVIEW );
//Load identity matrix
glLoadIdentity();

glColor3f( 0.0, 1.0, 0.0 ); //green
glBegin(GL_LINE_STRIP);
for (int i = 0; i < (int)points.size(); i++)
{
glVertex2f(points.at(i).x - screenWidth/2, -(points.at(i).y - screenHeight/2));
}
glEnd();


for (int i = 0; i < (int)lines.size(); i++)
{
glBegin(GL_LINE_STRIP);
for (int j = 0; j < (int)lines.at(i).size(); j++)
{
glVertex2f(lines.at(i).at(j).x - screenWidth/2, -(lines.at(i).at(j).y - screenHeight/2));
}
glEnd();
}

glFlush();
}

void mouse(int button, int state, int x, int y) {
if (state == 0 &amp;&amp; button == 0) {
mouseClicked = true;
cout << x << ", " << y << endl;
cout << "button #: " << button << endl;
cout << "button state: " << state << endl;
point newPoint;
newPoint.x = x;
newPoint.y = y;
points.push_back(newPoint);

vector<point> newPoints;
lines.push_back(newPoints);
}
if (state == 1 &amp;&amp; button == 0)
mouseClicked = false;
display();
}

void mouseMove(int x, int y)
{
if (mouseClicked == true)
{
point point;
point.x = x;
point.y = y;
lines.at(lines.size() - 1).push_back(point);
}
}

void reshape(int width, int height)
{

}

int main(int argc, char** argv)
{
cout << "\n\n\t***************************************";
cout << "\n\n\t\tSimple Drawing Program" << endl;
cout << "\t\t Hit 'q' to exit." << endl << endl;
cout << "\t***************************************";

//Initialize GLUT
glutInit(&amp;argc,argv);
//set color and buffering modes
//glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
//set intial window size
glutInitWindowSize(screenWidth,screenHeight);
//set intial window position
glutInitWindowPosition(screenLeft, screenTop);
//Create window
glutCreateWindow(" OpenGL Visualization");
//Initialize window
init();
//Reshaping window function
glutReshapeFunc(NULL);
//Specify display callback
glutDisplayFunc(display);
//Specify idle display
glutIdleFunc(idleDisplay);
//register mouse click callback
glutMouseFunc(mouse);
//register mouse motion callback
glutMotionFunc(mouseMove);
//Start even processing loop
glutMainLoop();
return 0;
}

If anyone has any hints or tips, please inform. I'm trying to learn, so make me think a little. :)

Thank you for your time.

BionicBytes
04-12-2012, 02:56 AM
sounds to me like the mouse coordinates in the mouse events are the key to this.
Are those window coordinates or screen/desktop coordinates.
Use a print statement (or what ever) to print the mouse coordinates as you move the mouse around the window. That way you can see if they go from 0 --> Window width/height or 0 -> screen width/height.
From that result, you'll know what the issue is - simply adjust the values. That could be adding or subtracting the top-left window position from the mouse XY, for example.