/* This program is a modified version of the program "image.c"
* appearing in _The OpenGL Programming Guide_ (red book).
* It demonstrates drawing pixels and shows the effect
* of glDrawPixels() and glCopyPixels().
* Interaction: moving the mouse while pressing the mouse button
* will copy the image in the lower-left corner of the window
* to the mouse position.
* There is no attempt to prevent you from drawing over the original
* image.
*
* IMPORTANT: The window with the image must be the active window, i.e.,
* it must be clicked or the mouse must be over it, for input to occur.
* The result will be shown in the window you started the program from,
* but don't make the mistake of typing in this window: it won't work.
*
*/
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <exception>
#include <string>
using namespace std;
extern unsigned int ImageWidth;
extern unsigned int ImageLength;
extern GLubyte *ImageData;
/* Create checkerboard image */
#define checkImageWidth 1024
#define checkImageHeight 1024
GLubyte checkImage[checkImageHeight][checkImageWidth][3];
static GLint height;
void
makeCheckImage(void)
{
int i, j, c;
for (i = 0; i < checkImageHeight; i++) {
for (j = 0; j < checkImageWidth; j++) {
c = ((((i&0x8)==0)^((j&0x8)==0)))*255;
checkImage[i][j][0] = (GLubyte) c;
checkImage[i][j][1] = (GLubyte) c;
checkImage[i][j][2] = (GLubyte) c;
}
}
}
/*
* Function name: init
* Description: Operations that only need be performed once for rendering
* are done here.
* Arguments: none
* Globals: none
* Returns: void
*/
void
init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
makeCheckImage();
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
}
/*
* Function name: display
* Description: The glut display callback, operations to render and
* re-render are here.
* Arguments: none
* Globals: none
* Returns: void
*/
void
display(void)
{
if (ImageData == NULL) {
glClear(GL_COLOR_BUFFER_BIT);//commented out?
glRasterPos2i(0, 0);
glDrawPixels(checkImageWidth, checkImageHeight, GL_RGB,
GL_UNSIGNED_BYTE, &checkImage);
glFlush();
}
else {
glutReshapeWindow(ImageWidth,ImageLength);
glClear(GL_COLOR_BUFFER_BIT);
glRasterPos2i(0, 0);
glDrawPixels(ImageWidth, ImageLength, GL_RGB,
GL_UNSIGNED_BYTE, ImageData);
glFlush();
}
glutSwapBuffers();
}
/*
* Function name: reshape
* Description: glut reshape callback, what actions to take when the
* window is reshaped.
* Arguments: w - the new width
* h - the new height
* Globals: none
* Returns: void
*/
void
reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
height = (GLint) h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble) w, 0.0, (GLdouble) h);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
/*
* Function name: motion
* Description: glut mouse motion callback, copies the image.
* Arguments: x - x position of mouse
* y - y positions of mouse
* Globals: none
* Returns: void
*/
void
motion(int x, int y)
{
static GLint screeny;
screeny = height - (GLint) y;
glRasterPos2i (x, screeny);
glCopyPixels (0, 0, checkImageWidth, checkImageHeight, GL_COLOR);
glFlush ();
}
#include "cli.h"
#define startover \
purgestack(); \
stacksize = 0; \
cfd = stdin; \
strcpy(filename,"/dev/tty"); \
struct link *stack=NULL;
/*
* Function name: main_loop
* Description: Process the line of input
* Arguments: line - the line of input
* Globals: none
* Returns: void
*/
void
main_loop(char line[])
{
/* PUT YOUR CLI CODE HERE! */
std::string junk;
char command[MAXLINE];
int stacksize; /* number of file handles on stack */
FILE *cfd; /* current file handle */
char filename[MAXNAME]="/dev/tty";
int pick;
startover;
#ifdef DEBUG
if (stack != NULL) printf("debug: stack should be empty!");
else printf("debug: stack is empty\n");
#endif DEBUG
do { /* loop over commands */
if (cfd == stdin) pick = SUCCESS;
else pick = getline(line, cfd);
switch (pick) { /* read next line */
case TOOLONG: /* line too long */
printf("%s\n",line);
printf("Line too long\n");
startover;
#ifdef DEBUG
if (stack != NULL) printf("debug: stack should be empty!");
else printf("debug: stack is empty\n");
#endif DEBUG
goto out;
case EOF: /* reached end of file */
#ifdef DEBUG
printf("debug: reached EOF of file %s\n", filename);
#endif DEBUG
if (cfd == stdin) goto out;
cfd = pop(filename);
#ifdef DEBUG
printf("debug: popped %s of the stack\n",filename);
#endif DEBUG
if (cfd == stdin) goto out;
continue;
case SUCCESS: /* process line */
#ifdef DEBUG
printf("debug: line = %s\n", line);
#endif DEBUG
if (strlen(line) == 0 || line[0] == '#' ||
strspn(line," ") == strlen(line) ) {
/* empty line or comment or white space only */
if (cfd == stdin) goto out;
continue;
}
#ifdef DEBUG
printf("debug: before stripping comments:\n");
printf("debug: line = %s\n",line);
printf("debug: after stripping comments:\n");
#endif DEBUG
strcpy(command, strtok(line,"#")); /* strip comments */
#ifdef DEBUG
printf("debug: command = %s\n",command);
#endif DEBUG
if (strlen(command) == 0 ||
strspn(command," ") == strlen(command) ) {
/* command empty */
if (cfd == stdin) goto out;
continue;
}
switch (dispatch(command)){
case SUCCESS:
if (cfd == stdin) goto out;
continue;
case READING:
stacksize += 1;
push(cfd, filename);
cfd = openfile(command, filename);
if (cfd == NULL) {
printf("Error in command: %s\n",command);
startover;
goto out;
}
continue;
case ERROR:
printf("Error: cannot process command:\n");
printf("%s\n", command);
startover;
goto out;
default: /* shouldn't be here */
printf("Bug in main\n");
exit(1);
} /* end switch */
default: /* shouldn't be here */
printf("Bug in main\n");
exit(1);
} /* end switch */
} while (1); /* end do */
out:
if (line == NULL)
{
printf("Exiting...\n");
exit(0);
}
else {
;
#ifdef DEBUG
printf("RESULT: %s\n",line);
#endif DEBUG
}
printf("CLI> ");
fflush(stdout);
return;
}
/* ASCII Values for return, backspace, delete, escape and ctrl-d */
#define CR 13
#define BS 8
#define DEL 127
#define ESC 27
#define EOT 4
/*
* Function name: read_next_command
* Description: The glut callback function which is called when a key
* is pressed. ESC redraws the screen, ctrl-d exits.
* Arguments: key - The ASCII value of the key pressed.
* x - x position of the mouse (window-relative)
* y - y position of the mouse (window-relative)
* Globals: none
* Returns: void
*/
void
read_next_command(unsigned char key, int x, int y)
{
static char line[MAXLINE];
static int count;
if(count >= MAXLINE - 1)
{
printf("Error: Maximum line length exceeded. Discarded input.\n");
count = 0;
return;
}
putchar(key);
fflush(stdout); /* Immediate output (gratification) */
if (key != CR && key != BS && key != DEL && key != ESC && key != EOT)
{
line[count]=key;
count++;
}
else if (key == BS || key == DEL)
{
if(count == 0)
return;
count--;
}
else if (key == CR)
{
printf("\n");
line[count] = '\0';
count = 0;
main_loop(line);
}
else if (key == ESC)
{
glutPostRedisplay();
}
else
{
printf("Exiting...\n");
exit(0);
}
}
/*
* Function name: main
* Description: Set up GL window and render
* Arguments: command line arguments.
* Globals: none
* Returns: void
*/
int
main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(250, 250);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(read_next_command);
glutMotionFunc(motion);
printf("CLI> ");
fflush(stdout);
glutMainLoop();
return 0;
}
Welcome to the bulix.org / pastebin. Please don't use this pastebin for illegal purposes, defamation or kitten-squashing.
This pastebin is written using PHP and MySQL and relies on Alex Gorbatchev's syntax hhighlighter (JavaScript based). To avoid spam, you will be required to complete a small mathematical challenge when adding a new paste.
New! Try the pastebin command-line tool: paste.py (requires Python and python-beautifulsoup).
Powered by the Bulix.org Code Pastebin, by Maxime Petazzoni. View pastebin statistics.