Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Rotation amounts
- static GLfloat xRot = 0.0f;
- static GLfloat yRot = 0.0f;
- // Light values and coordinates
- GLfloat lightPos[] = { 0.0f, 3.0f, 0.0f, 1.0f };
- GLfloat specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
- GLfloat specref[] = { 1.0f, 1.0f, 1.0f, 1.0f };
- GLfloat ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
- GLfloat spotDir[] = { 0.0f, 0.0f, -1.0f };
- // put inside a function to use in the main game loop or just paste into the main game loop
- // First place the light
- // Save the coordinate transformation
- glPushMatrix();
- // Rotate coordinate system
- glRotatef(yRot, 0.0f, 1.0f, 0.0f);
- glRotatef(xRot, 1.0f, 0.0f, 0.0f);
- // Specify new position and direction in rotated coords.
- glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
- glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spotDir);
- // Draw a red cone to enclose the light source
- glColor3f(1, 0, 0);
- // Translate origin to move the cone out to where the light
- // is positioned.
- glTranslatef(lightPos[0], lightPos[1], lightPos[2]);
- //glutSolidCone(0.5f, 3.0f, 4, 4);
- GLUquadric *quad;
- quad = gluNewQuadric();
- gluSphere(quad, 0.5, 100, 20);
- // Draw a smaller displaced sphere to denote the light bulb
- // Save the lighting state variables
- glPushAttrib(GL_LIGHTING_BIT);
- // Turn off lighting and specify a bright yellow sphere
- glDisable(GL_LIGHTING);
- glColor3ub(255, 255, 0);
- glRotated(180, 0, 0, 0);
- GLUquadricObj *theQuadric;
- theQuadric = gluNewQuadric();
- gluQuadricDrawStyle(theQuadric, GLU_FILL);
- gluQuadricNormals(theQuadric, GLU_SMOOTH);
- gluCylinder(theQuadric, 0.8, 0, 1, 100, 20);
- gluDeleteQuadric(theQuadric);
- //glutSolidSphere(0.5f, 7.5, 7.5);
- // Restore lighting state variables
- glPopAttrib();
- // Restore coordinate transformations
- glPopMatrix();
- // Set material color and draw a sphere in the middle
- glColor3f(1, 1, 1);
- glEnable(GL_DEPTH_TEST); // Hidden surface removal
- glFrontFace(GL_CCW); // Counter clock-wise polygons face out
- //glEnable(GL_CULL_FACE); // Do not try to display the back sides
- // Enable lighting
- glEnable(GL_LIGHTING);
- // Setup and enable light 0
- // Supply a slight ambient light so the objects can be seen
- glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);
- // The light is composed of just a diffuse and specular components
- glLightfv(GL_LIGHT0, GL_DIFFUSE, ambientLight);
- glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
- glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
- // Specific spot effects
- // Cut off angle is 60 degrees
- glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 15.0f);
- // Enable this light in particular
- glEnable(GL_LIGHT0);
- // Enable color tracking
- glEnable(GL_COLOR_MATERIAL);
- // Set Material properties to follow glColor values
- glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
- // All materials hereafter have full specular reflectivity
- // with a high shine
- glMaterialfv(GL_FRONT, GL_SPECULAR, specref);
- glMateriali(GL_FRONT, GL_SHININESS, 128);
- // Black background
- glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement