2017-02-08 15 views
2

私は、ウィンドウにピクセルを1ずつ描画する関数を持っています。赤以外の異なる色。前もって感謝します。 glSetColor、glColor3fなどのようなものをいくつか試しましたが、ピクセルをさまざまな色で表示しようとしましたが、これまでに何も動作していなかったようです。glDrawPixelsを使用しているときのピクセルの色を変更するにはどうすればいいですか?ピクセルは常に赤色です

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

答えて

2

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 
+0

ああ、私は「* 3」の部分なしでpixLocationを設定するとき、なぜそれは私にいくつかの奇妙な色を与えることがありますか?また、本当に速い応答に感謝します。 – Eldandor

関連する問題