1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
// main.cpp
#include "mygl.hpp"
#include <ctime>
#include <chrono>
int main(int argc, char *argv[])
{
std::vector <MG_Vertex> triangle =
{
{0.0f, 1.0f, 0.0f},
{-1.0f, -1.0f, 0.0f},
{1.0f, -1.0f, 0.0f}
};
std::vector <MG_Vertex> quadrilateral =
{
{-1.0f, 1.0f, 0.0f},
{1.0f, 1.0f, 0.0f},
{1.0f, -1.0f, 0.0f},
{-1.0f, -1.0f, 0.0f}
};
double theta = 0.0;
double fps_max = 60.0;
try
{
My_GL display;
while (!display.check_input())
{
LARGE_INTEGER frame_start, frame_stop, frame_diff;
QueryPerformanceCounter(&frame_start);
std::vector <MG_Vertex> quad1(quadrilateral.size());
for (unsigned int i = 0; i < quadrilateral.size(); i++)
{
quad1[i].x = quadrilateral[i].x * cos(theta) - quadrilateral[i].y * sin(theta);
quad1[i].y = quadrilateral[i].y * cos(theta) + quadrilateral[i].x * sin(theta);
}
display.MG_Clear_Screen();
display.MG_Reset_Matrix();
display.MG_Move_Matrix((const MG_Vertex){-1.5f, 0.0f, -6.0f});
display.MG_Save_Matrix();
display.MG_Rotate_Matrix(theta * (180 / M_PI), (const MG_Vertex){0.0f, 0.0f, 1.0f});
display.MG_Pick_Color(MG_Color::MG_GREEN);
display.MG_Draw_Triangle(triangle);
display.MG_Restore_Matrix();
display.MG_Move_Matrix((const MG_Vertex){3.0f, 0.0f, 0.0f});
display.MG_Pick_Color(MG_Color::MG_WHITE);
display.MG_Draw_Quadrilateral(quad1);
display.MG_Draw_Screen();
QueryPerformanceCounter(&frame_stop);
frame_diff.QuadPart = frame_stop.QuadPart - frame_start.QuadPart;
double frame_adjust = fps_max * (double)frame_diff.QuadPart / 1000000.0;
theta += (M_PI / 180.0) * frame_adjust;
if (theta >= (2 * M_PI))
theta = theta - (2 * M_PI);
}
}
catch (int x)
{
if (x == 10)
{
std::cout << "Exiting program due to error(s).\n";
return 0;
}
}
return 0;
}
// mygl.hpp
#ifndef MYGL_HPP
#define MYGL_HPP
#define _USE_MATH_DEFINES
#include <cmath>
#include <iostream>
#include <vector>
#include <SDL.h>
#include <SDL_opengl.h>
enum MG_Color {MG_BLACK, MG_BLUE, MG_GREEN, MG_CYAN, MG_RED, MG_MAGENTA,
MG_YELLOW, MG_WHITE, MG_BRIGHT_BLACK, MG_BRIGHT_BLUE,
MG_BRIGHT_GREEN, MG_BRIGHT_CYAN, MG_BRIGHT_RED,
MG_BRIGHT_MAGENTA, MG_BRIGHT_YELLOW, MG_BRIGHT_WHITE};
struct MG_Color_Format
{
GLfloat red;
GLfloat green;
GLfloat blue;
GLfloat alpha;
};
struct MG_Vertex
{
GLfloat x;
GLfloat y;
GLfloat z;
};
constexpr int SCREEN_WIDTH = 800;
constexpr int SCREEN_HEIGHT = 600;
constexpr char WINDOW_TITLE[] = "Very basic SDL2 OpenGL application";
constexpr GLfloat aspect = (GLfloat) SCREEN_WIDTH / (GLfloat) SCREEN_HEIGHT;
constexpr GLdouble fovY = 45.0f;
constexpr GLdouble zNear = 0.1f;
constexpr GLdouble zFar = 100.0f;
constexpr GLdouble fH = tan(fovY / 360 * M_PI) * zNear;
constexpr GLdouble fW = fH * aspect;
constexpr int OPENGL_MAJOR_VERSION = 2;
constexpr int OPENGL_MINOR_VERSION = 1;
constexpr SDL_GLprofile OPENGL_PROFILE = SDL_GLprofile::SDL_GL_CONTEXT_PROFILE_CORE;
constexpr GLclampf BG_CLEAR_RED = 0.33f;
constexpr GLclampf BG_CLEAR_GREEN = 0.33f;
constexpr GLclampf BG_CLEAR_BLUE = 0.33f;
constexpr GLclampf BG_CLEAR_ALPHA = 0.0f;
class My_GL
{
SDL_Window* displayWindow;
SDL_GLContext context;
SDL_Event e;
MG_Color_Format MG_16[16] =
{
{0.0f, 0.0f, 0.0f, 1.0f}, // BLACK
{0.0f, 0.0f, 0.67f, 1.0f}, // BLUE
{0.0f, 0.67f, 0.0f, 1.0f}, // GREEN
{0.0f, 0.67f, 0.67f, 1.0f}, // CYAN
{0.67f, 0.0f, 0.0f, 1.0f}, // RED
{0.67f, 0.0f, 0.67f, 1.0f}, // MAGENTA
{0.67f, 0.67f, 0.0f, 1.0f}, // YELLOW
{0.67f, 0.67f, 0.67f, 1.0f}, // WHITE
{0.33f, 0.33f, 0.33f, 1.0f}, // BRIGHT BLACK
{0.33f, 0.33f, 1.0f, 1.0f}, // BRIGHT BLUE
{0.33f, 1.0f, 0.33f, 1.0f}, // BRIGHT GREEN
{0.33f, 1.0f, 1.0f, 1.0f}, // BRIGHT CYAN
{1.0f, 0.33f, 0.33f, 1.0f}, // BRIGHT RED
{1.0f, 0.33f, 1.0f, 1.0f}, // BRIGHT MAGENTA
{1.0f, 1.0f, 0.33f, 1.0f}, // BRIGHT YELLOW
{1.0f, 1.0f, 1.0f, 1.0f} // BRIGHT WHITE
};
public:
My_GL();
~My_GL();
void MG_Clear_Screen();
void MG_Pick_Color(unsigned int color);
void MG_Draw_Triangle(const std::vector<MG_Vertex> & triangle);
void MG_Draw_Quadrilateral(const std::vector<MG_Vertex> & quadrilateral);
void MG_Reset_Matrix();
void MG_Move_Matrix(MG_Vertex move_matrix);
void MG_Rotate_Matrix(const GLfloat angle, const MG_Vertex move_matrix);
void MG_Save_Matrix();
void MG_Restore_Matrix();
void MG_Draw_Screen();
void MG_Delay(unsigned int wait_time);
bool check_input();
};
#endif
|