gpt4 book ai didi

c++ - 使用 glDrawPixels() 在 OpenGL 上绘制像素

转载 作者:行者123 更新时间:2023-11-28 04:31:47 26 4
gpt4 key购买 nike

我想在 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,而不是 HEIGHT WIDTH 就像你的代码假设的那样。 WIDTH 未初始化(或者可能是默认构造的,我不确定在这种情况下 C++ 规范要求什么;我得到 0在我的系统上运行时)。

一起:

screenshot of white pixel

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

关于c++ - 使用 glDrawPixels() 在 OpenGL 上绘制像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52694005/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com