Reputation: 25
This is some of my code!~~ Why I changed the colour of square, but it does not work? Can any one help me? And How can I use the mouse to move these three square?
#include <GL/glut.h>
#include <stdio.h>
const GLint pickSize = 32;
int winWidth = 400, winHeight = 300;
void Initial(void)
{
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
}
void DrawRect(GLenum mode)
{
if(mode == GL_SELECT) glPushName(1);
glColor3f(1.0f,0.0f,0.0f);
glRectf(60.0f,50.0f,150.0f,150.0f);
if(mode == GL_SELECT) glPushName(2);
glColor3f(0.0f,1.0f,0.0f);
glRectf(230.0f,50.0f,330.0f,150.0f);
if(mode == GL_SELECT) glPushName(3);
glColor3f(0.0f,0.0f,1.0f);
glRectf(140.0f,140.0f,240.0f,240.0f);
}
void ProcessPicks(GLint nPicks, GLuint pickBuffer[])
{
GLint i;
GLuint name, *ptr;
ptr=pickBuffer;
for(i=0;i<nPicks; i++){
name=*ptr;
ptr+=3;
ptr+=name-1;
Why I change the colour here, but it does not work? Can any one help me? And How can I use the mouse to move these three square?
if(*ptr==1) {glColor3f(1.0f,1.0f,1.0f);}
//printf("The color is red\n");
if(*ptr==2) printf("The colour is green.\n");
if(*ptr==3) printf("The colour is blue.\n");
ptr++;
}
printf("\n\n");
}
void ChangeSize(int w, int h)
{
winWidth = w;
winHeight = h;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,winWidth,0.0,winHeight);
}
void Display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
DrawRect(GL_RENDER);
glFlush();
}
void MousePlot(GLint button, GLint action, GLint xMouse, GLint yMouse)
{
GLuint pickBuffer[pickSize];
GLint nPicks, vp[4];
if(button == GLUT_LEFT_BUTTON && action == GLUT_DOWN){
glSelectBuffer(pickSize,pickBuffer);
glRenderMode(GL_SELECT);
glInitNames();
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glGetIntegerv(GL_VIEWPORT, vp);
gluPickMatrix(GLdouble(xMouse), GLdouble(vp[3]-yMouse),10.0,10.0,vp);
gluOrtho2D(0.0,winWidth,0.0,winHeight);
DrawRect(GL_SELECT);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glFlush();
nPicks = glRenderMode(GL_RENDER);
ProcessPicks(nPicks, pickBuffer);
glutPostRedisplay();
}
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400,300);
glutInitWindowPosition(100,100);
glutCreateWindow("Picking");
glutDisplayFunc(Display);
glutReshapeFunc(ChangeSize);
glutMouseFunc(MousePlot);
Initial();
glutMainLoop();
return 0;
}
Upvotes: 0
Views: 3592
Reputation: 162307
OpenGL is a drawing API, not a scene graph. If you want to change something in the visible scene, you have to redraw it. Executing some random OpenGL state commands in a event handler will just set OpenGL state, but not change anything visible.
In the event handler, change a variable's value, then issue a redraw (with GLUT redrawing is issued by calling glutPostRedisplay()
). In the drawing code, use the variable's value to control the drawing process, like setting the color used for the next thing drawn.
Here's code of a very simple GLUT based program that cycles a quads color through white, red, green, blue, white, … when clicking into the window
/* Language: ANSI-C */
#include <GL/glut.h>
#define N_COLORS 4
static GLfloat colors[4][3] = {
{1,1,1},
{1,0,0},
{0,1,0},
{0,0,1}
};
static int colorindex = 0;
static GLfloat const quad[][2] = {
-1, -1,
1, -1,
1, 1,
-1, 1
};
static void cycle_color(void)
{
colorindex = (colorindex+1) % N_COLORS;
}
static void redraw(float width, float height)
{
float const aspect = width/height;
glClearColor(0,0,0,1);
glClear(GL_COLOR_BUFFER_BIT);
glViewport(0,0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.5*aspect, 1.5*aspect, -1.5, 1.5, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, quad);
glColor3fv(colors[colorindex]);
glDrawArrays(GL_QUADS, 0, 4);
}
static void onMouseClick(int btn, int state, int x, int y)
{
if( GLUT_DOWN == state ) {
cycle_color();
}
glutPostRedisplay();
}
static void onDisplay(void)
{
int const win_width = glutGet(GLUT_WINDOW_WIDTH);
int const win_height = glutGet(GLUT_WINDOW_HEIGHT);
redraw(win_width, win_height);
glutSwapBuffers();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutCreateWindow("click to change color");
glutDisplayFunc(onDisplay);
glutMouseFunc(onMouseClick);
glutMainLoop();
return 0;
}
Upvotes: 3