Reputation: 1
I want to make makePixel(...) function in C++ that can place a pixel in specified x and y. But I have no idea why my approach is not working.
#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();
}
In "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;
}
}
Upvotes: 0
Views: 6641
Reputation: 52084
int WIDTH, HEIGHT = 400;
only assigns 400
to HEIGHT
, not HEIGHT
and WIDTH
like your code is assuming. WIDTH
is left uninitialized (or possibly default-constructed, I'm not sure what the C++ spec demands in this case off the top of my head; I'm getting 0
on my system at run-time).
All together:
#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;
Upvotes: 2