Last active
September 14, 2017 07:39
-
-
Save xiongjia/82731d29c62b9b25cff4 to your computer and use it in GitHub Desktop.
OpenGL Tests (GL + GLEW + GLUT + Boost) #devsample #boost #gl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# CMake build script | |
cmake_minimum_required(VERSION 2.8) | |
# project name & version | |
project(GLTest) | |
# GLEW, GLUT & boost | |
find_package(GLEW REQUIRED) | |
find_package(GLUT REQUIRED) | |
find_package(Boost REQUIRED) | |
if(MSVC) | |
set(CMAKE_C_STANDARD_LIBRARIES "") | |
set(CMAKE_CXX_STANDARD_LIBRARIES "") | |
# /SAFESEH:NO : This option is added for glut | |
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SAFESEH:NO") | |
endif() | |
include_directories("${PROJECT_SOURCE_DIR}" | |
"${GLEW_INCLUDE_DIR}" | |
"${GLUT_INCLUDE_DIR}" | |
"${Boost_INCLUDE_DIR}") | |
set(GLTEST_DBG_LIBS | |
"${GLEW_LIBRARY}" | |
"${GLUT_glut_LIBRARY}") | |
set(GLTEST_OPT_LIBS | |
"${GLEW_LIBRARY}" | |
"${GLUT_glut_LIBRARY}") | |
add_executable(GLTest | |
"${PROJECT_SOURCE_DIR}/glt_misc.cpp" | |
"${PROJECT_SOURCE_DIR}/glt_misc.h" | |
"${PROJECT_SOURCE_DIR}/gltest.cpp" | |
"${PROJECT_SOURCE_DIR}/gltest.h" | |
"${PROJECT_SOURCE_DIR}/main.cpp") | |
target_link_libraries(GLTest | |
debug "${GLTEST_DBG_LIBS}" | |
optimized "${GLTEST_OPT_LIBS}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* OpenGL Tests | |
*/ | |
#include <stdio.h> | |
#include <string.h> | |
#include <string> | |
#include <vector> | |
#include "GL/glew.h" | |
#include "glut.h" | |
#include "glt_misc.h" | |
namespace gltest { | |
void log_write(const char *srcFile, unsigned int srcLine, | |
LOG_MASK mask, const char *log) | |
{ | |
/* TODO write the log line to FS */ | |
fprintf(stdout, "%s\n", log); | |
} | |
void log_write_vfmt(const char *srcFile, unsigned int srcLine, | |
LOG_MASK mask, const char *fmt, va_list args) | |
{ | |
const int logLen = _vscprintf(fmt, args) + sizeof(char); | |
if (0 >= logLen) | |
{ | |
return; | |
} | |
std::vector<char> buf(logLen, '\0'); | |
const int bufLen = _vsnprintf_s(&buf[0], buf.size(), logLen, fmt, args); | |
if (0 >= bufLen) | |
{ | |
return; | |
} | |
std::string log(&buf[0]); | |
log_write(srcFile, srcLine, mask, log.c_str()); | |
} | |
void log_write_fmt(const char *srcFile, unsigned int srcLine, | |
LOG_MASK mask, const char *fmt, ...) | |
{ | |
va_list args = NULL; | |
va_start(args, fmt); | |
log_write_vfmt(srcFile, srcLine, mask, fmt, args); | |
va_end(args); | |
} | |
} /* namespace gltest */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* OpenGL Tests | |
*/ | |
#ifndef _GLT_MISC_H_ | |
#define _GLT_MISC_H_ 1 | |
#include <stdarg.h> | |
#include <boost/shared_ptr.hpp> | |
#include <boost/utility.hpp> | |
#include <boost/array.hpp> | |
namespace gltest { | |
typedef enum _LOG_MASK { | |
LOG_ERR = (1 << 0), | |
LOG_WARN = (1 << 1), | |
LOG_INFO = (1 << 2), | |
LOG_DBG = (1 << 3), | |
} LOG_MASK; | |
void log_write(const char *srcFile, | |
unsigned int srcLine, | |
LOG_MASK mask, | |
const char *log); | |
void log_write_vfmt(const char *srcFile, | |
unsigned int srcLine, | |
LOG_MASK mask, | |
const char *fmt, | |
va_list args); | |
void log_write_fmt(const char *srcFile, | |
unsigned int srcLine, | |
LOG_MASK mask, | |
const char *fmt, ...); | |
} /* namespace gltest */ | |
#define GLT_LOG(_mask, _fmt, ...) \ | |
gltest::log_write_fmt(__FILE__, __LINE__, _mask, _fmt, __VA_ARGS__); | |
#endif /* !defined(_GLT_MISC_H_) */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* OpenGL Tests | |
*/ | |
#include "GL/glew.h" | |
#include "glut.h" | |
#include "gltest.h" | |
#include "glt_misc.h" | |
namespace gltest { | |
const char *WND_TITLE = "GLTest"; | |
const int WND_WIDTH = 800; | |
const int WND_HEIGHT = 800; | |
class GLTestImpl : public GLTest | |
{ | |
public: | |
static boost::shared_ptr<GLTest> m_inst; | |
private: | |
GLuint m_vertexArrayID; | |
GLuint m_vertexBuf; | |
public: | |
GLTestImpl(void); | |
~GLTestImpl(void); | |
virtual void run(void); | |
virtual void onReshape(int w, int h); | |
virtual void onDisplay(void); | |
public: | |
static void onGLUTReshape(int w, int h) | |
{ | |
m_inst->onReshape(w, h); | |
} | |
static void onGLUTDisplay(void) | |
{ | |
m_inst->onDisplay(); | |
} | |
}; | |
boost::shared_ptr<GLTest> GLTestImpl::m_inst; | |
GLTestImpl::GLTestImpl(void) | |
: GLTest() | |
, m_vertexArrayID(0) | |
, m_vertexBuf(0) | |
{ | |
/* create the main GLUT window */ | |
glutInitWindowSize(WND_WIDTH, WND_HEIGHT); | |
glutCreateWindow(WND_TITLE); | |
glutReshapeFunc(GLTestImpl::onGLUTReshape); | |
glutDisplayFunc(GLTestImpl::onGLUTDisplay); | |
glewExperimental = GL_TRUE; | |
GLenum err = glewInit(); | |
if (err != GLEW_OK) | |
{ | |
GLT_LOG(gltest::LOG_ERR, "Failed to initialize GLEW"); | |
} | |
} | |
GLTestImpl::~GLTestImpl(void) | |
{ | |
if (0 != m_vertexBuf) | |
{ | |
glDeleteBuffers(1, &m_vertexBuf); | |
m_vertexBuf = 0; | |
} | |
if (0 != m_vertexArrayID) | |
{ | |
glDeleteVertexArrays(1, &m_vertexArrayID); | |
m_vertexArrayID = 0; | |
} | |
} | |
void GLTestImpl::run(void) | |
{ | |
GLT_LOG(LOG_DBG, "GLTestImpl::Run()"); | |
/* Blue background */ | |
glClearColor(0.0f, 0.0f, 1.0f, 1.0f); | |
glGenVertexArrays(1, &m_vertexArrayID); | |
glBindVertexArray(m_vertexArrayID); | |
static const GLfloat vertex_buffer_data[] = { | |
-1.0f, -1.0f, 0.0f, | |
1.0f, -1.0f, 0.0f, | |
0.0f, 1.0f, 0.0f, | |
}; | |
glGenBuffers(1, &m_vertexBuf); | |
glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuf); | |
glBufferData(GL_ARRAY_BUFFER, | |
sizeof(vertex_buffer_data), vertex_buffer_data, GL_STATIC_DRAW); | |
glutMainLoop(); | |
} | |
void GLTestImpl::onReshape(int w, int h) | |
{ | |
glViewport(0, 0, w, h); | |
} | |
void GLTestImpl::onDisplay(void) | |
{ | |
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT); | |
/* TODO call glUseProgram() here */ | |
glEnableVertexAttribArray(0); | |
glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuf); | |
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0); | |
glDrawArrays(GL_TRIANGLES, 0, 3); | |
glDisableVertexAttribArray(0); | |
glutSwapBuffers(); | |
} | |
boost::shared_ptr<GLTest> GLTest::instance(void) | |
{ | |
if (GLTestImpl::m_inst == NULL) | |
{ | |
GLTestImpl::m_inst = boost::shared_ptr<GLTest>(new GLTestImpl()); | |
} | |
return GLTestImpl::m_inst; | |
} | |
} /* namespace gltest */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* OpenGL Tests | |
*/ | |
#ifndef _GLTEST_H_ | |
#define _GLTEST_H_ 1 | |
#include <boost/shared_ptr.hpp> | |
#include <boost/utility.hpp> | |
namespace gltest { | |
class GLTest : boost::noncopyable | |
{ | |
public: | |
static boost::shared_ptr<GLTest> instance(void); | |
public: | |
virtual void run(void) = 0; | |
public: | |
virtual void onReshape(int w, int h) = 0; | |
virtual void onDisplay(void) = 0; | |
}; | |
} /* namespace gltest */ | |
#endif /* !defined(_GLTEST_H_) */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* OpenGL Tests | |
*/ | |
#include "GL/glew.h" | |
#include "glut.h" | |
#include "gltest.h" | |
#include "glt_misc.h" | |
int main(int argc, char **argv) | |
{ | |
GLT_LOG(gltest::LOG_DBG, "Starting GLTest ..."); | |
glutInit(&argc, argv); | |
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_DEPTH|GLUT_STENCIL); | |
boost::shared_ptr<gltest::GLTest> inst = gltest::GLTest::instance(); | |
inst->run(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment