OpenGL/GLUT GL_LINES 的光照不正确
Posted
技术标签:
【中文标题】OpenGL/GLUT GL_LINES 的光照不正确【英文标题】:OpenGL/GLUT Incorrect lighting for GL_LINES 【发布时间】:2014-11-14 01:55:58 【问题描述】:我被要求制作一个简单的旋转木马,通过鼠标左右单击来旋转。对于我使用GL_LINES
的棍子,我遇到的问题是无法定义法线向量或与GL_POLYGON
类似的东西来使照明正确。我在网上搜索并没有找到任何解释 GL_LINES
照明的来源(有人告诉我照明是自动的,我不需要为 GL_LINES
指定任何内容),这就是为什么我问问题。
这是正面的截图,从正面看一切都很好:
这是显示照明问题的背面屏幕截图。特别是,光源在后面,但那些棍子仍然很亮。
这是背面的另一个屏幕截图,也显示了照明问题。
这是指定水平和垂直棒的两个函数:
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();
这里是完整的源代码:
#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;
【问题讨论】:
代码有点多。考虑使用 github? @Minestrone-Soup 肯定会,下次我会使用github。感谢您的建议。 K,但只是说,如果你真的想要这个问题的答案,我认为没有多少人会去钓鱼你的代码。尝试将其编辑为至少喜欢几十行。 ;) GL_lines 是二维对象,因此很难让它们对光照做出正确反应。您最好使用“真实”对象(圆柱体或横梁)。 @didierc 我只为那个中心杆和栅栏使用了横梁。 【参考方案1】:线条的照明与其他对象的照明相同。颜色/亮度是根据法线、光照和材质参数计算得出的。
如果您使用即时模式绘制命令,您可以在为线条调用glVertex3f()
之前使用glNormal3f()
指定法线。如果您不以这种方式指定法线,则基于先前对 glNormal*()
的调用的当前法线将用作您的线路的法线。
现在,显而易见的后续问题是:一条线的法线是多少? 简单的答案是:无论您想要什么,都能给您想要的结果。
举一个典型的例子,如果你在线框模式下绘制一个解析曲面,这个解析曲面的法线可以作为法线。这将给出一个线框模型,其中阴影/亮度对应于表面的形状。
在您的示例中,最明显且可以说是最好的方法是不画线,而是使用某种细梁/圆柱体用多边形表示几何图形。但作为一个练习,我们可以弄清楚这将如何与线条一起使用。
在这种情况下,您希望线条代表无限细的圆柱体。一个实际的圆柱体的每个顶点的法线都指向外面。但是由于这条线只有两个顶点,所以你必须在从端点向外指向并且与线正交的无限向量集中选择一个方向。
我相信通过选择最接近指向视点的法线向量,您会得到合理的结果。如果您实际使用圆柱体,这将导致您通过查看圆柱体的中心线获得的亮度。这些法线可以通过以下方式计算:
-
将模型转换应用于线的原始端点
v1
和v2
。我们将转换后的端点称为v1t
和v2t
。
计算变换线的方向向量为:
v1d = v2t - v1t
v1d.normalize()
计算线端点到视点vp
的向量,并相对于线的方向正交化,归一化:
v1n = vp - v1
v1n -= dot(v1d * v1n) * v1d
v1n.normalize()
对v1n
应用逆模型变换以获得原始对象空间中的法线。
对第二个顶点执行等效计算。
您可以通过对视点进行逆变换,然后直接在对象空间中计算法线来获得相同的结果。
【讨论】:
有趣的解决方案.. 在现代 OpenGL 中,它会在几何着色器中实现吗?您是否也有关于 GL_POINTS 的信息?以上是关于OpenGL/GLUT GL_LINES 的光照不正确的主要内容,如果未能解决你的问题,请参考以下文章