Sign in to follow this  

Drawing over a loaded picture

Recommended Posts

souhail    122
I loaded a ppm image and I'm having trouble drawing over this image, I just need to draw square or shapes over it and update coordinates. Here is my code: #include<windows.h> // use as needed for your system #include <stdio.h> #include <stdlib.h> #include<gl/Gl.h> #include<gl/glut.h> int n; int m; int *image; void myInit (void) { glClearColor ( 1.0, 1.0, 1.0 , 1.0); glColor3f ( 1.0f, 0.0f, 0.0f ); glPointSize ( 4.0 ); glMatrixMode ( GL_PROJECTION ); glLoadIdentity ( ); gluOrtho2D ( 0.0, 400.0, 1.0, 400.0 ); } void display(void) { //glClear ( GL_COLOR_BUFFER_BIT ); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glRasterPos2i(0,0);glMatrixMode(GL_MODELVIEW); glDrawPixels(n,m,GL_RGB, GL_UNSIGNED_INT, image); //glFlush ( ); glPushMatrix(); glBegin(GL_QUADS); glNormal3f(0.0, 1.0f, 0.0f); glTexCoord2f(0.0f, 0.0f); glVertex3f(-2.5f, -2.5f, 2.5f); glTexCoord2f(1.0f, 0.0f); glVertex3f(2.5f, -2.5f, 2.5f); glTexCoord2f(1.0f, 1.0f); glVertex3f(2.5f, -2.5f, -2.5f); glTexCoord2f(0.0f, 1.0f); glVertex3f(-2.5f, -2.5f, -2.5f); glEnd(); glPopMatrix(); glutSwapBuffers(); } void myreshape(int h, int w) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode (GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, (GLfloat) n, 0.0, (GLfloat) m); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glViewport(0,0,h,w); } int main(int argc, char**argv) { FILE *fd; int k, nm; char c; char b[70]; float s; char red, green, blue; int x, y; fd = fopen("au.ppm", "r"); if(fd == 0) { exit(0); } fscanf(fd, "%s", b); if((b[0] != 'P') || (b[1] != '6')) { printf("%s is not a PPM file!\n", b); exit(0); } fscanf(fd, "%c", &c); fscanf(fd, "%c", &c); while(c == '#') { fscanf(fd, "%[^\n]", b); printf("%s\n", b); fscanf(fd, "%c", &c); printf("%c", c); } ungetc(c,fd); fscanf(fd, "%d %d %d", &n, &m, &k); printf("%d rows %d colums max value = %d\n", n, m, k); nm = n*m; image = malloc(3*sizeof(GLint)*nm); s = 255./k; for(x = 0; x < m; x++) { for(y = n-1; y >= 0; y--) { fscanf(fd, "%c", &red); fscanf(fd, "%c", &green); fscanf(fd, "%c", &blue); image[3*nm - 3*(x*n +y) -3] = green; image[3*nm - 3*(x*n +y) -2] = blue; image[3*nm - 3*(x*n +y) -1] = red; } } glutInit(&argc, argv); //glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitDisplayMode(GLUT_DOUBLE |GLUT_RGB ); glutInitWindowSize(n,m); glutInitWindowPosition(0,0); glutCreateWindow("image"); glutReshapeFunc(myreshape); glutDisplayFunc(display); glPixelTransferf(GL_RED_SCALE, s); glPixelTransferf(GL_GREEN_SCALE, s); glPixelTransferf(GL_BLUE_SCALE, s); glPixelStorei(GL_UNPACK_SWAP_BYTES, GL_TRUE); glClearColor(1.0, 1.0, 1.0, 1.0); myInit ( ); glutMainLoop ( ); } Can somebody help me with this. The only output I'm getting is the image. Any OpenGL experts? Thank you so much!

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this