C ++ OpenGL Draw Pixel

问题描述 投票:2回答:1

我正在学习如何通过在线课程使用OpenGL,并且练习问题要求创建一个绘制像素的函数:

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

我知道我可以做这样的事情来画一个:

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

但是考虑到问题的背景,它不是一个有效的解决方案。

该问题还要求通过在循环内调用SetPixel函数来随机在屏幕上放置像素。我理解如何实现它,但它是绘制我不理解的实际像素,特别是在给定程序的上下文中。

该程序(使用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)
{


}

有人可以帮我解决这个问题吗?我如何优雅地实现SetPixel功能?

c++ opengl glut
1个回答
1
投票

根据您对像素格式的配置,(x, y)处像素的红色分量位于3 * (y * width + x)索引处,绿色/蓝色处于+1/2位:

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

请注意,不清楚SetPixel是否应该写两个缓冲区,或者应该有一个标志指示哪一个,所以我只是写了pFrame作为占位符。

© www.soinside.com 2019 - 2024. All rights reserved.