Hello,
i´m trying load two ppm files and show the first image with glDrawPixels, but i´m only getting a white screen. This is the Code:
#include "../common/cg_common.h"
#include <iostream>
#include <fstream>
#include <string>
GLubyte* first_image;
GLubyte* second_image;
int win_width, win_height;
void skipComment(FILE *fin)
{
char buf[120];
while (true) {
fscanf(fin, "%1s", &buf);
if (buf[0] != '#') {
ungetc(buf[0], fin);
return;
}
else fgets(buf, 120, fin);
}
}
GLubyte *readPPM(FILE *fin, GLsizei *wid, GLsizei *ht)
{
GLubyte *bytes;
char cookie[3];
int width, height, maxComp;
int n, r,c;
fscanf(fin, "%2s", &cookie);
if (strcmp("P6", cookie))
return NULL;
skipComment(fin);
fscanf(fin, "%d", &width); *wid = width;
skipComment(fin);
fscanf(fin, "%d", &height); *ht = height;
skipComment(fin);
fscanf(fin, "%d", &maxComp);
if (maxComp > 255)
return NULL;
fgetc(fin);
bytes = (GLubyte *) malloc(width*height*3);
if (bytes == NULL)
return NULL;
for (r=0; r < height; r++)
for (c=0; c < width; c++) {
bytes[3*(r*width + c)] = fgetc(fin);
bytes[3*(r*width + c)+1] = fgetc(fin);
bytes[3*(r*width + c)+2] = fgetc(fin);
//std::cout << "Rot: " << bytes[3*(r*width + c)] << " Gelb: " << bytes[3*(r*width + c)+1] << " Blau: " << bytes[3*(r*width + c)+2] << std::endl;
}
return bytes;
}
void initView() {
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 256.0, 0, 256.0);
glMatrixMode(GL_MODELVIEW);
}
void init() {
GLsizei width;
GLsizei height;
std::string filename = "image1.ppm";
GLubyte* image = readPPM(fopen(filename.c_str(),"r"),&width,&height);
if(image != NULL)
first_image = image;
else
std::cout << filename << " konnte nicht richtig geladen werden " << std::endl;
filename = "image2.ppm";
image = readPPM(fopen(filename.c_str(),"r"),&width,&height);
if(image != NULL)
second_image = image;
else
std::cout << filename << " konnte nicht richtig geladen werden " << std::endl;
win_height = height;
win_width = width;
initView();
}
void initGL() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // black background
}
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawPixels(win_width,win_height,GL_RGB,GL_UNSIGNED_BYTE,first_image);
glFlush();
}
void reshape(int /*width*/, int /*height*/) {
}
void key(unsigned char k, int /*x*/, int /*y*/) {
if (k == 27)
exit(0);
}
// Main function for bringing it all together
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB);
glutInitWindowSize(256, 256);
glutCreateWindow("Aufgabe 3.6");
initGL();
init();
// setup GLUT callbacks
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(key);
// glutIdleFunc(display); // if continuous animation is required
glutMainLoop(); // start the main event loop
}
These are the two images:
I have no Idea what i´m doing wrong
thank you!
EDIT: Okay i think i found the failure. The read method reads only -1 after a few correct numbers