gpt4 book ai didi

opengl - 使用 openGL

转载 作者:行者123 更新时间:2023-12-02 11:35:58 25 4
gpt4 key购买 nike

我正在尝试使用 openGL 制作一个小 3D 场景并包含一些照明,我对我的场景很好(尽管它没有什么特别的),并且我正在尝试添加一些照明以赋予其一些效果。然而,我可以在我的讲台上添加 Material (没有纹理),这给了我光,而任何有纹理的东西都不会应用任何 Material ,所以它违背了拥有灯光的意义。这是一些代码。

// Setup GL_LIGHT0
glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient); // Setup ambient light
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse); // Setup diffuse light
glLightfv(GL_LIGHT0, GL_SPECULAR, lightSpecular); // Setup specular light

glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, ca);
glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, la);
glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, qa);

glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 180.0f);
glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 0.0);

// Setup GL_LIGHT1
glLightfv(GL_LIGHT1, GL_AMBIENT, lightAmbient); // Setup ambient light
glLightfv(GL_LIGHT1, GL_DIFFUSE, lightDiffuse); // Setup diffuse light
glLightfv(GL_LIGHT1, GL_SPECULAR, lightSpecular); // Setup specular light

glLightf(GL_LIGHT1, GL_CONSTANT_ATTENUATION, ca);
glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, la);
glLightf(GL_LIGHT1, GL_QUADRATIC_ATTENUATION, qa);

glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 180.0f);
glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, 0.0);
// Setup GL_LIGHT2
glLightfv(GL_LIGHT2, GL_AMBIENT, lightAmbient); // Setup ambient light
glLightfv(GL_LIGHT2, GL_DIFFUSE, lightDiffuse); // Setup diffuse light
glLightfv(GL_LIGHT2, GL_SPECULAR, lightSpecular); // Setup specular light

glLightf(GL_LIGHT2, GL_CONSTANT_ATTENUATION, ca);
glLightf(GL_LIGHT2, GL_LINEAR_ATTENUATION, la);
glLightf(GL_LIGHT2, GL_QUADRATIC_ATTENUATION, qa);

glLightf(GL_LIGHT2, GL_SPOT_CUTOFF, 180.0f);
glLightf(GL_LIGHT2, GL_SPOT_EXPONENT, 0.0);
// Setup GL_LIGHT3
glLightfv(GL_LIGHT3, GL_AMBIENT, lightAmbient); // Setup ambient light
glLightfv(GL_LIGHT3, GL_DIFFUSE, lightDiffuse); // Setup diffuse light
glLightfv(GL_LIGHT3, GL_SPECULAR, lightSpecular); // Setup specular light

glLightf(GL_LIGHT3, GL_CONSTANT_ATTENUATION, ca);
glLightf(GL_LIGHT3, GL_LINEAR_ATTENUATION, la);
glLightf(GL_LIGHT3, GL_QUADRATIC_ATTENUATION, qa);

glLightf(GL_LIGHT3, GL_SPOT_CUTOFF, 180.0f);
glLightf(GL_LIGHT3, GL_SPOT_EXPONENT, 0.0);

// OpenGL provides a global ambient light component - we don't want this so set to zero
GLfloat global_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient);

并渲染我的房间 + 3d 对象。

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHT2);
glEnable(GL_LIGHT3);

// Define position and direction
// NOTE: Placing these commands AFTER the above viewing (camera) transformations means the light position and direction appear to have a set point / direction in the 3D environment. If these commands are placed BEFORE the above viewing transformations, then the light appears to move with the camera (as if it is attached to the camera!)
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, lightDirection);
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);

glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, lightDirection1);
glLightfv(GL_LIGHT1, GL_POSITION, lightPosition1);

glLightfv(GL_LIGHT2, GL_SPOT_DIRECTION, lightDirection2);
glLightfv(GL_LIGHT2, GL_POSITION, lightPosition2);

glLightfv(GL_LIGHT3, GL_SPOT_DIRECTION, lightDirection3);
glLightfv(GL_LIGHT3, GL_POSITION, lightPosition3);


// setup materials for objects to draw
glMaterialfv(GL_FRONT, GL_AMBIENT, ambientMaterial);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuseMaterial);
glMaterialfv(GL_FRONT, GL_SPECULAR, specularMaterial);


// 2. Draw scene
glPushMatrix();
drawRoom();
glTranslatef(0.0,-0.75,0.0);
glRotatef(90.0, 1.0,0.0, 0.0);
glRotatef(-spin, 0.0,0.0, 1.0);
glColor3f(1.0, 1.0, 1.0);
drawPodium();
glPopMatrix();

// Save transformations prior to rendering scene
glPushMatrix();

glColor3f(0.0, 0.0, 1.0);

// Setup texture state
glEnable(GL_TEXTURE_2D);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

glRotatef(spin, 0.0,1.0, 0.0);
// Render model

glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuseMaterial2);

glTranslatef(0.0,0.8,0.0);
glRotatef(1.0,1.0,0.0,0.0);
myModel->renderTexturedModel();

// Reset texture state
glDisable(GL_TEXTURE_2D);

// restore transformations after rendering scene
glPopMatrix();

我的 4 个灯的当前结果,在直升机上方的方形形状中,全部向下看向 0.0 是:

My scene

如果您能辨认出白色虚线,它们就是灯光所注视的方向。讲台的表面被照亮,讲台的侧面在旋转时也被照亮。然而,房间和直升机的其余部分对光线没有反应,甚至没有启用照明,因此我认为这是因为除了讲台之外的所有东西都有纹理。

最佳答案

您需要设置 glTexEnv 来指定纹理对象的光照方式

参见:

21.030 当我打开纹理映射时,为什么照明不起作用? http://www.opengl.org/resources/faq/technical/texture.htm

或者更直接地解决您的问题,该行

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

是导致该行为的原因。正如常见问题解答所解释的,当您指定 GL_REPLACE 时,您将用纹理颜色替换原始照明颜色,这会覆盖所有照明计算。您可以完全删除此行,因为 GL_MODULATE 是默认行为。

关于opengl - 使用 openGL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9624198/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com