Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- float xRot = 0.0f;
- float yRot = 0.0f;
- float zRot = 0.0f;
- float zTrans = -60.0f;
- int boxList, texList;
- unsigned char* Pixels;
- GLuint texture;
- GLuint width, height, imgSize;
- static void display()
- {
- static GLint s_v[4] = { 2, 0, 0, 0 };
- static GLint t_v[4] = { 0, 0, 2, 0 };
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glColor3f(0.0f, 0.0f, 1.0f);
- glPushMatrix();
- glTranslatef(0.0f, 0.0f, zTrans);
- glRotatef(xRot, 1.0f, 0.0f, 0.0f);
- glRotatef(yRot, 0.0f, 1.0f, 0.0f);
- glRotatef(zRot, 0.0f, 0.0f, 1.0f);
- glEnable(GL_TEXTURE_2D);
- glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
- glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
- glTexGeniv(GL_S, GL_OBJECT_PLANE, s_v);
- glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
- glTexGeniv(GL_T, GL_OBJECT_PLANE, t_v);
- glCallList(texList);
- glCallList(boxList);
- glDisable(GL_TEXTURE_2D);
- glPopMatrix();
- glFlush();
- glutSwapBuffers();
- }
- static void init()
- {
- GLfloat box1[3][3] = {{ 500, 500, -500}, { 500, -500, -500}, { -500, -500, -500}};
- GLfloat box2[3][3] = {{ -500, 500, -500}, { -500, -500, -500}, { -500, -500, 500}};
- GLfloat box3[3][3] = {{ -500, 500, 500}, { -500, -500, 500}, { 500, -500, 500}};
- GLfloat box4[3][3] = {{ 500, 500, 500}, { 500, -500, 500}, { 500, -500, -500}};
- GLfloat normals[3];
- glColor3f(0.0f, 0.0f, 1.0f);
- glNewList(boxList = glGenLists(1), GL_COMPILE);
- glBegin(GL_QUADS);
- glFrontFace(GL_CW);
- calcNormal(box1, normals);
- glNormal3fv(normals);
- glVertex3fv(box1[0]);
- glVertex3fv(box1[1]);
- glVertex3fv(box1[2]);
- glVertex3fv(box2[0]);
- //do the same for the other boxes, box 4 uses a vertex from box1
- glEnd();
- glEndList();
- }
- int main()
- {
- //setup glut window & callbacks
- glClearColor(0,0,0,1);
- glColor3f(0.0f, 1.0f, 0.0f);
- glEnable(GL_DEPTH_TEST);
- //glEnable(GL_LIGHTING);
- glEnable(GL_COLOR_MATERIAL);
- //glEnable(GL_CULL_FACE);
- //glCullFace(GL_BACK);
- glEnable(GL_TEXTURE_GEN_S);
- glEnable(GL_TEXTURE_GEN_T);
- GLfloat am[] = { 1.0, 1.0, 1.0, 1.0 };
- GLfloat df[] = { .4f, .4f, .4f };
- GLfloat pos[] = { -10.0f, -10.0f, -50.0f, 1.0f };
- glLightfv(GL_LIGHT0, GL_AMBIENT, am);
- glLightfv(GL_LIGHT0, GL_DIFFUSE, df);
- glLightfv(GL_LIGHT0, GL_POSITION, pos);
- glEnable(GL_LIGHT0);
- glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
- glNewList(texList = glGenLists(1), GL_COMPILE);
- texture = LoadBitmap("wood.bmp");
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST );
- gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height,GL_RGB, GL_UNSIGNED_BYTE, Pixels );
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, Pixels);
- glEndList();
- init();
- glutMainLoop();
- delete[] Pixels;
- return EXIT_SUCCESS;
- }
- unsigned int LoadBitmap(string FilePath)
- {
- unsigned char bitmapHeader[54];
- unsigned int pos, texID;
- glGenTextures(1, &texID);
- glBindTexture(GL_TEXTURE_2D, texID);
- FILE* fp = fopen(FilePath.c_str(), "rb");
- if(!fp)
- {
- cout << "Bad file\n";
- return NULL;
- }
- fread(bitmapHeader, 1, sizeof(bitmapHeader), fp);
- if(bitmapHeader[0] != 'B' || bitmapHeader[1] != 'M')
- {
- cout << "Not a bitmap\n";
- return NULL;
- }
- imgSize = *(int*)&(bitmapHeader[0x22]);
- pos = *(int*)&(bitmapHeader[0x0A]);
- width = *(int*)&(bitmapHeader[0x12]);
- height = *(int*)&(bitmapHeader[0x16]);
- if (imgSize == NULL)
- imgSize = width * height * 3;
- if (pos == NULL)
- pos = 54;
- if(Pixels)
- delete[] Pixels;
- else
- Pixels = new unsigned char[imgSize];
- fseek(fp, pos, SEEK_SET);
- fread(Pixels, 1, imgSize, fp);
- fclose(fp);
- return texID;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement