我有一个函数,可以在窗口上绘制 1 x 1 像素,但我想知道的是如何让像素以红色以外的其他颜色绘制。提前谢谢。我尝试了一些东西,比如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;
};
在调用 glDrawPixels
时指定GL_RGB
作为格式。
然后计算像素在缓冲区中的正确位置:
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