gpt4 book ai didi

objective-c - 如何在Objective-C OpenGL中正确使用InitWindow?

转载 作者:行者123 更新时间:2023-11-30 17:58:34 24 4
gpt4 key购买 nike

我做错了什么?我正在尝试学习 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,但我希望任何苹果开发人员都可以帮助解决这个小问题。

最佳答案

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

关于objective-c - 如何在Objective-C OpenGL中正确使用InitWindow?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12117862/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com