Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 在Cocoa中,只有第一帧使用OpenGL渲染_Objective C_Cocoa_Opengl - Fatal编程技术网

Objective c 在Cocoa中,只有第一帧使用OpenGL渲染

Objective c 在Cocoa中,只有第一帧使用OpenGL渲染,objective-c,cocoa,opengl,Objective C,Cocoa,Opengl,几天来我一直在用这个把头撞墙。我读过很多关于OpenGL和Cocoa的文章。我只是不明白我的尝试在哪里失败,而其他的没有 下面列出了我的代码,它相当短,因为我要做的只是渲染一个旋转三角形。我对OpenGL或编程并不陌生,但我对Cocoa和Objective-C完全是新手 MyOpenGL.h #import <Cocoa/Cocoa.h> @interface MyOpenGL : NSOpenGLView { float rot; NSTimer *timer; } //+

几天来我一直在用这个把头撞墙。我读过很多关于OpenGL和Cocoa的文章。我只是不明白我的尝试在哪里失败,而其他的没有

下面列出了我的代码,它相当短,因为我要做的只是渲染一个旋转三角形。我对OpenGL或编程并不陌生,但我对Cocoa和Objective-C完全是新手

MyOpenGL.h

#import <Cocoa/Cocoa.h>

@interface MyOpenGL : NSOpenGLView
{
 float rot;
 NSTimer *timer;
}

//+(NSOpenGLPixelFormat*) basicPixelFormat;

-(void) animTimer : (NSTimer *) timer;
-(void) drawRect : (NSRect) bounds;

@end
#导入
@接口MyOpenGL:NSOpenGLView
{
漂浮腐烂;
n定时器*定时器;
}
//+(NSOpenGLPixelFormat*)基本像素格式;
-(void)animTimer:(NSTimer*)计时器;
-(void)drawRect:(NSRect)边界;
@结束
MyOpenGL.m

#include <OpenGL/gl.h>

#import <Cocoa/Cocoa.h>
#import "MyOpenGL.h"

@implementation MyOpenGL

/*
+ (NSOpenGLPixelFormat*) basicPixelFormat
{
    NSOpenGLPixelFormatAttribute attributes [] = {
        NSOpenGLPFAWindow,
        NSOpenGLPFADoubleBuffer,
        NSOpenGLPFADepthSize, (NSOpenGLPixelFormatAttribute)16,
        (NSOpenGLPixelFormatAttribute)nil
    };
    return [[[NSOpenGLPixelFormat alloc] initWithAttributes:attributes] autorelease];
}

-(id) initWithFrame : (NSRect) frameRect
{
 NSOpenGLPixelFormat * pf = [MyOpenGL basicPixelFormat];

 return self = [super initWithFrame: frameRect pixelFormat: pf];
}
*/

-(void) awakeFromNib
{
 rot = 0;
    timer = [NSTimer timerWithTimeInterval: (1.0f/60.0f)
         target:self
          selector:@selector(animTimer:)
          userInfo:nil
           repeats:YES];

 [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
 [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSEventTrackingRunLoopMode];
}

-(void) animTimer : (NSTimer *) atime
{
 [self setNeedsDisplay: YES];
 [self drawRect:[self bounds]];
}

-(void) drawRect: (NSRect) bounds
{
 glClearColor(0,0,0,0);
 glClear(GL_COLOR_BUFFER_BIT);
 glLoadIdentity();
 glRotatef(rot,1,0,0);

 glBegin(GL_TRIANGLES);
 {
  glColor3f(1,0,0); glVertex3f(0,0.6,0);
  glColor3f(0,1,0); glVertex3f(-0.2,-0.3,0);
  glColor3f(0,0,1); glVertex3f(0.2,0.3,0);
 }
 glEnd();

 glFlush();
 rot++;
}

@end
#包括
#进口
#输入“MyOpenGL.h”
@实现MyOpenGL
/*
+(NSOpenGLPixelFormat*)基本像素格式
{
NSOpenGLPixelFormatAttribute属性[]={
新南威尔士州,
NSOPENGLPFADOUBLEBUFER,
NSOpenGLPFADepthSize,(NSOpenGLPixelFormatAttribute)16,
(NSOpenGLPixelFormatAttribute)无
};
返回[[[NSOpenGLPixelFormat alloc]initWithAttributes:attributes]autorelease];
}
-(id)initWithFrame:(NSRect)frameRect
{
NSOpenGLPixelFormat*pf=[MyOpenGL-basicPixelFormat];
返回self=[super initWithFrame:frameRect pixelFormat:pf];
}
*/
-(无效)从NIB中唤醒
{
rot=0;
定时器=[n定时器定时器,其时间间隔为:(1.0f/60.0f)
目标:自我
选择器:@选择器(animTimer:)
用户信息:无
重复:是];
[[NSRunLoop currentlunloop]addTimer:timer forMode:NSDefaultRunLoopMode];
[[NSRunLoop currentlunloop]addTimer:timer forMode:NSEventTrackingRunLoopMode];
}
-(void)animTimer:(NSTimer*)atime
{
[自我设置需要显示:是];
[self-drawRect:[自边界]];
}
-(void)drawRect:(NSRect)边界
{
glClearColor(0,0,0,0);
glClear(GLU颜色缓冲位);
glLoadIdentity();
glRotatef(rot,1,0,0);
glBegin(GL_三角形);
{
glColor3f(1,0,0);glVertex3f(0,0.6,0);
glColor3f(0,1,0);glVertex3f(-0.2,-0.3,0);
glColor3f(0,0,1);glVertex3f(0.2,0.3,0);
}
格伦德();
glFlush();
rot++;
}
@结束
取消注释
+(NSOpenGLPixelFormat*)基本像素格式行和.m文件中的关联函数以及MyOpenGL.m文件中的
-(id)initWithFrame:(NSRect)frameRect
方法似乎没有什么区别


drawRect
方法似乎只调用一次。我得到了初始位置的三角形,但没有旋转。如果您有任何建议,我们将不胜感激。

我刚刚尝试将您上面的代码放到一个新项目中,它对我很有用。我看到一个三角形绕X轴旋转。您是否在-drawRect:方法上设置了断点


顺便说一句,sending-drawRect:如果您发送的是-setNeedsDisplay消息,那么您自己就是多余的。

没有断点。我将退出extra-drawRect,并在另一台机器上尝试一个新的项目。它不仅是多余的,而且我非常确定您会在运行日志中收到投诉,而且它不会工作,因为当时没有当前上下文。您需要在
drawRect:
之前发送
lockFocus
,在
drawRect:
之后发送
unlockFocus
(如果您还没有发送
setNeedsDisplay:
,一般情况下和本例中都会更好)。结果是我的计算机太旧,无法正确编译此代码:/Time升级!