| 1 |
|
| 2 |
#include <ClanLib/application.h> |
| 3 |
#include <ClanLib/core.h> |
| 4 |
#include <ClanLib/display.h> |
| 5 |
#include <ClanLib/gl.h> |
| 6 |
#include <ClanLib/GL/opengl_wrap.h> |
| 7 |
|
| 8 |
///////////////////////////////////////////////////////////////////////////// |
| 9 |
// Application class |
| 10 |
|
| 11 |
class Application : public CL_ClanApplication |
| 12 |
{ |
| 13 |
public: |
| 14 |
int main(int argc, char** argv) |
| 15 |
{ |
| 16 |
CL_ConsoleWindow debug_console("Debug console"); |
| 17 |
debug_console.redirect_stdio(); |
| 18 |
|
| 19 |
try |
| 20 |
{ |
| 21 |
CL_SetupCore setupCore; |
| 22 |
CL_SetupDisplay setupDisplay; |
| 23 |
CL_SetupGL setupGL; |
| 24 |
|
| 25 |
CL_DisplayWindowDescription desc; |
| 26 |
desc.set_title("ClanLib Shader Test"); |
| 27 |
desc.set_size(CL_Size(640, 480)); |
| 28 |
desc.set_bpp(32); |
| 29 |
desc.set_fullscreen(false); |
| 30 |
CL_DisplayWindow window(desc); |
| 31 |
|
| 32 |
CL_ResourceManager resources("resources.xml"); |
| 33 |
|
| 34 |
CL_OpenGLState state(window.get_gc()); |
| 35 |
state.set_active(); |
| 36 |
state.setup_2d(); |
| 37 |
|
| 38 |
std::cout << "Renderer: " << (const char *) clGetString(CL_RENDERER) << std::endl; |
| 39 |
std::cout << "Vendor: " << (const char *) clGetString(CL_VENDOR) << std::endl; |
| 40 |
std::cout << "Version: " << (const char *) clGetString(CL_VERSION) << std::endl; |
| 41 |
|
| 42 |
std::string extensions = (const char *) clGetString(CL_EXTENSIONS); |
| 43 |
if (extensions.find("GL_ARB_shader_objects") == std::string::npos) |
| 44 |
std::cout << "No shader support by this card." << std::endl; |
| 45 |
|
| 46 |
CL_ShaderObject shader("white_shader", &resources); |
| 47 |
std::cout << "Shader status: " << (shader.get_compile_status() ? "true" : "false") << std::endl; |
| 48 |
std::cout << "Shader log: " << shader.get_info_log() << std::endl; |
| 49 |
std::cout << "Shader handle: " << shader.get_handle() << std::endl; |
| 50 |
|
| 51 |
// CL_ProgramObject program("program", &resources); |
| 52 |
CL_ProgramObject program; |
| 53 |
program.attach(shader); |
| 54 |
program.link(); |
| 55 |
std::cout << "Program status: " << (program.get_link_status() ? "true" : "false") << std::endl; |
| 56 |
std::cout << "Program log: " << program.get_info_log() << std::endl; |
| 57 |
std::cout << "Program handle: " << program.get_handle() << std::endl; |
| 58 |
|
| 59 |
program.use(); |
| 60 |
program.validate(); |
| 61 |
std::cout << "Program validate status: " << (program.get_validate_status() ? "true" : "false") << std::endl; |
| 62 |
std::cout << "Program log: " << program.get_info_log() << std::endl; |
| 63 |
|
| 64 |
// print available uniform variables |
| 65 |
std::cout << "Uniform variables:" << std::endl; |
| 66 |
std::vector<CL_ProgramUniform> uniforms = program.get_uniforms(); |
| 67 |
std::vector<CL_ProgramUniform>::iterator it; |
| 68 |
|
| 69 |
for(it = uniforms.begin(); it != uniforms.end(); ++it ) |
| 70 |
{ |
| 71 |
std::cout << CL_String::format( "name: %1, type: %2", (*it).name, get_uniform_type_str((*it).type)) << std::endl; |
| 72 |
} |
| 73 |
|
| 74 |
if( program.has_uniform("color", CL_FLOAT_VEC4) ) |
| 75 |
program.set_uniform("color", 0.15f, 0.73f, 0.83f, 1.0f); |
| 76 |
|
| 77 |
while (!CL_Keyboard::get_keycode(CL_KEY_ESCAPE)) |
| 78 |
{ |
| 79 |
window.get_gc()->clear(CL_Color::blue); |
| 80 |
state.set_active(); |
| 81 |
clBegin(CL_QUADS); |
| 82 |
clColor3f(1.0, 0.0, 0.0); |
| 83 |
clVertex2i(100,100); |
| 84 |
clVertex2i(200,100); |
| 85 |
clVertex2i(200,200); |
| 86 |
clVertex2i(100,200); |
| 87 |
clEnd(); |
| 88 |
|
| 89 |
window.flip(); |
| 90 |
CL_System::keep_alive(20); |
| 91 |
} |
| 92 |
|
| 93 |
state.set_active(); |
| 94 |
program.disable(); |
| 95 |
} |
| 96 |
catch (CL_Error error) |
| 97 |
{ |
| 98 |
std::cout << "Uncaught error: " << error.message.c_str() << std::endl; |
| 99 |
} |
| 100 |
|
| 101 |
debug_console.wait_for_key(); |
| 102 |
return 0; |
| 103 |
} |
| 104 |
|
| 105 |
std::string get_uniform_type_str(int type) |
| 106 |
{ |
| 107 |
static std::map<int, std::string> names; |
| 108 |
static bool first_time = true; |
| 109 |
|
| 110 |
if( first_time ) |
| 111 |
{ |
| 112 |
names[CL_FLOAT] = "CL_FLOAT"; |
| 113 |
names[CL_FLOAT_VEC2] = "CL_FLOAT_VEC2"; |
| 114 |
names[CL_FLOAT_VEC3] = "CL_FLOAT_VEC3"; |
| 115 |
names[CL_FLOAT_VEC4] = "CL_FLOAT_VEC4"; |
| 116 |
names[CL_INT] = "CL_INT"; |
| 117 |
names[CL_INT_VEC2] = "CL_INT_VEC2"; |
| 118 |
names[CL_INT_VEC3] = "CL_INT_VEC3"; |
| 119 |
names[CL_INT_VEC4] = "CL_INT_VEC4"; |
| 120 |
names[CL_BOOL] = "CL_BOOL"; |
| 121 |
names[CL_BOOL_VEC2] = "CL_BOOL_VEC2"; |
| 122 |
names[CL_BOOL_VEC3] = "CL_BOOL_VEC3"; |
| 123 |
names[CL_BOOL_VEC4] = "CL_BOOL_VEC4"; |
| 124 |
names[CL_FLOAT_MAT2] = "CL_FLOAT_MAT2"; |
| 125 |
names[CL_FLOAT_MAT3] = "CL_FLOAT_MAT3"; |
| 126 |
names[CL_FLOAT_MAT4] = "CL_FLOAT_MAT4"; |
| 127 |
names[CL_SAMPLER_1D] = "CL_SAMPLER_1D"; |
| 128 |
names[CL_SAMPLER_2D] = "CL_SAMPLER_2D"; |
| 129 |
names[CL_SAMPLER_3D] = "CL_SAMPLER_3D"; |
| 130 |
names[CL_SAMPLER_CUBE] = "CL_SAMPLER_CUBE"; |
| 131 |
names[CL_SAMPLER_1D_SHADOW] = "CL_SAMPLER_1D_SHADOW"; |
| 132 |
names[CL_SAMPLER_2D_SHADOW] = "CL_SAMPLER_2D_SHADOW"; |
| 133 |
|
| 134 |
first_time = false; |
| 135 |
} |
| 136 |
|
| 137 |
return names[type]; |
| 138 |
} |
| 139 |
} app; |