0

做错了什么?我正在尝试学习 OpenGL 和 Objective-C,我正在将它的代码从 C 移植到 Objective-C。

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <GL/glew.h>
#include <GL/freeglut.h>
#define WINDOW_TITLE_PREFIX "Chapter 1"

int CurrentWidth = 800,
    CurrentHeight = 600,
    WindowHandle = 0;

void Initialize(int, char*[]);
void InitWindow(int, char*[]);
void ResizeFunction(int, int);
void RenderFunction(void);

int main(int argc, char* argv[])
{
    Initialize(argc, argv);

    glutMainLoop();

    exit(EXIT_SUCCESS);
}

void Initialize(int argc, char* argv[])
{
    InitWindow(argc, argv);

    fprintf(
        stdout,
        "INFO: OpenGL Version: %s\n",
        glGetString(GL_VERSION)
    );

    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
}

void InitWindow(int argc, char* argv[])
{
    glutInit(&argc, argv);

    glutInitContextVersion(4, 0);
    glutInitContextFlags(GLUT_FORWARD_COMPATIBLE);
    glutInitContextProfile(GLUT_CORE_PROFILE);

    glutSetOption(
        GLUT_ACTION_ON_WINDOW_CLOSE,
        GLUT_ACTION_GLUTMAINLOOP_RETURNS
    );

    glutInitWindowSize(CurrentWidth, CurrentHeight);

    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);

    WindowHandle = glutCreateWindow(WINDOW_TITLE_PREFIX);

    if(WindowHandle < 1) {
        fprintf(
            stderr,
            "ERROR: Could not create a new rendering window.\n"
        );
        exit(EXIT_FAILURE);
    }

    glutReshapeFunc(ResizeFunction);
    glutDisplayFunc(RenderFunction);
}

void ResizeFunction(int Width, int Height)
{
    CurrentWidth = Width;
    CurrentHeight = Height;
    glViewport(0, 0, CurrentWidth, CurrentHeight);
}

void RenderFunction(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glutSwapBuffers();
    glutPostRedisplay();
}

我从 http://openglbook.com/the-book/chapter-1-getting-started/得到了这段代码

我已经这样做了,但我不确定我认为问题出在指针的错误是什么。

头文件.h

#import <Foundation/Foundation.h>
#import <stdio.h>
#import <string.h>
#import <GL/glew.h>
#import <GL/freeglut.h>
#define WINDOW_TITLE_PREFIX "test gl objective c"

@interface princ : NSObject {
    int CurrentWidth;
    int CurrentHeight;
    int WindowHandle;

}

-(void)InitValues;
-(void)ResizeFunction;
-(void)RenderFunction;
-(void)InitWindow: (int)argc: (char*[])argv:(void*)FoundRZ:(void*)FoundRD;
-(void)Initialize: (int)argc: (char*[])argv;

@end

@implementation princ 

-(void)InitValues {

    CurrentWidth = 800;
    CurrentHeight = 600;
    WindowHandle = 0;
}

-(void)ResizeFunction {

    int Width = 0;
    int Height = 0;
    CurrentWidth = Width;
    CurrentHeight = Height;
    glViewport(0, 0, CurrentWidth, CurrentHeight);

}

-(void)RenderFunction { 

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glutSwapBuffers();
    glutPostRedisplay();


    }

-(void)InitWindow: (int)argc: (char*[])argv:(void*)FoundRZ:(void*)FoundRD {

    glutInit(&argc, argv);

    glutInitContextVersion(4, 0);
    glutInitContextFlags(GLUT_FORWARD_COMPATIBLE);
    glutInitContextProfile(GLUT_CORE_PROFILE);

    glutSetOption ( 
      GLUT_ACTION_ON_WINDOW_CLOSE,
      GLUT_ACTION_GLUTMAINLOOP_RETURNS
    );

    glutInitWindowSize(CurrentWidth, CurrentHeight);
    WindowHandle = glutCreateWindow(WINDOW_TITLE_PREFIX);

    if(WindowHandle < 1) {
        fprintf(
              stderr, "ERROR: Could not create a new rendering window.\n" 
              );
        exit(EXIT_FAILURE);
    }

    glutReshapeFunc(FoundRZ);
    glutDisplayFunc(FoundRD);


    }

-(void)Initialize: (int)argc: (char*[])argv {


    fprintf(stdout, "INFO: OpenGL Version: %s\n", glGetString(GL_VERSION));




}


@end

1米

#import "header.h"

int main (int argc, char *argv[]) {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSLog(@"hello");


    princ *GLMAIN = [[princ alloc] init];

    [GLMAIN InitValues];
    [GLMAIN InitWindow:argc:argv:[GLMAIN ResizeFunction]:[GLMAIN RenderFunction]];
    [GLMAIN Initialize:argc:argv];

    [pool drain];
    return 0;


}

错误 :

$ clang -ObjC 1.m -o 1  `gnustep-config --objc-flags` -I/usr/local/lib/gcc42/gcc/x86_64-portbld-freebsd9.0/4.2.5/include -fconstant-string-class=NSConstantString  -I/usr/local/GNUstep/System/Library/Headers -L/usr/local/GNUstep/System/Library/Libraries -L/usr/local/lib -lgnustep-base -I/usr/local/include -L/usr/local/lib -lglut -lGL
1.m:12:31: error: sending 'void' to parameter of incompatible type 'void *';
        [GLMAIN InitWindow:argc:argv:[GLMAIN ResizeFunction]:[GLMAIN RenderFunction]];
                                     ^~~~~~~~~~~~~~~~~~~~~~~
./header.h:18:52: note: passing argument to parameter 'FoundRZ' here
-(void)InitWindow: (int)argc: (char*[])argv:(void*)FoundRZ:(void*)FoundRD;
                                                   ^
1.m:12:55: error: sending 'void' to parameter of incompatible type 'void *';
        [GLMAIN InitWindow:argc:argv:[GLMAIN ResizeFunction]:[GLMAIN RenderFunction]];
                                                             ^~~~~~~~~~~~~~~~~~~~~~~
./header.h:18:67: note: passing argument to parameter 'FoundRD' here
-(void)InitWindow: (int)argc: (char*[])argv:(void*)FoundRZ:(void*)FoundRD;
                                                                  ^
2 errors generated.

我没有使用 mac,我使用的是 FreeBSD,但我希望任何苹果开发人员都可以帮助解决这个小问题。

4

1 回答 1

0

我认为这是您的 initWindow 方法。您正在传递一个方法,该方法将 void 返回给采用 void* 的参数。这里的主要问题是 glut 不支持 Objective-C 方法调用,而是需要 C 函数,所以将你的 resizeFunction 和 renderFunction 更改为 C 函数,然后传递它,它应该可以工作。

于 2012-08-25T00:14:35.357 回答