在OpenGL中为球体设置像素颜色

时间:2022-02-07 20:03:42

I have used QUAD_STRIP to display squares with different colours and a mouse function to capture the color. The captured color should be set to a spehre. I can read color values but I'm unable to set the read color values to the sphere. Sphere is not displayed when given within mouse function. But shows up when given in display() function.

我使用了QUAD_STRIP来显示不同颜色的方块和鼠标功能来捕捉颜色。捕获的颜色应该设置为spehre。我可以读取颜色值,但不能将读取的颜色值设置为球体。在鼠标功能中给定时,不显示Sphere。但在display()函数中给出时显示。

        #include <stdlib.h>
    #include <GL/gl.h>
    #include <GL/glu.h>
    #include <GL/glut.h>
    #include<windows.h>    
    float height=480,width=500;        
    GLUquadricObj *sphere = NULL;
    GLubyte *img;
    static void Init() {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
        glShadeModel(GL_FLAT);
                    sphere = gluNewQuadric();
        gluQuadricDrawStyle(sphere, GLU_FILL);
        gluQuadricTexture(sphere, GL_TRUE);
        gluQuadricNormals(sphere, GLU_SMOOTH);

        glClearColor(1,1,1,0);

    }

    static void display() {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   //glColor3f(0.0f, 0.0f, 0.0f); // sets color to black.
          glBegin(GL_QUAD_STRIP);
    //1 q
            glColor3f(.5,0.5,1);
            glVertex2f(-1, 0.5 );
            glVertex2f(-1, 0.75 );
            glVertex2f(-0.75, 0.5 );
            glVertex2f(-0.75, 0.75 );
            // 2q
            glColor3f(.25,.5,0);

            glVertex2f(-0.5, 0.5 );
            glVertex2f(-0.5, 0.75 );

            //3q
            glColor3f(0.8,0.8,0.4);

            glVertex2f(-0.25, 0.5 );
            glVertex2f(-0.25, 0.75 );
            // 4th QUAD
            glColor3f(0.7,0.3,0.6);
            glVertex2f(0, 0.5);
            glVertex2f(0, 0.75 );

            // 5th QUAD
            glColor3f(0, 0.7,0.3);

            glVertex2f( 0.25, 0.5);
            glVertex2f(0.25,  0.75 );

    glEnd();

   glBegin(GL_QUAD_STRIP);
    //1 q
            glColor3f(.5,0.5,.5);
            glVertex2f(-1, 0.25 );
            glVertex2f(-1, 0.5 );
            glVertex2f(-0.75, 0.25 );
            glVertex2f(-0.75, 0.5 );
            // 2q
            glColor3f(.5,0.1,0.1);

            glVertex2f(-0.5, 0.25 );
            glVertex2f(-0.5, 0.5 );

            //3q
            glColor3f(0.1,0.1,0.4);

            glVertex2f(-0.25, 0.25 );
            glVertex2f(-0.25, 0.5 );
            // 4th QUAD
            glColor3f(1,1,0.5);
            glVertex2f(0, 0.25);
            glVertex2f(0, 0.5 );

            // 5th QUAD
            glColor3f(0.76, 0.3,0);

            glVertex2f( 0.25, 0.25);
            glVertex2f(0.25,  0.5 );

    glEnd();

    glBegin(GL_QUAD_STRIP);
    //1 q
            glColor3f(.5,0,.5);
            glVertex2f(-1, 0 );
            glVertex2f(-1, 0.25 );
            glVertex2f(-0.75, 0 );
            glVertex2f(-0.75, 0.25 );
            // 2q
            glColor3f(.5,.3,0.3);

            glVertex2f(-0.5, 0 );
            glVertex2f(-0.5, 0.25 );

            //3q
            glColor3f(0.2,0.3,0);

            glVertex2f(-0.25, 0 );
            glVertex2f(-0.25, 0.25 );
            // 4th QUAD
            glColor3f(0,0,0);
            glVertex2f(0, 0);
            glVertex2f(0, 0.25 );

            // 5th QUAD
            glColor3f(1.0, 0,.5);

            glVertex2f( 0.25, 0);
            glVertex2f(0.25,  0.25 );

    glEnd();

    glBegin(GL_QUAD_STRIP);
            glColor3f(1,0.5,0);
            glVertex2f(-1, -0.25 );
            glVertex2f(-1, 0.0 );
            glVertex2f(-0.75, -0.25 );
            glVertex2f(-0.75, 0.0 );

            glColor3f(1.0,1.0,0.0);

            glVertex2f(-0.5, -0.25 );
            glVertex2f(-0.5, 0.0 );


            glColor3f(0.0,0.0,1.0);

            glVertex2f(-0.25, -0.25 );
            glVertex2f(-0.25, 0.0 );
            // 2nd QUAD
            glColor3f(1.0, 0.0, 0.0);
            glVertex2f(-0.25, -0.25 );
            glVertex2f(-0.25, 0.0 );

            glVertex2f( 0, -0.25 );
            glVertex2f(0,  0.0 );

            // 3rd QUAD
            glColor3f(0.0, 1.0, 0.0);

            glVertex2f( 0.25, -0.25 );
            glVertex2f(0.25,  0.0 );

    glEnd();
glutSwapBuffers();

    }
    void moveBall(int key, int x, int y) {
    }
        void mouseMove(int x,int y) {
            y = glutGet( GLUT_WINDOW_HEIGHT ) - y;
            GLubyte pixel[3];
            float r,g,b;
            glReadPixels(x, y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, pixel);
            r = pixel[0]/255.0;
            g = pixel[1]/255.0;
            b = pixel[2]/255.0;
            printf("red %f",r);
            printf("green %f",g);
            printf("blue %f",b);
            glColor3f(r,g,b);
            glTranslatef(0.75,0.25,0);
            gluSphere(sphere,0.1,20,20);
        }
    int main(int argc, char** argv) {

        glutInit(&argc,argv);
        glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
        glutInitWindowPosition( 10, 10);
        glutInitWindowSize(height,width);
        glutCreateWindow( "ColorBlocks");
        Init();
        glutDisplayFunc(display);
        glutSpecialFunc(moveBall);
        glutMotionFunc(mouseMove);
        glutMainLoop();

    }

1 个解决方案

#1


0  

What you should do is put the drawing of the sphere in the display and position it using the variables you get in mouseMove and have set to a global:

你要做的是把球体的绘图放在显示器中,然后用你在mouseMove中得到的变量设置为全局变量:

float sphereR,sphereG,sphereB;
void display(){
    //everything before the glutSwapBuffers(); call...

    glPushMatrix()
        glColor3f(r,g,b);
        glTranslatef(0.75,0.25,0);
        gluSphere(sphere,0.1,20,20);
    glPopMatrix()
    glutSwapBuffers();
}

void mouseMove(int x,int y) {
    y = glutGet( GLUT_WINDOW_HEIGHT ) - y;
    GLubyte pixel[3];

    glReadPixels(x, y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, pixel);
    sphereR = pixel[0]/255.0;
    sphereG = pixel[1]/255.0;
    sphereB = pixel[2]/255.0;
    printf("red %f",r);
    printf("green %f",g);
    printf("blue %f",b);

}

#1


0  

What you should do is put the drawing of the sphere in the display and position it using the variables you get in mouseMove and have set to a global:

你要做的是把球体的绘图放在显示器中,然后用你在mouseMove中得到的变量设置为全局变量:

float sphereR,sphereG,sphereB;
void display(){
    //everything before the glutSwapBuffers(); call...

    glPushMatrix()
        glColor3f(r,g,b);
        glTranslatef(0.75,0.25,0);
        gluSphere(sphere,0.1,20,20);
    glPopMatrix()
    glutSwapBuffers();
}

void mouseMove(int x,int y) {
    y = glutGet( GLUT_WINDOW_HEIGHT ) - y;
    GLubyte pixel[3];

    glReadPixels(x, y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, pixel);
    sphereR = pixel[0]/255.0;
    sphereG = pixel[1]/255.0;
    sphereB = pixel[2]/255.0;
    printf("red %f",r);
    printf("green %f",g);
    printf("blue %f",b);

}