Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "GL/freeglut.h"
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct{ GLubyte r, g, b; } PIXEL;
- PIXEL *imagineBMP;
- GLint width, height;
- PIXEL *ReadBMP(char *nume, int *ncol, int *nlin)
- {
- int i, j, w, h, data_offset, r, lglin, tcompr;
- long dofs;
- short nbpp;
- char buf[20];
- PIXEL *img;
- GLubyte t;
- FILE *f;
- if ((f = fopen(nume, "rb")) == NULL)
- {
- fprintf(stderr, "ReadBMP --eroare deschidere fisier\n");
- exit(1);
- }
- else
- {
- fprintf(stderr, "Fisierul a fost deschis cu succes!");
- }
- fread(buf, 1, 2, f);
- buf[2] = '\0';
- fseek(f, 28L, SEEK_SET);
- fread(&nbpp, sizeof(short), 1, f);
- fseek(f, 30L, SEEK_SET);
- fread(&tcompr, sizeof(int), 1, f);
- if (strcmp(buf, "BM") != 0 || nbpp != 24 || tcompr != 0)
- {
- //se citeste un fisier BMP necomprimat
- fprintf(stderr, "ReadBMP -- format BMP incorect\n");
- exit(1);
- }
- fseek(f, 18L, SEEK_SET);
- fread(&w, sizeof(int), 1, f); //citesc numarul de coloane
- fread(&h, sizeof(int), 1, f); //citesc numarul de linii
- img = (PIXEL *)malloc(h*w*sizeof(PIXEL));
- fseek(f, 10L, SEEK_SET); //cauta deplasamentul zonei de date
- fread(&data_offset, sizeof(int), 1, f); //citesc deplas. zonei de
- // date fata de inceputul fisierului
- lglin = w * 3;
- if (r = lglin % 4) lglin += 4 - r;
- for (dofs = data_offset, i = 0; i < h; dofs += lglin, i++)
- {
- fseek(f, dofs, SEEK_SET);
- fread(&img[i*w], sizeof(PIXEL), w, f);
- for (j = 0; j < w; j++) //schimba red/blue
- {
- t = img[i*w + j].r;
- img[i*w + j].r = img[i*w + j].b;
- img[i*w + j].b = t;
- }
- }
- *nlin = h;
- *ncol = w;
- fclose(f);
- return img;
- }
- PIXEL *image;
- static GLuint texName[1];
- void Plane()
- {
- glBegin(GL_QUADS);
- glVertex3d(0, 0, 1);
- glTexCoord2f(0.0, 0.0);
- glVertex3d(3, 0, 1);
- glTexCoord2f(1.0, 0.0);
- glVertex3d(3, 3,-1);
- glTexCoord2f(1.0, 1.0);
- glVertex3d(0, 3, -1);
- glTexCoord2f(0.0, 1.0);
- glEnd();
- }
- void init(void)
- {
- glClearColor(1, 1, 1, 0);
- //texturare
- image = ReadBMP("bricks.bmp", &width, &height);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glEnable(GL_TEXTURE_2D);
- }
- void display(void)
- {
- glClear(GL_COLOR_BUFFER_BIT);
- glPushMatrix();
- glTranslatef(-1.5, -1.5, -5);
- Plane();
- glPopMatrix();
- glutSwapBuffers();
- }
- void reshape(int w, int h)
- {
- glViewport(0, 0, (GLsizei)w, (GLsizei)h);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(60.0, (GLfloat)w / (GLfloat)h, 0.1, 100);
- glMatrixMode(GL_TEXTURE);
- glLoadIdentity();
- glScaled(2, 2, 2);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- }
- int main(int argc, char **argv)
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
- glutInitWindowPosition(200, 200);
- glutInitWindowSize(600, 600);
- glutCreateWindow("SPG OpenGL");
- init();
- glutDisplayFunc(display);
- glutReshapeFunc(reshape);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement