2017-02-08 6 views
2

저는 창에 픽셀을 1 씩 그리는 함수를 가지고 있습니다. 그러나 내가 알고 싶은 것은 한 픽셀을 그려 넣는 방법입니다. 빨간색이 아닌 다른 색. 미리 감사드립니다. 나는 glSetColor, glColor3f 등과 같은 것들을 시도했다. 픽셀을 다른 색으로 표시하려고 시도했지만 지금까지는 아무 것도 작동하지 않는 것처럼 보였다. glDrawPixels를 호출 할 때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

당신은 형식으로 GL_RGB을 지정합니다.

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

을하지만 당신은 단지 다음 줄에 레드 픽셀 강도 값을 설정 :

그런 다음 당신은 라인에 버퍼의 픽셀의 정확한 위치를 계산한다. 이처럼 버퍼에 다른 색상 값에 액세스 할 수 있습니다 : 나는 "* 3"부분없이 pixLocation을 설정할 때 왜

PixelBuffer[pixelLocation + 0] = 1; // Red pixel intensity 
PixelBuffer[pixelLocation + 1] = 1; // Green pixel intensity 
PixelBuffer[pixelLocation + 2] = 1; // Blue pixel intensity 
+0

아, 좀 이상한 색상을 제공되었는지? 또한 정말 빠른 응답에 감사드립니다. – Eldandor