2

我有一个在窗口上逐一绘制像素的函数,但我想知道如何让像素以红色以外的其他颜色绘制。提前致谢。我尝试了一些类似 glSetColor、glColor3f 等的东西。只是为了尝试让像素以不同的颜色显示,但到目前为止似乎没有任何效果。

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

using namespace std;

float *PixelBuffer;
void setPixel(int, int);

void display();

int size = 400 * 400 * 3;

int main(int argc, char *argv[])
{

    PixelBuffer = new float[400 * 400 * 3];

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

    glutInitWindowSize(400, 400);

    glutInitWindowPosition(100, 100);
    glColor3f(0, 1.0, 0);

    int firstWindow = glutCreateWindow("First Color");



    glClearColor(0, 0, 0, 0); //clears the buffer of OpenGL

  for(int i = 0; i < 20; i++)
  {
    setPixel(i, 10);
  }



    glutDisplayFunc(display);

    glutMainLoop();


    return 0;
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity();

    glDrawPixels(400, 400, GL_RGB, GL_FLOAT, PixelBuffer);
    glFlush();
}

void setPixel(int x, int y)
{
  int pixelLocation;
  int width = 400;
  pixLocation = (y * width * 3) + (x * 3);
  PixelBuffer[pixelLocation] = 1;
};
4

1 回答 1

2

您在调用时指定GL_RGB为格式glDrawPixels

然后你计算像素在你的缓冲区中的正确位置:

pixLocation = (y * width * 3) + (x * 3);

但是,您只需在下一行设置红色像素强度值。您可以像这样访问缓冲区中的其他颜色值:

PixelBuffer[pixelLocation + 0] = 1; // Red pixel intensity
PixelBuffer[pixelLocation + 1] = 1; // Green pixel intensity
PixelBuffer[pixelLocation + 2] = 1; // Blue pixel intensity
于 2017-02-08T12:43:30.300 回答