2013-07-17 2 views
0

마운틴 라이온의 NSOpenGL을 사용하여 OpenGL에서 빨간색 배경의 매우 단순한 파란색 사각형을 그리려합니다. 코드가 간단하고 작동해야합니다, 나는 문맥을 설치하는 것이 나에게 문제가 있다고 가정하고있다.OpenGL 및 코코아가 작동하지 않음

#import <Cocoa/Cocoa.h> 
#import <OpenGL/gl.h> 
#import <GLKit/GLKit.h> 

typedef struct { 
    char *Name; 
    GLint Location; 
} Uniform; 

@interface MyOpenGLView : NSOpenGLView { 
    Uniform *_uniformArray; 
    int _uniformArraySize; 
    GLKMatrix4 _projectionMatrix; 
    GLKMatrix4 _modelViewMatrix; 
    IBOutlet NSWindow *window; 
    int height, width; 
} 

-(void)drawRect:(NSRect)bounds; 

@end 

구현 :

GLfloat square[] = { 
    -0.5, -0.5, 
    0.5, -0.5, 
    -0.5, 0.5, 
    0.5, 0.5 
}; 

- (id)initWithFrame:(NSRect)frame { 
    self = [super initWithFrame:frame]; 
    if (self) { 

    } 
    return self; 
} 

-(void)awakeFromNib { 
    NSString *vertexShaderSource = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"VertexShader" ofType:@"vsh"] encoding:NSUTF8StringEncoding error:nil]; 

    const char *vertexShaderSourceCString = [vertexShaderSource cStringUsingEncoding:NSUTF8StringEncoding]; 
    NSLog(@"%s",vertexShaderSourceCString); 

    NSString *fragmentShaderSource = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"FragmentShader" ofType:@"fsh"] encoding:NSUTF8StringEncoding error:nil]; 
    const char *fragmentShaderSourceCString = [fragmentShaderSource cStringUsingEncoding:NSUTF8StringEncoding]; 

    NSLog(@"%s",fragmentShaderSourceCString); 

    NSOpenGLContext *glContext = [self openGLContext]; 
    [glContext makeCurrentContext]; 

    GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); 
    glShaderSource(fragmentShader, 1, &fragmentShaderSourceCString, NULL); 
    glCompileShader(fragmentShader); 

    GLint compileSuccess; 
    glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &compileSuccess); 
    if (compileSuccess == GL_FALSE) { 
     GLint logLength; 
     glGetShaderiv(fragmentShader, GL_INFO_LOG_LENGTH, &logLength); 
     if(logLength > 0) { 
      GLchar *log = (GLchar *)malloc(logLength); 
      glGetShaderInfoLog(fragmentShader, logLength, &logLength, log); 
      NSLog(@"Shader compile log:\n%s", log); 
      free(log); 
     } 
     exit(1); 
    } 

    GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER); 
    glShaderSource(vertexShader, 1, &vertexShaderSourceCString, NULL); 
    glCompileShader(vertexShader); 

    GLuint program = glCreateProgram(); 
    glAttachShader(program, fragmentShader); 
    glAttachShader(program, vertexShader); 
    glLinkProgram(program); 

    glUseProgram(program); 

    const char *aPositionCString = [@"a_position" cStringUsingEncoding:NSUTF8StringEncoding]; 
    GLuint aPosition = glGetAttribLocation(program, aPositionCString); 

    glVertexAttribPointer(aPosition, 2, GL_FLOAT, GL_FALSE, 0, square); 
    glEnable(aPosition); 

    GLint maxUniformLength; 
    GLint numberOfUniforms; 
    char *uniformName; 

    glGetProgramiv(program, GL_ACTIVE_UNIFORMS, &numberOfUniforms); 
    glGetProgramiv(program, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxUniformLength); 

    _uniformArray = malloc(numberOfUniforms * sizeof(Uniform)); 
    _uniformArraySize = numberOfUniforms; 

    for (int i =0; i <numberOfUniforms; i++) { 
     GLint size; 
     GLenum type; 
     GLint location; 

     uniformName = malloc(sizeof(char*)*maxUniformLength); 
     glGetActiveUniform(program, i, maxUniformLength, NULL, &size, &type, uniformName); 
     _uniformArray[i].Name = uniformName; 
     location = glGetUniformLocation(program, uniformName); 
     _uniformArray[i].Location = location; 
    } 

    _modelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, 0.0f); 
    _projectionMatrix = GLKMatrix4MakeOrtho(-1, 1, -1.5, 1.5, -1, 1); 
} 

- (void)drawRect:(NSRect)bounds { 
    glClearColor(1.0, 0.0, 0.0, 0.0); 
    glClear(GL_COLOR_BUFFER_BIT); 

    glViewport(0, 0, 480, 360); 

    for (int i = 0; i <_uniformArraySize; i++) { 
     if (strcmp(_uniformArray[i].Name, "ModelViewProjectionMatrix")==0) { 
      // Multiply the transformation matrices together 
      GLKMatrix4 modelViewProjectionMatrix = GLKMatrix4Multiply(_projectionMatrix,  _modelViewMatrix); 
      glUniformMatrix4fv(_uniformArray[i].Location, 1, GL_FALSE,   modelViewProjectionMatrix.m); 
     } 
    } 
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); 

    glFlush(); 
} 

내 아주 간단한 조각 쉐이더 :

void main() { 
    gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0); 
} 

간단한 버텍스 쉐이더 :

attribute vec4 a_position; 

uniform mat4 ModelViewProjectionMatrix; 

void main() { 
    gl_Position = a_position * ModelViewProjectionMatrix; 
} 
여기

내 GLView 인터페이스입니다

컴파일 오류가없고 빨간색 화면과 파란색 사각형이 없습니다. 누군가 내가 잘못 생각해 내도록 도울 수 있습니까? Gl.h와 gl3.h가 모두 포함되어 있다는 경고가 나옵니다. OpenGL 2를 사용하고 싶습니다.

답변

0

보기 매트릭스는 어디에 있습니까 (일명 look-at 매트릭스)? NSOpenGLView의 경우 적어도 3 개의 행렬, 회전 및 변환 행렬의 곱셈 결과로 구성된 모델 뷰, 투영 행렬 (절두체 크기, 종횡비, 평면 근처 및 원위 평면을 고려함)) 및 뷰 매트릭스 ("세계"에서 시청자의 눈 위치, 초점 지점 및 위쪽 벡터의 방향을 고려한) 나는 awakeFromNib에 코드를 배치하는 것에 큰 업적을 말할 것이며, 뭔가 가르쳐 주었다.

관련 문제