Updated simpleCpu to use GLFW for all platforms

This commit is contained in:
Jeremy Cowles 2013-02-16 12:03:21 -08:00
parent 5a7e33a792
commit e9b29cda95
4 changed files with 164 additions and 265 deletions

View File

@ -97,21 +97,12 @@ foreach(shader_file ${SHADER_FILES})
)
endforeach()
if(APPLE)
_add_glfw_executable(simpleCpu
mainApple.mm
simpleCpuSubdivision.cpp
${SHADER_FILES}
${INC_FILES}
)
else()
_add_glfw_executable(simpleCpu
simpleCpuSubdivision.cpp
mainGlfw.cpp
${SHADER_FILES}
${INC_FILES}
)
endif()
_add_glfw_executable(simpleCpu
main.cpp
simpleCpuSubdivision.cpp
${SHADER_FILES}
${INC_FILES}
)
target_link_libraries(simpleCpu
${PLATFORM_LIBRARIES}

158
examples/simpleCpu/main.cpp Normal file
View File

@ -0,0 +1,158 @@
#if defined(__APPLE__)
#include <OpenGL/gl3.h>
#define GLFW_INCLUDE_GL3
#define GLFW_NO_GLU
#include <stdio.h>
#else
#include <stdlib.h>
#include <GL/glew.h>
#if defined(WIN32)
#include <GL/wglew.h>
#endif
#endif
#if defined(GLFW_VERSION_3)
#include <GL/glfw3.h>
GLFWwindow* g_window=0;
GLFWmonitor* g_primary=0;
#else
#include <GL/glfw.h>
#endif
extern void initOsd();
extern void updateGeom();
extern void display();
extern int g_width, g_height, g_frame;
static bool g_running = true;
static void
setGLCoreProfile()
{
#if GLFW_VERSION_MAJOR>=3
#define glfwOpenWindowHint glfwWindowHint
#define GLFW_OPENGL_VERSION_MAJOR GLFW_CONTEXT_VERSION_MAJOR
#define GLFW_OPENGL_VERSION_MINOR GLFW_CONTEXT_VERSION_MINOR
#endif
glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#if not defined(__APPLE__)
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 4);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2);
glfwOpenWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#else
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2);
#endif
}
//
// ### Misc. GLFW Callback Methods
// Reshape is called when the window is resized, here we need the width and
// height so that we can correctly adjust the aspect ratio of the projection
// matrix.
//
void
#if GLFW_VERSION_MAJOR>=3
reshape(GLFWwindow *, int width, int height) {
#else
reshape(int width, int height) {
#endif
g_width = width;
g_height = height;
}
#if GLFW_VERSION_MAJOR>=3
int windowClose(GLFWwindow*) {
#else
int windowClose() {
#endif
g_running = false;
return GL_TRUE;
}
//
// Idle is called between frames, here we advance the frame number and update
// the procedural animation that is being applied to the mesh
//
void
idle()
{
g_frame++;
updateGeom();
}
int main(int argc, char ** argv)
{
//
// Setup GLFW, glew and some initial GL state
//
static const char windowTitle[] = "CPU Subdivision Example";
if (not glfwInit()) {
printf("Failed to initialize GLFW\n");
return 1;
}
setGLCoreProfile();
#if GLFW_VERSION_MAJOR>=3
if (not (g_window=glfwCreateWindow(g_width, g_height, windowTitle, NULL, NULL))) {
printf("Failed to open window.\n");
glfwTerminate();
return 1;
}
glfwMakeContextCurrent(g_window);
glfwSetWindowSizeCallback(g_window, reshape);
#else
if (glfwOpenWindow(g_width, g_height, 8, 8, 8, 8, 24, 8, GLFW_WINDOW) == GL_FALSE) {
printf("Failed to open window.\n");
glfwTerminate();
return 1;
}
glfwSetWindowTitle(windowTitle);
glfwSetWindowSizeCallback(reshape);
#endif
glfwSetWindowCloseCallback(windowClose);
#if not defined(__APPLE__)
#ifdef CORE_PROFILE
// this is the only way to initialize glew correctly under core profile context.
glewExperimental = true;
#endif
if (GLenum r = glewInit() != GLEW_OK) {
printf("Failed to initialize glew. Error = %s\n", glewGetErrorString(r));
exit(1);
}
#ifdef CORE_PROFILE
// clear GL errors which was generated during glewInit()
glGetError();
#endif
#endif
initOsd();
//
// Start the main glut drawing loop
//
while (g_running) {
idle();
display();
#if GLFW_VERSION_MAJOR>=3
glfwPollEvents();
glfwSwapBuffers(g_window);
#else
glfwSwapBuffers();
#endif
glFinish();
}
}

View File

@ -1,176 +0,0 @@
#import <Cocoa/Cocoa.h>
#import <OpenGL/OpenGL.h>
#import <OpenGL/glu.h>
#import <wchar.h>
#import <iostream>
//
// Hooks back into the example code
//
extern void initOsd();
extern void updateGeom();
extern void display();
//
// Shared global state from the example
//
extern int g_width, g_height, g_frame;
//
// OSX application bootstrap
//
@class View;
@interface View : NSOpenGLView <NSWindowDelegate> {
NSRect m_frameRect;
BOOL m_didInit;
uint64_t m_previousTime;
NSTimer* m_timer;
}
- (void) animate;
@end
@implementation View
-(void)windowWillClose:(NSNotification *)note
{
[[NSApplication sharedApplication] terminate:self];
}
- (void) timerFired:(NSTimer*) timer
{
[self animate];
}
- (id) initWithFrame: (NSRect) frame
{
m_didInit = FALSE;
//
// Various GL state, of note is the 3.2 Core profile selection
// and 8x antialiasing, which we might want to disable for performance
//
int attribs[] = {
NSOpenGLPFAAccelerated,
NSOpenGLPFADoubleBuffer,
NSOpenGLPFADepthSize, 24,
NSOpenGLPFAAlphaSize, 8,
NSOpenGLPFAColorSize, 32,
NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
NSOpenGLPFANoRecovery,
kCGLPFASampleBuffers, 1,
kCGLPFASamples, 8,
0
};
NSOpenGLPixelFormat *fmt = [[NSOpenGLPixelFormat alloc]
initWithAttributes:(NSOpenGLPixelFormatAttribute*) attribs];
self = [self initWithFrame:frame pixelFormat:fmt];
[fmt release];
m_frameRect = frame;
m_timer = [[NSTimer
scheduledTimerWithTimeInterval:1.0f/120.0f
target:self
selector:@selector(timerFired:)
userInfo:nil
repeats:YES] retain];
return self;
}
- (void) drawRect:(NSRect) theRect
{
if (!m_didInit) {
initOsd();
m_didInit = YES;
[[self window] setLevel: NSFloatingWindowLevel];
[[self window] makeKeyAndOrderFront: self];
[[self window] setTitle: [NSString stringWithUTF8String: "SimpleCPU"]];
}
// run the example code
display();
// make sure GL state is clean
assert(glGetError() == GL_NO_ERROR);
[[self openGLContext] flushBuffer];
}
- (void) animate
{
g_frame++;
updateGeom();
[self display];
}
@end
int main(int argc, const char *argv[])
{
//
// It seems that some ATI cards fall back to software when rendering geometry shaders
// but only *sometimes*. Post this notice so it's obvious that OSD isn't the problem.
//
// XXX(jcowles): we should only use vertex and fragment shaders to avoid this potential confusion
//
std::cout << "--------------------------------------------------------------------------------" << std::endl;
std::cout << "NOTICE: Some Apple hardware runs geometry shaders in software, which will cause" << std::endl;
std::cout << " this demo to run extremely slowly. That slowness is not related to OSD" << std::endl;
std::cout << " and can be avoided by not using OpenGL geometry shaders." << std::endl;
std::cout << "--------------------------------------------------------------------------------" << std::endl;
NSAutoreleasePool *pool = [NSAutoreleasePool new];
NSApplication *NSApp = [NSApplication sharedApplication];
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
id menubar = [[NSMenu new] autorelease];
id appMenuItem = [[NSMenuItem new] autorelease];
[menubar addItem:appMenuItem];
[NSApp setMainMenu:menubar];
id appMenu = [[NSMenu new] autorelease];
id appName = [[NSProcessInfo processInfo] processName];
id quitTitle = [@"Quit " stringByAppendingString:appName];
id quitMenuItem = [[[NSMenuItem alloc] initWithTitle:quitTitle
action:@selector(terminate:) keyEquivalent:@"q"] autorelease];
[appMenu addItem:quitMenuItem];
[appMenuItem setSubmenu:appMenu];
NSRect screenBounds = [[NSScreen mainScreen] frame];
g_width = 512;
g_height = 512;
NSRect viewBounds = NSMakeRect(0, 0, g_width, g_height);
View* view = [[View alloc] initWithFrame:viewBounds];
NSRect centered = NSMakeRect(NSMidX(screenBounds) - NSMidX(viewBounds),
NSMidY(screenBounds) - NSMidY(viewBounds),
viewBounds.size.width, viewBounds.size.height);
NSWindow *window = [[NSWindow alloc]
initWithContentRect:centered
styleMask:NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask
backing:NSBackingStoreBuffered
defer:NO];
[window setContentView:view];
[window setDelegate:view];
[window makeKeyAndOrderFront:nil];
[NSApp activateIgnoringOtherApps:YES];
[view release];
[NSApp run];
[pool release];
return EXIT_SUCCESS;
}

View File

@ -1,74 +0,0 @@
#include <stdlib.h>
#include <GL/glew.h>
#include <GL/glut.h>
extern void initOsd();
extern void updateGeom();
extern void display();
extern int g_width, g_height, g_frame;
//
// ### Misc. GLUT Callback Methods
// Reshape is called when the window is resized, here we need the width and
// height so that we can correctly adjust the aspect ratio of the projection
// matrix.
//
void
reshape(int width, int height)
{
g_width = width;
g_height = height;
}
//
// Idle is called between frames, here we advance the frame number and update
// the procedural animation that is being applied to the mesh
//
void
idle()
{
g_frame++;
updateGeom();
glutPostRedisplay();
}
//
// ### Draw the Mesh
// Display handles all drawing per frame. We first call the setupForDisplay
// helper method to setup some uninteresting GL state and then bind the mesh
// using the buffers provided by our OSD objects
//
void
glutDisplay()
{
display();
glutSwapBuffers();
}
int main(int argc, char ** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA |GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(1024, 1024);
glutCreateWindow("CPU Subdivision Example");
//
// Setup glut, glew and some initial GL state
//
glutDisplayFunc(glutDisplay);
glutReshapeFunc(reshape);
glewInit();
initOsd();
//
// Start the main glut drawing loop
//
glutIdleFunc(idle);
glutMainLoop();
}