openGL SuperBible 7th 初编译

openGL SuperBible 6th的源码中使用的还是glfw2.x版本库,而glfw升级到3.x之后,直接停止了对2.x中函数的支持(轻量级库的好处是简洁明快,缺点是版本维护),因此当初编译时费了好大的劲儿。

这次通过百度找到了sb7的源码,里面的glfw库也随之升级为3.x了。于是再次编译。


源码中的gl3w.h是一个轻量级的openGL扩展库,功用类似glew,因为我之前编译过glew,于是用glew代替之。


又sb7.h中application中声明了一个静态成员类,所以在主函数调用前需要先实例化一下。


于是,改后的头文件代码如下:


#ifndef __SB7_H__

#define __SB7_H__

#ifdef WIN32
	#pragma once
	#define _CRT_SECURE_NO_WARNINGS 1
	
	#define WIN32_LEAN_AND_MEAN 1
	#include 
#else
	#include 
	#define Sleep(t) sleep(t)
#endif

#include 

#define GLFW_NO_GLU 1
#define GLFW_INCLUDE_GLCOREARB 1

#include 
#include 

#pragma comment(lib, "glew32.lib")
#pragma comment(lib, "glfw3.lib")
#pragma comment(lib, "opengl32.lib")

//#include "sb7ext.h"

#include 
#include 
#include 

namespace sb7
{
	class application
	{
	private:
		//static void APIENTRY debug_callback(GLenum source,
		//	GLenum type,
		//	GLuint id,
		//	GLenum severity,
		//	GLsizei length,
		//	const GLchar* message,
		//	GLvoid* userParam);
	public:
		application(){}
		virtual ~application(){}
		virtual void run(sb7::application* the_app)
		{
			bool running = true;
			app = the_app;

			if (!glfwInit())
			{
				fprintf(stderr, "Failed to initialize GLFW\n");
				return;
			}

			init();

			glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, info.majorVersion);
			glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, info.minorVersion);

#ifndef _DEBUG
			if (info.flags.debug)
#endif /*_DEBUG*/
			{
				glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
			}
			if (info.flags.robust)
			{
				glfwWindowHint(GLFW_CONTEXT_ROBUSTNESS, GLFW_LOSE_CONTEXT_ON_RESET);
			}
			glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
			glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
			glfwWindowHint(GLFW_SAMPLES, info.samples);
			glfwWindowHint(GLFW_STEREO, info.flags.stereo ? GL_TRUE : GL_FALSE);
			if (info.flags.fullscreen)
			{
				const GLFWvidmode* mode;
				GLFWmonitor* primary = glfwGetPrimaryMonitor();
				if (info.windowWidth == 0 || info.windowHeight == 0)
				{					
					mode = glfwGetVideoMode(primary);
					info.windowWidth = mode->width;
					info.windowHeight = mode->height;
				}
				window = glfwCreateWindow(info.windowWidth, info.windowHeight, info.title, primary, NULL);
			}
			else
			{
				window = glfwCreateWindow(info.windowWidth, info.windowHeight, info.title, NULL, NULL);
				if (!window)
				{
					fprintf(stderr, "Failed to open window\n");
					return;
				}
			}

			glfwMakeContextCurrent(window);

			glfwSetWindowSizeCallback(window, glfw_onResize);
			glfwSetKeyCallback(window, glfw_onKey);
			glfwSetMouseButtonCallback(window, glfw_onMouseButton);
			glfwSetCursorPosCallback(window, glfw_onMouseMove);
			glfwSetScrollCallback(window, glfw_onMouseWheel);
			if (!info.flags.cursor)
			{
				glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
			}

			info.flags.stereo = (glfwGetWindowAttrib(window, GLFW_STEREO) ? 1 : 0);

			glewInit();
#ifdef _DEBUG
			fprintf(stderr, "VENDER: %s\n", (char*)glGetString(GL_VENDOR));
			fprintf(stderr, "VERSION: %s\n", (char*)glGetString(GL_VERSION));
			fprintf(stderr, "RENDERER: %s\n", (char*)glGetString(GL_RENDERER));
#endif
			
			if (info.flags.debug)
			{
				//if (glewIsSupported("4.3"))
				//{
				//	glDebugMessageCallback((GLDEBUGPROC)debug_callback, this);
				//	glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
				//}
			}

			startup();

			do
			{
				render(glfwGetTime());

				glfwSwapBuffers(window);
				glfwPollEvents();

				running &= (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_RELEASE);
				running &= (glfwWindowShouldClose(window) != GL_TRUE);
			} while (running);

			shutdown();

			glfwDestroyWindow(window);
			glfwTerminate();
		}

		virtual void init()
		{
			strcpy(info.title, "OpenGL SuperBible Example");
			info.windowWidth = 800;
			info.windowHeight = 600;
#ifdef __APPLE__
			info.majorVersion = 3;
			info.minorVersion = 2;
#else
			info.majorVersion = 4;
			info.minorVersion = 3;
#endif
			info.samples = 0;
			info.flags.all = 0;
			info.flags.cursor = 1;
#ifdef _DEBUG
			info.flags.debug = 1;
#endif
		}

		virtual void startup(){}
		virtual void render(double currentTime){}
		virtual void shutdown(){}
		
		void setWindowTitle(const char* title)
		{
			glfwSetWindowTitle(window, title);
		}

		virtual void onResize(int w, int h)
		{
			info.windowWidth = w;
			info.windowHeight = h;
		}

		virtual void onKey(int key, int action){}
		virtual void onMouseButton(int button, int action){}
		virtual void onMouseMove(int x, int y){}
		virtual void onMouseWheel(int pos){}
		virtual void onDebugMessage(
			GLenum source,
			GLenum type,
			GLuint id,
			GLenum severity,
			GLsizei length,
			const GLchar* message
			)
		{
#ifdef _WIN32
			OutputDebugStringA(message);
			OutputDebugStringA("\n");
#endif/*_WIN32*/
		}

		void getMousePosition(int& x, int& y)
		{
			double dx, dy;
			glfwGetCursorPos(window, &dx, &dy);
			x = static_cast(floor(dx));
			y = static_cast(floor(dy));
		}
	public:
		struct APPINFO
			{
				char title[128];
				int windowWidth;
				int windowHeight;
				int majorVersion;
				int minorVersion;
				int samples;
				union
				{
					struct
					{
						unsigned int fullscreen : 1;
						unsigned int vsync : 1;
						unsigned int cursor : 1;
						unsigned int stereo : 1;
						unsigned int debug : 1;
						unsigned int robust : 1;
					};
					unsigned int all;
				}flags;
			};
	protected:
		APPINFO info;
		static sb7::application* app;
		GLFWwindow* window;

		static void glfw_onResize(GLFWwindow* window, int w, int h)
		{
			app->onResize(w, h);
		}

		static void glfw_onKey(GLFWwindow* window, int key, int scancode, int action, int mods)
		{
			app->onKey(key, action);
		}

		static void glfw_onMouseButton(GLFWwindow* window, int button, int action, int mods)
		{
			app->onMouseButton(button, action);
		}
		
		static void glfw_onMouseMove(GLFWwindow* window, double x, double y)
		{
			app->onMouseMove(static_cast(x), static_cast(y));
		}

		static void glfw_onMouseWheel(GLFWwindow* window, double xoffset, double yoffset)
		{
			app->onMouseWheel(static_cast(yoffset));
		}

		void setVsync(bool enable)
		{
			info.flags.vsync = enable ? 1 : 0;
			glfwSwapInterval((int)info.flags.vsync);
		}
	};
};
#endif/*__SB7_H__*/

实际调用时代码如下:

#include "sb7.h"

class my_application : public sb7::application
{
public:
	void render(double currentTime)
	{
		static const GLfloat red[] = { 1.0f, 0.0f, 0.0f, 1.0f };
		glClearBufferfv(GL_COLOR, 0, red);
	}
};

sb7::application* sb7::application::app = 0;
int main(int argc, char** argv)
{
	my_application* app = new my_application;
	app->run(app);
	delete app;
}


你可能感兴趣的:(openGL)