multiple stuff is turning

This commit is contained in:
Eri - 2014-01-24 11:23:49 +01:00
parent 0c088e247c
commit c6434642e9
1 changed files with 264 additions and 106 deletions

View File

@ -12,25 +12,152 @@
#include <unistd.h> // Header File For sleeping.
#include <stdio.h>
#include <stdlib.h>
#define PI 3.1415
/* ASCII code for the escape key. */
#define ESCAPE 27
#define PAGE_UP 73
#define PAGE_DOWN 81
#define UP_ARROW 72
#define DOWN_ARROW 80
#define LEFT_ARROW 75
#define RIGHT_ARROW 77
/* The number of our GLUT window */
int window;
float xrot = 0.0f;
float yrot = 0.0f;
float zrot = 0.0f;
float i = 0.15;
int full = 0;
int oszi = 0;
GLuint texture[1]; // Stores one new Texture
struct Image {
unsigned long sizeX;
unsigned long sizeY;
char *data;
};
typedef struct Image Image;
int ImageLoad(char *filename, Image *image) {
FILE *file;
unsigned long size; // size of the image in bytes.
unsigned long i; // standard counter.
unsigned short int planes; // number of planes in image (must be 1)
unsigned short int bpp; // number of bits per pixel (must be 24)
char temp1; // temporary color storage for bgr-rgb conversion.
char temp2;
char temp3;
// make sure the file is there.
if ((file = fopen(filename, "rb"))==NULL)
{
printf("File Not Found : %s\n",filename);
return 0;
}
// seek through the bmp header, up to the width/height:
fseek(file, 18, SEEK_CUR);
// read the width
if ((i = fread(&image->sizeX, 4, 1, file)) != 1) {
printf("Error reading width from %s.\n", filename);
return 0;
}
printf("Width of %s: %lu\n", filename, image->sizeX);
// read the height
if ((i = fread(&image->sizeY, 4, 1, file)) != 1) {
printf("Error reading height from %s.\n", filename);
return 0;
}
printf("Height of %s: %lu\n", filename, image->sizeY);
// calculate the size (assuming 24 bits or 3 bytes per pixel).
size = image->sizeX * image->sizeY * 3;
// read the planes
if ((fread(&planes, 2, 1, file)) != 1) {
printf("Error reading planes from %s.\n", filename);
return 0;
}
if (planes != 1) {
printf("Planes from %s is not 1: %u\n", filename, planes);
return 0;
}
// read the bpp
if ((i = fread(&bpp, 2, 1, file)) != 1) {
printf("Error reading bpp from %s.\n", filename);
return 0;
}
if (bpp != 24) {
printf("Bpp from %s is not 24: %u\n", filename, bpp);
return 0;
}
// seek past the rest of the bitmap header.
fseek(file, 24, SEEK_CUR);
// read the data.
image->data = (char *) malloc(size);
if (image->data == NULL) {
printf("Error allocating memory for color-corrected image data");
return 0;
}
if ((i = fread(image->data, size, 1, file)) != 1) {
printf("Error reading image data from %s.\n", filename);
return 0;
}
for (i=0;i<size;i+=3) { // reverse all of the colors. (bgr -> rgb)
temp1 = image->data[ i ]; // b -- don't ask its what gimp gave me
temp2 = image->data[i+1]; // r
temp3 = image->data[i+2]; // g
image->data[i] = temp2;
image->data[i+1] = temp1;
image->data[i+2] = temp3;
}
// we're done.
return 1;
}
// Load Bitmaps And Convert To Textures
void LoadGLTextures() {
// Load Texture
Image *image1;
// allocate space for texture
image1 = (Image *) malloc(sizeof(Image));
if (image1 == NULL) {
printf("Error allocating space for image");
exit(0);
}
if (!ImageLoad("/home/eri/Bilder/texturefun1.bmp", image1)) {
exit(1);
}
// Create Texture
glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]); // 2d texture (x and y size)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // scale linearly when image bigger than texture
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // scale linearly when image smalled than texture
// 2d texture, level of detail 0 (normal), 3 components (red, green, blue), x size from image, y size from image,
// border 0 (normal), rgb color data, unsigned byte data, and finally the data itself.
glTexImage2D(GL_TEXTURE_2D, 0, 3, image1->sizeX, image1->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, image1->data);
};
/* A general OpenGL initialization function. Sets all of the initial parameters. */
void InitGL(int Width, int Height) // We call this right after our OpenGL window is created.
{
LoadGLTextures(); // Load The Texture(s)
glEnable(GL_TEXTURE_2D);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // This Will Clear The Background Color To Black
glClearDepth(1.0); // Enables Clearing Of The Depth Buffer
glDepthFunc(GL_LESS); // The Type Of Depth Test To Do
@ -61,6 +188,99 @@ void ReSizeGLScene(int Width, int Height)
glMatrixMode(GL_MODELVIEW);
}
void rota(int what)
{
usleep(10);
switch(what)
{
case 0:
xrot += i;
break;
case 1:
yrot += i;
break;
case 2:
zrot += i;
break;
default:
xrot += i;
yrot += i;
zrot += i;
}
if (xrot > 360) xrot = 0;
if (yrot > 360) yrot = 0;
if (zrot > 360) zrot = 0;
}
void DrawBracket(float i){
// draw a square (quadrilateral)
glBegin(GL_QUADS); // start drawing a polygon (4 sided)
// Down
// Front Face (note that the texture's corners have to match the quad's corners)
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f + i, -0.5f, 0.5f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.7f + i, -0.5f, 0.5f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.9f + i, 0.0f, 0.5f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.2f + i, 0.0f, 0.5f); // Top Left Of The Texture and Quad
// Back Face
glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.7f + i, -0.5f, -0.5f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.9f + i, 0.0f, -0.5f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.2f + i, 0.0f, -0.5f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f + i, -0.5f, -0.5f); // Bottom Left Of The Texture and Quad
// Bottom Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f + i, -0.5f, -0.5f); // Top Right Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.7f + i, -0.5f, -0.5f); // Top Left Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.7f + i, -0.5f, 0.5f); // Bottom Left Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f + i, -0.5f, 0.5f); // Bottom Right Of The Texture and Quad
// Right face
glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.7f + i, -0.5f, -0.5f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.9f + i, 0.0f, -0.5f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.9f + i, 0.0f, 0.5f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.7f + i, -0.5f, 0.5f); // Bottom Left Of The Texture and Quad
// Left Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f + i, -0.5f, 0.5f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f + i, -0.5f, -0.5f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.2f + i, 0.0f, -0.5f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.2f + i, 0.0f, 0.5f); // Top Left Of The Texture and Quad
// Up
// Front Face (note that the texture's corners have to match the quad's corners)
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.2f + i, 0.0f, 0.5f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.9f + i, 0.0f, 0.5f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.7f + i, 0.5f, 0.5f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f + i, 0.5f, 0.5f); // Top Left Of The Texture and Quad
// Back Face
glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.9f + i, 0.0f, -0.5f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.7f + i, 0.5f, -0.5f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f + i, 0.5f, -0.5f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.2f + i, 0.0f, -0.5f); // Bottom Left Of The Texture and Quad
// Top Face
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f + i, 0.5f, -0.5f); // Top Left Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f + i, 0.5f, 0.5f); // Bottom Left Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.7f + i, 0.5f, 0.5f); // Bottom Right Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.7f + i, 0.5f, -0.5f); // Top Right Of The Texture and Quad
// Left Face
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.2f + i, -0.0f, -0.5f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f + i, 0.5f, -0.5f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f + i, 0.5f, 0.5f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.2f + i, -0.0f, 0.5f); // Bottom Left Of The Texture and Quad
// Right face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.9f + i, 0.0f, 0.5f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.9f + i, 0.0f, -0.5f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.7f + i, 0.5f, -0.5f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.7f + i, 0.5f, 0.5f); // Top Left Of The Texture and Quad
glEnd(); // done with the polygon
}
/* The main drawing function. */
void DrawGLScene()
{
@ -72,106 +292,21 @@ void DrawGLScene()
glRotatef(xrot, 1.0f, 0.0f, 0.0f);
glRotatef(yrot, 0.0f, 1.0f, 0.0f);
glRotatef(zrot, 0.0f, 0.0f, 1.0f);
// draw a square (quadrilateral)
glBegin(GL_QUADS); // start drawing a polygon (4 sided)
glBindTexture(GL_TEXTURE_2D, texture[0]); // choose the texture to use.
/* --- Top --- */
glColor3f( 1.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Front
DrawBracket(0);
glColor3f( 1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Front
glRotatef(xrot, 1.0f, 0.0f, 0.0f);
DrawBracket(0.7);
glColor3f( 1.0f, 0.0f, 1.0f);
glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Back
glColor3f( 1.0f, 0.0f, 0.0f);
glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Back
/* --- Left Side --- */
glColor3f( 0.0f, 0.0f, 0.0f);
glVertex3f(-1.0f,-1.0f,-1.0f); // Left Side Bottom Back
glColor3f( 0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 1.0f); // Left Side Bottom Front
glColor3f( 1.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, 1.0f, 1.0f); // Left Side Top Front
glColor3f( 1.0f, 0.0f, 0.0f);
glVertex3f(-1.0f, 1.0f,-1.0f); // Left Side Top Back
/* --- Right Side --- */
glColor3f( 0.0f, 0.0f, 1.0f);
glVertex3f( 1.0f,-1.0f,-1.0f); // Right Side Bottom Back
glColor3f( 0.0f, 1.0f, 1.0f);
glVertex3f( 1.0f,-1.0f, 1.0f); // Right Side Bottom Front
glColor3f( 1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f); // Right Side Top Front
glColor3f( 1.0f, 0.0f, 1.0f);
glVertex3f( 1.0f, 1.0f,-1.0f); // Right Side Top Back
/* --- Back Side --- */
glColor3f( 0.0f, 0.0f, 1.0f);
glVertex3f( 1.0f,-1.0f,-1.0f); // Back Right Side Bottom
glColor3f( 0.0f, 0.0f, 0.0f);
glVertex3f(-1.0f,-1.0f,-1.0f); // Back Left Side Bottom
glColor3f( 1.0f, 0.0f, 0.0f);
glVertex3f(-1.0f, 1.0f,-1.0f); // Back Left Side Top
glColor3f( 1.0f, 0.0f, 1.0f);
glVertex3f( 1.0f, 1.0f,-1.0f); // Back Right Side Top
/* --- Front --- */
glColor3f( 0.0f, 1.0f, 1.0f);
glVertex3f( 1.0f,-1.0f, 1.0f); // Front Right Side Bottom
glColor3f( 0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 1.0f); // Front Left Side Bottom
glColor3f( 1.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, 1.0f, 1.0f); // Front Left Side Top
glColor3f( 1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f); // Front Right Side Top
/* --- Bottom --- */
glColor3f( 0.0f, 0.0f, 0.0f);
glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Back
glColor3f( 0.0f, 0.0f, 1.0f);
glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Back
glColor3f( 0.0f, 1.0f, 1.0f);
glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Right Front
glColor3f( 0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Left Front
glEnd(); // done with the polygon
glRotatef(xrot, 1.0f, 0.0f, 0.0f);
DrawBracket(1.4);
// swap buffers to display, since we're double buffered.
glutSwapBuffers();
}
void rota()
{
glClear(GL_COLOR_BUFFER_BIT);
glRotatef(5,1,0.5,0.8);
DrawGLScene();
glFlush();
glutTimerFunc(10.0, &rota, 5);
if (oszi) rota(0);
}
/* The function called whenever a key is pressed. */
@ -182,10 +317,29 @@ void keyPressed(unsigned char key, int x, int y)
switch (key)
{
case 'f': glutFullScreen();
case 'f': if (!full)
{
glutFullScreen();
full = 1;
} else {
glutReshapeWindow(640,480);
full = 0;
}
break;
case 'w': glutReshapeWindow(640,480);
case 'm': if (!oszi)
{
oszi = 1;
} else {
oszi = 0;
}
break;
case ' ':
zrot += 5;
break;
case '0': zrot = xrot = yrot = 0; break;
case 27: glutDestroyWindow(window);
exit(0);
default:
@ -201,16 +355,20 @@ void specialKeyPressed(int key, int x, int y)
switch (key)
{
case GLUT_KEY_UP:
xrot += 10;
xrot += 5;
oszi = 0;
break;
case GLUT_KEY_DOWN:
xrot -= 10;
xrot -= 5;
oszi = 0;
break;
case GLUT_KEY_LEFT:
yrot += 10;
yrot += 5;
oszi = 0;
break;
case GLUT_KEY_RIGHT:
yrot -= 10;
yrot -= 5;
oszi = 0;
break;
default:
break;
@ -234,7 +392,7 @@ int main(int argc, char **argv)
glutInitWindowSize(640, 480);
/* the window starts at the upper left corner of the screen */
glutInitWindowPosition(0, 0);
glutInitWindowPosition(400, 210);
/* Open a window */
window = glutCreateWindow("Jeff Molofee's GL Code Tutorial ... NeHe '99");