我有一个第三方库,它有一个方法,将函数指针作为第一个参数:
int third_party_method(void (*func)(double*, double*, int, int, double*), ...);
Run Code Online (Sandbox Code Playgroud)
我想传递一个指向类声明如下的方法的指针:
class TestClass
{
public:
void myFunction (double*, double*, int, int, void*);
Run Code Online (Sandbox Code Playgroud)
我试图传递这个函数如下:
TestClass* tc = new TestClass();
using namespace std::placeholders;
third_party_method(std::bind(&TestClass::myFunction, tc, _1, _2, _3, _4, _5), ...);
Run Code Online (Sandbox Code Playgroud)
但是,这不编译:
Conversion of parameter 1 from 'std::tr1::_Bind<_Result_type,_Ret,_BindN>' to 'void (__cdecl *)(double *,double *,int,int,void *)' is not possible
with
[
_Result_type=void,
_Ret=void,
_BindN=std::tr1::_Bind6<std::tr1::_Callable_pmf<void (__thiscall TestClass::* const )(double *,double *,int,int,void *),TestClass,false>,TestClass *,std::tr1::_Ph<1>,std::tr1::_Ph<2>,std::tr1::_Ph<3>,std::tr1::_Ph<4>,std::tr1::_Ph<5>>
]
Run Code Online (Sandbox Code Playgroud)
有什么方法可以将成员传递给函数吗?
我有一个MenuItem带有控制模板的控件模板,可以像这样MenuItem更改Foreground:
<ControlTemplate TargetType="MenuItem">
<StackPanel Background="{Binding Background}" Orientation="Horizontal">
<ContentPresenter Content="{TemplateBinding Icon}"/>
<TextBlock Text="{TemplateBinding Header}"/>
</StackPanel>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Foreground" Value="Red"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)
我想将图标的颜色绑定到该前景色。在这种情况下,图标只是一个圆圈。我尝试了以下绑定:
<MenuItem Header="Sub">
<MenuItem.Icon>
<Ellipse Width="16" Height="16" Fill="{Binding Foreground, RelativeSource={RelativeSource AncestorType=MenuItem}}"/>
</MenuItem.Icon>
</MenuItem>
Run Code Online (Sandbox Code Playgroud)
但是,这会在应用程序启动时出现以下绑定错误,并且椭圆最终不会被渲染:
System.Windows.Data 错误:4:找不到引用“RelativeSource FindAncestor,AncestorType='System.Windows.Controls.MenuItem',AncestorLevel='1”的绑定源。BindingExpression:Path=前景;数据项=空;目标元素是“椭圆”(名称=“”);目标属性是“填充”(类型“画笔”)
我还尝试在图标中放置一个虚拟元素,以传播前景并绑定到此:
<MenuItem.Icon>
<Grid>
<TextBlock Name="foregroundCapture"/>
<Ellipse Width="16" Height="16" Fill="{Binding Foreground, ElementName=foregroundCapture}"/>
</Grid>
</MenuItem.Icon>
Run Code Online (Sandbox Code Playgroud)
但这给出了类似的错误:
System.Windows.Data 错误:4:找不到引用“ElementName=foregroundCapture”的绑定源。BindingExpression:Path=前景;数据项=空;目标元素是“椭圆”(名称=“”);目标属性是“填充”(类型“画笔”)
我怎样才能使绑定工作?仅供参考,图标最终将是资源字典中的一个对象。因此它将无法访问实际的菜单项。
我可以想到几个替代解决方案。但实际上它们都不是很好:
MouseOverIcon并让触发器使用它。Foreground传播属性。这将允许直接在控件内进行直接绑定。<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" …Run Code Online (Sandbox Code Playgroud) 我有一个自定义用户控件,用于注册依赖项属性HoverHeight:
public sealed partial class VirtualPointer : UserControl
{
public static readonly DependencyProperty HoverHeightProperty =
DependencyProperty.Register("HoverHeight", typeof(double),
typeof(VirtualPointer), new PropertyMetadata(1.0,OnHoverHeightChanged));
public double HoverHeight
{
get { return (double)GetValue(HoverHeightProperty); }
set { SetValue(HoverHeightProperty, value); }
}
...
Run Code Online (Sandbox Code Playgroud)
我使用此属性来计算Margins一些子控件与一个相应的组合IValueConverter.
在使用此控件的页面中,我创建了一个Storyboard应该为该HoverHeight属性设置动画的页面:
<Page ...>
<Page.Resources>
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="virtualPointer" Storyboard.TargetProperty="HoverHeight">
<LinearDoubleKeyFrame Value="1.0" KeyTime="0:0:0"/>
<LinearDoubleKeyFrame Value="0.0" KeyTime="0:0:1"/>
<LinearDoubleKeyFrame Value="1.0" KeyTime="0:0:2"/>
<LinearDoubleKeyFrame Value="0.0" KeyTime="0:0:3"/>
<LinearDoubleKeyFrame Value="1.0" KeyTime="0:0:4"/>
</DoubleAnimationUsingKeyFrames>
<!-- other DoubleAnimationUsingKeyFrames -->
</Storyboard>
</Page.Resources>
<!-- ... --> …Run Code Online (Sandbox Code Playgroud) 我有一个简单的应用程序,用单个定向光绘制球体.我是通过从八面体开始并将每个三角形细分为4个较小的三角形来创建球体的.
只需漫射照明,球体看起来非常光滑.但是,当我添加镜面高光时,三角形的边缘显示得相当强烈.这里有些例子:
仅扩散:

漫反射和镜面反射:

我相信法线正确插值.只看法线,我明白了:

事实上,如果我切换到平面着色,法线是每个多边形而不是每个顶点,我得到这个:

在我的顶点着色器中,我将模型的法线乘以转置逆模型视图矩阵:
#version 330 core
layout (location = 0) in vec4 vPosition;
layout (location = 1) in vec3 vNormal;
layout (location = 2) in vec2 vTexCoord;
out vec3 fNormal;
out vec2 fTexCoord;
uniform mat4 transInvModelView;
uniform mat4 ModelViewMatrix;
uniform mat4 ProjectionMatrix;
void main()
{
fNormal = vec3(transInvModelView * vec4(vNormal, 0.0));
fTexCoord = vTexCoord;
gl_Position = ProjectionMatrix * ModelViewMatrix * vPosition;
}
Run Code Online (Sandbox Code Playgroud)
在片段着色器中,我正在计算镜面高光,如下所示:
#version 330 core
in vec3 fNormal;
in vec2 fTexCoord;
out vec4 color;
uniform …Run Code Online (Sandbox Code Playgroud) 注意:以下问题与模板方法设计模式和C ++函数模板有关。为了区分两者,在引用设计模式时将使用斜体,在引用C ++模板时将使用粗体。
模板方法模式的思想是使算法的各个部分可互换。这通常是通过继承来实现的,子类提供了具体的实现,这些实现被插入到基类的算法中。但是,如果挂钩方法需要为template,则此方法将无法工作,因为模板不能是虚拟的。这是一个不编译的简单示例:
class Base
{
public:
// This is the template method
template <typename T>
void doSomething(T input)
{
//...
auto converted = ConvertInput(input);
//...
std::cout << converted;
}
protected:
//compile error "member function templates cannot be virtual"
template <typename T>
virtual T ConvertInput(T input) = 0;
};
class Derived : public Base
{
protected:
template <typename T>
T ConvertInput(T input)
{
return 2 * …Run Code Online (Sandbox Code Playgroud) 这是我之前提出的问题的后续问题.基本上我有一个游戏,其中主要动作发生在画布上,该画布位于从View扩展的自定义类中.
我将分数保存在字符串中,我试图将其绘制到屏幕的左上角.
在我的onDraw()方法的底部,我有以下代码行:
canvas.drawText(score, 5, 5, null);
Run Code Online (Sandbox Code Playgroud)
当我尝试运行游戏时,log cat会给我以下错误:
03-02 22:30:29.955: ERROR/AndroidRuntime(8140): FATAL EXCEPTION: main
03-02 22:30:29.955: ERROR/AndroidRuntime(8140): java.lang.NullPointerException
03-02 22:30:29.955: ERROR/AndroidRuntime(8140): at android.graphics.Canvas.drawText(Native Method)
03-02 22:30:29.955: ERROR/AndroidRuntime(8140): at com.mattdrewery.supercatch.GameView.onDraw(GameView.java:143)
03-02 22:30:29.955: ERROR/AndroidRuntime(8140): at android.view.View.draw(View.java:6880)
03-02 22:30:29.955: ERROR/AndroidRuntime(8140): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
03-02 22:30:29.955: ERROR/AndroidRuntime(8140): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
03-02 22:30:29.955: ERROR/AndroidRuntime(8140): at android.view.View.draw(View.java:6883)
03-02 22:30:29.955: ERROR/AndroidRuntime(8140): at android.widget.FrameLayout.draw(FrameLayout.java:357)
03-02 22:30:29.955: ERROR/AndroidRuntime(8140): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
03-02 22:30:29.955: ERROR/AndroidRuntime(8140): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
03-02 22:30:29.955: ERROR/AndroidRuntime(8140): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
03-02 22:30:29.955: ERROR/AndroidRuntime(8140): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
03-02 22:30:29.955: ERROR/AndroidRuntime(8140): at android.view.View.draw(View.java:6883) …Run Code Online (Sandbox Code Playgroud) 我的代码给了我错误"无法在int和T之间隐式转换"
public class Vector3D<T>
{
public T x;
public T y;
public T z;
Vector3D()
{
x = 0;
y = 0;
z = 0;
}
}
Run Code Online (Sandbox Code Playgroud)