如何使 OpenGl 光定向?
How to make OpenGl Light Directional?
我写这段代码是为了在y方向发出红光。我希望它看起来像聚光灯。它是红色的,但它到处都亮着,而不仅仅是 y 方向。知道要改变什么才能让它看起来像聚光灯吗?
glEnable(GL_LIGHT1);
GLfloat ambient1[] = { 0.7f, 0.0f, 0.0f, 1.0f };
glLightfv(GL_LIGHT1, GL_AMBIENT, ambient1);
GLfloat diffuse1[] = { 1.0, 0.0, 0.0, 1.0 };
glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse1);
GLfloat specular1[] = { 1.0f, 1.0f, 1.0f, 1.0f };
glLightfv(GL_LIGHT1, GL_SPECULAR, specular1);
GLfloat light_position1[] = { 0.0f, 0.0f, 0.2f, 0.0f };
glLightfv(GL_LIGHT1, GL_POSITION, light_position1);
GLfloat spotCUtOff[] = { 10.0f };
glLightfv(GL_LIGHT1, GL_SPOT_CUTOFF, spotCUtOff);
GLfloat SPOTDIRECTION[] = { 0.0f, 2.0f, 0.0f };
glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, SPOTDIRECTION);
GLfloat SPOTEXPONENT[] = { 2.0f };
glLightfv(GL_LIGHT1, GL_SPOT_EXPONENT, SPOTEXPONENT);
您的指数和截止值太低。 GL_SPOT_CUTOFF
指定光线分布的角度,GL_SPOT_EXPONENT
指定截止后光线消散的强度。
为了获得合理的聚光灯效果,您应该尝试提高这些值:
GLfloat spotExp = 20.0;
glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, spotExp);
GLfloat spotCutoff = 60.0;
glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, spotCutoff);
这是根据 Mark J. Kilgard here:
的示例改编的 MCVE
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>
static void
Init(void)
{
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_LIGHT1);
GLfloat ambient1[] = { 0.7f, 0.0f, 0.0f, 1.0f };
glLightfv(GL_LIGHT1, GL_AMBIENT, ambient1);
GLfloat diffuse1[] = { 1.0, 0.0, 0.0, 1.0 };
glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse1);
GLfloat specular1[] = { 1.0f, 1.0f, 1.0f, 1.0f };
glLightfv(GL_LIGHT1, GL_SPECULAR, specular1);
GLfloat light_position1[] = { 0.0f, 0.0f, 0.2f, 0.0f };
glLightfv(GL_LIGHT1, GL_POSITION, light_position1);
GLfloat spotExp = 20.0;
glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, spotExp);
GLfloat spotCutoff = 60.0;
glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, spotCutoff);
}
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();
}
}
static void
Draw(void)
{
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glTranslatef(0,1.25,0);
float light_pos[4] = {0,0,0,1};
glLightfv(GL_LIGHT1, GL_POSITION, light_pos);
float light_dir[3] = {0,-1,0};
glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, light_dir);
glPopMatrix();
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();
glFlush();
}
int
main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE);
glutCreateWindow("spotlight");
Init();
glutDisplayFunc(Draw);
glutMainLoop();
return 0;
}
我写这段代码是为了在y方向发出红光。我希望它看起来像聚光灯。它是红色的,但它到处都亮着,而不仅仅是 y 方向。知道要改变什么才能让它看起来像聚光灯吗?
glEnable(GL_LIGHT1);
GLfloat ambient1[] = { 0.7f, 0.0f, 0.0f, 1.0f };
glLightfv(GL_LIGHT1, GL_AMBIENT, ambient1);
GLfloat diffuse1[] = { 1.0, 0.0, 0.0, 1.0 };
glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse1);
GLfloat specular1[] = { 1.0f, 1.0f, 1.0f, 1.0f };
glLightfv(GL_LIGHT1, GL_SPECULAR, specular1);
GLfloat light_position1[] = { 0.0f, 0.0f, 0.2f, 0.0f };
glLightfv(GL_LIGHT1, GL_POSITION, light_position1);
GLfloat spotCUtOff[] = { 10.0f };
glLightfv(GL_LIGHT1, GL_SPOT_CUTOFF, spotCUtOff);
GLfloat SPOTDIRECTION[] = { 0.0f, 2.0f, 0.0f };
glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, SPOTDIRECTION);
GLfloat SPOTEXPONENT[] = { 2.0f };
glLightfv(GL_LIGHT1, GL_SPOT_EXPONENT, SPOTEXPONENT);
您的指数和截止值太低。 GL_SPOT_CUTOFF
指定光线分布的角度,GL_SPOT_EXPONENT
指定截止后光线消散的强度。
为了获得合理的聚光灯效果,您应该尝试提高这些值:
GLfloat spotExp = 20.0;
glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, spotExp);
GLfloat spotCutoff = 60.0;
glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, spotCutoff);
这是根据 Mark J. Kilgard here:
的示例改编的 MCVE#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>
static void
Init(void)
{
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_LIGHT1);
GLfloat ambient1[] = { 0.7f, 0.0f, 0.0f, 1.0f };
glLightfv(GL_LIGHT1, GL_AMBIENT, ambient1);
GLfloat diffuse1[] = { 1.0, 0.0, 0.0, 1.0 };
glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse1);
GLfloat specular1[] = { 1.0f, 1.0f, 1.0f, 1.0f };
glLightfv(GL_LIGHT1, GL_SPECULAR, specular1);
GLfloat light_position1[] = { 0.0f, 0.0f, 0.2f, 0.0f };
glLightfv(GL_LIGHT1, GL_POSITION, light_position1);
GLfloat spotExp = 20.0;
glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, spotExp);
GLfloat spotCutoff = 60.0;
glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, spotCutoff);
}
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();
}
}
static void
Draw(void)
{
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glTranslatef(0,1.25,0);
float light_pos[4] = {0,0,0,1};
glLightfv(GL_LIGHT1, GL_POSITION, light_pos);
float light_dir[3] = {0,-1,0};
glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, light_dir);
glPopMatrix();
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();
glFlush();
}
int
main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE);
glutCreateWindow("spotlight");
Init();
glutDisplayFunc(Draw);
glutMainLoop();
return 0;
}