0

これは私のコードの一部です!~~なぜ正方形の色を変更したのに、機能しないのですか?誰か助けてもらえますか?そして、どうすればこれらの3つの正方形を動かすためにマウスを使うことができますか?

#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; 

ここで色を変更するのに、機能しないのはなぜですか?誰か助けてもらえますか?そして、どうすればこれらの3つの正方形を動かすためにマウスを使うことができますか?

    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;
}
4

1 に答える 1

3

OpenGLは描画APIであり、シーングラフではありません。表示されているシーンで何かを変更したい場合は、それを再描画する必要があります。イベントハンドラーでいくつかのランダムなOpenGL状態コマンドを実行すると、OpenGL状態が設定されるだけで、表示されているものは何も変更されません。

イベントハンドラーで、変数の値を変更してから、再描画を発行します(GLUTの再描画はを呼び出すことによって発行されglutPostRedisplay()ます)。描画コードでは、変数の値を使用して、次に描画されるものに使用される色を設定するなど、描画プロセスを制御します。

アップデート

これは、ウィンドウをクリックすると、クワッドの色を白、赤、緑、青、白などに循環させる非常に単純な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;
}
于 2013-03-01T18:29:34.130 に答える