我在opengl中使用FreeImage 来保存绘制后的图形,但保存后却是一张黑色图片,求解 2C

#include
#define GLUT_DISABLE_ATEXIT_HACK
#define MAX_CHAR 128

#include
#include
#include
#include
#include
#include "FreeImage.h"
#if def_DEBUG
#pragma comment(lib, "FreeImaged.lib")
#else
#pragma comment(lib, "FreeImage.lib")
#endif
#pragma comment(lib,"glut32.lib")

using namespace std;
string num2string(float i) {
stringstream ss;
ss << i;
return ss.str();
}

void drawString(string strn) {
static int isFirstCall = 1;
static GLuint lists;
const char *str = strn.c_str();
if (isFirstCall) { // 如果是第一次调用,执行初始化
// 为每一个ASCII字符产生一个显示列表
isFirstCall = 0;

    // 申请MAX_CHAR个连续的显示列表编号
    lists = glGenLists(MAX_CHAR);

    // 把每个字符的绘制命令都装到对应的显示列表中
    wglUseFontBitmaps(wglGetCurrentDC(), 0, MAX_CHAR, lists);
}
// 调用每个字符对应的显示列表,绘制每个字符
for (; *str != '\0'; ++str)
    glCallList(lists + *str);

}

void grab()
{
int WIDTH=800, HEIGHT=500;
unsigned char*mpixels = new unsigned char[WIDTH*HEIGHT*3];
glReadBuffer(GL_FRONT);
glReadPixels(0, 0, WIDTH, HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, mpixels);
glReadBuffer(GL_BACK);
for (int i=0; i<(int)WIDTH*HEIGHT*3; i+=3)
{
mpixels[i]^= mpixels[i+2]^= mpixels[i]^= mpixels[i+2];
}
FIBITMAP *bitmap= FreeImage_Allocate(WIDTH, HEIGHT, 24, 8, 8, 8);

for (unsigned int y=0; y<FreeImage_GetHeight(bitmap); y++)
{
    BYTE *bits=FreeImage_GetScanLine(bitmap,y);
    for (unsigned int x=0; x<FreeImage_GetWidth(bitmap); x++)
    {
        bits[0]=mpixels[(y*WIDTH+x)*3+0];
        bits[1]=mpixels[(y*WIDTH+x)*3+1];
        bits[2]=mpixels[(y*WIDTH+x)*3+2];
        bits += 3;
    }

}

FreeImage_Save(FIF_JPEG, bitmap, "test.jpg", JPEG_DEFAULT);

FreeImage_Unload(bitmap);

}

void createCoordinate(GLfloat x0, GLfloat y0)
{
int i = 0, k = 12,j = 9;
GLfloat x[100], y[10];
for (i = 0; i < k; i++) {
x[i] = x0 + 0.1*(i + 1);
}
for (i = 0; i < j; i++) {
y[i] = y0 + 0.1*(i + 1);
}
//设置颜色
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f, 0.0f, 0.0f);

GLfloat lineWidth = 0.5f;
glLineWidth(lineWidth);
glBegin(GL_LINES);
glVertex2f(0.0f, -0.5f);   //纵轴
glVertex2f(0.0f, 0.5f);
glVertex2f(0.02f, 0.48f);
glVertex2f(0.0f, 0.5f);
glVertex2f(-0.02f, 0.48f);
glVertex2f(0.0f, 0.5f);

glVertex2f(-0.1f, 0.0f);   //横轴
glVertex2f(1.05f, 0.0f);
glVertex2f(1.03f, 0.02f);
glVertex2f(1.05f, 0.0f);
glVertex2f(1.03f, -0.02f);
glVertex2f(1.05f, 0.0f);
glEnd();

lineWidth = 0.1f;
glLineWidth(lineWidth);
glBegin(GL_LINES);
for (i = 0; i < k; i++) {
    glVertex2f(x[i], 0.02);
    glVertex2f(x[i], 0);
}
for (i = 0; i < j; i++) {
    glVertex2f(0.02, y[i]);
    glVertex2f(0, y[i]);
}
glEnd();

string s;
for (i = 0; i < k; i++) {
    s = num2string(0.1 * (i + 1)-0.2);
    glRasterPos2f(x[i], -0.05f);
    drawString(s);

}
for (i = 0; i < j; i++) {
    s = num2string(0.1 * (i + 1) - 0.5);
    glRasterPos2f(-0.05f, y[i]);
    drawString(s);
}

}

void data()
{
float data;
GLfloat a[50000], b[1440][2];
int i=0 , k = 0;
FILE *fp = fopen("E:\ice.txt", "r");
if (!fp)
{
printf("can't open file\n");

}
while (!feof(fp))
{

    fscanf(fp, "%f", &data);
    a[i] = data;
    i++;
}

k = i /2;
printf("\n");
fclose(fp);
int j, t = 0;
for (i = 0; i<k; i++)
    for (j = 0; j<2; j++)
    {
        b[i][j] = a[t++];

    }
for (i = 0; i < k; i++)
{
    printf("%f %f \n", b[i][0], b[i][1]);
}
GLfloat pointSize = 5.0f;
glPointSize(pointSize);
glBegin(GL_LINE_STRIP);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
for (i = 0; i<k; i++)
{   
    glVertex2f(b[i][0], b[i][1]);
}
glEnd();

}

void Display()
{
//GLfloat x0 = 10.0f, y0 = 10.0f;
GLfloat x0 = -0.2f, y0 = -0.5f;
createCoordinate(x0, y0);
data();

//glFlush();
glutSwapBuffers();

}

void Initial()
{
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); //清屏颜色
glMatrixMode(GL_PROJECTION);
gluOrtho2D(-0.2, 1.1, -0.5, 0.5); //投影到裁剪窗大小:世界
}

//void timerProc(int id)
//{
// Display();

//glutTimerFunc(33, timerProc, 1);//需要在函数中再调用一次,才能保证循环??

//}

int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
//glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 500);
glutInitWindowPosition(300, 300);
glutCreateWindow("Scatter Plot");
glutDisplayFunc(Display);
grab();
Initial();

glutMainLoop();
//glutTimerFunc(330, timerProc, 1);
return 0;

}

1个回答

for (int i=0; i<(int)WIDTH*HEIGHT*3; i+=3)
{
mpixels[i]^= mpixels[i+2]^= mpixels[i]^= mpixels[i+2]; //这里获取的数据是否正确??
}

qq_33317253
子丑乙卯 这个数据怎么看是够获取正确
大约 3 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问