//> //Texture.cpp //Copyright (c) 2016 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. // 2016/08/27 Updated to use OpenGLApplication instead of Application. #include <oz++/motif/PixmapFile.h> #include <oz++/opengl/OpenGLMainView.h> #include <oz++/opengl/OpenGLView.h> #include <oz++/opengl/OpenGLGC.h> #include <oz++/opengl/OpenGLTexture2D.h> namespace OZ { class MainView :public OpenGLMainView { private: //Inner class starts. class SimpleView: public OpenGLView { private: OpenGLGC gc; SmartPtr<OpenGLTexture2D> texture; SmartPtr<PixmapFile> pixmapFile; public: virtual void display() { gc.clearColor(1.0, 1.0, 1.0, 1.0); gc.clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); gc.color(1.0, 1.0, 1.0); gc.rotate(1.0, 0.5, 1.0, 0.25); gc.begin(GL_QUADS); texture -> coord(0.0, 0.0); gc.vertex(-1.0, 1.0, 0.0); texture -> coord(1.0, 0.0); gc.vertex( 1.0, 1.0, 0.0); texture -> coord(1.0, 1.0); gc.vertex( 1.0, -1.0, 0.0); texture -> coord(0.0, 1.0); gc.vertex(-1.0, -1.0, 0.0); gc.end(); } virtual void resize(int w, int h) { if (w == 0 || h == 0) { return; } gc.matrixMode(GL_PROJECTION); gc.loadIdentity(); gc.ortho(-1.25, 1.25, -1.25, 1.25, 1., 20.); gc.matrixMode(GL_MODELVIEW); gc.loadIdentity(); gc.lookAt(0., 0., 10., 0., 0., 0., 0., 1., 0.); } virtual void initialize() { gc.enable(GL_DEPTH_TEST); gc.enable(GL_TEXTURE_2D); texture = new OpenGLTexture2D(); texture -> bind(); texture -> parameter(GL_TEXTURE_MIN_FILTER, GL_LINEAR); texture -> parameter(GL_TEXTURE_MAG_FILTER, GL_LINEAR); texture -> env(GL_TEXTURE_ENV_MODE, GL_MODULATE); const char* jpgfile = "./images/fudasho_18.jpg"; try { pixmapFile = new PixmapFile(getDisplay(), jpgfile); flush(); XImage* ximage = pixmapFile -> getImage(); unsigned int width, height; pixmapFile -> getGeometry(&width, &height); printf("Pixmap w=%d h=%d\n", width, height); if (ximage) { printf("XImage depth=%d\n", ximage->depth); texture -> image(0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (void*)(ximage->data)); XDestroyImage(ximage); } else { printf("ximage is NULL\n"); } } catch (Exception& ex) { caught(ex); } } public: SimpleView(OpenGLMainView* parent, const char* name, Args& args) :OpenGLView(parent, name, args) { } ~SimpleView() { } }; //Inner class ends. private: SmartPtr<SimpleView> view; public: MainView(OpenGLApplication& applet, const char* name, Args& args) :OpenGLMainView(applet, name, args) { Args ar; ar.set(XmNopenGLBufferType, (XtArgVal)getBufferType()); view = new SimpleView(this, "", ar); } ~MainView() { } }; } // int main(int argc, char** argv) { try { const char* appclass = argv[0]; OpenGLApplication applet(appclass, argc, argv); Args args; args.set(XmNgeometry, "500x300"); MainView view(applet, argv[0], args); view.realize(); applet.run(); } catch (Exception& ex) { caught(ex); } return 0; } |