Skip to content

Instantly share code, notes, and snippets.

@colesnicov
Created December 24, 2016 00:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colesnicov/0ee58aad11b6a8b9fce4b58dceaf370d to your computer and use it in GitHub Desktop.
Save colesnicov/0ee58aad11b6a8b9fce4b58dceaf370d to your computer and use it in GitHub Desktop.
Одно из изначальных можных реализаций OSG + IMGUI. НЕДОКОНЧЕНО!! IMGUI не принимает ввод од пользователя!! Есть идеи, кидай в комменты.
// \LICENCE WTFPL
// Denis Colesnicov <eugustus@gmail.com>
#include <GL/glut.h>
#include <iostream>
#include <osg/Config>
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgGA/TrackballManipulator>
#include <osgDB/ReadFile>
#include "imgui/imgui.h"
osg::ref_ptr<osgViewer::Viewer> viewer;
osg::observer_ptr<osgViewer::GraphicsWindow> window;
GLuint g_FontTexture;
void ImImpl_RenderDrawLists (ImDrawData* draw_data)
{
GLint last_texture;
glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
glPushAttrib(GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT | GL_TRANSFORM_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
glEnable(GL_SCISSOR_TEST);
glDisable (GL_LIGHTING);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glEnable(GL_TEXTURE_2D);
ImGuiIO& io = ImGui::GetIO();
float fb_height = io.DisplaySize.y * io.DisplayFramebufferScale.y;
draw_data->ScaleClipRects(io.DisplayFramebufferScale);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0.0f, io.DisplaySize.x, io.DisplaySize.y, 0.0f, -1.0f, +1.0f);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
#define OFFSETOF(TYPE, ELEMENT) ((size_t)&(((TYPE *)0)->ELEMENT))
for (int n = 0; n < draw_data->CmdListsCount; n++)
{
const ImDrawList* cmd_list = draw_data->CmdLists[n];
const unsigned char* vtx_buffer = (const unsigned char*)&cmd_list->VtxBuffer.front();
const ImDrawIdx* idx_buffer = &cmd_list->IdxBuffer.front();
glVertexPointer(2, GL_FLOAT, sizeof(ImDrawVert), (void*)(vtx_buffer + OFFSETOF(ImDrawVert, pos)));
glTexCoordPointer(2, GL_FLOAT, sizeof(ImDrawVert), (void*)(vtx_buffer + OFFSETOF(ImDrawVert, uv)));
glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(ImDrawVert), (void*)(vtx_buffer + OFFSETOF(ImDrawVert, col)));
for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.size(); cmd_i++)
{
const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
if (pcmd->UserCallback)
{
pcmd->UserCallback(cmd_list, pcmd);
}
else
{
glBindTexture(GL_TEXTURE_2D, (GLuint)(intptr_t)pcmd->TextureId);
glScissor((int)pcmd->ClipRect.x, (int)(fb_height - pcmd->ClipRect.w), (int)(pcmd->ClipRect.z - pcmd->ClipRect.x), (int)(pcmd->ClipRect.w - pcmd->ClipRect.y));
glDrawElements(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, GL_UNSIGNED_SHORT, idx_buffer);
}
idx_buffer += pcmd->ElemCount;
}
}
#undef OFFSETOF
// Restore modified state
//glDisableClientState(GL_COLOR_ARRAY);
//glDisableClientState(GL_TEXTURE_COORD_ARRAY);
//glDisableClientState(GL_VERTEX_ARRAY);
glBindTexture(GL_TEXTURE_2D, last_texture);
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glPopAttrib();
}
void ImGuiInit()
{
ImGuiIO& io = ImGui::GetIO();
io.DisplaySize.x = 800; // 1680.0f;
io.DisplaySize.y = 600; // 1050.0f;
io.DeltaTime = 1.0f / 60.0f;
io.RenderDrawListsFn = ImImpl_RenderDrawLists;
io.KeyMap[0] = 9; // tab
io.KeyMap[1] = GLUT_KEY_LEFT; // Left
io.KeyMap[2] = GLUT_KEY_RIGHT; // Right
io.KeyMap[3] = GLUT_KEY_UP; // Up
io.KeyMap[4] = GLUT_KEY_DOWN; // Down
io.KeyMap[10] = 27; // Escape
// Build texture atlas
unsigned char* pixels;
int width, height;
io.Fonts->GetTexDataAsAlpha8(&pixels, &width, &height);
GLint last_texture;
glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
glGenTextures(1, &g_FontTexture);
glBindTexture(GL_TEXTURE_2D, g_FontTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, pixels);
// Store our identifier
io.Fonts->TexID = (void *)(intptr_t)g_FontTexture;
// Cleanup (don't clear the input data if you want to append new fonts later)
io.Fonts->ClearInputData();
io.Fonts->ClearTexData();
glBindTexture(GL_TEXTURE_2D, last_texture);
io.RenderDrawListsFn = ImImpl_RenderDrawLists;
}
void display(void)
{
if (!viewer->done())
{
ImGui::NewFrame();
bool o(true);
char* buf = {"Test"};
ImGui::SetNextWindowSize(ImVec2(200, 100), ImGuiSetCond_Appearing);
ImGui::SetNextWindowPos(ImVec2(0, 0));
ImGui::Begin((const char*) buf, &o);
ImGui::InputText("Entire", buf, 8);
ImGui::Text("Hello Denis");
ImGui::End();
viewer->frame();
ImGui::Render();
glutSwapBuffers();
glutPostRedisplay();
}
}
void reshape( int w, int h )
{
if (window.valid())
{
window->resized(window->getTraits()->x, window->getTraits()->y, w, h);
window->getEventQueue()->windowResize(window->getTraits()->x, window->getTraits()->y, w, h );
}
}
void mousebutton( int button, int state, int x, int y )
{
if (window.valid())
{
if (state==0)
{
window->getEventQueue()->mouseButtonPress( x, y, button+1 );
}
else
{
window->getEventQueue()->mouseButtonRelease( x, y, button+1 );
}
}
}
void mousemove( int x, int y )
{
if (window.valid())
{
window->getEventQueue()->mouseMotion( x, y );
}
}
void keyboard( unsigned char key, int /*x*/, int /*y*/ )
{
switch( key )
{
case 27:
if (viewer.valid()) viewer = 0;
glutDestroyWindow(glutGetWindow());
break;
default:
if (window.valid())
{
window->getEventQueue()->keyPress( (osgGA::GUIEventAdapter::KeySymbol) key );
window->getEventQueue()->keyRelease( (osgGA::GUIEventAdapter::KeySymbol) key );
}
break;
}
}
int main( int argc, char **argv )
{
glutInit(&argc, argv);
if (argc<2)
{
std::cout << argv[0] <<": requires filename argument." << std::endl;
return 1;
}
// load the scene.
osg::ref_ptr<osg::Node> loadedModel = osgDB::readRefNodeFile(argv[1]);
if (!loadedModel)
{
std::cout << argv[0] <<": No data loaded." << std::endl;
return 1;
}
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_ALPHA );
glutInitWindowPosition( 100, 100 );
glutInitWindowSize( 800, 600 );
glutCreateWindow( argv[0] );
glutDisplayFunc( display );
glutReshapeFunc( reshape );
glutMouseFunc( mousebutton );
glutMotionFunc( mousemove );
glutKeyboardFunc( keyboard );
ImGuiInit();
viewer = new osgViewer::Viewer;
window = viewer->setUpViewerAsEmbeddedInWindow(100,100,800,600);
viewer->setSceneData(loadedModel.get());
viewer->setCameraManipulator(new osgGA::TrackballManipulator);
viewer->addEventHandler(new osgViewer::StatsHandler);
viewer->realize();
glutMainLoop();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment