OpenGL/GLUT Incorrect lighting for GL_LINES - c

I am asked to make a simple carousel that rotates by right and left click of mouse. For the sticks I used GL_LINES and the problem I am having is that there is no way to define normal vector or something similar as I did with GL_POLYGON to make the lighting be correct. I searched the web and I didn't find any source explaining the lighting for the GL_LINES (some people told me the lighting is automatic and I don't need to specify anything for GL_LINES), that is why I asked the questions.
Here is the screenshot of the front which show everything is fine from front:
Here is the screenshot of the back that shows the problems in lighting. In particular, the light source is in the back but still those sticks are bright.
Here is the another screenshot of the back that show the problems in lighting as well.
These are the two functions that specify horizontal and vertical sticks:
void DrawHorizontalStick(){
glLineWidth(15);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINES);
glVertex3f(0.0, 7.0, 0.0);
glVertex3f(4.0 * cos(radian__IN_RANGE), 7.0 + 4.0 * sin(radian__IN_RANGE), 0.0);
glEnd();
}
void DrawVerticalStick(){
glLineWidth(5);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINES);
glVertex3f(4.0 * cos(radian__IN_RANGE), 7.0 + 4.0 * sin(radian__IN_RANGE), 0.0);
glVertex3f(4.0 * cos(radian__IN_RANGE), 7.0 + 4.0 * sin(radian__IN_RANGE) - 1, 0.0);
glEnd();
}
Here is the complete source code:
#include <GL/glut.h>
#include <stdlib.h>
#include <Windows.h>
#include <math.h>
#include <stdio.h>
#define PI 3.14159265
#define numberOfRotationTypes 3
static GLfloat lpos[] = { 0.0, 6.0, 8.0, 1.0 };
static GLfloat black[] = { 0.0, 0.0, 0.0, 1.0 };
static GLfloat white[] = { 1.0, 1.0, 1.0, 1.0 };
static GLfloat red[] = { 1.0, 0.0, 0.0, 1.0 };
static GLfloat green[] = { 0.0, 1.0, 0.0, 1.0 };
static GLfloat blue[] = { 0.0, 0.0, 1.0, 1.0 };
static GLfloat yellow[] = { 1.0, 1.0, 0.0, 1.0 };
static GLfloat magenta[] = { 1.0, 0.0, 1.0, 1.0 };
static GLfloat cyan[] = { 0.0, 1.0, 1.0, 1.0 };
static GLfloat lightgreen[] = { 0.5, 1.0, 0.5, 1.0 };
static float alpha = 0.0;
static float beta = PI / 6.0;
static float zoom = 25.0;
static bool lightSource = true;
float numberOfTriangles = 1;
static GLdouble cpos[3];
static double fenceHeight = -0.5;
static int angle = 0;
static int angle__IN_RANGE = 0.0;
static double radian__IN_RANGE = 0.0;
static int arrayOfAnglesInRange[181];
static int id = 0;
static int speed = 0;
static int signal = 1;
static GLint window[2];
static int rotationType = 0;
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
/* since back "face" appears in wireframe mode */
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
}
void writemessage()
{
}
void processAngle(){
angle__IN_RANGE = arrayOfAnglesInRange[abs(angle) % 181];
}
void setRadian_IN_RANGE(){
radian__IN_RANGE = ((float)angle__IN_RANGE / 180) * PI;
}
void fillArray(){
int j = -45;
for (int i = 0; i < 181; i++)
{
if (i < 90)
arrayOfAnglesInRange[i] = j++;
else
arrayOfAnglesInRange[i] = j--;
}
//for (int i = 0; i < 182; i++)
//{
// printf("%d\n", arrayOfAnglesInRange[i]);
//}
}
void keepTrackOfID(){
int tempAngle = angle;
if (id % 4 == 0)
angle += 0;
else if (id % 4 == 1)
angle += 30;
else if (id % 4 == 2)
angle += 60;
else if (id % 4 == 3)
angle += 90;
processAngle();
setRadian_IN_RANGE();
angle = tempAngle;
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (GLfloat)w / (GLfloat)h, 0.01, 50.0);
glMatrixMode(GL_MODELVIEW);
}
void DrawSticksArroundYard(){
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
glMaterialfv(GL_BACK, GL_AMBIENT_AND_DIFFUSE, black);
GLUquadricObj *quadObj;
// Right-Line
glPushMatrix();
glTranslatef(6.8, 1.0 + fenceHeight, -7.0);
quadObj = gluNewQuadric();
gluCylinder(quadObj, 0.1, 0.1, 14.0, 10, 10);
glPopMatrix();
// Left-Line
glPushMatrix();
glTranslatef(-6.8, 1.0 + fenceHeight, -7.0);
quadObj = gluNewQuadric();
gluCylinder(quadObj, 0.1, 0.1, 14.0, 10, 10);
glPopMatrix();
// Back-Line
glPushMatrix();
glTranslatef(-6.8, 1.0 + fenceHeight, -7.0);
glRotatef(90, 0, 1, 0);
quadObj = gluNewQuadric();
gluCylinder(quadObj, 0.1, 0.1, 13.7, 10, 10);
glRotatef(-90, 0, 1, 0);
glPopMatrix();
// Front-Line
glPushMatrix();
glTranslatef(6.8, 1.0 + fenceHeight, 7.0);
glRotatef(-90, 0, 1, 0);
quadObj = gluNewQuadric();
gluCylinder(quadObj, 0.1, 0.1, 13.7, 10, 10);
glRotatef(90, 0, 1, 0);
glPopMatrix();
// Pin-Front-Right
glPushMatrix();
glTranslatef(6.8, 0, 7.0);
glRotatef(-90, 1, 0, 0);
quadObj = gluNewQuadric();
gluCylinder(quadObj, 0.2, 0.1, 1.3 + fenceHeight, 10, 10);
glRotatef(90, 1, 0, 0);
glPopMatrix();
// Pin-Front-Left
glPushMatrix();
glTranslatef(-6.8, 0, 7.0);
glRotatef(-90, 1, 0, 0);
quadObj = gluNewQuadric();
gluCylinder(quadObj, 0.2, 0.1, 1.3 + fenceHeight, 10, 10);
glRotatef(90, 1, 0, 0);
glPopMatrix();
// Pin-Back-Left
glPushMatrix();
glTranslatef(-6.8, 0, -7.0);
glRotatef(-90, 1, 0, 0);
quadObj = gluNewQuadric();
gluCylinder(quadObj, 0.2, 0.1, 1.3 + fenceHeight, 10, 10);
glRotatef(90, 1, 0, 0);
glPopMatrix();
// Pin-Back-Right
glPushMatrix();
glTranslatef(6.8, 0, -7.0);
glRotatef(-90, 1, 0, 0);
quadObj = gluNewQuadric();
gluCylinder(quadObj, 0.2, 0.1, 1.3 + fenceHeight, 10, 10);
glRotatef(90, 1, 0, 0);
glPopMatrix();
// Pin-Back-Center
glPushMatrix();
glTranslatef(0, 0, -7.0);
glRotatef(-90, 1, 0, 0);
quadObj = gluNewQuadric();
gluCylinder(quadObj, 0.2, 0.1, 1.3 + fenceHeight, 10, 10);
glRotatef(90, 1, 0, 0);
glPopMatrix();
// Pin-Front-Center
glPushMatrix();
glTranslatef(0, 0, 7.0);
glRotatef(-90, 1, 0, 0);
quadObj = gluNewQuadric();
gluCylinder(quadObj, 0.2, 0.1, 1.3 + fenceHeight, 10, 10);
glRotatef(90, 1, 0, 0);
glPopMatrix();
// Pin-Right-Center
glPushMatrix();
glTranslatef(6.8, 0, 0);
glRotatef(-90, 1, 0, 0);
quadObj = gluNewQuadric();
gluCylinder(quadObj, 0.2, 0.1, 1.3 + fenceHeight, 10, 10);
glRotatef(90, 1, 0, 0);
glPopMatrix();
// Pin-Left-Center
glPushMatrix();
glTranslatef(-6.8, 0, 0);
glRotatef(-90, 1, 0, 0);
quadObj = gluNewQuadric();
gluCylinder(quadObj, 0.2, 0.1, 1.3 + fenceHeight, 10, 10);
glRotatef(90, 1, 0, 0);
glPopMatrix();
}
void DrawYardFloor(){
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, lightgreen);
glMaterialfv(GL_BACK, GL_AMBIENT_AND_DIFFUSE, lightgreen);
glBegin(GL_POLYGON);
glNormal3f(0, 1, 0);
glVertex3f(-7.3, -0.005, -7.3);
glVertex3f(-7.3, -0.005, 7.3);
glVertex3f(7.3, -0.005, 7.3);
glVertex3f(7.3, -0.005, -7.3);
glEnd();
}
void DrawCenterPin(){
glRotatef(-90, 1, 0, 0);
GLUquadricObj *quadObj = gluNewQuadric();
gluCylinder(quadObj, 0.2, 0.2, 7, 10, 10);
glRotatef(90, 1, 0, 0);
}
void DrawBase(){
glRotatef(-90, 1, 0, 0);
GLUquadricObj *quadObj = gluNewQuadric();
gluCylinder(quadObj, 0.5, 0.1, 2, 10, 10);
glRotatef(90, 1, 0, 0);
}
void DrawTop(){
glPushMatrix();
glTranslatef(0, 7, 0);
glRotatef(-90, 1, 0, 0);
GLUquadricObj *quadObj = gluNewQuadric();
gluCylinder(quadObj, 0.2, 0.0, 0.5, 10, 10);
glRotatef(90, 1, 0, 0);
glPopMatrix();
}
void DrawHorizontalStick(){
glLineWidth(15);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINES);
glVertex3f(0.0, 7.0, 0.0);
glVertex3f(4.0 * cos(radian__IN_RANGE), 7.0 + 4.0 * sin(radian__IN_RANGE), 0.0);
glEnd();
}
void DrawVerticalStick(){
glLineWidth(5);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINES);
glVertex3f(4.0 * cos(radian__IN_RANGE), 7.0 + 4.0 * sin(radian__IN_RANGE), 0.0);
glVertex3f(4.0 * cos(radian__IN_RANGE), 7.0 + 4.0 * sin(radian__IN_RANGE) - 1, 0.0);
glEnd();
}
void DrawCabin(){
// Back
glNormal3f(0.0, 0.0, -1.0);
glBegin(GL_POLYGON);
glVertex3f(0, 0, -1);
glVertex3f(0, 1, -1);
glVertex3f(2, 1, -1);
glVertex3f(2, 0, -1);
glEnd();
glNormal3f(0.0, 0.0, -1.0);
glBegin(GL_POLYGON);
glVertex3f(0, 1.7, -1);
glVertex3f(0, 2, -1);
glVertex3f(2, 2, -1);
glVertex3f(2, 1.7, -1);
glEnd();
glNormal3f(0.0, 0.0, -1.0);
glBegin(GL_POLYGON);
glVertex3f(0, 1, -1);
glVertex3f(0, 1.7, -1);
glVertex3f(0.2, 1.7, -1);
glVertex3f(0.2, 1, -1);
glEnd();
glNormal3f(0.0, 0.0, -1.0);
glBegin(GL_POLYGON);
glVertex3f(1.8, 1, -1);
glVertex3f(1.8, 1.7, -1);
glVertex3f(2, 1.7, -1);
glVertex3f(2, 1, -1);
glEnd();
// Front
glNormal3f(0.0, 0.0, 1.0);
glBegin(GL_POLYGON);
glVertex3f(2, 0, 1);
glVertex3f(2, 1, 1);
glVertex3f(0, 1, 1);
glVertex3f(0, 0, 1);
glEnd();
glNormal3f(0.0, 0.0, 1.0);
glBegin(GL_POLYGON);
glVertex3f(2, 1.7, 1);
glVertex3f(2, 2, 1);
glVertex3f(0, 2, 1);
glVertex3f(0, 1.7, 1);
glEnd();
glNormal3f(0.0, 0.0, 1.0);
glBegin(GL_POLYGON);
glVertex3f(0.2, 1, 1);
glVertex3f(0.2, 1.7, 1);
glVertex3f(0, 1.7, 1);
glVertex3f(0, 1, 1);
glEnd();
glNormal3f(0.0, 0.0, 1.0);
glBegin(GL_POLYGON);
glVertex3f(2, 1, 1);
glVertex3f(2, 1.7, 1);
glVertex3f(1.8, 1.7, 1);
glVertex3f(1.8, 1, 1);
glEnd();
// Floor
glNormal3f(0.0, -1.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(2, 0, -1);
glVertex3f(2, 0, 1);
glVertex3f(0, 0, 1);
glVertex3f(0, 0, -1);
glEnd();
// Top
glNormal3f(0.0, 1.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(2, 2, 1);
glVertex3f(2, 2, -1);
glVertex3f(0, 2, -1);
glVertex3f(0, 2, 1);
glEnd();
// Right
glNormal3f(1.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(2, 0, -1);
glVertex3f(2, 1, -1);
glVertex3f(2, 1, 1);
glVertex3f(2, 0, 1);
glEnd();
glNormal3f(1.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(2, 1.7, -1);
glVertex3f(2, 2, -1);
glVertex3f(2, 2, 1);
glVertex3f(2, 1.7, 1);
glEnd();
glNormal3f(1.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(2, 1, -1);
glVertex3f(2, 1.7, -1);
glVertex3f(2, 1.7, -0.8);
glVertex3f(2, 1, -0.8);
glEnd();
glNormal3f(1.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(2, 1, 0.8);
glVertex3f(2, 1.7, 0.8);
glVertex3f(2, 1.7, 1);
glVertex3f(2, 1, 1);
glEnd();
// Left
glNormal3f(-1.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(0, 0, -1);
glVertex3f(0, 0, 1);
glVertex3f(0, 1, 1);
glVertex3f(0, 1, -1);
glEnd();
glNormal3f(-1.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(0, 1.7, -1);
glVertex3f(0, 1.7, 1);
glVertex3f(0, 2, 1);
glVertex3f(0, 2, -1);
glEnd();
glNormal3f(-1.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(0, 1, -1);
glVertex3f(0, 1, -0.8);
glVertex3f(0, 1.7, -0.8);
glVertex3f(0, 1.7, -1);
glEnd();
glNormal3f(-1.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(0, 1, 0.8);
glVertex3f(0, 1, 1);
glVertex3f(0, 1.7, 1);
glVertex3f(0, 1.7, 0.8);
glEnd();
}
void darwCabin__FINAL(){
glPushMatrix();
glTranslatef(4.0 * cos(radian__IN_RANGE), 7.0 + 4.0 * sin(radian__IN_RANGE) - 3, 0.0);
glRotatef(angle, 0, 1, 0);
glPushMatrix();
glTranslatef(-1, 0, 0);
DrawCabin();
glPopMatrix();
glRotatef(-angle, 0, 1, 0);
glPopMatrix();
}
void display(void)
{
for (int i = 0; i < 2; i++) {
glutSetWindow(window[i]); // set the current window to window[i]
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 64);
if (i == 1) {
gluLookAt(0.7, 0, 0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glRotatef(-angle, 0, 1, 0);
glTranslatef(-4.0 * cos(radian__IN_RANGE), -5.4 + 4.0 * sin(radian__IN_RANGE), 0);
glRotatef(-angle, 0, 1, 0);
}
else if (i == 0) {
cpos[0] = zoom * cos(beta) * sin(alpha);
cpos[1] = zoom * sin(beta);
cpos[2] = zoom * cos(beta) * cos(alpha);
gluLookAt(cpos[0], cpos[1], cpos[2], 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
if (lightSource == true){
glLightfv(GL_LIGHT0, GL_POSITION, lpos);
glMaterialfv(GL_FRONT, GL_EMISSION, white);
glPushMatrix();
glTranslatef(lpos[0], lpos[1], lpos[2]);
glutSolidSphere(0.1, 10, 8);
glPopMatrix();
glMaterialfv(GL_FRONT, GL_EMISSION, black);
}
DrawYardFloor();
DrawSticksArroundYard();
DrawCenterPin();
DrawBase();
DrawTop();
glRotatef(angle, 0, 1, 0);
for (int j = 0; j < 4; j++){
glMaterialfv(GL_FRONT, GL_SPECULAR, white);
glMaterialf(GL_FRONT, GL_SHININESS, 64);
if (id % 4 == 0)
{
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
glMaterialfv(GL_BACK, GL_AMBIENT_AND_DIFFUSE, black);
}
else if (id % 4 == 1)
{
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
glMaterialfv(GL_BACK, GL_AMBIENT_AND_DIFFUSE, black);
}
else if (id % 4 == 2)
{
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, white);
glMaterialfv(GL_BACK, GL_AMBIENT_AND_DIFFUSE, black);
}
else if (id % 4 == 3)
{
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, cyan);
glMaterialfv(GL_BACK, GL_AMBIENT_AND_DIFFUSE, black);
}
glPushMatrix();
glRotatef(j * 360 / 4, 0, 1, 0);
keepTrackOfID();
DrawHorizontalStick();
DrawVerticalStick();
darwCabin__FINAL();
id++;
glPopMatrix();
}
glRotatef(-angle, 0, 1, 0);
glFlush();
glutSwapBuffers();
}
}
void keyboard(unsigned char key, int x, int y)
{
static int polygonmode[2];
switch (key) {
case 27:
exit(0);
break;
case 'x':
if (lightSource == true)
lpos[0] = lpos[0] + 0.2;
glutPostRedisplay();
break;
case 'X':
if (lightSource == true)
lpos[0] = lpos[0] - 0.2;
glutPostRedisplay();
break;
case 'y':
if (lightSource == true)
lpos[1] = lpos[1] + 0.2;
glutPostRedisplay();
break;
case 'Y':
if (lightSource == true)
lpos[1] = lpos[1] - 0.2;
glutPostRedisplay();
break;
case 'z':
if (lightSource == true)
lpos[2] = lpos[2] + 0.2;
glutPostRedisplay();
break;
case 'Z':
if (lightSource == true)
lpos[2] = lpos[2] - 0.2;
glutPostRedisplay();
break;
case '+':
if (zoom != 1.5)zoom = zoom - 0.5;
glutPostRedisplay();
break;
case '-':
if (zoom != 30)zoom = zoom + 0.5;
glutPostRedisplay();
break;
case '0':
if (lightSource == true){
glDisable(GL_LIGHT0);
lightSource = false;
}
else{
glEnable(GL_LIGHT0);
lightSource = true;
}
glutPostRedisplay();
break;
case 'e':
if (fenceHeight < 2)
fenceHeight += 0.5;
glutPostRedisplay();
break;
case 'd':
if (fenceHeight > -0.5)
fenceHeight -= 0.5;
glutPostRedisplay();
break;
case 'w':
glGetIntegerv(GL_POLYGON_MODE, polygonmode);
if (polygonmode[0] == GL_FILL)
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
else glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glutPostRedisplay();
break;
case 'n':
angle++;
processAngle();
setRadian_IN_RANGE();
glutPostRedisplay();
break;
case 'm':
angle--;
processAngle();
setRadian_IN_RANGE();
glutPostRedisplay();
break;
default:
break;
}
}
void mouse(int button, int state, int x, int y)
{
switch (button) {
case GLUT_LEFT_BUTTON:
signal = 0;
if (speed <= 15)
speed++;
break;
case GLUT_MIDDLE_BUTTON:
case GLUT_RIGHT_BUTTON:
signal = 1;
if (speed >= 1)
speed--;
break;
default:
break;
}
}
void specialkey(GLint key, int x, int y)
{
switch (key) {
case GLUT_KEY_RIGHT:
alpha = alpha + PI / 180;
if (alpha > 2 * PI) alpha = alpha - 2 * PI;
glutPostRedisplay();
break;
case GLUT_KEY_LEFT:
alpha = alpha - PI / 180;
if (alpha < 0) alpha = alpha + 2 * PI;
glutPostRedisplay();
break;
case GLUT_KEY_UP:
if (beta < 0.45*PI) beta = beta + PI / 180;
glutPostRedisplay();
break;
case GLUT_KEY_DOWN:
if (beta > -0.05*PI) beta = beta - PI / 180;
glutPostRedisplay();
break;
default:
break;
}
}
void anim(){
if (signal == 0){
angle++;
glutPostRedisplay();
Sleep((int)(50 / speed));
}
else if (signal == 1){
if (speed != 0){
angle++;
glutPostRedisplay();
Sleep((int)(50 / speed));
}
}
}
int main(int argc, char** argv)
{
writemessage();
fillArray();
processAngle();
setRadian_IN_RANGE();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
window[0] = glutCreateWindow("First");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(anim);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
glutSpecialFunc(specialkey);
glutInitWindowSize(500, 500);
glutInitWindowPosition(600, 10);
window[1] = glutCreateWindow("Second");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(anim);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
glutSpecialFunc(specialkey);
glutMainLoop();
return 0;
}

Lighting for lines works just the same as for other objects. The color/brightness is calculated based on normals, and the lighting and material parameters.
If you're using immediate mode draw commands, you can specify normals with glNormal3f() before the glVertex3f() calls you make for the lines. If you don't specify the normal this way, whatever is the current normal based on earlier calls to glNormal*() will be used as the normal for your lines.
Now, the obvious follow-up question is: What is the normal of a line?
The simple answer is: Whatever you want it to be, to give you the desired result.
For a typical example, if you draw an analytic surface in wireframe mode, the normals of this analytic surface can be used as the normals. This will give a wireframe model where the shading/brightness corresponds to the shape of the surface.
In your example, the most obvious and arguably best approach is to not draw lines, but represent the geometry with polygons, using some kind of thin beam/cylinder. But as an exercise, we can figure out how this would work with lines anyway.
In this case, you want the line to represent an infinitely thin cylinder. An actual cylinder would have normals pointing outwards for each vertex. But since the line has only two vertices, you have to pick one single direction among the infinite set of vectors that point outwards from the end point, and are orthogonal to the line.
I believe you would get reasonable results by choosing the normal vector that is closest to pointing towards the viewpoint. This would result in the brightness you would get from looking at the center line of the cylinder if you actually used a cylinder. These normals could be calculated by:
Apply your model transformation to the original end points of the line, v1 and v2. Let's called the transformed end points v1t and v2t.
Calculate the direction vector of the transformed line as:
v1d = v2t - v1t
v1d.normalize()
Calculate the vector from line end point to view point vp, and orthogonalize it relative to the direction of the line, and normalize it:
v1n = vp - v1
v1n -= dot(v1d * v1n) * v1d
v1n.normalize()
Apply the inverse model transform to v1n to get the normal in the original object space.
Perform equivalent calculation for second vertex.
You could achieve the same result by doing the inverse transformation on the view point, and then calculating the normals directly in object space.

Related

movement repeat in 2d game by opengl

When the red car is moving, it is repeated with every movement and at every chance has repeated, I had searched but not found a solution, so what is the solution to the problem?
#include <GL\glut.h>
#define drawW 400
#define drawH 300
void reshape(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float ratio = (float)width / height;
if (width > height)
{
glOrtho(-drawW * ratio, drawW * ratio, -drawH, drawH, -1, 1);
}
else
{
glOrtho(-drawW, drawW, -drawH / ratio, drawH / ratio, -1, 1);
}
glClear(GL_DEPTH_BUFFER_BIT);
glClearColor(0.21, 0.22, 0.18, 1);
}
void car() {
//drawing the movement car
glTranslated(0, -100, 0);
glBegin(GL_POLYGON);
glColor3f(0.73, 0.63, 0.56);
glVertex2f(50, 50);
glVertex2f(0, 100);
glVertex2f(-50, 50);
glVertex2f(-50, -100);
glVertex2f(50, -100);
glEnd();
//drawing the 1st wheel in the 1st quarter
glBegin(GL_POLYGON);
glColor3f(0, 0.0, 0);
glVertex2f(75, 25);
glVertex2f(50, 25);
glVertex2f(50, 0);
glVertex2f(75, 0);
glEnd();
//drawing the 2nd wheel in the 2nd quarter
glBegin(GL_POLYGON);
glColor3f(0, 0.0, 0);
glVertex2f(-50, 25);
glVertex2f(-75, 25);
glVertex2f(-75, 0);
glVertex2f(-50, 0);
glEnd();
//drawing the 3rd wheel in the 3rd quarter
glBegin(GL_POLYGON);
glColor3f(0, 0.0, 0);
glVertex2f(-50, -50);
glVertex2f(-75, -50);
glVertex2f(-75, -75);
glVertex2f(-50, -75);
glEnd();
//drawing the 4th wheel in the 4th quarter
glBegin(GL_POLYGON);
glColor3f(0, 0.0, 0);
glVertex2f(75, -50);
glVertex2f(50, -50);
glVertex2f(50, -75);
glVertex2f(75, -75);
glEnd();
glFlush();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
//drawing the green car in the 1st quarter
glBegin(GL_POLYGON);
glColor3f(0.28, 0.64, 0.47);
glVertex2i(350, 300);
glVertex2i(300, 300);
glVertex2i(300, 150);
glVertex2i(325, 125);
glVertex2i(350, 150);
glEnd();
//drawing the 1st wheel of the car
glBegin(GL_POLYGON);
glColor3f(0, 0, 0.0);
glVertex2i(300, 275.0);
glVertex2i(283, 275);
glVertex2i(283, 250);
glVertex2i(300, 250.0);
glEnd();
//drawing the 2nd wheel of the car
glBegin(GL_POLYGON);
glColor3f(0, 0, 0.0);
glVertex2i(300, 200.0);
glVertex2i(283, 200);
glVertex2i(283, 175);
glVertex2i(300, 175.0);
glEnd();
//drawing the 3rd wheel of the car
glBegin(GL_POLYGON);
glColor3f(0, 0, 0.0);
glVertex2i(365, 200);
glVertex2i(350, 200);
glVertex2i(350, 175);
glVertex2i(365, 175);
glEnd();
//drawing the 4th wheel of the car
glBegin(GL_POLYGON);
glColor3f(0, 0, 0.0);
glVertex2i(365, 275);
glVertex2i(350, 275);
glVertex2i(350, 250);
glVertex2i(365, 250);
glEnd();
glFlush();
//drawing the purple car
glBegin(GL_POLYGON);
glColor3f(0.48, 0.48, 0.64);
glVertex2i(-350, 300);
glVertex2i(-300, 300);
glVertex2i(-300, 150);
glVertex2i(-325, 125);
glVertex2i(-350, 150);
glEnd();
//drawing the 1st wheel of the car
glBegin(GL_POLYGON);
glColor3f(0, 0, 0.0);
glVertex2i(-300, 275.0);
glVertex2i(-283, 275);
glVertex2i(-283, 250);
glVertex2i(-300, 250.0);
glEnd();
//drawing the 2nd wheel of the car
glBegin(GL_POLYGON);
glColor3f(0, 0, 0.0);
glVertex2i(-300, 200.0);
glVertex2i(-283, 200);
glVertex2i(-283, 175);
glVertex2i(-300, 175.0);
glEnd();
//drawing the 3rd wheel of the car
glBegin(GL_POLYGON);
glColor3f(0, 0, 0.0);
glVertex2i(-365, 200);
glVertex2i(-350, 200);
glVertex2i(-350, 175);
glVertex2i(-365, 175);
glEnd();
//drawing the 4th wheel of the car
glBegin(GL_POLYGON);
glColor3f(0, 0, 0.0);
glVertex2i(-365, 275);
glVertex2i(-350, 275);
glVertex2i(-350, 250);
glVertex2i(-365, 250);
glEnd();
//the break
glBegin(GL_POLYGON);
glColor3f(0.0, 0, 0.0);
glVertex2i(-300, 100);
glVertex2i(-300, 75);
glVertex2i(-150, 100);
glVertex2i(-150, 75);
glEnd();
//drawing the line in the middle of the road
glBegin(GL_POLYGON);
glColor3f(30, 30, 0);
glVertex2f(20, 200);
glVertex2f(-20, 200);
glVertex2f(-20, 50);
glVertex2f(20, 50);
glEnd();
//drawing the red object of the car
glTranslated(0, -100, 0);
glBegin(GL_POLYGON);
glColor3f(0.91, 0.07, 0.09);
glVertex2f(50, 50);
glVertex2f(0, 100);
glVertex2f(-50, 50);
glVertex2f(-50, -100);
glVertex2f(50, -100);
glEnd();
//drawing the 1st wheel in the 1st quarter
glBegin(GL_POLYGON);
glColor3f(0, 0.0, 0);
glVertex2f(75, 25);
glVertex2f(50, 25);
glVertex2f(50, 0);
glVertex2f(75, 0);
glEnd();
//drawing the 2nd wheel in the 2nd quarter
glBegin(GL_POLYGON);
glColor3f(0, 0.0, 0);
glVertex2f(-50, 25);
glVertex2f(-75, 25);
glVertex2f(-75, 0);
glVertex2f(-50, 0);
glEnd();
//drawing the 3rd wheel in the 3rd quarter
glBegin(GL_POLYGON);
glColor3f(0, 0.0, 0);
glVertex2f(-50, -50);
glVertex2f(-75, -50);
glVertex2f(-75, -75);
glVertex2f(-50, -75);
glEnd();
//drawing the 4th wheel in the 4th quarter
glBegin(GL_POLYGON);
glColor3f(0, 0.0, 0);
glVertex2f(75, -50);
glVertex2f(50, -50);
glVertex2f(50, -75);
glVertex2f(75, -75);
glEnd();
glFlush();
}
void processNormalKeys(unsigned char key, int x, int y)
{
switch (key)
{
case 'd':
case 'D':
glTranslated(200, 100, 0);
car();
glutPostRedisplay;
break;
case 'a':
case 'A':
glTranslated(-200, 100, 0);
car();
break;
}
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitWindowSize(1000, 600);
glutCreateWindow("Speedy Car Race");
glutSwapBuffers;
glutDisplayFunc(car);
glutPostRedisplay();
glutDisplayFunc(display);
glutKeyboardFunc(processNormalKeys);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
I try adding glutSwapBuffer(), glutPostRedisplay(), glClear(GL_COLOR_BUFFER_BIT)
but not of them working?
This line DOES NOT call glutPostRedisplay:
glutPostRedisplay;
Therefore your keyboard events do not trigger the display() function, glClear isn't called, and the buffer isn't cleared.
To actually call glutPostRedisplay, you must put parentheses after the function name:
glutPostRedisplay();

Opengl viewpoint transformation how to gluLookAt

I would like to use the polar coordinates to manipulate the upper, lower, left, and right camera points with my keyboard.
#include <stdlib.h>
#include <glut.h>
GLint TopLeftX, TopLeftY, BottomRightX, BottomRightY ;
static int HourOfDay = 0;
static int DayOfYear = 10;
void init(void) {
glClearColor(0.0, 0.0, 0.0, 0.0);
}
void myDisplay(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix(); // Origin
glClear(GL_DEPTH_BUFFER_BIT);
/* Sun */
glColor3f(1.0, 1.0, 0.0);
glutSolidSphere(1.0, 20, 10);
/* Mercury */
glPushMatrix();
glRotatef((GLfloat)DayOfYear, 0.0, 0.0, 1.0);
glTranslatef(3.0, 0.0, 0.0);
glRotatef((GLfloat)HourOfDay, 0.0, 0.0, 1.0);
glColor3f(0.87, 0.53, 0.25);
glutSolidSphere(0.3, 10, 2);
glPopMatrix();
/* Venus */
glPushMatrix();
glRotatef((GLfloat)DayOfYear + 195, 0.0, 0.0, 1.0);
glTranslatef(5.0, 0.0, 0.0);
glRotatef((GLfloat)HourOfDay, 0.0, 0.0, 1.0);
glColor3f(0.99, 0.91, 0.66);
glutSolidSphere(0.5, 10, 2);
glPopMatrix();
/* Earth */
glPushMatrix();
glRotatef((GLfloat)DayOfYear + 80, 0.0, 0.0, 1.0);
glTranslatef(7.0, 0.0, 0.0);
glRotatef((GLfloat)HourOfDay, 0.0, 0.0, 1.0);
glColor3f(0.47, 0.82, 0.98);
glutSolidSphere(0.5, 10, 2);
/* Earth's Moon 1*/
glPushMatrix();
glRotatef((GLfloat)DayOfYear, 0.0, 0.0, 1.0);
glTranslatef(0.7, 0.0, 0.0);
glColor3f(0.89, 0.93, 0.95);
glutSolidSphere(0.1, 5, 5);
glPopMatrix();
glPopMatrix();
/* Mars */
glPushMatrix();
glRotatef((GLfloat)DayOfYear + 275, 0.0, 0.0, 1.0);
glTranslatef(9.0, 0.0, 0.0);
glRotatef((GLfloat)HourOfDay, 0.0, 0.0, 1.0);
glColor3f(0.84, 0.16, 0.15);
glutSolidSphere(0.35, 10, 2);
glPopMatrix();
/* Jupiter */
glPushMatrix();
glRotatef((GLfloat)DayOfYear + 33, 0.0, 0.0, 1.0);
glTranslatef(11.0, 0.0, 0.0);
glRotatef((GLfloat)HourOfDay, 0.0, 0.0, 1.0);
glColor3f(0.93, 0.64, 0.27);
glutSolidSphere(1.0, 10, 2);
glPopMatrix();
/* Saturn */
glPushMatrix();
glRotatef((GLfloat)DayOfYear + 180, 0.0, 0.0, 1.0);
glTranslatef(15.0, 0.0, 0.0);
glRotatef((GLfloat)HourOfDay, 0.0, 0.0, 1.0);
glColor3f(0.92, 0.82, 0.45);
glutSolidSphere(0.9, 10, 2);
glPopMatrix();
/* Uranus */
glPushMatrix();
glRotatef((GLfloat)DayOfYear + 90, 0.0, 0.0, 1.0);
glTranslatef(17.0, 0.0, 0.0);
glRotatef((GLfloat)HourOfDay, 0.0, 0.0, 1.0);
glColor3f(0.64, 0.84, 0.78);
glutSolidSphere(0.8, 10, 2);
glPopMatrix();
/* Neptune */
glPushMatrix();
glRotatef((GLfloat)DayOfYear + 150, 0.0, 0.0, 1.0);
glTranslatef(20.0, 0.0, 0.0);
glRotatef((GLfloat)HourOfDay, 0.0, 0.0, 1.0);
glColor3f(0.29, 0.74, 0.95);
glutSolidSphere(0.25, 10, 2);
glPopMatrix();
glutSwapBuffers();
// Animation State
DayOfYear = (DayOfYear + 1) % 360;
HourOfDay = (HourOfDay + 5) % 360;
}
void Timer(int iUnused) {
glutPostRedisplay();
glutTimerFunc(30, Timer, 0);
}
static float cam_axis_x = 0.0;
static float cam_axis_y = 0.0;
static float cam_axis_z = -50.0;
void reshape(int w, int h) {
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (GLfloat)w / (GLfloat)h, 1.0, 90.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(cam_axis_x, cam_axis_y, cam_axis_z);
}
/*void reshape(int w, int h) {
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (GLsizei)w / (GLsizei)h, 0.0, 90.0);
}*/
void keyboard(unsigned char key, int x, int y) {
switch (key) {
case 'q': // Esc
exit(0);
break;
case '75': // left
break;
case '77': // right
break;
case '72': // up
break;
case '80': // down
break;
}
}
void MyMouseClick(GLint Button, GLint State, GLint X, GLint Y) {
if (Button == GLUT_LEFT_BUTTON && State == GLUT_DOWN) {
TopLeftX = X;
TopLeftY = Y;
}
}
void MyMouseMove(GLint X, GLint Y) {
BottomRightX = X;
BottomRightY = Y;
glutPostRedisplay();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(myDisplay);
glutReshapeFunc(reshape);
Timer(0);
glutKeyboardFunc(keyboard);
glutMouseFunc(MyMouseClick);
glutMotionFunc(MyMouseMove);
glutMainLoop();
return 0;
}
I want to move the camera around the sun
Can you tell me about gluLookAt?
I don't know what you mean by polar coordinates, normally it's for 2D. maybe you mean Spherical coordinates.
The method would be quite identical:
Assuming your sun is at the origin of the world (which it currently does)
You have 2 methods
Assuming Theta is the angle you want your camera to orbit around the sun
and Phi is some "pitching" around your xAxis frame (you may or may not want it).
1) Compute the x,y,z position of your camera using the basic formula:
https://en.wikipedia.org/wiki/Spherical_coordinate_system
Spherical Coordinates:
// r is the distance to the sun
cam_axis_x = r * sin(theta) * cos(phi);
cam_axis_y = r * r * cos(theta);
cam_axis_z = r * sin(theta) * sin(phi);
"Polar coordinates":
cam_axis_x = r * sin(theta);
cam_axis_z = r * cos(theta);
then apply the translation as you did before
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(cam_axis_x, cam_axis_y, cam_axis_z);
2) Apply a rotation on your camera after translating it to its original position:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(cam_axis_x, cam_axis_y, cam_axis_z);
glRotatef(0, theta, phi);

Calc texcoord for some pixmap

I have a pixmap for some app in Xephyr and I need draw this pixmap on rectangle openGL. I seen example from khronos for pixmap when created in code but it's not working for me:
mPixmap = XCreatePixmap(dpy, client->getWindow(), static_cast<unsigned int>(attr.width),
static_cast<unsigned int>(attr.height), static_cast<unsigned int>(attr.depth));
And code where I drawed:
void startPluginPixmap() {
XEvent xev;
GLuint texture_id;
GLint pix_att[5] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None };
vi = glXChooseVisual(dpy, 0, pix_att);
swa.event_mask = ExposureMask | KeyPressMask;
swa.colormap = XCreateColormap(dpy, root, vi->visual, AllocNone);
uint64_t window_width = 300;
uint64_t window_height = 400;
uint32_t window_depth = vi->depth;
win = XCreateWindow(dpy, root, 0, 0, window_width, window_height, 0, window_depth, InputOutput, vi->visual, CWEventMask | CWColormap, &swa);
XMapWindow(dpy, win);
XStoreName(dpy, win, "PIXMAP TEST");
glc = glXCreateContext(dpy, vi, NULL, GL_TRUE);
if (!glc) {
exit(0);
}
glXMakeCurrent(dpy, win, glc);
glEnable(GL_DEPTH_TEST);
uint64_t pixmap_width = 150;
uint64_t pixmap_height = 100;
GC gc = DefaultGC(dpy, 0);
XFlush(dpy);
XImage * xim = XGetImage(dpy, mPixmap, 0, 0, pixmap_width, pixmap_height, AllPlanes, ZPixmap);
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &texture_id);
glBindTexture(GL_TEXTURE_2D, texture_id);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, pixmap_width, pixmap_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (void *)(&(xim->data[0])));
XDestroyImage(xim);
while(true) {
XNextEvent(dpy, &xev);
if (xev.type == Expose) {
redrawPixmapImage();
}
else if (xev.type == KeyPress && xev.xkeymap.type == 0x19) {
glXMakeCurrent(dpy, None, NULL);
glXDestroyContext(dpy, glc);
XDestroyWindow(dpy, win);
XCloseDisplay(dpy);
exit(0);
}
}
}
void Cube::redrawPixmapImage() {
XWindowAttributes gwa;
XGetWindowAttributes(dpy, win, &gwa);
glViewport(0, 0, gwa.width, gwa.height);
glClearColor(.0, 1.0, .0, .5);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.25, 1.25, -1.25, 1.25, 1., 20.);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0., 0., 10., 0., 0., 0., 0., 1., 0.);
glColor3f(1.f, 1.f, 1.f);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0);
//glColor3f(1.f, 0.f, 0.f);
glVertex3f(-1.0, 1.0, 0.0);
glTexCoord2f(1.0, 0.0);
glVertex3f( 1.0, 1.0, 0.0);
glTexCoord2f(1.0, 1.0);
glVertex3f( 1.0, -1.0, 0.0);
glTexCoord2f(0.0, 1.0);
glVertex3f(-1.0, -1.0, 0.0);
glTexCoord2f(1.0, 1.0);
glVertex3f(-1.0, 1.0, 0.0);
glEnd();
glXSwapBuffers(dpy, win);
}
In this code I enter width and height manual because get it in debug info.
And for me this calculation of textcoord doesn't work (When I run in Xephyr xeyes, and then this code, I seeing artifacts and green area).
So, how I can calculate texcoords for pixmap some app?

OpenGL not displaying anything when gluLookAt called

I am working on a practice OpenGL program. I am basically trying to display a pyramid. I pasted the code I have so far bellow. The issue I am having is that when I call gluLookAt my screen becomes completely black. This does not make sense to me. My pyramid has triangles with dimensions bounded within a box of size (1, 1, 1) and it is at the origin. So therefore if I position the eye at something far away if (10, 10, 10) and look at (0, 0, 0) why do I not see the pyramid. When I comment out gluLookAt I can see it clearly.
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
struct
{
GLuint trifan;
GLuint tribase;
GLuint colors;
} b;
void display();
void init_buffer();
void check_errors();
void reshape(int w, int h);
void keyboard(unsigned char k, int x, int y);
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitWindowSize(500, 500);
glutCreateWindow("Pyramid");
init_buffer();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
void init_buffer()
{
glGenBuffers(1, &(b.trifan));
glBindBuffer(GL_ARRAY_BUFFER, b.trifan);
GLfloat trifan[6][3] =
{
{0.0, 1.0, 0.0},
{1.0, 0.0, 1.0},
{-1.0, 0.0, 1.0},
{-1.0, 0.0, -1.0},
{1.0, 0.0, -1.0},
{1.0, 0.0, 1.0}
};
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3 * 6, trifan, GL_STATIC_COPY);
glGenBuffers(1, &b.colors);
glBindBuffer(GL_ARRAY_BUFFER, b.colors);
GLfloat colors[6][3] =
{
{1.0, 0.0, 0.0},
{1.0, 0.0, 0.0},
{1.0, 0.0, 0.0},
{1.0, 0.0, 1.0},
{0.5, 0.0, 1.0},
{1.0, 0.0, 0.5},
};
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3 * 6, colors, GL_STATIC_COPY);
check_errors();
}
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (float)w / (float)h, 1.0, 100.0);
}
float angle = 0.0;
float zoom = 1.0;
void keyboard(unsigned char k, int x, int y)
{
if(k == 'x')
{
angle += 10;
}
else if(k == 'y')
{
angle -= 10;
}
else if(k == 'w')
{
zoom += 0.1;
}
else if(k == 's')
{
zoom -= 0.1;
}
glutPostRedisplay();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotated(angle, 1.0, 0.0, 0.0);
gluLookAt(10.0, 10.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
{
glBindBuffer(GL_ARRAY_BUFFER, b.trifan);
glVertexPointer(3, GL_FLOAT, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, b.colors);
glColorPointer(3, GL_FLOAT, 0, 0);
glDrawArrays(GL_TRIANGLE_FAN, 0, 6);
}
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
check_errors();
glFlush();
}
void check_errors()
{
GLint err = glGetError();
if(err != GL_NO_ERROR)
{
fputs((char*)gluErrorString(err), stderr);
exit(1);
}
}

Solid sphere not visible on the screen

I'm trying to combine lighting with 3D graphics.There's sphere that is on depth 100, and it has 100 as radius.I use translatef to make it be far from the eye position.
But nothing is displayed on the screen:
#import <OpenGL/OpenGL.h>
#import <GLUT/GLUT.h>
GLfloat width=500, height=500;
GLfloat angle=0.0;
void makeRound (GLfloat* angle)
{
if(*angle>360.0)
{
*angle-=360.0;
}
else if(*angle<0.0)
{
*angle+=360.0;
}
}
void init(void)
{
GLfloat mat_specular[] = { 1.0, 0.0, 0.0, 0.0 };
GLfloat mat_diffuse[] = { 0.9, 0.0, 0.0, 0.0 };
GLfloat mat_shininess[] = { 50.0 };
GLfloat light_position[] = { 5.0, 5.0, 5.0, 1.0 };
GLfloat white_light[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat lmodel_ambient[] = { 0.1, 0.5, 0.1, 1.0 };
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, 0, 0, 0, 100, 0, 1, 0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, 1, 1, 1000);
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_SMOOTH);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light);
glLightfv(GL_LIGHT0, GL_SPECULAR,white_light);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT,lmodel_ambient);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
}
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glRotatef(angle, 0, 1, 0);
glTranslatef(0, 0, 100);
glutSolidSphere (100.0, 80, 64);
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case '+':
angle+=5.0;
makeRound(&angle);
glutPostRedisplay();
break;
case '-':
angle-=5.0;
makeRound(&angle);
glutPostRedisplay();
break;
default:
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (width, height);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
init ();
glutMainLoop();
return 0;
}
UPDATE
This is what I see with :
glTranslatef(0,0,-200);
As a result of your translate you are actually positioned on the edge of the sphere. That means the polys actually go though "you". You won't see anything as a result of your near clip plane and thanks to back-face culling you can't see the tris on the other side of the sphere.
Set either:
glCullFace( GL_FRONT );
Or set the transform to push the sphere further away:
glTranslatef(0, 0, -200);

Resources