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
", 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;
}