133 lines
4.5 KiB
C++
133 lines
4.5 KiB
C++
#include <GL/glut.h>
|
|
#include "JGL/JGL.h"
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
#include <windows.h>
|
|
#endif
|
|
|
|
void initGL()
|
|
{
|
|
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
|
}
|
|
|
|
|
|
void display() {
|
|
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer
|
|
glMatrixMode(GL_MODELVIEW); // To operate on the Model-View matrix
|
|
glLoadIdentity(); // Reset the model-view matrix.
|
|
|
|
|
|
// Define shapes enclosed within a pair of glBegin and glEnd
|
|
|
|
glBegin(GL_QUADS); // Each set of 4 vertices form a quad
|
|
glColor3f(1.f, 0.f, 0.f);
|
|
glVertex2f(-0.8f, 0.1f);
|
|
glVertex2f(-0.2f, 0.1f);
|
|
glVertex2f(-0.2f, 0.7f);
|
|
glVertex2f(-0.8f, 0.7f);
|
|
|
|
glColor3f(0.0f, 1.0f, 0.0f); // Green
|
|
glVertex2f(-0.7f, -0.6f);
|
|
glVertex2f(-0.1f, -0.6f);
|
|
glVertex2f(-0.1f, 0.0f);
|
|
glVertex2f(-0.7f, 0.0f);
|
|
|
|
glColor3f(0.2f, 0.2f, 0.2f); // Dark Gray
|
|
glVertex2f(-0.9f, -0.7f);
|
|
glColor3f(1.f, 1.f, 1.f); // White
|
|
glVertex2f(-0.5f, -0.7f);
|
|
glColor3f(.2f, .2f, .2f); // Dark Gray
|
|
glVertex2f(-0.5f, -0.3f);
|
|
glColor3f(1.f, 1.f, 1.f); // White
|
|
glVertex2f(-0.9f, -0.3f);
|
|
glEnd();
|
|
|
|
JGL::FillRect2D();
|
|
|
|
glBegin(GL_TRIANGLES);
|
|
glColor3f(0.0f, 0.0f, 1.f); // Blue
|
|
glVertex2f(0.1f, -0.6f);
|
|
glVertex2f(0.7f, -0.6f);
|
|
glVertex2f(0.4f, -0.1f);
|
|
|
|
glColor3f(1.f, 0.f, 0.f); // Red
|
|
glVertex2f(0.3f, -0.4f);
|
|
glColor3f(0.0f, 1.0f, 0.f); // Green
|
|
glVertex2f(0.9f, -0.4f);
|
|
glColor3f(0.f, 0.f, 1.f); // Blue
|
|
glVertex2f(0.6f, -0.9f);
|
|
glEnd();
|
|
|
|
glBegin(GL_POLYGON); // These verts form a closed polygon
|
|
glColor3f(1.f, 1.f, 0.f); // Yellow
|
|
glVertex2f(0.4f, 0.2f);
|
|
glVertex2f(0.6f, 0.2f);
|
|
glVertex2f(0.7f, 0.4f);
|
|
glVertex2f(0.6f, 0.6f);
|
|
glVertex2f(0.4f, 0.6f);
|
|
glVertex2f(0.3f, 0.4f);
|
|
glEnd();
|
|
|
|
glFlush(); // Render now
|
|
}
|
|
|
|
int windowWidth = 640; // Windowed mode's width
|
|
int windowHeight = 480; // Windowed mode's height
|
|
int windowPosX = 50; // Windowed mode's top-left corner x
|
|
int windowPosY = 50; // Windowed mode's top-left corner y
|
|
bool fullscreenMode = true;
|
|
|
|
void specialKeys(int key, int x, int y) {
|
|
switch(key) {
|
|
case GLUT_KEY_F1:
|
|
fullscreenMode = !fullscreenMode;
|
|
if (fullscreenMode) {
|
|
windowPosX = glutGet(GLUT_WINDOW_X);
|
|
windowPosY = glutGet(GLUT_WINDOW_Y);
|
|
windowWidth = glutGet(GLUT_WINDOW_WIDTH);
|
|
windowHeight = glutGet(GLUT_WINDOW_HEIGHT);
|
|
|
|
glutFullScreen();
|
|
} else {
|
|
glutReshapeWindow(windowWidth, windowHeight);
|
|
glutPositionWindow(windowPosX, windowPosY);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Handler for window re-size event. Called back when the window first appears
|
|
// and whenever the window is re-sized with its new width and height
|
|
void reshape(GLsizei width, GLsizei height) {
|
|
if (height == 0) height = 1;
|
|
GLfloat aspect = (GLfloat) width / (GLfloat) height;
|
|
|
|
// Set the viewport to cover the new window
|
|
glViewport(0, 0, width, height);
|
|
|
|
// Set the aspect ratio of the clipping area to match the viewport
|
|
glMatrixMode(GL_PROJECTION); // Operate on the Projection Matrix;
|
|
glLoadIdentity(); // Reset the projection matrix
|
|
if (width >= height) {
|
|
// Aspect >= 1, set the height from -1 to 1
|
|
gluOrtho2D(-1.0 * aspect, 1.0 * aspect, -1.0, 1.0);
|
|
} else {
|
|
// Aspect < 1, set the width from -1 to 1, with larger height
|
|
gluOrtho2D(-1.0, 1.0, -1.0 / aspect, 1.0 / aspect);
|
|
}
|
|
}
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
{
|
|
glutInit(&argc, argv); // Initialize GLUT
|
|
glutCreateWindow("Vertex, Primitive, & Color"); // Create window with given title
|
|
glutInitWindowSize(320, 320); // Set the window's initial width & height - non-square
|
|
glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
|
|
glutDisplayFunc(display); // Register callback handler for window re-paint event
|
|
glutReshapeFunc(reshape); // Register callback handler for window re-size event
|
|
glutSpecialFunc(specialKeys); // Register callback handler for special-key event
|
|
initGL(); // Our own OpenGL initialization
|
|
glutMainLoop();
|
|
return 0;
|
|
} |