how can we make spot lights - c++

i need to draw street lights, but it is illuminating the entire environment, i couldnt make it seem like a spot light. The code below is the one i tried
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0) ;
GLfloat color[4] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat position[4] = {SCREEN_W/2,SCREEN_H/2,0.0,0.0};
float LightDir[3] = {0.0f, 0.0f, -1.0f}; // towards the viewer
glLighti(GL_LIGHT0, GL_SPOT_CUTOFF, 30);
glLightfv(GL_LIGHT0, GL_POSITION, position);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, LightDir);
glLightfv(GL_LIGHT0, GL_DIFFUSE, color);

Like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <GL/glut.h>
/* Some <math.h> files do not define M_PI... */
#ifndef M_PI
#define M_PI 3.14159265
#endif
#define TWO_PI (2*M_PI)
typedef struct lightRec {
float amb[4];
float diff[4];
float spec[4];
float pos[4];
float spotDir[3];
float spotExp;
float spotCutoff;
float atten[3];
float trans[3];
float rot[3];
float swing[3];
float arc[3];
float arcIncr[3];
} Light;
static int useSAME_AMB_SPEC = 1;
/* *INDENT-OFF* */
static float modelAmb[4] = {0.2, 0.2, 0.2, 1.0};
static float matAmb[4] = {0.2, 0.2, 0.2, 1.0};
static float matDiff[4] = {0.8, 0.8, 0.8, 1.0};
static float matSpec[4] = {0.4, 0.4, 0.4, 1.0};
static float matEmission[4] = {0.0, 0.0, 0.0, 1.0};
/* *INDENT-ON* */
#define NUM_LIGHTS 3
static Light spots[] =
{
{
{0.2, 0.0, 0.0, 1.0}, /* ambient */
{0.8, 0.0, 0.0, 1.0}, /* diffuse */
{0.4, 0.0, 0.0, 1.0}, /* specular */
{0.0, 0.0, 0.0, 1.0}, /* position */
{0.0, -1.0, 0.0}, /* direction */
{20.0},
{60.0}, /* exponent, cutoff */
{1.0, 0.0, 0.0}, /* attenuation */
{0.0, 1.25, 0.0}, /* translation */
{0.0, 0.0, 0.0}, /* rotation */
{20.0, 0.0, 40.0}, /* swing */
{0.0, 0.0, 0.0}, /* arc */
{TWO_PI / 70.0, 0.0, TWO_PI / 140.0} /* arc increment */
},
{
{0.0, 0.2, 0.0, 1.0}, /* ambient */
{0.0, 0.8, 0.0, 1.0}, /* diffuse */
{0.0, 0.4, 0.0, 1.0}, /* specular */
{0.0, 0.0, 0.0, 1.0}, /* position */
{0.0, -1.0, 0.0}, /* direction */
{20.0},
{60.0}, /* exponent, cutoff */
{1.0, 0.0, 0.0}, /* attenuation */
{0.0, 1.25, 0.0}, /* translation */
{0.0, 0.0, 0.0}, /* rotation */
{20.0, 0.0, 40.0}, /* swing */
{0.0, 0.0, 0.0}, /* arc */
{TWO_PI / 120.0, 0.0, TWO_PI / 60.0} /* arc increment */
},
{
{0.0, 0.0, 0.2, 1.0}, /* ambient */
{0.0, 0.0, 0.8, 1.0}, /* diffuse */
{0.0, 0.0, 0.4, 1.0}, /* specular */
{0.0, 0.0, 0.0, 1.0}, /* position */
{0.0, -1.0, 0.0}, /* direction */
{20.0},
{60.0}, /* exponent, cutoff */
{1.0, 0.0, 0.0}, /* attenuation */
{0.0, 1.25, 0.0}, /* translation */
{0.0, 0.0, 0.0}, /* rotation */
{20.0, 0.0, 40.0}, /* swing */
{0.0, 0.0, 0.0}, /* arc */
{TWO_PI / 50.0, 0.0, TWO_PI / 100.0} /* arc increment */
}
};
static void
usage(char *name)
{
printf("\n");
printf("usage: %s [options]\n", name);
printf("\n");
printf(" Options:\n");
printf(" -geometry Specify size and position WxH+X+Y\n");
printf(" -lm Toggle lighting(SPECULAR and AMBIENT are/not same\n");
printf("\n");
#ifndef EXIT_FAILURE /* should be defined by ANSI C <stdlib.h> */
#define EXIT_FAILURE 1
#endif
exit(EXIT_FAILURE);
}
static void
initLights(void)
{
int k;
for (k = 0; k < NUM_LIGHTS; ++k) {
int lt = GL_LIGHT0 + k;
Light *light = &spots[k];
glEnable(lt);
glLightfv(lt, GL_AMBIENT, light->amb);
glLightfv(lt, GL_DIFFUSE, light->diff);
if (useSAME_AMB_SPEC)
glLightfv(lt, GL_SPECULAR, light->amb);
else
glLightfv(lt, GL_SPECULAR, light->spec);
glLightf(lt, GL_SPOT_EXPONENT, light->spotExp);
glLightf(lt, GL_SPOT_CUTOFF, light->spotCutoff);
glLightf(lt, GL_CONSTANT_ATTENUATION, light->atten[0]);
glLightf(lt, GL_LINEAR_ATTENUATION, light->atten[1]);
glLightf(lt, GL_QUADRATIC_ATTENUATION, light->atten[2]);
}
}
static void
aimLights(void)
{
int k;
for (k = 0; k < NUM_LIGHTS; ++k) {
Light *light = &spots[k];
light->rot[0] = light->swing[0] * sin(light->arc[0]);
light->arc[0] += light->arcIncr[0];
if (light->arc[0] > TWO_PI)
light->arc[0] -= TWO_PI;
light->rot[1] = light->swing[1] * sin(light->arc[1]);
light->arc[1] += light->arcIncr[1];
if (light->arc[1] > TWO_PI)
light->arc[1] -= TWO_PI;
light->rot[2] = light->swing[2] * sin(light->arc[2]);
light->arc[2] += light->arcIncr[2];
if (light->arc[2] > TWO_PI)
light->arc[2] -= TWO_PI;
}
}
static void
setLights(void)
{
int k;
for (k = 0; k < NUM_LIGHTS; ++k) {
int lt = GL_LIGHT0 + k;
Light *light = &spots[k];
glPushMatrix();
glTranslatef(light->trans[0], light->trans[1], light->trans[2]);
glRotatef(light->rot[0], 1, 0, 0);
glRotatef(light->rot[1], 0, 1, 0);
glRotatef(light->rot[2], 0, 0, 1);
glLightfv(lt, GL_POSITION, light->pos);
glLightfv(lt, GL_SPOT_DIRECTION, light->spotDir);
glPopMatrix();
}
}
static void
drawLights(void)
{
int k;
glDisable(GL_LIGHTING);
for (k = 0; k < NUM_LIGHTS; ++k) {
Light *light = &spots[k];
glColor4fv(light->diff);
glPushMatrix();
glTranslatef(light->trans[0], light->trans[1], light->trans[2]);
glRotatef(light->rot[0], 1, 0, 0);
glRotatef(light->rot[1], 0, 1, 0);
glRotatef(light->rot[2], 0, 0, 1);
glBegin(GL_LINES);
glVertex3f(light->pos[0], light->pos[1], light->pos[2]);
glVertex3f(light->spotDir[0], light->spotDir[1], light->spotDir[2]);
glEnd();
glPopMatrix();
}
glEnable(GL_LIGHTING);
}
static void
drawPlane(int w, int h)
{
int i, j;
float dw = 1.0 / w;
float dh = 1.0 / h;
glNormal3f(0.0, 0.0, 1.0);
for (j = 0; j < h; ++j) {
glBegin(GL_TRIANGLE_STRIP);
for (i = 0; i <= w; ++i) {
glVertex2f(dw * i, dh * (j + 1));
glVertex2f(dw * i, dh * j);
}
glEnd();
}
}
int spin = 0;
void
display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRotatef(spin, 0, 1, 0);
aimLights();
setLights();
glPushMatrix();
glRotatef(-90.0, 1, 0, 0);
glScalef(1.9, 1.9, 1.0);
glTranslatef(-0.5, -0.5, 0.0);
drawPlane(16, 16);
glPopMatrix();
drawLights();
glPopMatrix();
glutSwapBuffers();
}
void
animate(void)
{
spin += 0.5;
if (spin > 360.0)
spin -= 360.0;
glutPostRedisplay();
}
void
visibility(int state)
{
if (state == GLUT_VISIBLE) {
glutIdleFunc(animate);
} else {
glutIdleFunc(NULL);
}
}
int
main(int argc, char **argv)
{
int i;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
/* process commmand line args */
for (i = 1; i < argc; ++i) {
if (!strcmp("-lm", argv[i])) {
useSAME_AMB_SPEC = !useSAME_AMB_SPEC;
} else {
usage(argv[0]);
}
}
glutCreateWindow("GLUT spotlight swing");
glutDisplayFunc(display);
glutVisibilityFunc(visibility);
glMatrixMode(GL_PROJECTION);
glFrustum(-1, 1, -1, 1, 2, 6);
glMatrixMode(GL_MODELVIEW);
glTranslatef(0.0, 0.0, -3.0);
glRotatef(45.0, 1, 0, 0);
glEnable(GL_LIGHTING);
glEnable(GL_NORMALIZE);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, modelAmb);
glLightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
glMaterialfv(GL_FRONT, GL_AMBIENT, matAmb);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matDiff);
glMaterialfv(GL_FRONT, GL_SPECULAR, matSpec);
glMaterialfv(GL_FRONT, GL_EMISSION, matEmission);
glMaterialf(GL_FRONT, GL_SHININESS, 10.0);
initLights();
glutMainLoop();
return 0;
}

Related

OpenGL Light doesn't show up in my program

I want to make a simple OpenGL Program with a sun shining. Here is my code so far:
void init() {
glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
glClear (GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
// Lighting set up
glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
// Set lighting intensity and color
GLfloat qaAmbientLight[] = {0.2, 0.2, 0.2, 1.0};
GLfloat qaDiffuseLight[] = {0.8, 0.8, 0.8, 1.0};
GLfloat qaSpecularLight[] = {1.0, 1.0, 1.0, 1.0};
glLightfv(GL_LIGHT0, GL_AMBIENT, qaAmbientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, qaDiffuseLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, qaSpecularLight);
// Set the light position
GLfloat qaLightPosition[] = {0, 0.758, 0.0, 1.0};
glLightfv(GL_LIGHT0, GL_POSITION, qaLightPosition);
}
void drawSun() {
glClear(GL_COLOR_BUFFER_BIT);
// Set material properties
GLfloat Yellow[] = {1.0, 0.647, 0.0, 1.0};
GLfloat White[] = {1.0, 1.0, 1.0, 1.0};
glMaterialfv(GL_FRONT, GL_AMBIENT, Yellow);
glMaterialfv(GL_FRONT, GL_DIFFUSE, Yellow);
glMaterialfv(GL_FRONT, GL_SPECULAR, White);
glMaterialf(GL_FRONT, GL_SHININESS, 60.0);
float x, y;
float radius = 0.095f;
float x_mid = 0;
float y_mid = 0.758;
glBegin(GL_POLYGON);
x = x_mid + (float)radius * cos(359 * PI / 180.0f);
y = y_mid + (float)radius * sin(359 * PI / 180.0f);
for (int j = 0; j < 360; j++)
{
glVertex2f(x, y);
x = x_mid + (float)radius * cos(j * PI / 180.0f);
y = y_mid + (float)radius * sin(j * PI / 180.0f);
glVertex2f(x, y);
}
glEnd();
}
But the sun doesn't shine, it looks like this :
I want to know if I make some mistakes in my code, any helps will be appreciated.
Shouldn't you call glColorMaterial to enable GL_SPECULAR that is off by default?
Also you are completely skipping all the error checking.

How to change colors in OpenGL?

I want to color every part of the coordinate plane in different color (y in red, x in blue, z in green) but it seems that he remembers only the color of the lighting(red). So if someone can help me with that problem, i would be grateful.
#include <GL/glut.h>
//func
void setCamera();
void drawCP();
void drawPoints();
void drawCrtice();
GLfloat light_diffuse[] = {1.0, 0.0, .0, 0.0}; /* Red diffuse light. */
GLfloat light_position[] = {0.0, 0.0, 1.0, 0.0}; /* Infinite light location. */
GLfloat n[6][3] = {/* Normals for the 6 faces of a cube. */
{-1.0, 0.0, 0.0},
{0.0, 1.0, 0.0},
{1.0, 0.0, 0.0},
{0.0, -1.0, 0.0},
{0.0, 0.0, 1.0},
{0.0, 0.0, -1.0}
};
GLint faces[6][4] = {/* Vertex indices for the 6 faces of a cube. */
{0, 1, 2, 3},
{3, 2, 6, 7},
{7, 6, 5, 4},
{4, 5, 1, 0},
{5, 6, 2, 1},
{7, 4, 0, 3}
};
GLfloat v[8][3]; /* Will be filled in with X,Y,Z vertexes. */
void
drawBox(void) {
int i;
for (i = 0; i < 6; i++) {
glBegin(GL_QUADS);
glNormal3fv(&n[i][0]);
glVertex3fv(&v[faces[i][0]][0]);
glVertex3fv(&v[faces[i][1]][0]);
glVertex3fv(&v[faces[i][2]][0]);
glVertex3fv(&v[faces[i][3]][0]);
glEnd();
}
}
void
display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
drawBox();
drawCP();
glClearColor(0.6f, 0.6f, 0.6f, 1.0f);
glRotatef(0.2, 0, 5, 0);
glutPostRedisplay();
glutSwapBuffers();}
void
init(void) {
/* Setup cube vertex data. */
v[0][0] = v[1][0] = v[2][0] = v[3][0] = -1;
v[4][0] = v[5][0] = v[6][0] = v[7][0] = 1;
v[0][1] = v[1][1] = v[4][1] = v[5][1] = -1;
v[2][1] = v[3][1] = v[6][1] = v[7][1] = 1;
v[0][2] = v[3][2] = v[4][2] = v[7][2] = 1;
v[1][2] = v[2][2] = v[5][2] = v[6][2] = -1;
/* Enable a single OpenGL light. */
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
/* Use depth buffering for hidden surface elimination. */
glEnable(GL_DEPTH_TEST);
setCamera();}
void drawLines() {
glBegin(GL_LINES);
for (int i = 10; i != 0; i--) {
// po x osi
glVertex2f(-i, -0.1);
glVertex2f(-i, 0.1);
glVertex2f(i, -0.1);
glVertex2f(i, 0.1);
//po y osi
glVertex2f(-0.1, -i);
glVertex2f(0.1, -i);
glVertex2f(-0.1, i);
glVertex2f(0.1, i);
//po z osi
glColor3f(1.0f, 0.0f, 0.0f); //seems it doesnt register
glVertex3f(0, -0.3, -i);
glVertex3f(0, 0.3, -i);
glVertex3f(0, -0.3, i);
glVertex3f(0, 0.3, i);}
glEnd();}
void drawCP() {
//drawPoints();
drawLines();
glBegin(GL_LINES);
glLineWidth(100);
glVertex2f(-10.0f, 0);
glVertex2f(10.0f, 0);
glVertex2f(0.0f, 10);
glVertex2f(0.0f, -10);
glEnd();
}
void setCamera() {
/* Setup the view of the cube. */
glMatrixMode(GL_PROJECTION);
gluPerspective(/* field of view in degree */ 50.0, /* aspect ratio */ 1, /* Z near */ 1, /* Z far */ 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
/* Setup the view of the cube. */
gluLookAt(0, 0, 10 /*pozicija kamere*/, 0, 0, 0/* u koju tocku gleda ta kamera*/, 0, 1, 0 /*moran jos viti ca je to*/);
}
int
main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("red 3D lighted cube");
glutDisplayFunc(display);
init();
// glutTimerFunc(10, rotate, 10);
glutMainLoop();
return 0; /* ANSI C requires main to return int. */
}
Try adding in: glEnable(GL_COLOR_MATERIAL);. That tells OpenGL your material properties are going to be defined with the glColor() function, it can be expanded on but that should get you what you expect.

OpenGL analog clock doesn't show correctly?

I've a problem with my clock. In the first 10 minutes the hour hand doesn't show the correct hour and it changes when the minute hand's change but after 10 minutes it shows correctly. Could you please help me to fix it?
I think there is a problem in these lines but I am not sure:
static void TimeEvent(int te)
{
rx = 30 * cos( angle );
ry = 30 * sin( angle );
rz = 30 * cos( angle );
angle += 0.01;
if (angle > M_TWOPI) angle = 0;
glutPostRedisplay();
glutTimerFunc( 100, TimeEvent, 1);
}
Here is my whole code:
#include <GL/glut.h>
#include <gl/gl.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
GLUquadricObj *Cylinder;
struct tm *newtime;
time_t l_time;
int M_TWOPI=0;
GLfloat rx, ry, rz, angle;
GLfloat LightAmbient[]= { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat LightDiffuse[]= { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat LightPosition[]= { 5.0f, 25.0f, 15.0f, 1.0f };
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
static int view_state = 1; // Ortho view = 1, Perspective = 0
void newLine(float Start, float End, float angle){
float c = cos(angle), s = sin(angle);
glVertex2f( -8.0f*Start*c, -8.0f*Start*s);
glVertex2f( -8.0f*End*c, -8.0f*End*s);
}
void Sprint( float x, float y, char *st)
{
int l,i;
l=strlen( st );
glRasterPos3f( x, y, -1);
for( i=0; i < l; i++)
{
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, st[i]);
}
}
static void TimeEvent(int te)
{
rx = 30 * cos( angle );
ry = 30 * sin( angle );
rz = 30 * cos( angle );
angle += 0.01;
if (angle > M_TWOPI) angle = 0;
glutPostRedisplay();
glutTimerFunc( 100, TimeEvent, 1);
}
void Draw_clock( GLfloat cx, GLfloat cy, GLfloat cz )
{
int hour_ticks , sec_ticks;
glPushMatrix();
glTranslatef(cx,cy,cz);
glRotatef(180, 1.0, 0.0, 0.0);
glPushMatrix();
glColor3f(1.0, 0.5, 0.0);
glTranslatef( 0, 0, 0.0);
glRotatef((360/12) * newtime->tm_hour + (360/60) * (60 / (newtime->tm_min+1)), 0.0, 0.0, 1.0);
glPushMatrix();
glTranslatef(0.0, 0.0, 2.0);
glPopMatrix();
glRotatef(90, 1.0, 0.0, 0.0);
gluCylinder(Cylinder, 0.75, 0, 4, 16, 16);
glPopMatrix();
glPushMatrix();
glColor3f(1.0, 0.0, 1.0);
glTranslatef( 0, 0, 0.0);
glRotatef( (360/60) * newtime->tm_min, 0.0, 0.0, 1.0);
glPushMatrix();
glTranslatef(0.0, 0.0, 3.0);
glScalef(0.5, 0.5, 1.0);
glPopMatrix();
glRotatef( 90, 1.0, 0.0, 0.0);
glutSolidCone (0.5, 6, 6, 16);
glPopMatrix();
glPushMatrix();
glColor3f(1.0, 0.0, 0.0);
glTranslatef( 0, 0, -0.0);
glRotatef( (360/60) * newtime->tm_sec, 0.0, 0.0, 1.0);
glPushMatrix();
glTranslatef(0.0, 0.0, 4.0);
glScalef(0.25, 0.25, 1.0);
glPopMatrix();
glRotatef( 90, 1.0, 0.0, 0.0);
gluCylinder(Cylinder, 0.25, 0, 6, 16, 16);
glPopMatrix();
for(hour_ticks = 0; hour_ticks < 12; hour_ticks++)
{
glPushMatrix();// Draw next arm axis.
glColor3f(0.0, 1.0, 1.0); // give it a color
glTranslatef(0.0, 0.0, 0.0);
glRotatef( (360/12) * hour_ticks, 0.0, 0.0, 1.0);
glTranslatef( 6.0, 0.0, 0.0);
glEnable(GL_LINE_SMOOTH);
glBegin(GL_LINES);
newLine(0.08f, 0.2f, 0.0f);
glEnd();
glPopMatrix();
}
for(sec_ticks = 0; sec_ticks < 60; sec_ticks++)
{
glPushMatrix();
glTranslatef(0.0, 0.0, 0.0);
glRotatef( (360/60) * sec_ticks, 0.0, 0.0, 1.0);
glTranslatef(6.0, 0.0, 0.0);
glutSolidCone(1.0, 2.0, 3, 4);
glPopMatrix();
}
glPopMatrix();
}
void num()
{
glColor3f( 0.0, 0.0, 1.0);
Sprint(-6.2,-0.2,"9"); //counting from center
Sprint(-0.2,-6.2,"6");
Sprint(2.8,-5.5,"5");
Sprint(5.0,-3.2,"4");
Sprint(5.0,+2.8,"2");
Sprint(2.8,+5.0,"1");
Sprint(-3.33,+4.95,"11");
Sprint(-0.2,-6.2,"6");
Sprint(-3.2,-5.45,"7");
Sprint(-0.4,5.7,"12");
Sprint(-5.35,-3.25,"8");
Sprint(-5.55,+2.8,"10");
Sprint(5.8,-0.2,"3");
}
void display_clock()
{
time(&l_time); // Get time
newtime = localtime(&l_time); // Convert to local time
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// easy way to put text on the screen.
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(-8.0, 8.0, -8.0, 8.0, 1.0, 60.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glDisable(GL_LIGHTING);
glDisable(GL_COLOR_MATERIAL);
glColor3f( 1.0, 1.0, 1.0);
Sprint(-2, 3, " Clock");
Draw_clock( 0.0, 0.0, -14.0);
num();
glutSwapBuffers();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
display_clock();
glFlush();
}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (50, 50);
glutCreateWindow (argv[0]);
glutSetWindowTitle("Clock");
Cylinder = gluNewQuadric();
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc( 10, TimeEvent, 1);
glutMainLoop();
return 0;
}
You are doing integer arithmetic:
glRotatef((360/12) * newtime->tm_hour + (360/60) * (60 / (newtime->tm_min+1)), 0.0, 0.0, 1.0);
should be:
glRotatef(30.0 * newtime->tm_hour + 6.0) * (60.0 / (newtime->tm_min+1)), 0.0, 0.0, 1.0);
and so on.
Though, as 360/12 and 360/60 are constants you might as well replace them with the calculated values.
I believe your problem is here:
glRotatef((360/12) * newtime->tm_hour + (360/60) * (60 / (newtime->tm_min+1)), 0.0, 0.0, 1.0);
It must be:
glRotatef((360/12) * newtime->tm_hour + (360.0/12 / 60 ) * (newtime->tm_min+1), 0.0, 0.0, 1.0);

Moving a 3D object with a mouse in OpenGL

I have been trying to make a whale in OpenGL. We have been trying to move the whale using a mouse input. The problem we are facing is with the syntax as well as the logic. We understand that the whale we are making is in a 3 dimensional world whereas the mouse input in in 2D for x and y on the screen.
If someone can be very specific with the syntax of using mouse input relevant to our code, that'd be great!
#include <Windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <stdlib.h>
#include "glut.h"
#include <math.h>
GLUquadric *qobja;
float movez = 0;
#define W_SCREEN 1366
#define H_SCREEN 768
#define TERR_D 75
#define TERR_W 750
#define pi 3.14
int mou_x = 0, mou_y = 0;
float alpha = 0;
float fahad = 0;
GLfloat ctrlpoints[4][4][3] = {
{ { -1.5, -1.5, 4.0 },
{ -0.5, -1.5, 2.0 },
{ 0.5, -1.5, -1.0 },
{ 1.5, -1.5, 2.0 } },
{ { -1.5, -0.5, 1.0 },
{ -0.5, -0.5, 3.0 },
{ 0.5, -0.5, 0.0 },
{ 1.5, -0.5, -1.0 } },
{ { -1.5, -1.5, 4.0 },
{ -0.5, -1.5, 2.0 },
{ 0.5, -1.5, -1.0 },
{ 1.5, -1.5, 2.0 } },
{ { -1.5, 1.5, -2.0 },
{ -0.5, 1.5, -2.0 },
{ 0.5, 1.5, 0.0 },
{ 1.5, 1.5, -1.0 } }
};
float cam_xrot = 180, cam_yrot = 180, cam_zrot = 0;
GLfloat no_mat[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat mat_ambient[] = { 0.7, 0.7, 0.7, 1.0 };
GLfloat mat_ambient_color[] = { 0.8, 0.8, 0.2, 1.0 };
GLfloat mat_diffuse[] = { 0.1, 0.5, 0.8, 1.0 };
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat no_shininess[] = { 0.0 };
GLfloat low_shininess[] = { 5.0 };
GLfloat high_shininess[] = { 100.0 };
GLfloat mat_emission[] = { 0.3, 0.2, 0.2, 0.0 };
// Function Prototypes ////////////////////////////////////
void drawTerrain();
void drawAxes();
//drawing function decs
void draw_frust(float inner, float outer, float height, float m_z);
void cleanup();
void camera();
void pyramid();
void draw_closed_cyl(float a, float b, float c);
void draw_whale();
/* Initialize z-buffer, projection matrix, light source,
* and lighting model. Do not specify a material property here.
*/
void initLight(void)
{
GLfloat ambient[] = { 0.2, 0.2, 0.2, 1.0 };
GLfloat position[] = { 0.0, 0.0, 2.0, 1.0 };
GLfloat mat_diffuse[] = { 0.6, 0.6, 0.6, 1.0 };
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 50.0 };
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
glLightfv(GL_LIGHT0, GL_POSITION, position);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
}
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4,
0, 1, 12, 4, &ctrlpoints[0][0][0]);
glEnable(GL_MAP2_VERTEX_3);
glEnable(GL_AUTO_NORMAL);
glMapGrid2f(20, 0.0, 1.0, 20, 0.0, 1.0);
initLight();
}
///////////////////////////////////////////////////////////
void display(void)
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
camera();
drawAxes();
qobja = gluNewQuadric();
gluQuadricNormals(qobja, GLU_SMOOTH);
glPushMatrix();
glTranslatef(0, 0, movez);
draw_whale();
glPopMatrix();
}
void draw_whale()
{
glPushMatrix();
glTranslatef(0, 2, 90);
glRotatef(180, 0.0, 0.0, 1.0);
glScalef(1, 1, 2);
glEvalMesh2(GL_FILL, 0, 25, 0, 25);
glPopMatrix();
glPushMatrix();
float count = 5;
float a = 5;
for (float i = 0; i <= 16; i = i + 1)
{
draw_frust(a*sqrt(i), a*sqrt((i + 1)), 2, count * 2);
count++;
alpha = i + 1;
}
//a few smooth runs
for (int j = 0; j < 3; j++)
{
draw_frust(a*sqrt(alpha), a*sqrt(alpha), 2, count * 2);
count++;
}
float co = count;
for (float i = 17; i >= 3; i = i - 1)
{
draw_frust(a*sqrt(i), a*sqrt(i - 1), 2, co * 2);
co++;
fahad = a*sqrt(i - 1);
}
draw_frust(7.07, 5, 2, co * 2);
co++;
draw_frust(5, 3, 2, co * 2);
co++;
draw_frust(3, 1, 2, co * 2);
glPushMatrix();
//fin left
glPushMatrix();
glTranslatef(-25.5, 10, 25);
glRotatef(100, 1, 0, 0);
glRotatef(135, 0, 1, 0);
glRotatef(45, 0, 0, 1);
glScalef(1, 6, 1);
draw_closed_cyl(0, 0, 0);
glPopMatrix();
//fin right
glPushMatrix();
glTranslatef(25.5, 10, 25);
glRotatef(100, 1, 0, 0);
glRotatef(-135, 0, 1, 0);
glRotatef(-45, 0, 0, 1);
glScalef(1, 6, 1);
draw_closed_cyl(0, 0, 0);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void draw_frust(float inner, float outer, float height, float m_z)
{
qobja = gluNewQuadric();
gluQuadricNormals(qobja, GLU_SMOOTH);
glPushMatrix();
glTranslatef(0, 0, m_z);
gluDisk(qobja, 0.0, inner, 200, 20);
gluCylinder(qobja, inner, outer, height, 200, 200);
glPopMatrix();
}
void draw_closed_cyl(float a, float b, float c)
{
qobja = gluNewQuadric();
gluQuadricNormals(qobja, GLU_SMOOTH);
glPushMatrix();
glTranslatef(a, b + 2, c);
glRotatef(120, 0, 0, 1);
gluCylinder(qobja, 3.0, 3.0, 2.0, 20, 20);
gluDisk(qobja, 0, 3.0, 20, 20);
glTranslatef(0, 0, 2.0);
gluDisk(qobja, 0, 3.0, 20, 20);
glPopMatrix();
}
void drawTerrain(){
GLfloat color[] = { 0.2, 0.8, 0.2 };
glMaterialfv(GL_FRONT, GL_AMBIENT, color);
glColor3f(0.2, 0.8, 0.2); // this line is not needed when lighting in enabled
glPushMatrix();
glTranslatef(-TERR_W / 2, 0.0, -TERR_D / 2);
glBegin(GL_POLYGON);
glVertex3f(0, 0, 0);
glVertex3f(TERR_W, 0, 0);
glVertex3f(TERR_W, 0, TERR_D);
glVertex3f(0, 0, TERR_D);
glVertex3f(0, 0, 0);
glEnd();
glPopMatrix();
}
void drawAxes(){
glColor3d(1, 0, 0);
glBegin(GL_LINES);
glVertex3f(0, 0, 0);
glVertex3f(3, 0, 0);
glEnd();
glColor3d(0, 1, 0);
glBegin(GL_LINES);
glVertex3f(0, 0, 0);
glVertex3f(0, 3, 0);
glEnd();
glColor3d(0, 0, 1);
glBegin(GL_LINES);
glVertex3f(0, 0, 0);
glVertex3f(0, 0, 3);
glEnd();
}
///////////////////////////////////////////////////////////
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(1.5*60.0, (GLfloat)w / (GLfloat)h, 1, 1.5*120.0);
glMatrixMode(GL_MODELVIEW);
camera();
}
///////////////////////////////////////////////////////////
void camera(){
glLoadIdentity();
glTranslatef(0, 0, -110);
glRotatef(cam_xrot, 1, 0, 0);
glRotatef(cam_yrot, 0, 1, 0);
glRotatef(cam_zrot, 0, 0, 1);
}
///////////////////////////////////////////////////////////
void keyboard(unsigned char key, int x, int y)
{
// Camera controls - Rotation along principle axis
switch (key) {
case 'n':
if (movez <= -100)
movez = movez + 100;
else
movez = movez - 3;
break;
case 'q':
cam_xrot += 10;
if (cam_xrot >360) cam_xrot -= 360;
break;
case 'z':
cam_xrot -= 10;
if (cam_xrot < -360) cam_xrot += 360;
break;
case 'a':
cam_yrot += 10;
if (cam_yrot >360) cam_yrot -= 360;
break;
case 'd':
cam_yrot -= 10;
if (cam_yrot < -360) cam_yrot += 360;
break;
case 'w':
cam_zrot += 10;
if (cam_zrot >360) cam_zrot -= 360;
break;
case 'x':
cam_zrot -= 10;
if (cam_zrot < -360) cam_zrot += 360;
break;
case 27:
cleanup();
exit(0);
break;
default:
break;
}
glutPostRedisplay();
}
///////////////////////////////////////////////////////////
void cleanup() // call once when you exit program
{
}
///////////////////////////////////////////////////////////
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(W_SCREEN, H_SCREEN);
glutInitWindowPosition(0, 0);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc(25, update, 0);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}

Glowing particles opengl

My particles are not glowing. I searched on the internet that its been done with
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
This piece of code changes my particles, but it is not glowing. Can anybody see what is wrong in my code? Here are the relevant codes:
void init (void) {
glEnable (GL_LIGHTING); //enable the lighting
glEnable (GL_LIGHT0); //enable LIGHT0, our Diffuse Light
glEnable (GL_LIGHT1); //enable LIGHT1, our Ambient Light
glEnable(GL_BLEND);
// glEnable(GL_DEPTH);
// glEnable(GL_COLOR_MATERIAL);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
}
void update()
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(g_orientation, 0.0, 1.0, 0.0); // rotate in y axis
glTranslatef(-g_posX, -g_posY, -g_posZ);
glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_DEPTH_BUFFER_BIT);
GLfloat DiffuseLight[] = {dlr, dlg, dlb}; //set DiffuseLight[] to the specified values
GLfloat AmbientLight[] = {alr, alg, alb}; //set AmbientLight[] to the specified values
glLightfv (GL_LIGHT0, GL_DIFFUSE, DiffuseLight); //change the light accordingly
glLightfv (GL_LIGHT1, GL_AMBIENT, AmbientLight); //change the light accordingly
GLfloat LightPosition[] = {lx, ly, lz, lw}; //set the LightPosition to the specified values
glLightfv (GL_LIGHT0, GL_POSITION, LightPosition); //change the light accordingly
glColorMaterial(GL_FRONT,GL_DIFFUSE);
drawRockets();
if(explosion == true){
drawParticles();
}
drawMolen();
glutSwapBuffers();
if(rotate==1)
angle++;
}
//commonly used material values
GLfloat no_mat[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat default_ambient[] = {0.2, 0.2, 0.2, 1.0};
GLfloat mat_ambient[] = { 0.7, 0.7, 0.7, 1.0 };
GLfloat mat_ambient_color[] = { 0.8, 0.8, 0.2, 1.0 };
GLfloat mat_diffuse[] = { 0.1, 0.5, 0.8, 1.0 };
GLfloat default_diffuse[] = {0.8, 0.8, 0.8, 1.0};
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat no_shininess[] = { 0.0 };
GLfloat low_shininess[] = { 5.0 };
GLfloat high_shininess[] = { 100.0 };
GLfloat mat_emission[] = {0.3, 0.5, 0.9, 0.0};
GLfloat mat_emission1[] = {0.0, 1.0, 0.2, 1.0};
GLfloat mat_emission2[] = {0.0, 0.0, 1.0, 0.0};
GLfloat mat_emission3[] = {1.0, 1.0, 0.0, 0.0};
Here is another example how I want my glutsolidspheres to be:
Ball of Light (Light, Material?) glutsolidsphere