我目前正在学习OpenGL,并且我正在尝试编写类似于本教程中的应用程序的简单应用程序,但是由于某种原因,摄像头的行为确实很奇怪,我不确定是什么原因造成的。我希望我的相机看着立方体,然后用箭头键盘四处走动,但是它不会四处走动,甚至可能不是纯粹的相机问题。有人可以告诉我我到底在做什么错吗?我会喜欢一些代码。如果有人愿意提供帮助而不是在此处检查代码,而不是在此处检查代码,请在下面添加链接。
ClassCamera.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SharpGL;
namespace Demo
{
class Camera
{
static int id = 0;
public string name;
private static float eyeX, eyeY, eyeZ;
private static float centerX, centerY, centerZ;
private static float frontX, frontY, frontZ;
private static float upX, upY, upZ;
private const float cameraSpeed = 0.05f;
public void InitiCamera(OpenGL gl)
{
eyeX = 0;
eyeY = 0;
eyeZ = 3;
centerX = 0;
centerY = 0;
centerZ = 0;
frontX = 0.0f;
frontY = 0.0f;
frontZ = -1.0f;
upX = 0.0f;
upY = 1.0f;
upZ = 0.0f;
Look(gl);
}
public void Look(OpenGL gl)
{
gl.MatrixMode(OpenGL.GL_PROJECTION);
gl.LoadIdentity();
centerX = eyeX + frontX;
centerY = eyeX + frontY;
centerZ = eyeZ + frontZ;
gl.LookAt(eyeX, eyeY, eyeZ, centerX, centerY, centerZ, 0, 1, 0);
}
public void moveUp(OpenGL gl)
{
eyeX += upX * cameraSpeed;
eyeY += upY * cameraSpeed;
eyeZ += upZ * cameraSpeed;
Look(gl);
}
public void moveDown(OpenGL gl)
{
eyeX -= upX * cameraSpeed;
eyeY -= upY * cameraSpeed;
eyeZ -= upZ * cameraSpeed;
Look(gl);
}
public void strafeLeft(OpenGL gl)
{
eyeX -= (frontY * upZ - upY * frontZ) * cameraSpeed;
eyeY -= (frontZ * upX - upZ * frontX) * cameraSpeed;
eyeZ -= (frontX * upY - upX * frontY) * cameraSpeed;
Look(gl);
}
public void strafeRight(OpenGL gl)
{
eyeX += (frontY * upZ - upY * frontZ) * cameraSpeed;
eyeY += (frontZ * upX - upZ * frontX) * cameraSpeed;
eyeZ += (frontX * upY - upX * frontY) * cameraSpeed;
Look(gl);
}
public void moveForward(OpenGL gl)
{
eyeX += frontX * cameraSpeed;
eyeY += frontY * cameraSpeed;
eyeZ += frontZ * cameraSpeed;
Look(gl);
}
public void moveBackward(OpenGL gl)
{
eyeX -= frontX * cameraSpeed;
eyeY -= frontY * cameraSpeed;
eyeZ -= frontZ * cameraSpeed;
Look(gl);
}
}
}
Form.cs:
namespace Demo
{
public partial class Form1 : Form
{
OpenGL gl;
Camera cam = new Camera();
public Form1()
{
InitializeComponent();
cam.InitiCamera(gl);
}
private void openGLControl1_OpenGLInitialized(object sender, EventArgs e)
{
OpenGL gl = openGLControl1.OpenGL;
gl.ClearColor(0, 0, 0, 0);
}
private void openGLControl1_Resized(object sender, EventArgs e)
{
OpenGL gl = openGLControl1.OpenGL;
//set ma tran viewport
gl.Viewport(
0, 0,
openGLControl1.Width,
openGLControl1.Height);
//set ma tran phep chieu
gl.MatrixMode(OpenGL.GL_PROJECTION);
gl.Perspective(60,
openGLControl1.Width / openGLControl1.Height,
1.0, 20.0);
//set ma tran model view
gl.MatrixMode(OpenGL.GL_MODELVIEW);
gl.LookAt(
5, 7, 6,
2, 2, 2,
0, 1, 0);
}
private void openGLControl1_OpenGLDraw(object sender, RenderEventArgs args)
{
OpenGL gl = openGLControl1.OpenGL;
gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
// Draw a coloured cube
drawColouredCube(gl);
gl.Flush();
}
private void drawColouredCube(OpenGL gl)
{
float a = 2.0f;
gl.Begin(OpenGL.GL_QUADS);
gl.Color(0.0f, 1.0f, 0.0f); // Color Blue
gl.Vertex(0.0f, 0.0f, 0.0f); // Top Right Of The Quad (Top)
gl.Vertex(a, 0.0f, 0.0f); // Top Left Of The Quad (Top)
gl.Vertex(a, 0.0f, a); // Bottom Left Of The Quad (Top)
gl.Vertex(0.0f, 0.0f, a); // Bottom Right Of The Quad (Top)
gl.Color(1.0f, 0.5f, 0.0f); // Color Orange
gl.Vertex(0.0f, 0.0f, 0.0f); // Top Right Of The Quad (Bottom)
gl.Vertex(0.0f, a, 0.0f); // Top Left Of The Quad (Bottom)
gl.Vertex(a, a, 0.0f); // Bottom Left Of The Quad (Bottom)
gl.Vertex(a, 0.0f, 0.0f); // Bottom Right Of The Quad (Bottom)
gl.Color(1.0f, 0.0f, 0.0f); // Color Red
gl.Vertex(0.0f, 0.0f, 0.0f); // Top Right Of The Quad (Front)
gl.Vertex(0.0f, a, 0.0f); // Top Left Of The Quad (Front)
gl.Vertex(0.0f, a, a); // Bottom Left Of The Quad (Front)
gl.Vertex(0.0f, 0.0f, a); // Bottom Right Of The Quad (Front)
gl.Color(1.0f, 1.0f, 0.0f); // Color Yellow
gl.Vertex(0.0f, a, a); // Top Right Of The Quad (Back)
gl.Vertex(a, a, a); // Top Left Of The Quad (Back)
gl.Vertex(a, 0.0f, a); // Bottom Left Of The Quad (Back)
gl.Vertex(0.0f, 0.0f, a); // Bottom Right Of The Quad (Back)
gl.Color(0.0f, 0.0f, 1.0f); // Color Blue
gl.Vertex(a, a, a); // Top Right Of The Quad (Left)
gl.Vertex(a, a, 0.0f); // Top Left Of The Quad (Left)
gl.Vertex(a, 0.0f, 0.0f); // Bottom Left Of The Quad (Left)
gl.Vertex(a, 0.0f, a); // Bottom Right Of The Quad (Left)
gl.Color(1.0f, 0.0f, 1.0f); // Color Violet
gl.Vertex(0.0f, a, 0.0f); // Top Right Of The Quad (Right)
gl.Vertex(a, a, 0.0f); // Top Left Of The Quad (Right)
gl.Vertex(a, a, a); // Bottom Left Of The Quad (Right)
gl.Vertex(0.0f, a, a); // Bottom
gl.End();
}
private void openGLControl1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Left:
cam.strafeLeft(gl);
break;
case Keys.Right:
cam.strafeRight(gl);
break;
case Keys.Up:
cam.moveUp(gl);
break;
case Keys.Down:
cam.moveDown(gl);
break;
case Keys.Z:
cam.moveForward(gl);
break;
case Keys.X:
cam.moveBackward(gl);
break;
}
}
}
}
非常感谢