这是我的一些代码!~~ 为什么我改变了方形的颜色,但它不起作用?谁能帮我?我怎样才能用鼠标移动这三个方块?
#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;
}
答案 0 :(得分:3)
OpenGL是一个绘图API,而不是场景图。如果要在可见场景中更改某些内容,则必须重绘它。在事件处理程序中执行一些随机的OpenGL状态命令只会设置OpenGL状态,但不会更改任何可见的内容。
在事件处理程序中,更改变量的值,然后发出重绘(通过调用glutPostRedisplay()
发出GLUT重绘)。在绘图代码中,使用变量的值来控制绘图过程,例如设置用于下一个绘制事物的颜色。
这是一个非常简单的基于GLUT的程序的代码,当点击进入窗口时,它通过白色,红色,绿色,蓝色,白色......循环四边形颜色
/* 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;
}