【问题标题】:How do I change color of pixels when using glDrawPixels, my pixels are always red使用 glDrawPixels 时如何更改像素的颜色,我的像素总是红色
【发布时间】:2017-02-08 12:15:17
【问题描述】:

我有一个在窗口上逐一绘制像素的函数,但我想知道如何让像素以红色以外的其他颜色绘制。提前致谢。我尝试了一些类似 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;
};

【问题讨论】:

    标签: c++ opengl glut gldrawpixels


    【解决方案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
    

    【讨论】:

    • 哦,这就是为什么当我设置没有“ * 3”部分的 pixLocation 时它会给我一些奇怪的颜色?也感谢您的快速响应。
    猜你喜欢
    • 2019-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多