我的学校使用的是Windows电脑,但我家里只有一台电脑。 代码在windows中运行良好但由于某种原因我在xCode中运行它时出错。 我不确定要包含哪些框架,所以我添加了GLKit,Cocoa,GLUT和OpenGL。 我错过了什么? 我想得到Xcode设置,所以我可以使用相同的代码在Windows上工作。 这是代码
#include <stdio.h>
#include <stdarg.h>
#include <math.h>
#define GL_GLEXT_PROTOTYPES
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#define GLUT_KEY_ESCAPE 27
const GLsizei windowWidth = 500;
const GLsizei windowHeight = 500;
GLfloat cubeRotateX = 45.0f;
GLfloat cubeRotateY = 45.0f;
bool keys[255];
GLvoid establishProjectionMatrix(GLsizei width, GLsizei height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 0.1f, 200.0f);
}
GLvoid initGL(GLsizei width, GLsizei height)
{
establishProjectionMatrix(width, height);
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable(GL_PERSPECTIVE_CORRECTION_HINT);
}
GLvoid displayFPS(GLvoid)
{
static long lastTime = glutGet(GLUT_ELAPSED_TIME);
static int loops = 0;
static GLfloat fps = 0.0f;
int newTime = glutGet(GLUT_ELAPSED_TIME);
if (newTime - lastTime > 100)
{
float newFPS = (float)loops / float(newTime - lastTime) * 1000.0f;
fps = (fps + newFPS) / 2.0f;
char title[80];
sprintf_s(title, "OpenGL Demo - %.2f", fps);
glutSetWindowTitle(title);
lastTime = newTime;
loops = 0;
}
loops++;
}
GLvoid drawScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0, 0, -5.0f);
glRotatef(cubeRotateX, 1.0, 0.0, 0.0);
glRotatef(cubeRotateY, 0.0, 1.0, 0.0);
glBegin(GL_QUADS);
glColor3f(0.0f, 1.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f( 1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f( 1.0f, -1.0f, 1.0f);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f( 1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f( 1.0f, -1.0f, 1.0f);
glColor3f(1.0f, 1.0f, 0.0f);
glVertex3f( 1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f( 1.0f, -1.0f, -1.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glColor3f(1.0f, 0.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, -1.0f);
glVertex3f( 1.0f, -1.0f, -1.0f);
glVertex3f( 1.0f, -1.0f, 1.0f);
glEnd();
glFlush();
glutSwapBuffers();
displayFPS();
}
GLboolean checkKeys(GLvoid)
{
const GLfloat speed = 1.0f;
if ( keys[GLUT_KEY_ESCAPE] )
return true;
if (keys[GLUT_KEY_LEFT])
cubeRotateY -= speed;
if (keys[GLUT_KEY_RIGHT])
cubeRotateY += speed;
if (keys[GLUT_KEY_UP])
cubeRotateX -= speed;
if (keys[GLUT_KEY_DOWN])
cubeRotateX += speed;
return false;
}
GLvoid timerLoop(int value)
{
if (checkKeys())
exit(0);
glutPostRedisplay();
glutTimerFunc(1, timerLoop, 0);
}
GLvoid keyboardCB(unsigned char key, int x, int y)
{
keys[key] = true;
}
GLvoid keyboardUpCB(unsigned char key, int x, int y)
{
keys[key] = false;
}
GLvoid keyboardSpecialCB(int key, int x, int y)
{
keys[key] = true;
}
GLvoid keyboardSpecialUpCB(int key, int x, int y)
{
keys[key] = false;
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE);
int windowID = glutCreateWindow("OpenGL Cube Demo");
glutReshapeWindow(windowWidth, windowHeight);
initGL(windowWidth, windowHeight);
glutDisplayFunc(drawScene);
glutKeyboardFunc(keyboardCB);
glutKeyboardUpFunc(keyboardUpCB);
glutSpecialFunc(keyboardSpecialCB);
glutSpecialUpFunc(keyboardSpecialUpCB);
glutTimerFunc(1, timerLoop, 0);
glutMainLoop();
return 0;
}
为什么它在Visual Studio中工作而不在Xcode中?
答案 0 :(得分:1)
编译器的错误看起来很清楚。它不起作用的表面原因是因为此代码包含不可移植的Windows主题。
C ++不允许typedef void
指示空参数列表。您必须使用void
本身。因此,只需将GLvoid
更改为void
s。
sprintf_s()
是Windows特有的功能。您可以将其替换为snprintf()
,但您必须在缓冲区之后添加一个参数,该参数是缓冲区的大小。所以,替换这个:
sprintf_s(title, ...);
使用:
snprintf(title, sizeof(title), ...);
exit()
功能可跨平台使用,但您必须包含正确的标头。在文件顶部附近添加#include <stdlib.h>
。