簡體   English   中英

使用glDrawPixels()在OpenGL上繪制像素

[英]Drawing a Pixel on OpenGL using glDrawPixels()

我想在C ++中制作makePixel(...)函數,可以將像素放置在指定的x和y中。 但是我不知道為什么我的方法行不通。

#include "glut.h"

int WIDTH, HEIGHT = 400;
GLubyte* PixelBuffer = new GLubyte[WIDTH * HEIGHT * 3];

void display();


int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);

    glutInitWindowSize(WIDTH, HEIGHT); 
    glutInitWindowPosition(100, 100); 

    int MainWindow = glutCreateWindow("Hello Graphics!!"); 
    glClearColor(0.5, 0.5, 0.5, 0);

    makePixel(200,200,0,0,0,PixelBuffer);

    glutDisplayFunc(display); 
    glutMainLoop();
    return 0;
}



void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glDrawPixels(WIDTH, HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, PixelBuffer);
    glFlush(); 
}

在“ glut.h”中

void makePixel(int x, int y, int r, int g, int b, GLubyte* pixels)
{
    if (0 <= x && x < window.width && 0 <= y && y < window.height) {
        int position = (x + y * window.width) * 3;
        pixels[position] = r;
        pixels[position + 1] = g;
        pixels[position + 2] = b;
    }
}

int WIDTH, HEIGHT = 400; 僅將400分配給HEIGHT ,而不像您的代碼所假設的那樣分配HEIGHTWIDTH WIDTH尚未初始化(或者可能是默認構造的,在這種情況下,我不確定C ++規范要求什么;在運行時,我的系統上獲得0 )。

全部一起:

白色像素的屏幕截圖

#include <GL/glut.h>

int WIDTH = 400;
int HEIGHT = 400;
GLubyte* PixelBuffer = new GLubyte[WIDTH * HEIGHT * 3];

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glDrawPixels(WIDTH, HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, PixelBuffer);
    glutSwapBuffers(); 
}

void makePixel(int x, int y, int r, int g, int b, GLubyte* pixels, int width, int height)
{
    if (0 <= x && x < width && 0 <= y && y < height) {
        int position = (x + y * width) * 3;
        pixels[position] = r;
        pixels[position + 1] = g;
        pixels[position + 2] = b;
    }
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);

    glutInitWindowSize(WIDTH, HEIGHT); 
    glutInitWindowPosition(100, 100); 

    int MainWindow = glutCreateWindow("Hello Graphics!!"); 
    glClearColor(0.0, 0.0, 0.0, 0);

    makePixel(200,200,255,255,255,PixelBuffer, WIDTH, HEIGHT);
    glutDisplayFunc(display); 
    glutMainLoop();
    return 0;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM