如何从像素获得颜色?的OpenGL [英] How to get color from the pixel? OpenGL

查看:83
本文介绍了如何从像素获得颜色?的OpenGL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to get color from the pixel in my game and save this color in variable. How to do it in 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆