OpenGL/Screenshot

From ProgrammingExamples
< OpenGL
Revision as of 20:47, 23 November 2010 by Daviddoria (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Requires VXL.

Test.cxx

#include <iostream>
#include <cstdlib> //for exit()
 
#include <GL/glut.h>
 
#include <vil/vil_rgb.h>
#include <vil/vil_load.h>
#include <vil/vil_save.h>
#include <vil/vil_image_view.h>
#include <vil/vil_convert.h>
 
#include "Screenshot.h"
 
using namespace std;
 
void display(void);
void ProcessKeyboard(unsigned char key, int x, int y);
void DrawCube();
void DrawSizedCube(const float s);
void polygon(int a, int b, int c , int d);
void LookAt();
 
const unsigned int Width = 200;
const unsigned int Height = 100;
 
int main(int argc, char *argv[])
{
	cout << "OpenGL" << endl;
 
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize(Width, Height);
	glutInitWindowPosition(0, 0);
 
	//setup the main window
	glutCreateWindow("OpenGL Example");
 
	glutDisplayFunc(display);
	glutKeyboardFunc(ProcessKeyboard);
 
	glutMainLoop();
	return 0;
}
 
 
void display(void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glEnable(GL_DEPTH_TEST);
 
	LookAt();
	DrawCube();
 
	glutSwapBuffers();
 
}
 
 
void ProcessKeyboard(unsigned char key, int x, int y)
{
	if (key == 27) //escape
	{
		exit(0);
	}
	else if(key == 'q')
	{
		exit(0);
	}
	else if(key == 's')
	{
		cout << "Screenshot." << endl;
		Screenshot("Test.jpg");
		//ScreenshotFixed("Fixed.jpg");
	}
 
	display();
}
 
void DrawSizedCube(const float s)
{
	glPushMatrix();
		glScalef(s,s,s);
		DrawCube();
	glPopMatrix();
 
}
 
void DrawCube()
{
	//back
	glColor3f(1.0f, 0.0f, 0.0f);	// Color Red
	polygon(0,3,2,1);
 
	//top
	glColor3f(0.0f,1.0f,0.0f);	// Color Green
	polygon(2,3,7,6);
 
	//left
	glColor3f(0.0f, 0.0f, 1.0f);	// Color Blue
	polygon(0,4,7,3);
 
	//right
	glColor3f(1.0f,1.0f,0.0f);	// Color Yellow
	polygon(1,2,6,5);
 
	//front
	glColor3f(0.0f,1.0f,1.0f);	// Color Cyan
	polygon(4,5,6,7);
 
	//bottom
	glColor3f(1.0f,0.0f,1.0f);	// Color Magenta
	polygon(0,1,5,4);
 
}
 
 
GLfloat vertices[][3] = {{-1.0,-1.0,-1.0},{1.0,-1.0,-1.0},
		{1.0,1.0,-1.0}, {-1.0,1.0,-1.0}, {-1.0,-1.0,1.0},
{1.0,-1.0,1.0}, {1.0,1.0,1.0}, {-1.0,1.0,1.0}};
 
 
void polygon(int a, int b, int c , int d)
{
	glBegin(GL_QUADS);
	glVertex3fv(vertices[a]);
	glVertex3fv(vertices[b]);
	glVertex3fv(vertices[c]);
	glVertex3fv(vertices[d]);
	glEnd();
}
 
 
void LookAt()
{
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(70, 1, 1, 100);
 
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
 
	gluLookAt(3, 5, 5, 0, 0, 0, 0, 1, 0);
 
}

Screenshot.cxx

#include <iostream>
 
#include <GL/glut.h>
 
#include <vil/vil_rgb.h>
#include <vil/vil_load.h>
#include <vil/vil_save.h>
#include <vil/vil_image_view.h>
#include <vil/vil_convert.h>
 
#include <vbl/vbl_array_3d.h>
 
using namespace std;
 
void Screenshot(const string &filename)
{
  const unsigned int Width = glutGet(GLUT_WINDOW_WIDTH);
  const unsigned int Height = glutGet(GLUT_WINDOW_HEIGHT);
 
  vil_image_view<vil_rgb<vxl_byte> > RGBImage(Width, Height, 1, 1);
 
  vbl_array_3d<GLubyte> buffer(Height,Width,3);
 
  glReadBuffer(GL_FRONT);
 
  glReadPixels(0, 0, Width, Height, GL_RGB, GL_UNSIGNED_BYTE, buffer.begin());
 
  GLubyte r,g,b;
  for(unsigned int col = 0; col < Width; col++)
  {
      for(unsigned int row = 0; row < Height; row++)
      {
          //works, but upside down
// 			r = buffer(row, col, 0);
// 			g = buffer(row, col, 1);
// 			b = buffer(row, col, 2);
          r = buffer(Height - row - 1, col, 0);
          g = buffer(Height - row - 1, col, 1);
          b = buffer(Height - row - 1, col, 2);
          RGBImage(col, row) = vil_rgb<vxl_byte>(r,g,b);
      }
  }
 
  vil_save(RGBImage, filename.c_str());
 
  cout << "Done writing RGB image." << endl;
}
 
void ScreenshotFixed(const string &filename)
{
  unsigned int Width = 200;
  unsigned int Height = 100;
 
  GLubyte buffer[100][200][3];
  glReadPixels(0, 0, Width, Height, GL_RGB, GL_UNSIGNED_BYTE, buffer);
 
  vil_image_view<vil_rgb<vxl_byte> > RGBImage(Width, Height, 1, 1); //(cols, rows, 1, 1)
 
  GLubyte r,g,b;
 
  for(unsigned int i = 0; i < Width; ++i)
  {
      for(unsigned int j = 0; j < Height; ++j)
      {
          r = buffer[j][i][0];
          g = buffer[j][i][1];
          b = buffer[j][i][2];
          RGBImage(i, j) = vil_rgb<vxl_byte>(r,g,b);
      }
  }
 
  vil_save(RGBImage, filename.c_str());
 
  cout << "Done writing RGB image." << endl;
}

Screenshot.h

#ifndef SCREENSHOT_H
#define SCREENSHOT_H
 
#include <string>
 
using namespace std;
 
void Screenshot(const string &filename);
void ScreenshotFixed(const string &filename);
 
#endif

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
 
PROJECT(Screenshot)
 
FIND_PACKAGE(VXL REQUIRED)
INCLUDE(${VXL_CMAKE_DIR}/UseVXL.cmake)
 
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp") #g++
 
INCLUDE_DIRECTORIES(/usr/include/ /usr/local/include/)
 
LINK_DIRECTORIES(/usr/lib /usr/local/lib)
 
ADD_EXECUTABLE(Screenshot Test.cpp Screenshot.cpp)
 
TARGET_LINK_LIBRARIES(Screenshot
glut GLU GL Xmu X11
vil vgl vnl vgl_algo vbl
)