PDA

View Full Version : why wont it draw?



cubbie
10-03-2002, 01:00 PM
i hate to do this, but i'm going to post some code here, if you all dont mind ... i'm sure there are some errors in my logic in regards to the openGL functionality, so if someone could point some of that out, i'd really appreciate it.
the actual problem is that nothing is drawn on the screen when i click and draw the mouse (as of now, only the unfilled rectangle is meant to be working ...)
_____________


/* The GLUT include files*/
#include <windows.h>
#include <GL/glut.h>
#include <GL/glaux.h>
#include <GL/glu.h>
#include <GL/gl.h>
#include <vector>

using namespace std;

/* Constants */
#define MENU_CLEAR 1
#define RED 2
#define ORANGE 3
#define GREEN 4
#define BLUE 5
#define YELLOW 6
#define RECTANGLE 7
#define POLYGON 8
#define ELLIPSE 9
#define FILLED 10
#define UNFILLED 11
#define NOTHING 13
#define DEL 14
#define UNDEL 15
#define REC_FILLED 16
#define REC_UNFILLED 17
#define POLY_FILLED 18
#define POLY_UNFILLED 19
#define ELL_FILLED 20
#define ELL_UNFILLED 21
#define CYAN 22
#define MENU_EXIT 99
#define Max(A,B) ((A) >= (B) ? (A) : (B))

/* Global variables */
struct point{
int x;
int y;
};

struct shapes{
vector<point> points;
int shape;
int style;
int color;
};

vector<shapes> obj;
int theObject = RECTANGLE;
int theStyle = UNFILLED;
int theColor = RED;

void drawRectangle( vector<point> pt, int style, int color )
{
theColor = color;
vector<point>::iterator pos;
if ( style == UNFILLED ){
glBegin( GL_LINE_LOOP );
for ( pos = pt.begin(); pos != pt.end(); pos++ )
glVertex2f( pos->x, pos->y );
glEnd();
}

}

void drawPolygon( vector<point> pt, int style, int color )
{

}

void drawEllipse( vector<point> pt, int style, int color )
{

}

void drawScene()
{
vector<shapes>::iterator pos;
for ( pos = obj.begin(); pos != obj.end(); pos++ ){
switch( pos->shape ){
case RECTANGLE:
drawRectangle( pos->points, pos->style, pos->color );
break;
case POLYGON:
drawRectangle( pos->points, pos->style, pos->color );
break;
case ELLIPSE:
drawRectangle( pos->points, pos->style, pos->color );
break;
}
}
}

void reshape(int w, int h)
{
printf("Entered reshape with: %d %d; glutGet: %d %d\n", w, h,
glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT));
glutReshapeWindow(Max(w,200), Max(h,200));
glViewport(0, 0, Max(w,200), Max(h,200));
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,(double)Max(w,200), (double)Max(h,200), 0.0); /* l,r,bot,top */
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glutPostRedisplay();
}

void display ()
{
glClear(GL_COLOR_BUFFER_BIT);
drawScene();
glutSwapBuffers(); //added for double buffering
}

void handleMenu(int value)
{
switch(value) {
case MENU_CLEAR:
theObject = NOTHING;
break;
case DEL:
break;
case UNDEL:
break;
case MENU_EXIT:
exit(0);
}

glutPostRedisplay();
}

void shapeMenu(int value)
{
switch( value ) {
case REC_FILLED:
theObject = RECTANGLE;
theStyle = FILLED;
break;
case REC_UNFILLED:
theObject = RECTANGLE;
theStyle = UNFILLED;
break;
case POLY_FILLED:
theObject = POLYGON;
theStyle = FILLED;
break;
case POLY_UNFILLED:
theObject = POLYGON;
theStyle = UNFILLED;
break;
case ELL_FILLED:
theObject = ELLIPSE;
theStyle = FILLED;
break;
case ELL_UNFILLED:
theObject = ELLIPSE;
theStyle = UNFILLED;
break;
}
}

void colorMenu( int value ){
GLfloat r, g, b;

switch( value ){
case RED:
r = 1.0; g = 0.0; b = 0.0;
break;
case ORANGE:
r = 1.0; g = 0.5; b = 0.0;
break;
case GREEN:
r = 0.0; g = 1.0; b = 0.0;
break;
case BLUE:
r = 0.0; g = 0.0; b = 1.0;
break;
case YELLOW:
r = 1.0; g = 1.0; b = 0.0;
break;
case CYAN:
r = 0.0; g = 1.0; b = 1.0;
break;
}

glColor3f(r,g,b);
}

void mouseclick(int button, int state, int x, int y)
{
if ( button == GLUT_LEFT_BUTTON ){
if ( theObject == RECTANGLE ){
point start, end;
shapes rec;
rec.color = theColor;
rec.shape = theObject;
rec.style = theStyle;
if ( state == GLUT_DOWN ){
start.x = x;
end.x = x;
start.y = y;
end.y = y;
rec.points.push_back( start );
rec.points.push_back( end );
}else{
end.x = x;
end.y = y;
rec.points.push_back( end );
}
obj.push_back( rec );
}
}

glutPostRedisplay();
}


void userFeedback(int x, int y)
{
//endX = x; endY = y;
glutPostRedisplay();
}

void init()
{
int color_menu, shape_menu;

glClearColor(1.0,1.0,1.0,0.0);

glColor3f(1.0, 0.0, 0.0);

shape_menu = glutCreateMenu(shapeMenu);
glutAddMenuEntry("Filled Rectangle", REC_FILLED);
glutAddMenuEntry("Unfilled Rectangle", REC_UNFILLED);
glutAddMenuEntry("Filled Polygon", POLY_FILLED);
glutAddMenuEntry("Unfilled Polygon", POLY_UNFILLED);
glutAddMenuEntry("Filled Ellipse", ELL_FILLED);
glutAddMenuEntry("Unfilled Ellipse", ELL_UNFILLED);

color_menu = glutCreateMenu(colorMenu);
glutAddMenuEntry( "Red", RED );
glutAddMenuEntry( "Orange", ORANGE );
glutAddMenuEntry( "Blue", BLUE );
glutAddMenuEntry( "Green", GREEN );
glutAddMenuEntry( "Yellow", YELLOW );
glutAddMenuEntry( "Cyan", CYAN );

glutCreateMenu(handleMenu);
glutAddSubMenu("Draw",shape_menu);
glutAddSubMenu("Set Color",color_menu);
glutAddMenuEntry("Delete", DEL);
glutAddMenuEntry("Undelete", UNDEL);
glutAddMenuEntry("Clear Display", MENU_CLEAR);
glutAddMenuEntry("Exit", MENU_EXIT);
glutAttachMenu(GLUT_RIGHT_BUTTON);
}

int main (int argc, char **argv)
{
/* GLUT initialization: */
glutInit(&argc, argv);


glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(500, 500);
glutInitWindowPosition(200, 200);
glutCreateWindow("CSE 418 -- Project 1");

/* Initialize our window: */
init();

/* Post Event Handlers */
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMouseFunc(mouseclick);
glutMotionFunc(userFeedback);

/* Enter the main loop: */
glutMainLoop();
return 0;
}

Bob
10-03-2002, 02:00 PM
You don't set the projection matrix, which leaves you with the default matrix; an othrographic matrix which ranges from (-1, 1), (-1,1) and (-1, 1) in width, height and depth respectively.

Since you plug the mouse cooridnates right into the vector, as far as I can see, you will draw everything outside the view volume.

Put something similar to



glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-0.5, static_cast<double>(w) - 0.5, -0.1 static_cast<double>(h) - 0.5, -1, 1)
glMatrixMode(GL_MODELVIEW);

in the reshape function and see what happens.

cubbie
10-03-2002, 02:20 PM
that didnt seem to change anything.
i'm assuming the problem is in the drawRectangle routine ... perhaps something is not being passed properly?

thanks again.

Bob
10-03-2002, 03:30 PM
I think your mouse click callback is wrong. When you press a mouse button, you record the coordinate and put it in both the start and end variable, and then push them both to the vector. The line you draw will now have zero length, since start and end is the same.

When you release the mouse button, you enter mouseclick again, create a new shape, and assign it the mouse position. This new shape only contains one point, and can not form a valid line segment.

If you, in drawRectangle, changes GL_LINE_LOOP to GL_POINTS you should see something. The reason you don't see any lines can be that zero length lines are simply not draw at all.

Rob Fletcher
10-04-2002, 03:15 AM
I guess your idea was to

1. click and establish start for a line
2. move the mouse (still with button down)
3. release button for end point
4. draw line

I'll check if this behaviour will work in GLUT. If not, look into the motion callbacks
as well!

Rob.

(I'll post back an answer later re GLUT later)

cubbie
10-04-2002, 05:02 AM
actually, its meant to draw a rectangle ... i've almost got it but because there are only 2 points pushed into the vector here:

void mouseclick(int button, int state, int x, int y)
{
if ( button == GLUT_LEFT_BUTTON ){
if ( theObject == RECTANGLE ){
point start, mid1, mid2, end;
shapes rec;
rec.color = theColor;
rec.shape = theObject;
rec.style = theStyle;
if ( state == GLUT_DOWN ){
start.x = x;
start.y = y;
rec.points.push_back( start );
obj.push_back( rec );
}else{
end.x = x;
end.y = y;
obj.back().points.push_back( end );
}
}
}

glutPostRedisplay();

when the drawRectangle routine is called, only 2 points are in the vector.

void drawRectangle( vector<point> pt, int style, int color )
{
vector<point>::iterator pos;
if ( style == UNFILLED ){
glBegin( GL_LINE_LOOP );
for ( pos = pt.begin(); pos != pt.end(); pos++ )
glVertex2f( pos->x, pos->y );

glEnd();
}

}

this obviously only draws a line ...
so thats what i'm working on now.

thanks.