gpt4 book ai didi

c++ - Openframeworks 全局照明

转载 作者:搜寻专家 更新时间:2023-10-31 01:45:32 26 4
gpt4 key购买 nike

我正在开发一个 openframeworks 项目,该项目有一系列立方体,每个立方体内部都由点光源照亮。降低立方体的透明度以让光线通过。我已验证灯光设置为 0,0,0(即立方体的中心)。

理论上,点光源应该均匀地照亮立方体的每一面。然而,立方体的一侧明显较暗,而另一侧较浅。

enter image description here

所以,我想知道是否有一个我不知道的全局光照参数需要以某种方式关闭。我的代码如下:

立方体.cpp

#include "cube.h"

cube::cube(float _w, float _h, float _d, float _cubeHue)
{
w = _w;
h = _h;
d = _d;
cubeHue = _cubeHue;

GLfloat vdata[8][3] = {
{-w, -h, -d}, {-w, h, -d},
{w, h, -d}, {w, -h, -d},
{-w, -h, d}, {w, -h, d},
{-w, h, d}, {w, h, d}
};

GLint indices[6][4] = {
{3, 2, 1, 0},
{3, 5, 4, 0},
{3, 5, 7, 2},
{0, 4, 6, 1},
{1, 2, 7, 6},
{5, 4, 6, 7}
};

cubeColor = ofColor();
cubeColor.setHsb(cubeHue,ofRandom(500,255),ofRandom(100,255), 150);

for (int i=0; i<8; ++i)
{
mesh.addVertex(ofVec3f( vdata[i][0], vdata[i][1], vdata[i][2] ));
mesh.addColor(cubeColor);

for (int i=0; i<6; ++i)
{
mesh.addIndex(indices[i][0]);
mesh.addIndex(indices[i][1]);
mesh.addIndex(indices[i][2]);
mesh.addIndex(indices[i][3]);
}
}

ofEnableLighting();

pointLight.setPointLight();
pointLight.setAttenuation(0.5f);
pointLight.setPosition(ofVec3f(0,0,0));

myVbo.setMesh(mesh, GL_STATIC_DRAW);
}

void cube::draw()
{
pointLight.enable();

myVbo.drawElements(GL_QUADS, 24);
mesh.drawWireframe();

pointLight.disable();
}

void cube::update()
{

}

void cube::setLocation(float _x, float _y, float _z)
{
x = _x;
y = _y;
z = _z;
}

测试应用.cpp

#include "testApp.h"

//--------------------------------------------------------------
void testApp::setup()
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);

ofBackground(33, 33, 33);
ofSetFrameRate(24);

camera.setNearClip(0.1);
camera.setFarClip(1900);
camera.setPosition(ofVec3f(0,0,0));
camAngle = 0;
camX = 400;
camY = 0;
camZ = 400;

cube1 = new cube(50,50,50,75);
cube2 = new cube(50,50,50,220);
cube3 = new cube(50,50,50,80);
cube4 = new cube(50,50,50,190);
cube5 = new cube(50,50,50,120);

rotateAngle[0] = 0.0f;
rotateAngle[1] = 0.0f;
rotateAngle[2] = 0.0f;
rotateAngle[3] = 0.0f;

ofFloatColor diffuseColor;
diffuseColor.set(1.0,1.0,1.0);
}

//--------------------------------------------------------------
void testApp::update()
{
camAngle += 0.02f;

if (camAngle >= 360)
{
camAngle = 0;
}

camX = 800 * sin(camAngle);
camZ = 800 * cos(camAngle);
camera.lookAt(ofVec3f(0, 0, 0));
}

//--------------------------------------------------------------
void testApp::draw()
{
camera.begin();

camera.setPosition(ofVec3f(camX, camY, camZ));

rotateAngle[0] += 1.0f;
rotateAngle[1] += 0.5f;
rotateAngle[2] += 0.75f;
rotateAngle[3] += 1.5f;

if (rotateAngle[0] > 359)
{
rotateAngle[0] = 0.0f;
}

if (rotateAngle[1] > 359)
{
rotateAngle[1] = 0.0f;
}

if (rotateAngle[2] > 359)
{
rotateAngle[2] = 0.0f;
}

if (rotateAngle[3] > 359)
{
rotateAngle[3] = 0.0f;
}

ofPushMatrix();
ofTranslate(20,20,-30);
cube1->draw();
ofPopMatrix();

ofPushMatrix();
ofTranslate(100,-80,30);
cube2->draw();
ofPopMatrix();

ofPushMatrix();
ofTranslate(150,80,100);
cube3->draw();
ofPopMatrix();

ofPushMatrix();
ofTranslate(-150,180,-10);
cube4->draw();
ofPopMatrix();

ofPushMatrix();
ofTranslate(250,-180,-60);
cube5->draw();
ofPopMatrix();
}

最佳答案

我相信这是因为你所有的灯都是在原点创建的。

当您ofTranslate 时,当您渲染它们时,这些光不再位于立方体内部(它们位于“原始原点”)。

为什么不使用立方体的位置成员并将平移移动到立方体中,以便相应地平移光源?

关于c++ - Openframeworks 全局照明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22050234/

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