opengl y-axis 不随 x 或 z 缩放
opengl y-axis not scaling with x or z
我什至不确定如何描述这个问题所以请原谅糟糕的标题。我有一个简单的模型(它实际上是一个瓷砖,但我把它做成一个立方体以更好地说明这个问题)是 2 个单位高、宽和深。为了绘制一个连续的区域,我只需将 X 和 Z 适当地增加 2,它们就会很好地彼此相邻呈现。如果我想创建一个台阶,以便我的平场有一个新的水平,我将 2 添加到字段的一段的 Y 值,期望顶层的底部然后与较低层的顶部完美对齐.
实际发生的是顶层在较低层之上渲染了一段距离。为什么?什么会导致这个?我 运行 进行了一些测试,发现我必须将 Y 增加 0.6 到 0.7 之间的某个数字才能使底部与顶部正确对齐。
我以为可能是视口问题,但我认为这很好。模型看起来没有变形。有没有人 运行 以前遇到过这样的事情?
有关我所说内容的示例,请参见所附图片。红线说明了顶层和底层的这种 st运行ge 分离。
渲染函数
public void draw() throws Exception {
float x = 0;
double y = 0;
float z = 0;
int cidx = 0;
boolean firstCube = true;
glfwSwapBuffers(window); // swap the color buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer
//calc rotate camera
if (updatecamera == true){
updateCamera();
}
glUseProgram(shader.iProgram);
//some lighting...
Vector4f lp = new Vector4f(lightX, lightY, lightZ,1.0f);
//float[] lp = {xa, ya + 100, za - 120,1.0f}; //set light source to same as camera eye for now.
shader.setUniform(iLightCam, camera);
shader.setUniform(iLightVec, lp);
//get picking ray
if (worldClicked == true){
pick = makeRay(pick, cursorX, (DISPLAY_HEIGHT - ((DISPLAY_HEIGHT - VP_HEIGHT) / 2)) - cursorY);
}
for(Iterator<Quad> qd = quads.iterator(); qd.hasNext(); ) {
//init cull check
frust.cullIn = 0;
frust.cullOut = 0;
quad = qd.next();
pickthisQuad = false;
firstCube = true; //the first cube is used to set the values of the Quad OBB.
for(Iterator<Cube> i = quad.cubes.iterator(); i.hasNext(); ) {
cb = i.next();
x = cb.x;
z = cb.z;
//y = cb.y;
//testing odd Y behaviour
if ( y == 0) {
y = lightX;
}else{
y = 0;
}
System.out.println(" y: " + y);
//init
model.setIdentity();
//ROTATE
//set translate
vTrans.set(transx + x, (float) (transy + y), transz + z);
Matrix4f.translate(vTrans, model1, model);
vTrans.set(-(transx + x), (float) (-transy + y), -(transz + z));
Matrix4f.translate(vTrans, model, model);
Matrix4f.rotate((float) Math.toRadians(rotY), new Vector3f(0,1,0), model, model);
vTrans.set((transx + x), (float) (transy + y), (transz + z));
Matrix4f.translate(vTrans, model, model);
Matrix4f.mul(model, camera, modelview);
shader.setUniform(iModelView, modelview);
Matrix3f norm = new Matrix3f();
norm.m00 = modelview.m00;
norm.m01 = modelview.m01;
norm.m02 = modelview.m02;
norm.m10 = modelview.m10;
norm.m11 = modelview.m11;
norm.m12 = modelview.m12;
norm.m20 = modelview.m20;
norm.m21 = modelview.m21;
norm.m22 = modelview.m22;
shader.setUniform(iNorm, norm);
shader.setUniform(iProj, projection);
shader.setUniform(iCam, camera);
shader.setUniform(iModel, model);
test_renderFrustumandCrosslines();
manageTextures(cb);
render();
cidx++;
}//cubes
cidx = 0;
}//quads
/**
* TESTING
*/
glUseProgram(shaderLine.iProgram);
Matrix4f mvp = new Matrix4f();
mvp.setIdentity();
Matrix4f.mul(projection, camera, mvp);
shaderLine.setUniform(iMVPLine, mvp);
renderLine();
renderCross();
worldClicked = false;
glFinish();
}
对于轮换代码中的第2个翻译有什么特别的想法吗? x 和 z 平移将相互抵消,但不会抵消 y 轴。这可能是问题的根源。
vTrans.set(transx + x, (float) (transy + y), transz + z);
Matrix4f.translate(vTrans, model1, model);
vTrans.set(-(transx + x), (float) (-transy + y), -(transz + z));
Matrix4f.translate(vTrans, model, model);
如果删除这 4 行会发生什么?旋转后你还是做翻译。
我什至不确定如何描述这个问题所以请原谅糟糕的标题。我有一个简单的模型(它实际上是一个瓷砖,但我把它做成一个立方体以更好地说明这个问题)是 2 个单位高、宽和深。为了绘制一个连续的区域,我只需将 X 和 Z 适当地增加 2,它们就会很好地彼此相邻呈现。如果我想创建一个台阶,以便我的平场有一个新的水平,我将 2 添加到字段的一段的 Y 值,期望顶层的底部然后与较低层的顶部完美对齐.
实际发生的是顶层在较低层之上渲染了一段距离。为什么?什么会导致这个?我 运行 进行了一些测试,发现我必须将 Y 增加 0.6 到 0.7 之间的某个数字才能使底部与顶部正确对齐。
我以为可能是视口问题,但我认为这很好。模型看起来没有变形。有没有人 运行 以前遇到过这样的事情?
有关我所说内容的示例,请参见所附图片。红线说明了顶层和底层的这种 st运行ge 分离。
渲染函数
public void draw() throws Exception {
float x = 0;
double y = 0;
float z = 0;
int cidx = 0;
boolean firstCube = true;
glfwSwapBuffers(window); // swap the color buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer
//calc rotate camera
if (updatecamera == true){
updateCamera();
}
glUseProgram(shader.iProgram);
//some lighting...
Vector4f lp = new Vector4f(lightX, lightY, lightZ,1.0f);
//float[] lp = {xa, ya + 100, za - 120,1.0f}; //set light source to same as camera eye for now.
shader.setUniform(iLightCam, camera);
shader.setUniform(iLightVec, lp);
//get picking ray
if (worldClicked == true){
pick = makeRay(pick, cursorX, (DISPLAY_HEIGHT - ((DISPLAY_HEIGHT - VP_HEIGHT) / 2)) - cursorY);
}
for(Iterator<Quad> qd = quads.iterator(); qd.hasNext(); ) {
//init cull check
frust.cullIn = 0;
frust.cullOut = 0;
quad = qd.next();
pickthisQuad = false;
firstCube = true; //the first cube is used to set the values of the Quad OBB.
for(Iterator<Cube> i = quad.cubes.iterator(); i.hasNext(); ) {
cb = i.next();
x = cb.x;
z = cb.z;
//y = cb.y;
//testing odd Y behaviour
if ( y == 0) {
y = lightX;
}else{
y = 0;
}
System.out.println(" y: " + y);
//init
model.setIdentity();
//ROTATE
//set translate
vTrans.set(transx + x, (float) (transy + y), transz + z);
Matrix4f.translate(vTrans, model1, model);
vTrans.set(-(transx + x), (float) (-transy + y), -(transz + z));
Matrix4f.translate(vTrans, model, model);
Matrix4f.rotate((float) Math.toRadians(rotY), new Vector3f(0,1,0), model, model);
vTrans.set((transx + x), (float) (transy + y), (transz + z));
Matrix4f.translate(vTrans, model, model);
Matrix4f.mul(model, camera, modelview);
shader.setUniform(iModelView, modelview);
Matrix3f norm = new Matrix3f();
norm.m00 = modelview.m00;
norm.m01 = modelview.m01;
norm.m02 = modelview.m02;
norm.m10 = modelview.m10;
norm.m11 = modelview.m11;
norm.m12 = modelview.m12;
norm.m20 = modelview.m20;
norm.m21 = modelview.m21;
norm.m22 = modelview.m22;
shader.setUniform(iNorm, norm);
shader.setUniform(iProj, projection);
shader.setUniform(iCam, camera);
shader.setUniform(iModel, model);
test_renderFrustumandCrosslines();
manageTextures(cb);
render();
cidx++;
}//cubes
cidx = 0;
}//quads
/**
* TESTING
*/
glUseProgram(shaderLine.iProgram);
Matrix4f mvp = new Matrix4f();
mvp.setIdentity();
Matrix4f.mul(projection, camera, mvp);
shaderLine.setUniform(iMVPLine, mvp);
renderLine();
renderCross();
worldClicked = false;
glFinish();
}
对于轮换代码中的第2个翻译有什么特别的想法吗? x 和 z 平移将相互抵消,但不会抵消 y 轴。这可能是问题的根源。
vTrans.set(transx + x, (float) (transy + y), transz + z);
Matrix4f.translate(vTrans, model1, model);
vTrans.set(-(transx + x), (float) (-transy + y), -(transz + z));
Matrix4f.translate(vTrans, model, model);
如果删除这 4 行会发生什么?旋转后你还是做翻译。