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

标签 opengl

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

最佳答案

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

关于opengl - 如何从像素中获取颜色? OpenGL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8000921/

相关文章:

performance - 如何优化基于 QGraphicsView 的应用程序的性能?

c++ - 绑定(bind)在 OpenGL 中创建背景

c++ - 为什么我收到错误 LNK1104 : cannot open file 'glew32.lib'

c++ - GCC、字符串化和内联 GLSL?

c++ - 对 openGL 版本感到困惑

c++ - 在 OpenGL 中高效绘制大量点云点?

opengl - 应该为每个纹理单元应用 glEnable(GL_TEXTURE_2D)

c - OpenGL 1.4。当我使用鼠标拾取更改旋转中心时,场景会发生变化

c++ - 在屏幕上绘制点

python - 如何在opengl中制作坐标Y的动画