mac中的glaux替代品

时间:2013-04-17 21:13:18

标签: macos opengl

我开始使用OpenGL。我需要使用glaux.h做一个项目。我的问题是我正在运行Mac OS X Lion,所以我不能使用glaux。

我在网上搜索,我发现了一些试图取代glaux的代码。代码很好,但它们使用Windows Api变量。显然我不能使用它们。以下是我需要使用glaux阅读的代码:

#include <stdio.h>          // Header File For Standard Input/Output
#include <stdlib.h>
#include <GLUT/glut.h>
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include "bmp.h"
#include <string.h>

const int W_WIDTH = 500;    // PreselecciÛn del ancho de la ventana
const int W_HEIGHT = 500;   // PreselecciÛn del alto de la ventana
bool    keys[256];          // Array Used For The Keyboard Routine
bool    active=1;       // Window Active Flag Set To TRUE By Default
bool    fullscreen=TRUE;    // Fullscreen Flag Set To Fullscreen Mode By Default
bool    fullscreen=0;   // Fullscreen Flag Set To Fullscreen Mode By Default
GLuint  texture[1];         // Storage For 1 Texture
GLuint  box;                // Storage For The Box Display List
GLuint  top;                // Storage For The Top Display List
GLuint  xloop;              // Loop For X Axis
GLuint  yloop;              // Loop For Y Axis

GLfloat xrot;               // Rotates Cube On The X Axis
GLfloat yrot;               // Rotates Cube On The Y Axis

static GLfloat boxcol[5][3]=
{
{1.0f,0.0f,0.0f},{1.0f,0.5f,0.0f},{1.0f,1.0f,0.0f},{0.0f,1.0f,0.0f},{0.0f,1.0f,1.0f}
};

static GLfloat topcol[5][3]=
{
{.5f,0.0f,0.0f},{0.5f,0.25f,0.0f},{0.5f,0.5f,0.0f},{0.0f,0.5f,0.0f},{0.0f,0.5f,0.5f}
};

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);   // Declaration For WndProc

// Build Cube Display Lists
GLvoid BuildLists()
{
box=glGenLists(2);                                                    // Generate 2 Different Lists
glNewList(box,GL_COMPILE);          // Start With The Box List
glBegin(GL_QUADS);
// Bottom Face
glNormal3f( 0.0f,-1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
// Front Face
glNormal3f( 0.0f, 0.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
// Back Face
glNormal3f( 0.0f, 0.0f,-1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
// Right face
glNormal3f( 1.0f, 0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
// Left Face
glNormal3f(-1.0f, 0.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
glEnd();
glEndList();
top=box+1;                                          // Storage For "Top" Is "Box" Plus One
glNewList(top,GL_COMPILE);                          // Now The "Top" Display List
glBegin(GL_QUADS);
// Top Face
glNormal3f( 0.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
glEnd();
glEndList();
}

AUX_RGBImageRec *LoadBMP(char *Filename)    // Loads A Bitmap Image
{
FILE *File=NULL;                                            // File Handle

if (!Filename)                                      // Make Sure A Filename Was Given
{
    return NULL;                                    // If Not Return NULL
}

File=fopen(Filename,"r");                           // Check To See If The File Exists

if (File)                                           // Does The File Exist?
{
    fclose(File);                                   // Close The Handle
    return new AUX_RGBImageRec(Filename);               // Load The Bitmap And Return A Pointer
}

return NULL;                                        // If Load Failed Return NULL
}

int LoadGLTextures()        // Load Bitmaps And Convert To Textures
{
int Status=0;      // Status Indicator

char ruta[100]="Data/Cube.bmp";

AUX_RGBImageRec *TextureImage[1];   // Create Storage Space For The Texture

memset(TextureImage,0,sizeof(void *)*1);            // Set The Pointer To NULL

// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
if (TextureImage[0]==LoadBMP(ruta))
{
    Status=1;                                   // Set The Status To TRUE

    glGenTextures(1, &texture[0]);  // Create The Texture

    // Typical Texture Generation Using Data From The Bitmap
    glBindTexture(GL_TEXTURE_2D, texture[0]);
    glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    }

if (TextureImage[0])                                    // If Texture Exists
{
    if (TextureImage[0]->data)                          // If Texture Image Exists
    {
        free(TextureImage[0]->data);                    // Free The Texture Image Memory
    }

    free(TextureImage[0]);                              // Free The Image Structure
}

return Status;                                      // Return The Status
}

GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window
{
if (height==0)                                      // Prevent A Divide By Zero By
{
    height=1;                                       // Making Height Equal One
}

glViewport(0,0,width,height);                       // Reset The Current Viewport

glMatrixMode(GL_PROJECTION);                        // Select The Projection Matrix
glLoadIdentity();               // Reset The Projection Matrix

// Calculate The Aspect Ratio Of The Window
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);

glMatrixMode(GL_MODELVIEW);                         // Select The Modelview Matrix
glLoadIdentity();               // Reset The Modelview Matrix
}

int InitGL()                // All Setup For OpenGL Goes Here
{
if (!LoadGLTextures())          // Jump To Texture Loading Routine
{
    return 0;           // If Texture Didn't Load Return FALSE
}
BuildLists();           // Jump To The Code That Creates Our Display Lists

glEnable(GL_TEXTURE_2D);            // Enable Texture Mapping
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);               // Black Background
glClearDepth(1.0f);                 // Depth Buffer Setup
glEnable(GL_DEPTH_TEST);            // Enables Depth Testing
glEnable(GL_COLOR_MATERIAL);            // Enable Material Coloring
return 1;                   // Initialization Went OK
}

void DrawGLScene()                  // Here's Where We Do All The Drawing
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Clear The Screen And The DepthB 

glBindTexture(GL_TEXTURE_2D, texture[0]);
for (yloop=1;yloop<6;yloop++)
{
    for (xloop=0;xloop<yloop;xloop++)
    {
        //Poner vuestro codigo no hace falta que se ve axactamente igual
    }
}
// Vacia los buffers de dibujo
glutSwapBuffers();
glFlush();

}


int main(int argc, char **argv) {

glutInit(&argc, argv);
glutInitWindowPosition(100,100);
glutInitWindowSize (W_WIDTH, W_HEIGHT);
glutInitDisplayMode (GLUT_RGBA | GLUT_DOUBLE);
glutCreateWindow("Ventana con el Visual C++ y glut");

glutDisplayFunc(DrawGLScene);
glutReshapeFunc(ReSizeGLScene);

// Funciones de inicializacion
InitGL();

glutMainLoop();
return 0;
}

1 个答案:

答案 0 :(得分:1)

既然OS X上有GLKit可用,我建议你看看。 Erik Buck的书Learning OpenGL ES for iOS是围绕该框架编写的,以iOS为重点,但他有一个blog post示例项目,显示了OS X 10.8的端口。

如果允许您退出OpenGL,另一个想法是使用SceneKit。这是一个仅10.8的框架,除了Mac以外的任何平台都没有。它公开了大多数与OpenGL相同的控件,但它基本上是一个Objective-C,基于对象的设计,并且与Core Animation很好地集成。文档很少但是很容易让事情运行起来。我发现围绕SceneKit缠绕我的大脑比围绕OpenGL更容易。最佳介绍是WWDC 2012视频。