MRaster examples 21.0.0.0
Image Processing Library
Loading...
Searching...
No Matches
glut_image.cpp
Go to the documentation of this file.
1// -*- Mode:C++; Coding:us-ascii-unix; fill-column:158 -*-
2/*******************************************************************************************************************************************************.H.S.**/
3/**
4 @file glut_image.cpp
5 @author Mitch Richling http://www.mitchr.me/
6 @date 2022-07-23
7 @version VERSION
8 @brief One way to get images from a ramCanvas into OpenGL@EOL
9 @std C++20
10 @copyright
11 @parblock
12 Copyright (c) 2022, Mitchell Jay Richling <http://www.mitchr.me/> All rights reserved.
13
14 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
15
16 1. Redistributions of source code must retain the above copyright notice, this list of conditions, and the following disclaimer.
17
18 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions, and the following disclaimer in the documentation
19 and/or other materials provided with the distribution.
20
21 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software
22 without specific prior written permission.
23
24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
26 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
29 DAMAGE.
30 @endparblock
31 @filedetails
32
33 We illustrate how to use the exportRasterData() member to get data suitable for use as an OpenGL image. Note that ramCanvas.h also supports directly an
34 internal representation that is the same as that used by OpenGL --- so one can actually use the raw pixel store in the ramCanvas as OpenGL image data. This
35 program illustrates how one might get OpenGL image data if the ramCanvas for some reason was not internally compatible with OpenGL. Note this same
36 technique allows easy integration with other libraries requiring similar data formats.
37*/
38/*******************************************************************************************************************************************************.H.E.**/
39/** @cond exj */
40
41//--------------------------------------------------------------------------------------------------------------------------------------------------------------
42#include "ramCanvas.hpp"
43
44//--------------------------------------------------------------------------------------------------------------------------------------------------------------
45/* Apple puts GLUT into a framework named GLUT, while the rest of the world just sticks GLUT into the GL include directory... */
46#ifdef __APPLE__
47#include <GLUT/glut.h> /* Open GL Util APPLE */
48#else
49#include <GL/glut.h> /* Open GL Util OpenGL */
50#endif
51
52//--------------------------------------------------------------------------------------------------------------------------------------------------------------
53int XSIZE = 1024;
54int YSIZE = 1024;
55
56//--------------------------------------------------------------------------------------------------------------------------------------------------------------
57mjr::ramCanvas3c8b theRamCanvas = mjr::ramCanvas3c8b(XSIZE, YSIZE, -1, 1, -1, 1);
58
59//--------------------------------------------------------------------------------------------------------------------------------------------------------------
60void reshapeCall(int h, int w) {
61 XSIZE = h;
62 YSIZE = w;
63 glMatrixMode(GL_PROJECTION);
64 glLoadIdentity();
65 gluOrtho2D(0.0, XSIZE, 0.0, XSIZE);
66 glMatrixMode(GL_MODELVIEW);
67 glLoadIdentity();
68 glViewport(0,0,XSIZE,YSIZE);
69 glutPostRedisplay();
70} /* end func reshapeCall */
71
72//--------------------------------------------------------------------------------------------------------------------------------------------------------------
73void idleCall() {
74 static int offset;
75 for(int x=0; x<theRamCanvas.getNumPixX(); x++)
76 for(int y=0; y<theRamCanvas.getNumPixX(); y++)
77 theRamCanvas.drawPoint(x, y, mjr::ramCanvas3c8b::colorType(static_cast<mjr::ramCanvas3c8b::colorChanType>(offset+y-x),
78 static_cast<mjr::ramCanvas3c8b::colorChanType>(offset+x+y),
79 static_cast<mjr::ramCanvas3c8b::colorChanType>(offset+x-y)));
80 offset+=1;
81 glutPostRedisplay();
82} /* end func idleCall */
83
84//--------------------------------------------------------------------------------------------------------------------------------------------------------------
85void displayCall() {
86 static void *image;
87 float xMag, yMag;
88
89 int retVal;
90 if((retVal=theRamCanvas.exportRasterData(image, 0, 0, theRamCanvas.getNumPixX()-1, theRamCanvas.getNumPixY()-1, 0, 1, 2, 3))) {
91 printf("ERROR: Nonzero return from exportRasterData(): %d\n", retVal);
92 return;
93 }
94
95 glClear(GL_COLOR_BUFFER_BIT);
96 glRasterPos2i(0,0);
97 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
98
99 xMag = static_cast<float>(XSIZE) / static_cast<float>(theRamCanvas.getNumPixX());
100 yMag = static_cast<float>(YSIZE) / static_cast<float>(theRamCanvas.getNumPixY());
101
102 glPixelZoom(xMag, yMag);
103 glDrawPixels(theRamCanvas.getNumPixX(), theRamCanvas.getNumPixY(), GL_RGBA, GL_UNSIGNED_BYTE, (GLubyte*)image);
104 glFlush();
105} /* end func displayCall */
106
107//--------------------------------------------------------------------------------------------------------------------------------------------------------------
108int main(int argc, char *argv[]) {
109 for(int x=0; x<theRamCanvas.getNumPixX(); x++)
110 for(int y=0; y<theRamCanvas.getNumPixX(); y++)
111 theRamCanvas.drawPoint(x, y, mjr::ramCanvas3c8b::colorType(static_cast<mjr::ramCanvas3c8b::colorChanType>(y-x),
112 static_cast<mjr::ramCanvas3c8b::colorChanType>(x+y),
113 static_cast<mjr::ramCanvas3c8b::colorChanType>(x-y)));
114 glutInit(&argc, argv);
115 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
116 glutInitWindowSize(XSIZE, YSIZE);
117 glutInitWindowPosition(10, 10);
118 glutCreateWindow("ramCanvasDisplayViaGLUT");
119 glutReshapeFunc(reshapeCall);
120 glutDisplayFunc(displayCall);
121 glutIdleFunc(idleCall);
122 glutMainLoop();
123 return 0;
124} /* end func main */
125/** @endcond */
int main(int argc, char *argv[])