这是我的一些代码!~~为什么我改变了正方形的颜色,但它不起作用?谁能帮我?我怎样才能用鼠标移动这三个方块?
#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;
为什么我在这里改变颜色,但它不起作用?谁能帮我?我怎样才能用鼠标移动这三个方块?
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;
}