openglbeginner123

07-27-2014, 10:26 AM

Hello again!,

I just want to first say that I greatly appreciate the feedback I have been getting on this forum, and I hope you guys could help me out on this new issue I keep running into.

What I am struggling with, is lighting and depth. I want to draw a sort of a double side arrow on the screen and then have a loop run that will rotate it in x , y , and z axis. The rotation I have working, and so the loop. But the problem is that even though I am drawing everything in 3D and my calculations are right, something is off when I rotate the whole object. The negative effect that I am getting is basically that the object doesn't seem to look like its in proper 3D. The effect of "depth" is not there because the different parts of the object overlap, cross each other when I want them to be behind each other etc. and it is just confusing. There is no sense of a certain reference position in space if that makes any sense, because everything just rotates without the proper light change and the depth I am looking for.

I really do realize my explanation might be confusing but perhaps the key problem that I have is that my objects do not look to behave realistically.

I am therefore wondering if someone may know how to get the light functioning properly or the depth or how to fix whatever I am doing wrong.

As a side note, I posted all of my code and tried to give the best explanation I could. Drawing-wise, what I am doing to simulate the "arrow",is I loop through 360 degrees and draw a bunch of lines with points on the circle (the bottom of the cone) and one point at the "top". This creates the cone.

The set up that I have is for the light. Again, I don't know if I am setting it up right.. Also, I am quite sure I need normals for my object to get the light working so that might be the issue, however I have no idea how to set up the normals for a cone that I want to draw. I work in Xcode and writing an OSX application so the code is Objective - C, but the openGL is obviously the same. The drawRect method I have is the method that gets called very frame to update the drawing.

It would be awesome if someone could contribute and give some feedback. Please ask to clarify anything if something I wrote is confusing.

As always, thanks in advance for any feedback. :)

-(void) drawRect: (NSRect) bounds

{

glClearColor(0,0,0,0);

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

glEnable(GL_DEPTH);

glDepthFunc(GL_ALWAYS);

glEnable(GL_CULL_FACE);

glEnable(GL_LIGHTING);

glEnable(GL_LIGHT0);

GLfloat global_ambient[] = { 1, 1, 0, 1.0f };

glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient);

glShadeModel(GL_SMOOTH);

GLfloat specular[] = {1.0f, 1.0f, 1.0f , 1.0f};

glLightfv(GL_LIGHT0, GL_SPECULAR, specular);

GLfloat ambient[] = { 1.0f, 1.0f, 1.0f };

glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);

GLfloat position[] = { -1.0f, 1.0f, 0, 1.0f };

glLightfv(GL_LIGHT0, GL_POSITION, position);

glLoadIdentity();

glScalef(0.5, 0.5, 0.5);

glRotatef(rotation,1,0,0);

glRotatef(rotation,0,1,0);

glRotatef(rotation,0,0,1);

// After all of transformations - draw the graphics

double pi = 3.14159265359;

glColor3f(1, 0.12, 0.42);

glBegin(GL_LINES);

glVertex3f(0, 0, 0);

glVertex3f(0, 1, 0);

for(double theta = 0; theta < 360; theta++){

double x = 0.015 * cosf(theta * pi / 180);

double y = 0.015 * sinf(theta * pi / 180);

glVertex3f(x, 0, y);

glVertex3f(x, 1, y);

}

glEnd();

glBegin(GL_LINES);

for(double theta = 0; theta < 360; theta++){

double x = 0.1 * cosf(theta * pi / 180);

double y = 0.1 * sinf(theta * pi / 180);

glVertex3f(x, 1, y);

glVertex3f(0, 1.3, 0);

}

glEnd();

glColor3f(0.5, 0.5, 0.5);

glBegin(GL_POINTS);

for(double theta = 0; theta < 360; theta++){

double x = 0.3 * cosf(theta * pi / 180);

double y = 0.3 * sinf(theta * pi / 180);

glVertex3f(x, 0, y);

}

glEnd();

glColor3f(0.4, 0.2, 0.2);

glBegin(GL_LINES);

for(double theta = 0; theta < 360; theta++){

double x = 0.025 * cosf(theta * pi / 180);

double y = 0.025 * sinf(theta * pi / 180);

glVertex3f(x, 1, y);

}

glEnd();

// bottom arrow

glColor3f(1, 0.12, 0.42);

glBegin(GL_LINES);

glVertex3f(0, 0, 0);

glVertex3f(0, -1, 0);

for(double theta = 0; theta < 360; theta++){

double x = 0.015 * cosf(theta * pi / 180);

double y = 0.015 * sinf(theta * pi / 180);

glVertex3f(x, 0, y);

glVertex3f(x, -1, y);

}

glEnd();

glBegin(GL_LINES);

for(double theta = 0; theta < 360; theta++){

double x = 0.1 * cosf(theta * pi / 180);

double y = 0.1 * sinf(theta * pi / 180);

glVertex3f(x, -1, y);

glVertex3f(0, -1.3, 0);

}

glEnd();

glColor3f(0.5, 0.5, 0.5);

glBegin(GL_POINTS);

for(double theta = 0; theta < 360; theta++){

double x = 0.3 * cosf(theta * pi / 180);

double y = 0.3 * sinf(theta * pi / 180);

glVertex3f(x, -1, y);

}

glEnd();

glColor3f(0.4, 0.2, 0.2);

glBegin(GL_LINES);

for(double theta = 0; theta < 360; theta++){

double x = 0.025 * cosf(theta * pi / 180);

double y = 0.025 * sinf(theta * pi / 180);

glVertex3f(x, -1, y);

}

glEnd();

glFlush();

rotation++;

}

I just want to first say that I greatly appreciate the feedback I have been getting on this forum, and I hope you guys could help me out on this new issue I keep running into.

What I am struggling with, is lighting and depth. I want to draw a sort of a double side arrow on the screen and then have a loop run that will rotate it in x , y , and z axis. The rotation I have working, and so the loop. But the problem is that even though I am drawing everything in 3D and my calculations are right, something is off when I rotate the whole object. The negative effect that I am getting is basically that the object doesn't seem to look like its in proper 3D. The effect of "depth" is not there because the different parts of the object overlap, cross each other when I want them to be behind each other etc. and it is just confusing. There is no sense of a certain reference position in space if that makes any sense, because everything just rotates without the proper light change and the depth I am looking for.

I really do realize my explanation might be confusing but perhaps the key problem that I have is that my objects do not look to behave realistically.

I am therefore wondering if someone may know how to get the light functioning properly or the depth or how to fix whatever I am doing wrong.

As a side note, I posted all of my code and tried to give the best explanation I could. Drawing-wise, what I am doing to simulate the "arrow",is I loop through 360 degrees and draw a bunch of lines with points on the circle (the bottom of the cone) and one point at the "top". This creates the cone.

The set up that I have is for the light. Again, I don't know if I am setting it up right.. Also, I am quite sure I need normals for my object to get the light working so that might be the issue, however I have no idea how to set up the normals for a cone that I want to draw. I work in Xcode and writing an OSX application so the code is Objective - C, but the openGL is obviously the same. The drawRect method I have is the method that gets called very frame to update the drawing.

It would be awesome if someone could contribute and give some feedback. Please ask to clarify anything if something I wrote is confusing.

As always, thanks in advance for any feedback. :)

-(void) drawRect: (NSRect) bounds

{

glClearColor(0,0,0,0);

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

glEnable(GL_DEPTH);

glDepthFunc(GL_ALWAYS);

glEnable(GL_CULL_FACE);

glEnable(GL_LIGHTING);

glEnable(GL_LIGHT0);

GLfloat global_ambient[] = { 1, 1, 0, 1.0f };

glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient);

glShadeModel(GL_SMOOTH);

GLfloat specular[] = {1.0f, 1.0f, 1.0f , 1.0f};

glLightfv(GL_LIGHT0, GL_SPECULAR, specular);

GLfloat ambient[] = { 1.0f, 1.0f, 1.0f };

glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);

GLfloat position[] = { -1.0f, 1.0f, 0, 1.0f };

glLightfv(GL_LIGHT0, GL_POSITION, position);

glLoadIdentity();

glScalef(0.5, 0.5, 0.5);

glRotatef(rotation,1,0,0);

glRotatef(rotation,0,1,0);

glRotatef(rotation,0,0,1);

// After all of transformations - draw the graphics

double pi = 3.14159265359;

glColor3f(1, 0.12, 0.42);

glBegin(GL_LINES);

glVertex3f(0, 0, 0);

glVertex3f(0, 1, 0);

for(double theta = 0; theta < 360; theta++){

double x = 0.015 * cosf(theta * pi / 180);

double y = 0.015 * sinf(theta * pi / 180);

glVertex3f(x, 0, y);

glVertex3f(x, 1, y);

}

glEnd();

glBegin(GL_LINES);

for(double theta = 0; theta < 360; theta++){

double x = 0.1 * cosf(theta * pi / 180);

double y = 0.1 * sinf(theta * pi / 180);

glVertex3f(x, 1, y);

glVertex3f(0, 1.3, 0);

}

glEnd();

glColor3f(0.5, 0.5, 0.5);

glBegin(GL_POINTS);

for(double theta = 0; theta < 360; theta++){

double x = 0.3 * cosf(theta * pi / 180);

double y = 0.3 * sinf(theta * pi / 180);

glVertex3f(x, 0, y);

}

glEnd();

glColor3f(0.4, 0.2, 0.2);

glBegin(GL_LINES);

for(double theta = 0; theta < 360; theta++){

double x = 0.025 * cosf(theta * pi / 180);

double y = 0.025 * sinf(theta * pi / 180);

glVertex3f(x, 1, y);

}

glEnd();

// bottom arrow

glColor3f(1, 0.12, 0.42);

glBegin(GL_LINES);

glVertex3f(0, 0, 0);

glVertex3f(0, -1, 0);

for(double theta = 0; theta < 360; theta++){

double x = 0.015 * cosf(theta * pi / 180);

double y = 0.015 * sinf(theta * pi / 180);

glVertex3f(x, 0, y);

glVertex3f(x, -1, y);

}

glEnd();

glBegin(GL_LINES);

for(double theta = 0; theta < 360; theta++){

double x = 0.1 * cosf(theta * pi / 180);

double y = 0.1 * sinf(theta * pi / 180);

glVertex3f(x, -1, y);

glVertex3f(0, -1.3, 0);

}

glEnd();

glColor3f(0.5, 0.5, 0.5);

glBegin(GL_POINTS);

for(double theta = 0; theta < 360; theta++){

double x = 0.3 * cosf(theta * pi / 180);

double y = 0.3 * sinf(theta * pi / 180);

glVertex3f(x, -1, y);

}

glEnd();

glColor3f(0.4, 0.2, 0.2);

glBegin(GL_LINES);

for(double theta = 0; theta < 360; theta++){

double x = 0.025 * cosf(theta * pi / 180);

double y = 0.025 * sinf(theta * pi / 180);

glVertex3f(x, -1, y);

}

glEnd();

glFlush();

rotation++;

}