Skip to content

Instantly share code, notes, and snippets.

@GaProgMan
Created October 11, 2013 21:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GaProgMan/6942022 to your computer and use it in GitHub Desktop.
Save GaProgMan/6942022 to your computer and use it in GitHub Desktop.
/*
* Project Name: BigDipper
* Solution Name: BigDipper
* Original creation date: 24/03/2008
* Edit date: 19/01/2013
* Programmer name: Jamie Taylor (aka "GaProgMan")
* File name: VBigDipper.cpp
*
* Purpose of the project:
* My favourite constellation is the Big Dipper. So
* it seemed only natrual that my first project,
* post university, was going to be the re-create
* the constellation (as I saw it in the night sky)
* with C++ and OpenGL
*
* GNU Copyright information
* Copyright 2011 Jamie Taylor <jamie@taylorj.org.uk>
*
* This program is free software; you can redistribute
* it and/or modify it under the terms of the GNU General
* Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* You should have received a copy of the GNU General
* Public License along with this program; if not, write
* to the Free Software Foundation, Inc., 51 Franklin
* Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
// remove rarely used commands from attached headers
#define WIN32_LEAN_AND_MEAN
// the above command may only work on 32bit versions
// of windows
#define VC_EXTRALEAN
// multiple use headers, always good to include them. You
// never know when one of the commands might come in useful
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <atlstr.h>
#include <iostream>
#include <math.h>
#include <cstdlib>
#include <ctime>
// header required for windows
#include <windows.h>
//OpenGL and DevIL #includes
#include <math.h>
#include <GL\freeglut.h>
#include <GL\freeglut_ext.h>
#include <GL\freeglut_std.h>
#include <IL\il.h>
#include <IL\ilu.h>
#include <IL\ilut.h>
// required for some commands (cout and cin, etc)
using namespace std;
// Set the size of the OpenGL window, and screen space size
double winSizeX = 500;
double winSizeY = 500;
void DrawScene(void);
void renderBitmapString(float x, float y, void *font,char *string);
// This function is continuously called.
void Idle(void) {
DrawScene();
}
/***********************************************************
* renderBitmapString function
*
* This function takes a string, a set of co-ordinates,
* and a font, then outputs the string to the OpenGL
* window.
***********************************************************/
void renderBitmapString(float x, float y, void *font,char *string) {
// Fonts available are :
// GLUT_BITMAP_8_BY_13
// GLUT_BITMAP_9_BY_15
// GLUT_BITMAP_TIMES_ROMAN_10
// GLUT_BITMAP_TIMES_ROMAN_24
// GLUT_BITMAP_HELVETICA_10
// GLUT_BITMAP_HELVETICA_12
// GLUT_BITMAP_HELVETICA_18
glColor3f(1.0f, 1.0f, 1.0f);
char *c;
glRasterPos2f(x, y);
for (c=string; *c != '\0'; c++) {
glutBitmapCharacter(font, *c);
}
}
/***********************************************************
* Init function
*
* This function sets up the co-ordinate system, the point
* size, the background colour, and the drawing colour.
*
* The background colour is set to black
* (0 [red], 0 [green] , 0 [blue], 0 [alpha])
*
* The drawing colour is set to white
* (1 [red], 1 [green] , 1 [blue])
*
* The dot size is set to 1.0, which means that each drawn
* dot is 1 pixel by 1 pixel.
*
* The matrix, is set to Projection mode.
***********************************************************/
void Init() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glColor3f(1.0f, 1.0f, 1.0f);
glPointSize(2.0);
glMatrixMode(GL_PROJECTION);
glOrtho(0, winSizeX, 0, winSizeY,-1,1);
glMatrixMode(GL_MODELVIEW);
}
/***********************************************************
* Reshape function
*
* This function is required for when the user reshapes the window.
***********************************************************/
void Reshape(int width, int height) {
glViewport(0, 0, width, height);
}
// uses ASCII for character input
void Key(unsigned char key, int x, int y) {
switch (key) {
case 27:
// escape key
exit(0);
break;
case ' ':
glutIdleFunc(Idle);
break;
}
}
// OpenGL GLUT method, (obviously) requires GLUt and OpenGL to work
void SpecialKey(int Specialkey, int x, int y) {
switch (Specialkey) {
case GLUT_KEY_DOWN:
break;
case GLUT_KEY_UP:
break;
case GLUT_KEY_LEFT:
break;
case GLUT_KEY_RIGHT:
break;
}
}
/***********************************************************
* DrawScene callback function
*
* This function is called every time around the unending
* loop that is the glutMainLoop (in the main function)
*
* This function contains all of the drawing commands.
* Anything commands that draw on screen, are contained in
* this function.
*
* glClear; clears the screen and depth buffer
*
* All objects to be drawn fall between a glBegin() and
* glEnd(). A good practise is to seperate each object
* into seperate calls of pairs.
*
* glFlush() flushes the screen buffer, and forces every
* thing within the buffer to be drawn on screen
***********************************************************/
void DrawScene(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Drawing code here
// Big dipper constellation
glBegin(GL_POINTS);
glVertex2i(289, 190); // Merak
glVertex2i(320, 128); // Phecda
glVertex2i(194, 101); // Megrez
glVertex2i(129, 83); // Alioth
glVertex2i(75, 73); // Mizar
glVertex2i(74, 74); // Alcor
glVertex2i(20, 10); // Alkaid
glEnd();
// End of drawing code
glutSwapBuffers();
// swap the buffers, end the drawing phase
}
/***********************************************************
* main function
*
* The main function is where all C++ programs begin
*
* glutInit
* This command initializes the OpenGL Utility Toolkit (GLUT)
*
* glutInitDisplayMode
* Specifies how the display should be initialised. The
* arguments (GLUT_SINGLE and GLUT_RGB) are ORed together,
* and indicate a single display buffer with RGB should be used.
*
* glutInitWindowSize
* Specifies the size of the screen window.
*
* glutInitWindowPosition
* Specifies the initial position of the screen window.
*
* glutInitCreateWindow
* Opens and displays the window, with the argument string
* being the text that appears in the title bar (at the top of the window)
*
* glutDisplayFunction
* Registers the (programmer specified) redraw function.
*
* glutMainLoop
* Enters the system into a never ending "wait until some
* thing happens" loop. Required for "event driven programming"
***********************************************************/
int main(int argc, char **argv) {
glutInitWindowSize(winSizeX, winSizeY);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE);
glutCreateWindow("OpenGL Template");
Init();
glutReshapeFunc(Reshape);
glutSpecialFunc(SpecialKey);
glutKeyboardFunc(Key);
glutDisplayFunc(DrawScene);
glutIdleFunc(Idle);
glutMainLoop();
return 0; /* ANSI C requires main to return int. */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment