如何从像素中获取颜色? OpenGL的

时间:2011-11-03 19:45:49

标签: opengl

我想从游戏中的像素中获取颜色并将此颜色保存在变量中。如何在opengl中做到这一点?

3 个答案:

答案 0 :(得分:10)

glReadPixels()

#include <GL/glut.h>
#include <iostream>

using namespace std;

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-10, 10, -10, 10, -1, 1);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glPushMatrix();
        glScalef(5,5,5);
        glBegin(GL_TRIANGLES);
            glColor3ub(255,0,0);
            glVertex2f(-1,-1);
            glColor3ub(0,255,0);
            glVertex2f(1,-1);
            glColor3ub(0,0,255);
            glVertex2f(1,1);
        glEnd();
    glPopMatrix();

    glutSwapBuffers();
}

void motion(int x, int y)
{
    y = glutGet( GLUT_WINDOW_HEIGHT ) - y;

    unsigned char pixel[4];
    glReadPixels(x, y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, pixel);
    cout << "R: " << (int)pixel[0] << endl;
    cout << "G: " << (int)pixel[1] << endl;
    cout << "B: " << (int)pixel[2] << endl;
    cout << endl;
}

int main(int argc, char **argv)
{
    glutInitWindowSize(640,480);
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
    glutCreateWindow("glReadPixels()");

    glutDisplayFunc(display);
    glutPassiveMotionFunc(motion);
    glutMainLoop();
    return 0;
}

答案 1 :(得分:6)

struct{ GLubyte red, green, blue; } pixel;
glReadPixels(x, y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, &pixel);

答案 2 :(得分:1)

#include <math.h>
#include <gl/glut.h>

struct Point {
    GLint x;
    GLint y;
};

struct Color {
    GLfloat r;
    GLfloat g;
    GLfloat b;
};

void init() {
    glClearColor(1.0, 1.0, 1.0, 0.0);
    glColor3f(0.0, 0.0, 0.0);
    glPointSize(1.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0, 640, 0, 480);
}
  //this function is used for getting color of pixel
Color getPixelColor(GLint x, GLint y) {
    Color color;
    glReadPixels(x, y, 1, 1, GL_RGB, GL_FLOAT, &color);
    return color;
}

void setPixelColor(GLint x, GLint y, Color color) {
    glColor3f(color.r, color.g, color.b);
    glBegin(GL_POINTS);
    glVertex2i(x, y);
    glEnd();
    glFlush();
}

void floodFill(GLint x, GLint y, Color oldColor, Color newColor) {
    Color color;
    color = getPixelColor(x, y);

    if(color.r == oldColor.r && color.g == oldColor.g && color.b == oldColor.b)
    {
        setPixelColor(x, y, newColor);
        floodFill(x+1, y, oldColor, newColor);
        floodFill(x, y+1, oldColor, newColor);
        floodFill(x-1, y, oldColor, newColor);
        floodFill(x, y-1, oldColor, newColor);
    }
    return;
}

void onMouseClick(int button, int state, int x, int y)
{
    Color newColor = {1.0f, 0.0f, 0.0f};
    Color oldColor = {1.0f, 1.0f, 1.0f};

    floodFill(320, 240, oldColor, newColor);
}

void draw_circle(Point pC, GLfloat radius) {
    GLfloat step = 1/radius;
    GLfloat x, y;

    for(GLfloat theta = 0; theta <= 360; theta += step) {
        x = pC.x + (radius * cos(theta));
        y = pC.y + (radius * sin(theta));
        glVertex2i(x, y);
    }
}

void display(void) {
    Point pt = {320, 240};
    GLfloat radius = 50;

    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_POINTS);
        draw_circle(pt, radius);
    glEnd();
    glFlush();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowSize(640, 480);
    glutInitWindowPosition(200, 200);
    glutCreateWindow("Open GL");
    init();
    glutDisplayFunc(display);
    glutMouseFunc(onMouseClick);
    glutMainLoop();
    return 0;
}
相关问题