Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- #include <GL/glut.h>
- static float lightHeight = 10;
- GLfloat angle = -150; /* in degrees */
- GLfloat angle2 = 30; /* in degrees */
- int moving, startx, starty;
- static GLfloat lightPosition[4];
- static void makeFloorTexture(void)
- {
- #define TEXSIZE 256
- GLubyte floorTexture[TEXSIZE][TEXSIZE][3];
- int s, t;
- /* Setup RGB image for the texture. */
- for (t = 0; t < TEXSIZE; t++)
- {
- for (s = 0; s < TEXSIZE; s++)
- {
- int red = (t * (256/TEXSIZE)) % 256;
- int green = (s * (256/TEXSIZE)) % 256;
- int blue = (int)(((t*s) * (1./TEXSIZE)) + 0.5) % 256;
- floorTexture[t][s][0] = red;
- floorTexture[t][s][1] = blue;
- floorTexture[t][s][2] = green;
- }
- }
- glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
- 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);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, TEXSIZE, TEXSIZE, 0, GL_RGB, GL_UNSIGNED_BYTE, floorTexture);
- }
- static GLfloat floorVertices[4][3] =
- {
- { -20.0, 0.0, 20.0 },
- { 20.0, 0.0, 20.0 },
- { 20.0, 0.0, -20.0 },
- { -20.0, 0.0, -20.0 },
- };
- static void glVertex3fvScale(GLfloat x, GLfloat y, GLfloat radius)
- {
- GLfloat out[3];
- out[0] = x*radius*2 - radius;
- out[1] = 0;
- out[2] = y*radius*2 - radius;
- glVertex3fv(out);
- }
- static void drawFloor(void)
- {
- GLfloat radius = 20;
- #define FLOOR_PARTS 128
- glDisable(GL_TEXTURE_2D);
- //glEnable(GL_TEXTURE_2D);
- for(int y = 0; y < FLOOR_PARTS; y++)
- for(int x = 0; x < FLOOR_PARTS; x++)
- {
- GLfloat startx = (GLfloat)(x+0) / FLOOR_PARTS;
- GLfloat endx = (GLfloat)(x+1) / FLOOR_PARTS;
- GLfloat starty = (GLfloat)(y+0) / FLOOR_PARTS;
- GLfloat endy = (GLfloat)(y+1) / FLOOR_PARTS;
- glBegin(GL_QUADS);
- glTexCoord2f (startx*8.0, starty*8.0);
- glNormal3f(0,1,0);
- glVertex3fvScale(startx , starty, radius);
- glTexCoord2f (startx*8.0, endy*8.0);
- glNormal3f(0,1,0);
- glVertex3fvScale(startx , endy, radius);
- glTexCoord2f ( endx*8.0, endy*8.0);
- glNormal3f(0,1,0);
- glVertex3fvScale( endx , endy, radius);
- glTexCoord2f ( endx*8.0, starty*8.0);
- glNormal3f(0,1,0);
- glVertex3fvScale( endx , starty, radius);
- glEnd();
- }
- //glDisable(GL_TEXTURE_2D);
- }
- static void redraw(void)
- {
- GLfloat white[] = {1,1,1,1};
- GLfloat blue[] = {0,0,1,1};
- GLfloat red[] = {1,0,0,1};
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- lightPosition[0] = 0;
- lightPosition[1] = lightHeight;
- lightPosition[2] = 0;
- lightPosition[3] = 0;
- glLightfv(GL_LIGHT0, GL_DIFFUSE, white);
- glLightfv(GL_LIGHT0, GL_SPECULAR, white);
- glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 1.0);
- glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.5);
- glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.5);
- glEnable(GL_LIGHT0);
- glPushMatrix();
- /* Perform scene rotations based on user mouse input. */
- glRotatef(angle2, 1.0, 0.0, 0.0);
- glRotatef(angle, 0.0, 1.0, 0.0);
- glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
- glPushMatrix();
- glTranslatef(lightPosition[0],lightPosition[1],lightPosition[2]);
- glutSolidSphere(1,30,30);
- glPopMatrix();
- glMaterialfv(GL_FRONT, GL_DIFFUSE, red);
- glMaterialfv(GL_FRONT, GL_SPECULAR, white);
- glMateriali (GL_FRONT, GL_SHININESS, 50);
- glMaterialfv(GL_BACK, GL_DIFFUSE, blue);
- glMaterialfv(GL_BACK, GL_SPECULAR, white);
- glMateriali (GL_BACK, GL_SHININESS, 50);
- glColor3f(1.0, 1.0, 1.0);
- drawFloor();
- glPopMatrix();
- glutSwapBuffers();
- }
- static void mouse(int button, int state, int x, int y)
- {
- if (button == GLUT_LEFT_BUTTON)
- {
- if (state == GLUT_DOWN)
- {
- moving = 1;
- startx = x;
- starty = y;
- }
- if (state == GLUT_UP)
- moving = 0;
- }
- }
- static void motion(int x, int y)
- {
- if (moving)
- {
- angle = angle + (x - startx);
- angle2 = angle2 + (y - starty);
- startx = x;
- starty = y;
- glutPostRedisplay();
- }
- }
- /* Advance time varying state when idle callback registered. */
- static void idle(void)
- {
- glutPostRedisplay();
- }
- /* When not visible, stop animating. Restart when visible again. */
- static void visible(int vis)
- {
- glutIdleFunc(vis == GLUT_VISIBLE ? idle : NULL);
- }
- static void key(unsigned char c, int x, int y)
- {
- }
- static void special(int k, int x, int y)
- {
- }
- int main(int argc, char **argv)
- {
- glutInit(&argc, argv);
- glutInitWindowSize(800,800);
- glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
- glutCreateWindow("OpenGL Robot");
- /* Register GLUT callbacks. */
- glutDisplayFunc(redraw);
- glutMouseFunc(mouse);
- glutMotionFunc(motion);
- glutVisibilityFunc(visible);
- glutKeyboardFunc(key);
- glutSpecialFunc(special);
- glEnable(GL_DEPTH_TEST);
- glEnable(GL_TEXTURE_2D);
- glMatrixMode(GL_PROJECTION);
- gluPerspective(40.0, 1.0, 10.0, 100.0);
- glMatrixMode(GL_MODELVIEW);
- gluLookAt( 0.0, 8.0, 60.0, /* eye is at (0,8,60) */
- 0.0, 8.0, 0.0, /* center is at (0,8,0) */
- 0.0, 1.0, 0.); /* up is in postivie Y direction */
- glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
- glEnable(GL_LIGHTING);
- makeFloorTexture();
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement