Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * GLUT Shapes Demo
- *
- * Written by Nigel Stewart November 2003
- *
- * This program is test harness for the sphere, cone
- * and torus shapes in GLUT.
- *
- * Spinning wireframe and smooth shaded shapes are
- * displayed until the ESC or q key is pressed. The
- * number of geometry stacks and slices can be adjusted
- * using the + and - keys.
- */
- #include <windows.h>
- #ifdef __APPLE__
- #include <GLUT/glut.h>
- #else
- #include <GL/glut.h>
- #endif
- #include <stdio.h>
- #include <stdlib.h>
- static void resize(int width, int height);
- GLuint raw_texture_load(const char *filename, int width, int height)
- {
- GLuint texture;
- unsigned char *data;
- FILE *file;
- // open texture data
- file = fopen(filename, "rb");
- if (file == NULL) return 0;
- // allocate buffer
- data = (unsigned char*) malloc(width * height * 4);
- // read texture data
- fread(data, width * height * 4, 1, file);
- fclose(file);
- // allocate a texture name
- glGenTextures(1, &texture);
- // select our current texture
- glBindTexture(GL_TEXTURE_2D, texture);
- // select modulate to mix texture with color for shading
- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
- // when texture area is small, bilinear filter the closest mipmap
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- // when texture area is large, bilinear filter the first mipmap
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- // texture should tile
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
- // build our texture mipmaps
- gluBuild2DMipmaps(GL_TEXTURE_2D, 4, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data);
- // free buffer
- free(data);
- return texture;
- }
- static void resize(int width, int height)
- {
- glViewport(0, 0, width, height);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0.0f, width, height, 0.0f, 0.0f, 1.0f);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity() ;
- }
- void draw_texture(const char * nome, int x,int y,int w,int h)
- {
- // OpenGL animation code goes here
- // load our texture
- GLint texture = raw_texture_load( nome, x , y );
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- float theta = 0.0f;
- // setup texture mapping
- glEnable( GL_TEXTURE_2D );
- glBindTexture( GL_TEXTURE_2D, texture );
- glPushMatrix();
- glRotatef( theta, 0.0f, 0.0f, 1.0f );
- glBegin( GL_QUADS );
- glTexCoord2d(0.0,0.0); glVertex2d(0, 0);
- glTexCoord2d(32,0.0); glVertex2d(w, 0);
- glTexCoord2d(32,32); glVertex2d(w, h);
- glTexCoord2d(0.0,32); glVertex2d(0,h);
- glEnd();
- glPopMatrix();
- glutSwapBuffers();
- theta += 1.0f;
- }
- static void display(void)
- {
- draw_texture("image.raw", 32, 32, 320, 240);
- }
- /* Program entry point */
- // OpenGL ////////////////////////////////////////////////////////////
- // Enable OpenGL
- VOID EnableOpenGL( HWND hWnd, HDC * hDC, HGLRC * hRC )
- {
- PIXELFORMATDESCRIPTOR pfd;
- int iFormat;
- // get the device context (DC)
- *hDC = GetDC( hWnd );
- // set the pixel format for the DC
- ZeroMemory( &pfd, sizeof( pfd ) );
- pfd.nSize = sizeof( pfd );
- pfd.nVersion = 1;
- pfd.dwFlags = PFD_DRAW_TO_WINDOW |
- PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
- pfd.iPixelType = PFD_TYPE_RGBA;
- pfd.cColorBits = 24;
- pfd.cDepthBits = 16;
- pfd.iLayerType = PFD_MAIN_PLANE;
- iFormat = ChoosePixelFormat( *hDC, &pfd );
- SetPixelFormat( *hDC, iFormat, &pfd );
- // create and enable the render context (RC)
- *hRC = wglCreateContext( *hDC );
- wglMakeCurrent( *hDC, *hRC );
- }
- // Disable OpenGL
- VOID DisableOpenGL( HWND hWnd, HDC hDC, HGLRC hRC )
- {
- wglMakeCurrent( NULL, NULL );
- wglDeleteContext( hRC );
- ReleaseDC( hWnd, hDC );
- }
- int main(int argc, char *argv[])
- {
- glutInit(&argc, argv);
- glutInitWindowSize(640,480);
- glutInitWindowPosition(10,10);
- glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
- glutCreateWindow("GLUT Shapes");
- glutReshapeFunc(resize);
- glutDisplayFunc(display);
- glutMainLoop();
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement