- fix some rendering problems in visual mode

This commit is contained in:
Magnus Norddahl 2019-08-15 14:49:49 +02:00
parent a292300eaa
commit 8ea439a5c2
9 changed files with 51 additions and 92 deletions

View file

@ -3459,7 +3459,7 @@ namespace CodeImp.DoomBuilder.Data
// Setup matrices
Vector3 offset = new Vector3(0f, 0f, -1.8f); // Sphere size is 10 mu
Matrix mworld = Matrix.Multiply(Matrix.Identity, Matrix.Translation(offset) * Matrix.Scaling(1.0f, 1.0f, yscale));
Matrix mprojection = Matrix.PerspectiveFovLH(Angle2D.PIHALF, 1.0f, 0.5f, 100.0f);
Matrix mprojection = Matrix.PerspectiveFov(Angle2D.PIHALF, 1.0f, 0.5f, 100.0f);
// Place camera at origin
General.Map.Graphics.SetUniform(UniformName.campos, new Vector4());
@ -3738,7 +3738,7 @@ namespace CodeImp.DoomBuilder.Data
}
Vector3 eye = new Vector3();
return Matrix.LookAtLH(eye, lookdir, updir);
return Matrix.LookAt(eye, lookdir, updir);
}
private static Texture TextureFromBitmap(Image image)

View file

@ -135,77 +135,36 @@ namespace CodeImp.DoomBuilder.Rendering
return result;
}
public static Matrix LookAtLH(Vector3 eye, Vector3 target, Vector3 up)
public static Matrix LookAt(Vector3 eye, Vector3 target, Vector3 up)
{
Vector3 zaxis = Vector3.Normalize(target - eye);
Vector3 xaxis = Vector3.Cross(up, zaxis);
Vector3 xaxis = Vector3.Normalize(Vector3.Cross(up, zaxis));
Vector3 yaxis = Vector3.Cross(zaxis, xaxis);
Matrix result = Null;
result.M11 = xaxis.X;
result.M11 = -xaxis.X;
result.M12 = yaxis.X;
result.M13 = zaxis.X;
result.M21 = xaxis.Y;
result.M13 = -zaxis.X;
result.M21 = -xaxis.Y;
result.M22 = yaxis.Y;
result.M23 = zaxis.Y;
result.M31 = xaxis.Z;
result.M23 = -zaxis.Y;
result.M31 = -xaxis.Z;
result.M32 = yaxis.Z;
result.M33 = zaxis.Z;
result.M41 = -Vector3.Dot(xaxis, eye);
result.M42 = -Vector3.Dot(yaxis, eye);
result.M43 = -Vector3.Dot(zaxis, eye);
result.M33 = -zaxis.Z;
result.M44 = 1.0f;
return result;
return Matrix.Translation(-eye) * result;
}
public static Matrix LookAtRH(Vector3 eye, Vector3 target, Vector3 up)
public static Matrix PerspectiveFov(float fov, float aspect, float znear, float zfar)
{
Vector3 zaxis = Vector3.Normalize(target - eye);
Vector3 xaxis = Vector3.Cross(up, zaxis);
Vector3 yaxis = Vector3.Cross(zaxis, xaxis);
float f = (float)(1.0 / Math.Tan(fov * 0.5f));
Matrix result = Null;
result.M11 = xaxis.X;
result.M12 = yaxis.X;
result.M13 = zaxis.X;
result.M21 = xaxis.Y;
result.M22 = yaxis.Y;
result.M23 = zaxis.Y;
result.M31 = xaxis.Z;
result.M32 = yaxis.Z;
result.M33 = zaxis.Z;
result.M41 = Vector3.Dot(xaxis, eye);
result.M42 = Vector3.Dot(yaxis, eye);
result.M43 = Vector3.Dot(zaxis, eye);
result.M44 = 1.0f;
return result;
}
public static Matrix PerspectiveFovLH(float fov, float aspect, float znear, float zfar)
{
float yScale = (float)(1.0 / Math.Tan(fov * 0.5f));
float xScale = yScale / aspect;
Matrix result = Null;
result.M11 = xScale;
result.M22 = yScale;
result.M33 = zfar / (znear - zfar);
result.M43 = -2.0f * znear * zfar / (znear - zfar);
result.M34 = 1.0f;
return result;
}
public static Matrix PerspectiveFovRH(float fov, float aspect, float znear, float zfar)
{
float yScale = (float)(1.0 / Math.Tan(fov * 0.5f));
float xScale = yScale / aspect;
Matrix result = Null;
result.M11 = xScale;
result.M22 = yScale;
result.M33 = zfar / (znear - zfar);
result.M43 = 2.0f * znear * zfar / (znear - zfar);
result.M34 = -1.0f;
result.M11 = f / aspect;
result.M22 = f;
result.M33 = (zfar + znear) / (znear - zfar);
result.M34 = 2.0f * zfar * znear / (znear - zfar);
result.M43 = -1.0f;
return result;
}

View file

@ -28,7 +28,7 @@ namespace CodeImp.DoomBuilder.Rendering
device.SetVertexDeclaration(VertexDecl);
device.SetVertexBuffer(0, Vertices, 0, WorldVertex.Stride);
device.SetIndexBuffer(Indices);
device.DrawIndexed(PrimitiveType.TriangleList, 0, Count);
device.DrawIndexed(PrimitiveType.TriangleList, 0, Count / 3);
device.SetIndexBuffer(null);
device.SetVertexBuffer(0, null, 0, 0);
device.SetVertexDeclaration(null);

View file

@ -617,7 +617,7 @@ namespace CodeImp.DoomBuilder.Rendering
campos
}
public enum Cull : int { None, Counterclockwise }
public enum Cull : int { None, Clockwise }
public enum Blend : int { InverseSourceAlpha, SourceAlpha, One, BlendFactor }
public enum BlendOperation : int { Add, ReverseSubtract }
public enum FillMode : int { Solid, Wireframe }

View file

@ -243,7 +243,7 @@ namespace CodeImp.DoomBuilder.Rendering
float fovy = (float)Math.Atan(1.0f / reversefovy) * 2.0f;
// Make the projection matrix
projection = Matrix.PerspectiveFovRH(fovy, aspect, PROJ_NEAR_PLANE, General.Settings.ViewDistance);
projection = Matrix.PerspectiveFov(fovy, aspect, PROJ_NEAR_PLANE, General.Settings.ViewDistance);
viewproj = view3d * projection; //mxd
}
@ -260,9 +260,9 @@ namespace CodeImp.DoomBuilder.Rendering
// Create frustum
frustum = new ProjectedFrustum2D(pos, anglexy, anglez, PROJ_NEAR_PLANE,
General.Settings.ViewDistance, Angle2D.DegToRad(General.Settings.VisualFOV));
// Make the view matrix
view3d = Matrix.LookAtRH(RenderDevice.V3(pos), RenderDevice.V3(lookat), new Vector3(0f, 0f, 1f));
// Make the view matrix
view3d = Matrix.LookAt(RenderDevice.V3(pos), RenderDevice.V3(lookat), new Vector3(0f, 0f, 1f));
viewproj = view3d * projection; //mxd
// Make the billboard matrix
@ -376,7 +376,7 @@ namespace CodeImp.DoomBuilder.Rendering
UpdateLights();
// Initial renderstates
graphics.SetCullMode(Cull.Counterclockwise);
graphics.SetCullMode(Cull.Clockwise);
graphics.SetZEnable(true);
graphics.SetZWriteEnable(true);
graphics.SetAlphaBlendEnable(false);
@ -403,7 +403,7 @@ namespace CodeImp.DoomBuilder.Rendering
graphics.SetAlphaTestEnable(true);
graphics.SetCullMode(Cull.None);
RenderModels(false, false);
graphics.SetCullMode(Cull.Counterclockwise);
graphics.SetCullMode(Cull.Clockwise);
}
// MASK PASS
@ -435,7 +435,7 @@ namespace CodeImp.DoomBuilder.Rendering
graphics.SetUniform(UniformName.ignoreNormals, true);
RenderModels(true, false);
graphics.SetUniform(UniformName.ignoreNormals, false);
graphics.SetCullMode(Cull.Counterclockwise);
graphics.SetCullMode(Cull.Clockwise);
}
}
@ -948,7 +948,7 @@ namespace CodeImp.DoomBuilder.Rendering
// Texture addressing
graphics.SetSamplerState(0, TextureAddress.Wrap);
graphics.SetCullMode(Cull.Counterclockwise); //mxd
graphics.SetCullMode(Cull.Clockwise); //mxd
}
}
@ -1255,7 +1255,7 @@ namespace CodeImp.DoomBuilder.Rendering
// Texture addressing
graphics.SetSamplerState(0, TextureAddress.Wrap);
graphics.SetCullMode(Cull.Counterclockwise); //mxd
graphics.SetCullMode(Cull.Clockwise); //mxd
}
}

View file

@ -196,9 +196,8 @@ HGLRC OpenGLCreationHelper::CreateContext(HDC hdc, int major_version, int minor_
memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.dwFlags |= PFD_DOUBLEBUFFER;
pfd.cColorBits = 24;
pfd.cRedBits = 8;
pfd.cGreenBits = 8;

View file

@ -211,9 +211,22 @@ void RenderDevice::SetVertexDeclaration(VertexDeclaration* decl)
void RenderDevice::StartRendering(bool clear, int backcolor, Texture* target, bool usedepthbuffer)
{
Context.Begin();
ApplyRenderTarget(target, usedepthbuffer);
if (target)
{
glBindFramebuffer(GL_FRAMEBUFFER, target->GetFramebuffer(usedepthbuffer));
glViewport(0, 0, target->GetWidth(), target->GetHeight());
}
else
{
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, Context.GetWidth(), Context.GetHeight());
}
if (clear && usedepthbuffer)
{
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glClearColor(RPART(backcolor) / 255.0f, GPART(backcolor) / 255.0f, BPART(backcolor) / 255.0f, APART(backcolor) / 255.0f);
glClearDepthf(1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@ -223,6 +236,9 @@ void RenderDevice::StartRendering(bool clear, int backcolor, Texture* target, bo
glClearColor(RPART(backcolor) / 255.0f, GPART(backcolor) / 255.0f, BPART(backcolor) / 255.0f, APART(backcolor) / 255.0f);
glClear(GL_COLOR_BUFFER_BIT);
}
mNeedApply = true;
Context.End();
}
@ -359,7 +375,7 @@ void RenderDevice::ApplyRasterizerState()
else
{
glEnable(GL_CULL_FACE);
glFrontFace(GL_CCW);
glFrontFace(GL_CW);
}
GLenum fillMode2GL[] = { GL_FILL, GL_LINE };
@ -547,20 +563,6 @@ void RenderDevice::ApplyTextures()
}
}
void RenderDevice::ApplyRenderTarget(Texture* target, bool usedepthbuffer)
{
if (target)
{
glBindFramebuffer(GL_FRAMEBUFFER, target->GetFramebuffer(usedepthbuffer));
glViewport(0, 0, target->GetWidth(), target->GetHeight());
}
else
{
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, Context.GetWidth(), Context.GetHeight());
}
}
/////////////////////////////////////////////////////////////////////////////
RenderDevice* RenderDevice_New(HWND hwnd)

View file

@ -10,7 +10,7 @@ class ShaderManager;
class Shader;
enum class CubeMapFace;
enum class Cull : int { None, Counterclockwise };
enum class Cull : int { None, Clockwise };
enum class Blend : int { InverseSourceAlpha, SourceAlpha, One, BlendFactor };
enum class BlendOperation : int { Add, ReverseSubtract };
enum class FillMode : int { Solid, Wireframe };
@ -132,7 +132,6 @@ public:
void ApplyRasterizerState();
void ApplyBlendState();
void ApplyDepthState();
void ApplyRenderTarget(Texture* target, bool usedepthbuffer);
void CheckError();

View file

@ -3,7 +3,7 @@
static const char* display2D_vs = R"(
#version 150
in vec3 AttrPos;
in vec3 AttrPosition;
in vec4 AttrColor;
in vec2 AttrUV;
@ -14,7 +14,7 @@ static const char* display2D_vs = R"(
void main()
{
gl_Position = transformsettings * vec4(AttrPos, 1.0f);
gl_Position = transformsettings * vec4(AttrPosition, 1.0f);
Color = AttrColor;
UV = AttrUV;
}