- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要画路灯,但它照亮了整个环境,我无法让它看起来像一个聚光灯。下面的代码是我试过的代码
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);
最佳答案
像这样:
#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;
}
关于c++ - 我们怎样才能制作聚光灯,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8637975/
我想在一个页面上做一个按钮,可以在同一页面调用一个JS函数。该函数将需要创建(打开)新窗口,其 HTML 代码由 JS 函数本身提供。我该怎么做? 这样做的目的是从特定页面生成一个打印友好的页面。 请
我一直在用 php 开发这个项目。该项目的一半是使用 mysql_query 完成的,最新的模块是使用 mysqli 制作的。有很多模块,我不想更改代码。如果是这样的话会不会产生问题。或者我应该将其全
我安装了好几次 qt creator,但它从来没有像我现在的 PC 那样花钱;首先,我使用我的 Pendrive(Qt 5.8 的)上一直有的安装程序,告诉我我无法下载一些存储库,我下载了相同安装程序
我安装了 Qt Creator 5.10.1,当我构建项目时出现错误:“无法确定要运行哪个”make“命令。检查构建配置中的”make“步骤。”。 我已经在另一台 PC 上安装了 Qt,我看到了这个问
看看这个 makefile,它有某种原始的进度指示(可能是一个进度条)。 请给我建议/意见! # BUILD 最初是未定义的 ifndef 构建 # max 等于 256 个 x 十六:= x x x
这个问题会有点长,对此我很抱歉:) 我花了几天时间寻找最好的解决方案,以在 asp mvc 和 JQuery 中制作图像库。 主要问题是当用户点击拇指时显示图像。 我想让整个浏览器 View 变成黑色
我是Python方面的 super 高手。我一直在努力寻找适当的解决方案。这是列表,L = [0, 0, 0, 3, 4, 5, 6, 0, 0, 0, 0, 11, 12, 13, 14, 0, 0
让我们考虑两个简化的 CMakeLists.txt set(GTEST "/usr/local/lib/libgtest.a") set(GMOCK "/usr/local/lib/libgmock.
我如何制作 Makefile,因为这是按源代码分发程序的最佳方式。请记住,这是针对 C++ 程序的,而我是从 C 开发领域开始的。但是可以为我的 Python 程序制作 Makefile 吗? 最佳答
由于 Ord 是 Eq 的子类,我发现很难理解创建该类的新类型实例的样子。 我已经设法做到了: newtype NT1 = NT1 Integer instance Eq NT1 wh
在 PowerShell 中,我想编写一个函数,它接受不同的选项作为参数。没关系,如果它接收多个参数,但它必须接收至少一个参数。我想通过参数定义而不是之后的代码来强制执行它。我可以使用以下代码让它工作
我正在通过构建包使用 enable-ssl 在 heroku (ubuntu) 上安装 ffmpeg。我能够一直构建到这些错误: install: cannot create regular file
我是 FFmpeg 的新手,但作为一个学习一些 mysql 数据库的项目,我正在尝试创建一个视频上传网站。 当我尝试使用此代码制作缩略图时: shell_exec("/usr/local/bin/ff
我想要一个绘制可绘制对象的 Actor ,但将其剪辑为 Actor 的大小。我从 Widget 派生这个类,并使用一些硬编码的值作为一个简单的测试: public class MyWidget ext
我一直在查看 Faxien+Sinan 和 Rebar,Erlang OTP 的基本理念似乎是,在单个 Erlang 镜像实例上安装应用程序和版本。保持发布自包含的最佳实践是什么?有没有办法打包发布,
我正在尝试克隆存储库,但它应该是彼此独立的副本。这背后有什么魔法吗,或者只是使用 svn 客户端并克隆它? 谢谢 最佳答案 试试 svnadmin hotcopy .您可以在 repo mainten
我想做一个这样的菜单: Item 1 Item 2 Item 3 Subitem 1 Subitem 2 但我得到了这个:
为 Yii 创建扩展的最佳方式是什么? 这是我到目前为止所做的 我希望它可以通过 composer 安装,所以我为它创建了一个 github repo。 我在文件夹 vendor/githubname
我尝试制作一个ActionListener,但它给了我一个错误。我导入了事件,但它仍然不起作用。这是我的代码: send.addActionListener(new jj); private clas
我需要能够将 div 内的 HTML 代码恢复为页面就绪状态。我需要这个,因为我想在页面准备好后对 HTML 代码进行一些更改,然后在需要时将其恢复到页面准备好时的状态.. 我想使用克隆,但是如何只复
我是一名优秀的程序员,十分优秀!