我遇到了一些 GLSL 问题。当我只使用没有着色器的普通 VBO 时,一切都按预期显示在屏幕上,我可以四处走动。
一旦我链接着色器,它就像它“冻结”,绘制一次,并且不刷新。我检查了更新和渲染帧正在按应有的方式执行(进行了消息框输出检查以确保它到达代码)。
我认为它必须与矩阵有关,但我不知道它是什么
这是我所拥有的:
着色器创建:
private int vertShadHandle, fragShadHandle;
private int shadProgHandle;
private void ShaderCreate()
{
string vertShadeSource, fragShadeSource, vertcontent,fragcontent;
vertcontent = System.IO.File.ReadAllText(@"resources/shaders/vertcoordshader.glsl");
fragcontent = System.IO.File.ReadAllText(@"resources/shaders/fragcoordshader.glsl");
vertShadeSource = "resources/shaders/vertcoordshader.glsl";
fragShadeSource = "resources/shaders/fragcoordshader.glsl";
Console.WriteLine("\nvertex shader path:" + System.IO.File.Exists("/resources/shaders/vertcoordshader.glsl"));
Console.WriteLine("frag shader path:" + System.IO.File.Exists("/resources/shaders/fragcoordshader.glsl\n"));
vertShadHandle= GL.CreateShader(ShaderType.VertexShader); //connect the handles to created shaders
fragShadHandle = GL.CreateShader(ShaderType.FragmentShader);
shadProgHandle = GL.CreateProgram(); //create shader program
GL.ShaderSource(vertShadHandle,vertcontent); //attach file to the shader handle
GL.ShaderSource(fragShadHandle,fragcontent);
GL.CompileShader(vertShadHandle); //compile the shaders
GL.CompileShader(fragShadHandle);
Console.WriteLine("\n" + GL.GetShaderInfoLog(vertShadHandle) + "\n");
Console.WriteLine("\n" + GL.GetShaderInfoLog(fragShadHandle) + "\n");
GL.AttachShader(shadProgHandle, vertShadHandle); //attach the shader to the program created
GL.AttachShader(shadProgHandle, fragShadHandle);
//BindAttributes go here (optional)
GL.BindAttribLocation(shadProgHandle,0,"a_Vertex");
GL.BindAttribLocation(shadProgHandle, 1, "a_Color");
GL.UniformMatrix4(GL.GetUniformLocation(shadProgHandle,"MVP"), false, ref MVP);
GL.LinkProgram(shadProgHandle); //sanity check on the created shader program, links it
Console.WriteLine("\n"+ GL.GetProgramInfoLog(shadProgHandle) +"\n");
GL.DeleteShader(vertShadHandle);
GL.DeleteShader(fragShadHandle);
}
更新框架:
protected override void OnUpdateFrame(FrameEventArgs e)
{
const float PIE = 3.141592654f;
gamePad = XNA.GamePad.GetState(Microsoft.Xna.Framework.PlayerIndex.One);
camAngle.Y += gamePad.ThumbSticks.Right.X * (float)e.Time * 180;
camAngle.X += gamePad.ThumbSticks.Right.Y * (float)e.Time * 180;
Vector2 RadAng = new Vector2();
RadAng.X = (camAngle.X / 180 * PIE);
RadAng.Y = (camAngle.Y / 180 * PIE);
if (gamePad.ThumbSticks.Left.Y > 0)
{
camCoord.X -= (float)(Math.Sin(RadAng.Y) * gamePad.ThumbSticks.Left.Y) * (float)(e.Time);
camCoord.Z += (float)(Math.Cos(RadAng.Y) * gamePad.ThumbSticks.Left.Y) * (float)(e.Time);
camCoord.Y += (float)(Math.Sin(RadAng.X) * gamePad.ThumbSticks.Left.Y) * (float)(e.Time);
}
if (gamePad.ThumbSticks.Left.Y < 0)
{
camCoord.X -= (float)(Math.Sin(RadAng.Y) * gamePad.ThumbSticks.Left.Y) * (float)(e.Time);
camCoord.Z += (float)(Math.Cos(RadAng.Y) * gamePad.ThumbSticks.Left.Y) * (float)(e.Time);
camCoord.Y += (float)(Math.Sin(RadAng.X) * gamePad.ThumbSticks.Left.Y) * (float)(e.Time);
}
if (gamePad.ThumbSticks.Left.X > 0)
{
camCoord.X -= (float)(Math.Cos(RadAng.Y) * gamePad.ThumbSticks.Left.X) * (float)(e.Time);
camCoord.Z -= (float)(Math.Sin(RadAng.Y) * gamePad.ThumbSticks.Left.X) * (float)(e.Time);
}
if (gamePad.ThumbSticks.Left.X < 0)
{
camCoord.X -= (float)(Math.Cos(RadAng.Y) * gamePad.ThumbSticks.Left.X) * (float)(e.Time);
camCoord.Z -= (float)(Math.Sin(RadAng.Y) * gamePad.ThumbSticks.Left.X) * (float)(e.Time);
}
//camCoord.X -= (float)(Math.Sin(RadAng.Y));
//camCoord.Z += (float)(Math.Cos(RadAng.Y));
//camCoord.Y += (float)(Math.Sin(RadAng.X));
if (camAngle.X > 89)
{
camAngle.X = 89;
}
if (camAngle.X < -89)
{
camAngle.X = -89;
}
if (camAngle.Y > 360)
{
camAngle.Y -= 360;
}
else if (camAngle.Y < 0)
{
camAngle.Y += 360;
}
CameraMatrix();
MVP = Matrix4.Mult(camMatrix, perspectivematrix);
}
相机功能:
public Matrix4 MVP = new Matrix4();
public Matrix4 camMatrix = new Matrix4();
private void CameraMatrix()
{
camMatrix = Matrix4.Identity;
camMatrix = Matrix4.CreateRotationX(camAngle.X);
camMatrix = Matrix4.CreateRotationY(camAngle.Y);
camMatrix = Matrix4.CreateTranslation(camCoord);
//GL.Rotate(camAngle.X, 1, 0, 0);
//GL.Rotate(camAngle.Y, 0, 1, 0);
//GL.Translate(camCoord);
}
渲染帧:
protected override void OnRenderFrame(FrameEventArgs e)
{
//Qfont display debug info
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit);
coordSys.Draw(ref shadProgHandle);
GL.PushAttrib(AttribMask.ColorBufferBit);
int qFontLine = 0;
QFont.Begin();
InfFont.Print("FPS: " + Math.Round(RenderFrequency, 2), new Vector2(0, 0));
++qFontLine;
InfFont.Print(new String('\n', ++qFontLine) + "X: " + -Math.Round(camCoord.X, 2));
InfFont.Print(new String('\n', ++qFontLine) + "Y: " + -Math.Round(camCoord.Y, 2));
InfFont.Print(new String('\n', ++qFontLine) + "Z: " + -Math.Round(camCoord.Z, 2));
++qFontLine;
InfFont.Print(new String('\n', ++qFontLine) + "H-Angle: " + -Math.Round(camAngle.Y, 2));
InfFont.Print(new String('\n', ++qFontLine) + "V-Angle: " + -Math.Round(camAngle.X, 2));
++qFontLine;
InfFont.Print(new String('\n', ++qFontLine) + "Thumbsticks: ");
InfFont.Print(new String('\n', ++qFontLine) + "Left.X : " + Math.Round(gamePad.ThumbSticks.Left.X,2));
InfFont.Print(new String('\n', ++qFontLine) + "Left.Y : " + Math.Round(gamePad.ThumbSticks.Left.Y,2));
InfFont.Print(new String('\n', ++qFontLine) + "Right.X: " + Math.Round(gamePad.ThumbSticks.Right.X,2));
InfFont.Print(new String('\n', ++qFontLine) + "Right.X: " + Math.Round(gamePad.ThumbSticks.Right.Y,2));
QFont.End();
GL.PopAttrib();
SwapBuffers();
}
绘图命令
GL.UseProgram(ShaderProgramHandle);
GL.EnableVertexAttribArray(0);
GL.EnableVertexAttribArray(1);
GL.BindBuffer(BufferTarget.ArrayBuffer, _VBO[1]);
GL.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, false, 0, 0);
GL.BindBuffer(BufferTarget.ArrayBuffer, _VBO[0]);
GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 0, 0);
GL.DrawArrays(BeginMode.Lines, 0, 6);
GL.DisableVertexAttribArray(0);
GL.DisableVertexAttribArray(1);
最后,分别是顶点和片段着色器:
#version 330
in vec3 a_Vertex;
in vec3 a_Color;
uniform mat4 MVP;
out vec3 color;
void main()
{
gl_Position = vec4(a_Vertex,1.0);// * MVP;
color = a_Color;
}
#version 330
in vec3 color;
out vec4 outColor;
void main()
{
outColor = vec4(1,1,1,1.0);
}