I am using openGL and C to load a sprite as a ppm file, I use Gimp to turn a png into a ppm and then I turn it into an array and add commas it to make it readable in C. In the ppm file format there is the red green and blue values of each pixel separated by a comma and the array name is at the start. The only problem is when I load the file and try to see it in openGL it does not work and just leaves me with a blanc screen.
.ppm file format:
int Wall[]={
184,
184,
184,
etc
};
Code:
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
#ifdef __unix__
# include <unistd.h>
#elif defined _WIN32
# include <windows.h>
#define sleep(x) Sleep(1000 * (x))
#endif
#define or ||
#define and &&
#define pCharacter "%c \n"
#define pInteger "%d \n"
#define pFloat "%f \n"
#define pDouble "%lf \n"
#define pUnsighned "%u \n"
#define pString "%s \n"
#include <C:/Users/cuerv/Downloads/C/First/Sprites/wall.ppm>
int width=1000; //Screen width
int height=500; //Screen height
void drawSprites(){
int x;
int y;
for(y=0;y<33;y++){//The image is 33*33
for(x=0;y<33;x++){//The image is 33*33
int pixel=(y*33+x)*3;//Use the *3 because there are three RGB vals per pixel
int red=Wall[pixel+0];
int green=Wall[pixel+1];
int blue=Wall[pixel+2];
glPointSize(8);
glBegin(GL_POINTS);
glColor3ub(red, green, blue);
glVertex2i(x+8,y+8);
glEnd();
}
}
}
void init(){
glClearColor(0.3,0.3,0.3,0);
gluOrtho2D(0,width,height,0);
}
void userDisplay(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//-----------------------Draw----------------------
drawSprites();
glutSwapBuffers();
glutPostRedisplay();
}
void main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(width,height);
glutCreateWindow("OpenGL");
init();
glutDisplayFunc(userDisplay);
glutMainLoop();
}
It works when you correct the typo in this line. (It must be x<33 for x loop)
for(x=0;y<33;x++){//The image is 33*33
for(x=0;x<33;x++){//The image is 33*33
With the y<33 I get an Exception because you are reading beyond the limits (more Wall elements that you have)
But it shows a small colored area because you are painting in the area 0..x+8..41 and the window is 1000 pixels width.
Changing the addition (+8) by a multiplication (*8), you will see the whole points without overlapping each other
glVertex2i(x+8,y+8);
glVertex2i(x*8,y*8);
// Adding half of the point size to show the top row and left column completely.
void drawSprites() {
int size = 33;
int pointSize = 16;
for (int y = 0; y < size; y++) {//The image is 33*33
for (int x = 0; x < size; x++) {//The image is 33*33
int pixel = (y * size + x) * 3;//Use the *3 because there are three RGB vals per pixel
int red = Wall[pixel + 0];
int green = Wall[pixel + 1];
int blue = Wall[pixel + 2];
glPointSize(pointSize);
glBegin(GL_POINTS);
glColor3ub(red, green, blue);
//glVertex2i(x + 8, y + 8);
glVertex2i(x * pointSize + pointSize/2 , y * pointSize + pointSize / 2);
glEnd();
}
}
}
EXAMPLE: using some random colors vector "int Wall[]"
Provide more details about the desired outcome if you need additional help.