有人可以帮我解释为什么我无法使用SwapBuffers()吗? 我在Visual Studio 2015中使用WindowsForms c#和OpenTK。我正在使用HelloGL3_OpenTK代码来学习。其他一切似乎都没问题,但是SwapBuffers()行。我收到一条消息: SwapBuffers名称不会出现在当前上下文中。 我真的很想知道什么是错的,所以我希望有人可以提供帮助。
以下是完整的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Platform;
namespace Draw3Dv01wf
{
public partial class Form1 : Form
{
// vertex shader
string vertexShaderSource = @"
#version 140
precision highp float;
uniform mat4 projection_matrix;
uniform mat4 modelview_matrix;
in vec3 in_position;
in vec3 in_normal;
out vec3 normal;
void main(void)
{
//works only for orthogonal modelview
normal = (modelview_matrix * vec4(in_normal, 0)).xyz;
gl_Position = projection_matrix * modelview_matrix * vec4(in_position, 1);
}";
// fragment shader
string fragmentShaderSource = @"
#version 140
precision highp float;
const vec3 ambient = vec3(0.1, 0.1, 0.1);
const vec3 lightVecNormalized = normalize(vec3(0.5, 0.5, 2.0));
const vec3 lightColor = vec3(0.9, 0.9, 0.7);
in vec3 normal;
out vec4 out_frag_color;
void main(void)
{
float diffuse = clamp(dot(lightVecNormalized, normalize(normal)), 0.0, 1.0);
out_frag_color = vec4(ambient + diffuse * lightColor, 1.0);
}";
int vertexShaderHandle,
fragmentShaderHandle,
shaderProgramHandle,
modelviewMatrixLocation,
projectionMatrixLocation,
vaoHandle,
positionVboHandle,
normalVboHandle,
eboHandle;
Vector3[] positionVboData = new Vector3[]{
new Vector3(-1.0f, -1.0f, 1.0f),
new Vector3( 1.0f, -1.0f, 1.0f),
new Vector3( 1.0f, 1.0f, 1.0f),
new Vector3(-1.0f, 1.0f, 1.0f),
new Vector3(-1.0f, -1.0f, -1.0f),
new Vector3( 1.0f, -1.0f, -1.0f),
new Vector3( 1.0f, 1.0f, -1.0f),
new Vector3(-1.0f, 1.0f, -1.0f) };
int[] indicesVboData = new int[]{
// front face
0, 1, 2, 2, 3, 0,
// top face
3, 2, 6, 6, 7, 3,
// back face
7, 6, 5, 5, 4, 7,
// left face
4, 0, 3, 3, 7, 4,
// bottom face
0, 1, 5, 5, 4, 0,
// right face
1, 5, 6, 6, 2, 1, };
Matrix4 projectionMatrix, modelviewMatrix;
public Form1()
{
// required method for designer support
InitializeComponent();
}
// load event handler
private void Form1_Load(object sender, EventArgs e)
{
CreateShaders();
CreateVBOs();
CreateVAOs();
// set color
GL.ClearColor(System.Drawing.Color.MidnightBlue);
// enable or disable server-side capabilities
GL.Enable(EnableCap.DepthTest);
}
// render frame event handler
private void Form1_Render(object sender, EventArgs e)
{
// set viewport
GL.Viewport(0, 0, Width, Height);
// bind vertex array object
GL.BindVertexArray(vaoHandle);
// render primatives from array data
GL.DrawElements(
PrimitiveType.Triangles, // kind of primative ****
indicesVboData.Length, // number of elements
DrawElementsType.UnsignedInt, /* type of values in the
indices - must be UnsignedByte|Short|int */
IntPtr.Zero); /* pointer to location where the
indicies are stored */
// clear frame (to be drawn) and depth buffer
GL.Clear(
ClearBufferMask.ColorBufferBit |
ClearBufferMask.DepthBufferBit);
//**************** SwapBuffers() not recognized ******//
SwapBuffers()
}
// update frame event handler
private void Form1_Update(object sender, EventArgs e)
{
//Matrix4 rotation =
// Matrix4.CreateRotationY((float)e.Time);
//Matrix4.Mult(
// ref rotation, ref modelviewMatrix, out modelviewMatrix);
//GL.UniformMatrix4(
// modelviewMatrixLocation, false, ref modelviewMatrix);
//var keyboard = OpenTK.Input.Keyboard.GetState();
//if (keyboard[OpenTK.Input.Key.Escape])
//{
// Exit();
//}
}
//private void SwapBuffers()
//{
// throw new NotImplementedException();
//}
private void CreateShaders()
{
// create shader object
vertexShaderHandle =
GL.CreateShader(ShaderType.VertexShader);
fragmentShaderHandle =
GL.CreateShader(ShaderType.FragmentShader);
GL.ShaderSource(vertexShaderHandle, vertexShaderSource);
GL.ShaderSource(fragmentShaderHandle, fragmentShaderSource);
// compile shader object
GL.CompileShader(vertexShaderHandle);
GL.CompileShader(fragmentShaderHandle);
Debug.WriteLine(GL.GetShaderInfoLog(vertexShaderHandle));
Debug.WriteLine(GL.GetShaderInfoLog(fragmentShaderHandle));
// Create program
shaderProgramHandle = GL.CreateProgram();
// attach shader object to the program object
GL.AttachShader(shaderProgramHandle, vertexShaderHandle);
GL.AttachShader(shaderProgramHandle, fragmentShaderHandle);
/* associate a generic vertex attribute index with
a named attribute variable */
GL.BindAttribLocation(
shaderProgramHandle, // handle of the program object
0, // index of the generic vertex attribute to bind
"in_position"); /* string containing name of
vertex shader attribute variable to bind */
GL.BindAttribLocation(shaderProgramHandle, 1, "in_normal");
// link program object
GL.LinkProgram(shaderProgramHandle);
Debug.WriteLine(GL.GetProgramInfoLog(shaderProgramHandle));
// installs program object as part of current rendering state
GL.UseProgram(shaderProgramHandle);
// Set uniforms
projectionMatrixLocation =
// location of uniform variable
GL.GetUniformLocation(
shaderProgramHandle, "projection_matrix");
modelviewMatrixLocation =
GL.GetUniformLocation(
shaderProgramHandle, "modelview_matrix");
float aspectRatio = // aspect ratio - width / height
ClientSize.Width / (float)(ClientSize.Height);
// create a perspective projection matrix
Matrix4.CreatePerspectiveFieldOfView(
(float)Math.PI / 4, // angle of fov in y direction
aspectRatio, // aspect ratio of view (width/height)
1, // distance to the near clip plane
100, // distance to the far clip plane
out projectionMatrix); /* projection matrix which
transforms camera space to raster space */
modelviewMatrix =
// build a world spacve to camera space matrix
Matrix4.LookAt(
new Vector3(0, 3, 5), // eye
new Vector3(0, 0, 0), // target
new Vector3(0, 1, 0)); // up
GL.UniformMatrix4(
projectionMatrixLocation, // location
false, // transpose
ref projectionMatrix); // matrix
GL.UniformMatrix4(
modelviewMatrixLocation, false, ref modelviewMatrix);
}
private void CreateVBOs()
{
positionVboHandle = GL.GenBuffer(); // buffer object names
// bind named buffer object
GL.BindBuffer(BufferTarget.ArrayBuffer, positionVboHandle);
// create and initiatize buffer object's data store
GL.BufferData<Vector3>(
BufferTarget.ArrayBuffer, // target buffer object
new IntPtr(positionVboData.Length * Vector3.SizeInBytes),
/* size in bytes of buffer object' new data store */
positionVboData, /* pointer to the data that will be
copied into the data store */
BufferUsageHint.StaticDraw); /* the expected usage
pattern of the data store */
normalVboHandle = GL.GenBuffer();
GL.BindBuffer(BufferTarget.ArrayBuffer, normalVboHandle);
GL.BufferData<Vector3>(BufferTarget.ArrayBuffer,
new IntPtr(positionVboData.Length * Vector3.SizeInBytes),
positionVboData, BufferUsageHint.StaticDraw);
eboHandle = GL.GenBuffer();
GL.BindBuffer(BufferTarget.ElementArrayBuffer, eboHandle);
GL.BufferData(BufferTarget.ElementArrayBuffer,
new IntPtr(sizeof(uint) * indicesVboData.Length),
indicesVboData, BufferUsageHint.StaticDraw);
// bind named buffer object (buffer target, buffer name)
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
}
private void CreateVAOs()
{
/* GL3 allows us to store the vertex layout in a
* "vertex array object" (VAO).
* This means that we don't have to re-issue
* VertexAttribPointer calls every time we try to use a
* different vertex layout - these calls are stored in
* the VAO so we simply need to bind the correct VAO. */
vaoHandle = GL.GenVertexArray();
GL.BindVertexArray(vaoHandle); // bind vertex array object
// enable vertex attribute array
GL.EnableVertexAttribArray(0);
GL.BindBuffer(BufferTarget.ArrayBuffer, positionVboHandle);
GL.VertexAttribPointer(
0, // index
3, // size
VertexAttribPointerType.Float, // type
true, // normalized
Vector3.SizeInBytes, // stride
0); // offset
GL.EnableVertexAttribArray(1);
GL.BindBuffer(BufferTarget.ArrayBuffer, normalVboHandle);
GL.VertexAttribPointer(
1, 3, VertexAttribPointerType.Float,
true, Vector3.SizeInBytes, 0);
// bind buffer
GL.BindBuffer(BufferTarget.ElementArrayBuffer, eboHandle);
// bind vertex array object
GL.BindVertexArray(0);
}
}
}
答案 0 :(得分:0)
我认为第一个问题是你失踪了;
SwapBuffers();
据我所知,您必须从 OpenTK.GameWindow 类派生您的课程,其中
this.SwapBuffers();
已定义。
到上一个答案: 我没有意识到你没有使用以下链接中描述的Windows SwapBuffers(HDC hdc)功能https://msdn.microsoft.com/en-us/library/windows/desktop/dd369060(v=vs.85).aspx