Help - we're beginners! :)
We created a program where the user can decorate a Christmas tree. We
would like to limit the decorations to the tree only, not the
background. We are using glReadPixels to evaluate the color and
determine whether or not to draw the deocration. Unfortunately, we
can't get an accurate reading on the color. Here is our code if
anyone has any ideas. (Note: we're not using multiple buffers.)
Thanks!
********************************************************************************
/*This program creates a tree which the user can decorate
by clicking the mouse. */
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
void mouse(int, int, int, int);
void display(void);
void drawSquare(int, int);
void myReshape(GLsizei, GLsizei);
void myinit(void);
/* globals */
GLsizei wh = 500, ww = 500; /* initial window size */
GLfloat size = 3.0; /* half side length of square */
int draw_mode = 0; /* drawing mode */
GLfloat r = 1.0, g = 1.0, b = 1.0; /* drawing color */
int fill = 0; /* fill flag */
/*create tree decoration*/
void drawSquare(int x, int y)
{
/*convert from real world coordinates to gl*/
y=wh-y;
/*choose random color for tree decorations*/
glColor3ub( (char) rand()%256, (char) rand()%256, (char)
rand()%256);
/*create small polygons for decorations*/
glBegin(GL_POLYGON);
glVertex2f(x+size, y+size);
glVertex2f(x-size, y+size);
glVertex2f(x-size, y-size);
glVertex2f(x+size, y-size);
glEnd();
}
/*keeps tree size the same proportion when the window is resized*/
void myReshape(GLsizei w, GLsizei h)
{
/* adjust clipping box */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, (GLdouble)w, 0.0, (GLdouble)h, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
/* adjust viewport and clear */
glViewport(0,0,w,h);
glClearColor (0.8, 0.8, 0.8, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
display();
glFlush();
/* set global size for use by drawing routine */
ww = w;
wh = h;
}
void myinit(void)
{
glViewport(0,0,ww,wh);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, (GLdouble) ww , 0.0, (GLdouble) wh , -1.0, 1.0);
/*set background color to light blue*/
glClearColor (0.6, 0.9, 1.0, 1.0);
}
/*draw tree decorations when left mouse button is clicked*/
void mouse(int btn, int state, int x, int y)
{
/*do only if pixels are green*/
int color[3];
if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
{
/*glReadBuffer(GL_FRONT_LEFT);*/
glReadPixels(x,y,1,1,GL_RGB,GL_INT, color);
/*play with buffer*/
printf("%d\n",color[1]);
/*if(color[1] == 1372705233) */
{
drawSquare(x,y);
glFlush();
}
}
}
void display(void)
{
/*glPushAttrib(GL_ALL_ATTRIB_BITS);*/
glClear(GL_COLOR_BUFFER_BIT);
/*draw green triangle for tree, keep in proportion to window*/
glColor3f (0.0, .64, 0.0);
glBegin(GL_TRIANGLES);
glVertex2i(ww-50,wh-480);
glVertex2i(ww-450,wh-480);
glVertex2i(ww/2,wh-ww/10+ww/40);
glEnd();
/*glPointSize(30.0);*/
/*draw tree trunk*/
glColor3f (1.28, 0.64, 0.0);
glBegin(GL_POLYGON);
glVertex2i(260, 0);
glVertex2i(245, 0);
glVertex2i(245, 20);
glVertex2i(260, 20);
glEnd();
glFlush();
/*glPopAttrib();*/
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("Decorate Kim's Tree");
myinit ();
glutDisplayFunc(display);
glutMouseFunc (mouse) ;
glutMainLoop();
return 0;
}
|