Search code examples
c++openglglut

C++ OpenGL Draw Pixel


I'm learning how to use OpenGL by doing an online course and the practice problem asks to create a function to draw pixels:

void SetPixel(int x, int y, char r, char g, char b){}

I know I could do something like this to draw one:

glBegin(GL_POINTS);
   glColor3f(1.0,1.0,1.0);
   glVertex2i(100,100);
glEnd();

However given the context of the problem it's not a valid solution.

The problem also asks to use the SetPixel function by calling it within a loop to randomly place pixels across the screen. I understand how to implement that, but it's the drawing the actual pixel I don't understand, especially within the context of the program given.

The program (using GLUT):

#include <stdlib.h>         //- for exit()
#include <stdio.h>          //- for sprintf()
#include <string.h>         //- for memset()

#ifdef _WIN32
    #include "libs/glut.h"
    #include <windows.h>
    #pragma comment(lib, "winmm.lib")           //- not required but have it in anyway
    #pragma comment(lib, "libs/glut32.lib")
#elif __APPLE__
    #include <GLUT/glut.h>
#elif __unix__      
    #include <GL/glut.h>
#endif
#define FRAME_WIDE  1000
#define FRAME_HIGH  600

//====== Structs & typedefs =========
typedef unsigned char BYTE;
struct POINT2D {int x, y;};

//====== Global Variables ==========
BYTE    pFrameL[FRAME_WIDE * FRAME_HIGH * 3];
BYTE    pFrameR[FRAME_WIDE * FRAME_HIGH * 3];
int     shade = 0;
POINT2D xypos = {0,0};
int     stereo = 0;
int     eyes = 10;

//===== Forward Declarations ========
void ClearScreen();
void DrawFrame();
void Interlace(BYTE* pL, BYTE* pR);
void BuildFrame(BYTE *pFrame, int view);
void OnIdle(void);
void OnDisplay(void);
void reshape(int w, int h);

////////////////////////////////////////////////////////
// Program Entry Poinr
////////////////////////////////////////////////////////

int main(int argc, char** argv)
{
    //-- setup GLUT --
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);    //GLUT_3_2_CORE_PROFILE |
    glutInitWindowSize(FRAME_WIDE, FRAME_HIGH);
    glutInitWindowPosition(100, 100);
    glutCreateWindow(argv[0]);


    //--- set openGL state --
    glClearColor (0.0, 0.0, 0.0, 0.0);
    glShadeModel(GL_FLAT);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

    //-- register call back functions --
    glutIdleFunc(OnIdle);
    glutDisplayFunc(OnDisplay);
    glutReshapeFunc(reshape);

    //-- run the program
    glutMainLoop();
    return 0;
}


////////////////////////////////////////////////////////
// Event Handers
////////////////////////////////////////////////////////

void OnIdle(void)
{
    DrawFrame();
    glutPostRedisplay();
}


void OnDisplay(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glRasterPos2i(0, 0);
    glDrawPixels(FRAME_WIDE, FRAME_HIGH, GL_RGB,GL_UNSIGNED_BYTE, (GLubyte*)pFrameR);
    glutSwapBuffers();
    glFlush();
}

void reshape(int w, int h)
{
    glViewport(0, 0, (GLsizei) w, (GLsizei) h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0, (GLdouble) w, 0.0, (GLdouble) h);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}


////////////////////////////////////////////////////////
// Utility Functions
////////////////////////////////////////////////////////


void ClearScreen()
{
    memset(pFrameL, 0, FRAME_WIDE * FRAME_HIGH * 3);
    memset(pFrameR, 0, FRAME_WIDE * FRAME_HIGH * 3);
}


void Interlace(BYTE* pL, BYTE* pR)
{
    int rowlen = 3 * FRAME_WIDE;
    for (int y = 0; y < FRAME_HIGH; y+=2)
    {
        for (int x = 0; x < rowlen; x++) *pR++ = *pL++;
        pL += rowlen;
        pR += rowlen;
    }
}


void DrawFrame()
{
    ClearScreen();

    if (!stereo) BuildFrame(pFrameR, 0);
    else {
        BuildFrame(pFrameL, -eyes);
        BuildFrame(pFrameR, +eyes);
        Interlace((BYTE*)pFrameL, (BYTE*)pFrameR);
    }
}


////////////////////////////////////////////////////////
// Drawing Function
////////////////////////////////////////////////////////

void BuildFrame(BYTE *pFrame, int view)
{
    // create a loop in here that uses SetPixel function to randomly place 10,000 pixels

}

void SetPixel(int x, int y, char r, char g, char b)
{


}

Can someone please help me work this out? How would I implement the SetPixel function elegantly?


Solution

  • Based on your configurations for the pixel format, the red component of the pixel at (x, y) is at index 3 * (y * width + x), and +1 / 2 for green / blue:

    void SetPixel(int x, int y, char r, char g, char b)
    {
       if (x < 0 || x >= FRAME_WIDE || y < 0 || y >= FRAME_HIGH)
           return; // out of bounds
       int index = 3 * (y * FRAME_WIDE + x);
       pFrame[index] = r;
       pFrame[index+1] = g;
       pFrame[index+2] = b;
    }
    

    Note that it is unclear whether SetPixel should write both buffers or there should be a flag indicating which one, so I just wrote pFrame as a placeholder.