opengl 内部有不透明项目的翻译框

14ifxucb  于 2024-01-07  发布在  其他
关注(0)|答案(1)|浏览(214)

我试着画一个半透明的盒子,里面有不透明的东西。类似这样:


的数据
对于半透明的盒子,我也创建了一个纹理(whitebg.jpg),我想应用:



到目前为止,这是我尝试的:

主要功能

  1. private static void CreateItems(List<Items> packedItems)
  2. {
  3. Glut.glutInit();
  4. Glut.glutInitDisplayMode(Glut.GLUT_DOUBLE | Glut.GLUT_DEPTH);
  5. Glut.glutInitWindowSize(width, height);
  6. Glut.glutCreateWindow("pack");
  7. Glut.glutIdleFunc(OnRenderFrame);
  8. Glut.glutDisplayFunc(OnDisplay);
  9. Glut.glutKeyboardFunc(OnKeyboardDown);
  10. Glut.glutKeyboardUpFunc(OnKeyboardUp);
  11. Glut.glutMouseWheelFunc(OnMouseWheel);
  12. Glut.glutCloseFunc(OnClose);
  13. Glut.glutReshapeFunc(OnReshape);
  14. Gl.Disable(EnableCap.DepthTest);
  15. Gl.Enable(EnableCap.Blend);
  16. Gl.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
  17. program = new ShaderProgram(VertexShader, FragmentShader);
  18. program.Use();
  19. program["projection_matrix"].SetValue(Matrix4.CreatePerspectiveFieldOfView(0.45f, (float)width / height, 0.1f, 1000f));
  20. program["view_matrix"].SetValue(Matrix4.LookAt(new Vector3(0, 0, 10), Vector3.Zero, new Vector3(0, 1, 0)));
  21. program["light_direction"].SetValue(new Vector3(0, 0, 1));
  22. program["enable_lighting"].SetValue(lighting);
  23. bgTexture = new Texture("whitebg.jpg");
  24. }

字符串

  • 酒店GLUT. ALIDLEFUNC**
  1. private static void OnRenderFrame()
  2. {
  3. watch.Stop();
  4. float deltaTime = (float)watch.ElapsedTicks / System.Diagnostics.Stopwatch.Frequency;
  5. watch.Restart();
  6. // perform rotation of the cube depending on the keyboard state
  7. if (autoRotate)
  8. {
  9. xangle += deltaTime / 2;
  10. yangle += deltaTime;
  11. }
  12. if (right) yangle += deltaTime;
  13. if (left) yangle -= deltaTime;
  14. if (up) xangle -= deltaTime;
  15. if (down) xangle += deltaTime;
  16. // set up the viewport and clear the previous depth and color buffers
  17. Gl.Viewport(0, 0, width, height);
  18. Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
  19. // make sure the shader program and texture are being used
  20. Gl.UseProgram(program);
  21. Gl.BindTexture(bgTexture);
  22. // set up the model matrix and draw the cube
  23. program["model_matrix"].SetValue(Matrix4.CreateRotationY(yangle) * Matrix4.CreateRotationX(xangle));
  24. program["enable_lighting"].SetValue(lighting);
  25. for (int i = 0; i < virtual_items.Count; i++)
  26. {
  27. Gl.BindBufferToShaderAttribute(virtual_items[i], program, "vertexPosition");
  28. Gl.BindBufferToShaderAttribute(virtual_items_color[i], program, "vertexColor");
  29. Gl.BindBuffer(virtual_items_quads[i]);
  30. Gl.DrawElements(BeginMode.Quads, virtual_items_quads[i].Count, DrawElementsType.UnsignedInt, IntPtr.Zero);
  31. }
  32. Gl.BindBufferToShaderAttribute(virtual_container, program, "vertexPosition");
  33. Gl.BindBufferToShaderAttribute(virtual_container_normals, program, "vertexNormal");
  34. Gl.BindBufferToShaderAttribute(virtual_container_UV, program, "vertexUV");
  35. Gl.BindBuffer(virtual_container_Quads);
  36. Gl.DrawElements(BeginMode.Quads, virtual_container_Quads.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);
  37. Glut.glutSwapBuffers();
  38. }


但得到的结果是不是我所期望的。纹理是适用于框,也在里面的项目。这里有一个例子,我有一个框(2x1x1)和两个项目(1x1x1):



有没有可能只在盒子上应用纹理,并保持物品的真实的不透明颜色,就像第一张图片一样?

wnvonmuf

wnvonmuf1#

在你的初始化方法“测试项目”中,你正在禁用深度测试并启用混合:

  1. Gl.Disable(EnableCap.DepthTest);
  2. Gl.Enable(EnableCap.Blend);
  3. Gl.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);

字符串
这些状态在OnRenderFrame方法中不会改变。但是如果你想让项目不透明,那么你必须在绘制它们之前禁用混合。当然,在绘制半透明容器之前重新启用它。
如果你想只在背景中显示半透明的盒子,而项目不受它的影响。那么你应该使用命令glDepthMask(false)禁用深度缓冲来渲染它。然后你使用glDepthMask(true)重新打开它。
此外,您还需要启用深度测试,因为否则项目可能无法正确显示。即,远离相机的面可能会与较近的面重叠。

相关问题