struct
This commit is contained in:
parent
209ca9c2c0
commit
82c475f92e
792
main.cpp
792
main.cpp
|
@ -6,6 +6,8 @@
|
|||
#include <cstdlib>
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
|
||||
#define SDL_MAIN_HANDLED
|
||||
#include <SDL.h>
|
||||
#include <SDL_image.h>
|
||||
#include <SDL_ttf.h>
|
||||
|
@ -176,413 +178,425 @@ void set_framerate_indicator(int frame_count, GLuint id)
|
|||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
struct Game
|
||||
{
|
||||
setenv("SDL_VIDEO_X11_LEGACY_FULLSCREEN", "0", true);
|
||||
SDL_version version;
|
||||
SDL_GetVersion(&version);
|
||||
printf("SDL %d.%d.%d\n", version.major, version.minor, version.patch);
|
||||
fprintf(stderr, "stderr test message\n");
|
||||
SDL_SetMainReady();
|
||||
if (SDL_Init(SDL_INIT_VIDEO) < 0)
|
||||
SDL_Window *window;
|
||||
int sw, sh;
|
||||
|
||||
Game()
|
||||
{
|
||||
fprintf(stderr, "SDL could not initalize! SDL_Error: %s\n", SDL_GetError());
|
||||
}
|
||||
else
|
||||
{
|
||||
int sw = 640, sh = 480;
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
|
||||
SDL_Window *window = SDL_CreateWindow(
|
||||
"TARE control",
|
||||
SDL_WINDOWPOS_UNDEFINED,
|
||||
SDL_WINDOWPOS_UNDEFINED,
|
||||
sw,
|
||||
sh,
|
||||
SDL_WINDOW_OPENGL);
|
||||
if (window == NULL)
|
||||
setenv("SDL_VIDEO_X11_LEGACY_FULLSCREEN", "0", true);
|
||||
SDL_version version;
|
||||
SDL_GetVersion(&version);
|
||||
printf("SDL %d.%d.%d\n", version.major, version.minor, version.patch);
|
||||
fprintf(stderr, "stderr test message\n");
|
||||
SDL_SetMainReady();
|
||||
if (SDL_Init(SDL_INIT_VIDEO) < 0)
|
||||
{
|
||||
fprintf(stderr, "Could not create window: %s\n", SDL_GetError());
|
||||
return 1;
|
||||
fprintf(stderr, "SDL could not initalize! SDL_Error: %s\n", SDL_GetError());
|
||||
}
|
||||
else
|
||||
{
|
||||
SDL_ShowCursor(0);
|
||||
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
|
||||
if (renderer == NULL)
|
||||
sw = 640, sh = 480;
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
|
||||
window = SDL_CreateWindow(
|
||||
"TARE control",
|
||||
SDL_WINDOWPOS_UNDEFINED,
|
||||
SDL_WINDOWPOS_UNDEFINED,
|
||||
sw,
|
||||
sh,
|
||||
SDL_WINDOW_OPENGL);
|
||||
if (window == NULL)
|
||||
{
|
||||
fprintf(stderr, "Could not create renderer! SDL_Error: %s\n", SDL_GetError());
|
||||
fprintf(stderr, "Could not create window: %s\n", SDL_GetError());
|
||||
}
|
||||
else
|
||||
{
|
||||
SDL_Texture *texture = IMG_LoadTexture(renderer, "background.png");
|
||||
if (!texture)
|
||||
SDL_ShowCursor(0);
|
||||
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
|
||||
if (renderer == NULL)
|
||||
{
|
||||
fprintf(stderr, "Could not load image! SDL_Error: %s\n", SDL_GetError());
|
||||
fprintf(stderr, "Could not create renderer! SDL_Error: %s\n", SDL_GetError());
|
||||
}
|
||||
else
|
||||
{
|
||||
TTF_Init();
|
||||
SDL_GLContext glcontext = SDL_GL_CreateContext(window);
|
||||
printf("OpenGL %s\n", glGetString(GL_VERSION));
|
||||
SDL_Event event;
|
||||
int done = 0;
|
||||
float r = 0.0;
|
||||
GLuint vao, vbo;
|
||||
|
||||
// GLfloat diamond[4][2] = {
|
||||
// { 0.0, 1.0 },
|
||||
// { 1.0, 0.0 },
|
||||
// { 0.0, -1.0 },
|
||||
// { -1.0, 0.0 } };
|
||||
|
||||
// const GLfloat diamond_colors[4][3] = {
|
||||
// { 1.0, 0.0, 0.0 },
|
||||
// { 0.0, 1.0, 0.0 },
|
||||
// { 0.0, 0.0, 1.0 },
|
||||
// { 1.0, 1.0, 1.0 } };
|
||||
|
||||
/*
|
||||
v0-v1-v2 (front)
|
||||
v2-v3-v0
|
||||
v0-v3-v4 (right)
|
||||
v4-v5-v0
|
||||
v0-v5-v6 (top)
|
||||
v6-v1-v0
|
||||
v1-v6-v7 (left)
|
||||
v7-v2-v1
|
||||
v7-v4-v3 (bottom)
|
||||
v3-v2-v7
|
||||
v4-v7-v6 (back)
|
||||
v6-v5-v4
|
||||
*/
|
||||
std::array<glm::vec3, 36> cube = {{
|
||||
{1, 1, 1}, {-1, 1, 1}, {-1,-1, 1},
|
||||
{-1,-1, 1}, {1,-1, 1}, {1, 1, 1},
|
||||
|
||||
{1, 1, 1}, {1,-1, 1}, {1,-1,-1},
|
||||
{1,-1,-1}, {1, 1,-1}, {1, 1, 1},
|
||||
|
||||
{1, 1, 1}, {1, 1,-1}, {-1, 1,-1},
|
||||
{-1, 1,-1}, {-1, 1, 1}, {1, 1, 1},
|
||||
|
||||
{-1, 1, 1}, {-1, 1,-1}, {-1,-1,-1},
|
||||
{-1,-1,-1}, {-1,-1, 1}, {-1, 1, 1},
|
||||
|
||||
{-1,-1,-1}, {1,-1,-1}, {1,-1, 1},
|
||||
{1,-1, 1}, {-1,-1, 1}, {-1,-1,-1},
|
||||
|
||||
{1,-1,-1}, {-1,-1,-1}, {-1, 1,-1},
|
||||
{-1, 1,-1}, {1, 1,-1}, {1,-1,-1}
|
||||
}};
|
||||
// GLfloat cube_colors[36][3] = {
|
||||
// {1, 0, 0}, {1, 0, 0}, {1, 0, 0},
|
||||
// {1, 0, 0}, {1, 0, 0}, {1, 0, 0},
|
||||
|
||||
// {0, 1, 0}, {0, 1, 0}, {0, 1, 0},
|
||||
// {0, 1, 0}, {0, 1, 0}, {0, 1, 0},
|
||||
|
||||
// {0, 0, 1}, {0, 0, 1}, {0, 0, 1},
|
||||
// {0, 0, 1}, {0, 0, 1}, {0, 0, 1},
|
||||
|
||||
// {1, 1, 0}, {1, 1, 0}, {1, 1, 0},
|
||||
// {1, 1, 0}, {1, 1, 0}, {1, 1, 0},
|
||||
|
||||
// {0, 1, 1}, {0, 1, 1}, {0, 1, 1},
|
||||
// {0, 1, 1}, {0, 1, 1}, {0, 1, 1},
|
||||
|
||||
// {1, 0, 1}, {1, 0, 1}, {1, 0, 1},
|
||||
// {1, 0, 1}, {1, 0, 1}, {1, 0, 1}
|
||||
// };
|
||||
|
||||
std::array<glm::vec3, 6> background_vertices = {
|
||||
{
|
||||
{-1, 1, 0}, {1, 1, 0}, {-1, -1, 0},
|
||||
{1, 1, 0}, {1, -1, 0}, {-1, -1, 0}
|
||||
}};
|
||||
GLfloat background_colors[40][3] = {
|
||||
{.2, .6, .8}, {.2, .6, .8}, {1, 1, 0},
|
||||
{.2, .6, .8}, {1, 1, 0}, {1, 1, 0}
|
||||
};
|
||||
GLuint background_colors_buffer;
|
||||
glGenBuffers(1, &background_colors_buffer);
|
||||
|
||||
std::array<glm::vec3, 6> framerate_indicator_vertices = {
|
||||
{
|
||||
{.9, 1, 0}, {1, 1, 0}, {.9, .9, 0},
|
||||
{1, 1, 0}, {1, .9, 0}, {.9, .9, 0}
|
||||
}};
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
|
||||
std::vector<glm::vec3> vertices;
|
||||
vertices.reserve(cube.size() + background_vertices.size() + framerate_indicator_vertices.size());
|
||||
vertices.insert(vertices.begin(), cube.begin(), cube.end());
|
||||
vertices.insert(vertices.end(), background_vertices.begin(), background_vertices.end());
|
||||
vertices.insert(vertices.end(), framerate_indicator_vertices.begin(),
|
||||
framerate_indicator_vertices.end());
|
||||
|
||||
glm::mat4 projection = glm::perspective(glm::radians(45.0f),
|
||||
(float) sw / (float) sh, 0.1f, 100.0f);
|
||||
glm::mat4 view = glm::lookAt(
|
||||
glm::vec3(4, 3, 3),
|
||||
glm::vec3(0, 0, 0),
|
||||
glm::vec3(0, 1, 0));
|
||||
glm::mat4 model = glm::mat4(1.0f);
|
||||
glm::mat4 mvp;
|
||||
|
||||
SDL_Surface *surface = rotateSurface90Degrees(
|
||||
IMG_Load("tile.png"), 2);
|
||||
printf("bytes per pixel %i\n", surface->format->BytesPerPixel);
|
||||
GLuint t_id = get_gl_texture_from_surface(surface, GL_LINEAR);
|
||||
|
||||
std::array<glm::vec2, 6> framerate_indicator_uv = {
|
||||
{
|
||||
{0, 1}, {1, 1}, {0, 0},
|
||||
{1, 1}, {1, 0}, {0, 0}
|
||||
}};
|
||||
std::array<glm::vec2, 36> cube_uv = {
|
||||
{
|
||||
{1, 1}, {0, 1}, {0, 0},
|
||||
{0, 0}, {1, 0}, {1, 1},
|
||||
{0, 1}, {0, 0}, {1, 0},
|
||||
{1, 0}, {1, 1}, {0, 1},
|
||||
{0, 1}, {0, 0}, {1, 0},
|
||||
{1, 0}, {1, 1}, {0, 1},
|
||||
{1, 1}, {0, 1}, {0, 0},
|
||||
{0, 0}, {1, 0}, {1, 1},
|
||||
{0, 0}, {1, 0}, {1, 1},
|
||||
{1, 1}, {0, 1}, {0, 0},
|
||||
{0, 0}, {1, 0}, {1, 1},
|
||||
{1, 1}, {0, 1}, {0, 0}
|
||||
}};
|
||||
std::vector<glm::vec2> uv;
|
||||
uv.reserve(cube_uv.size() + background_vertices.size() + framerate_indicator_uv.size());
|
||||
std::copy(cube_uv.begin(), cube_uv.end(), uv.begin());
|
||||
std::copy(framerate_indicator_uv.begin(), framerate_indicator_uv.end(),
|
||||
uv.begin() + cube_uv.size() + background_vertices.size());
|
||||
// uv.insert(uv.end(), cube_uv.begin(), cube_uv.end());
|
||||
// uv.insert(uv.end(), framerate_indicator_uv.begin(),
|
||||
// framerate_indicator_uv.end());
|
||||
GLuint uvbuffer;
|
||||
glGenBuffers(1, &uvbuffer);
|
||||
|
||||
GLuint fake_texture_id;
|
||||
unsigned char fake_texture_color[4] = {255, 255, 255, 255};
|
||||
glCreateTextures(GL_TEXTURE_2D, 1, &fake_texture_id);
|
||||
glBindTexture(GL_TEXTURE_2D, fake_texture_id);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
|
||||
fake_texture_color);
|
||||
|
||||
// Allocate and assign a Vertex Array Object to our handle
|
||||
glGenVertexArrays(1, &vao);
|
||||
|
||||
// Bind our Vertex Array Object as the current used object
|
||||
glBindVertexArray(vao);
|
||||
|
||||
// Allocate and assign one Vertex Buffer Objects to our handle
|
||||
glGenBuffers(1, &vbo);
|
||||
|
||||
// Bind vbo as being the active buffer and storing vertex coordinates
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||
|
||||
// Copy vertex data into buffer
|
||||
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLfloat) * 3, &vertices.front(),
|
||||
GL_STATIC_DRAW);
|
||||
|
||||
// Specify that our coordinate data is going into attribute
|
||||
// index 0, and contains three floats per vertex
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
||||
|
||||
// Enable attribute index 0 as being used
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
// Bind our second VBO as being the active buffer and storing texture coordinates
|
||||
glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
|
||||
|
||||
// Copy the data from cube_uv to our buffer
|
||||
glBufferData(GL_ARRAY_BUFFER, uv.capacity() * sizeof(GLfloat) * 2, &uv.front(), GL_STATIC_DRAW);
|
||||
|
||||
// Specify that our texture coordinate data is going into attribute index 1
|
||||
// and contains two floats per vertex
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
|
||||
|
||||
// Enable attribute index 1
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
glEnableVertexAttribArray(2);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, background_colors_buffer);
|
||||
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLfloat) * 3, 0, GL_STATIC_DRAW);
|
||||
glBufferSubData(GL_ARRAY_BUFFER,
|
||||
(cube.size()) * sizeof(GLfloat) * 3,
|
||||
background_vertices.size() * sizeof(GLfloat) * 3, background_colors);
|
||||
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
||||
|
||||
GLuint vertex_shader = load_shader("triangle.vert", GL_VERTEX_SHADER);
|
||||
GLuint fragment_shader = load_shader("triangle.frag", GL_FRAGMENT_SHADER);
|
||||
GLuint flat_shader = load_shader("flat.vert", GL_VERTEX_SHADER);
|
||||
|
||||
GLuint world_program = glCreateProgram();
|
||||
glAttachShader(world_program, vertex_shader);
|
||||
glAttachShader(world_program, fragment_shader);
|
||||
glBindAttribLocation(world_program, 0, "in_Position");
|
||||
glBindAttribLocation(world_program, 1, "vertexUV");
|
||||
glBindAttribLocation(world_program, 2, "in_Color");
|
||||
link_shader(world_program);
|
||||
|
||||
GLuint flat_program = glCreateProgram();
|
||||
glAttachShader(flat_program, flat_shader);
|
||||
glAttachShader(flat_program, fragment_shader);
|
||||
glBindAttribLocation(flat_program, 0, "in_Position");
|
||||
glBindAttribLocation(flat_program, 1, "vertexUV");
|
||||
glBindAttribLocation(flat_program, 2, "in_Color");
|
||||
link_shader(flat_program);
|
||||
|
||||
GLuint m_id = glGetUniformLocation(world_program, "MVP");
|
||||
GLuint t_uniform_id = glGetUniformLocation(world_program, "myTextureSampler");
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, t_id);
|
||||
glUniform1i(t_uniform_id, 0);
|
||||
|
||||
glDepthFunc(GL_LESS);
|
||||
|
||||
int framerate = 60;
|
||||
float frame_length = 1000.0 / framerate;
|
||||
int last_frame_timestamp, frame_count_timestamp, last_capture_timestamp;
|
||||
last_frame_timestamp = frame_count_timestamp = last_capture_timestamp = SDL_GetTicks();
|
||||
bool is_recording = false, show_framerate = false;
|
||||
std::list<SDL_Surface*> frames;
|
||||
int recording_capture_framerate = 100, frame_time_overflow = 0, capture_time_overflow = 0,
|
||||
frame_count = 0, ticks;
|
||||
|
||||
GLuint framerate_texture_id = get_gl_texture_from_surface(
|
||||
get_framerate_indicator_surface(frame_count), GL_LINEAR);
|
||||
|
||||
while (!done)
|
||||
SDL_Texture *texture = IMG_LoadTexture(renderer, "background.png");
|
||||
if (!texture)
|
||||
{
|
||||
ticks = SDL_GetTicks();
|
||||
if (ticks - last_frame_timestamp + frame_time_overflow >= frame_length)
|
||||
{
|
||||
frame_count++;
|
||||
frame_time_overflow = ticks - last_frame_timestamp +
|
||||
frame_time_overflow - frame_length;
|
||||
last_frame_timestamp = ticks;
|
||||
for (int ii = 1; frame_time_overflow > frame_length;
|
||||
ii++, frame_time_overflow -= frame_length)
|
||||
{
|
||||
fprintf(stderr, "lost %i frame(s)\n", ii);
|
||||
}
|
||||
while (SDL_PollEvent(&event))
|
||||
{
|
||||
if (event.type == SDL_QUIT)
|
||||
{
|
||||
done = 1;
|
||||
}
|
||||
else if (event.type == SDL_KEYDOWN)
|
||||
{
|
||||
if (event.key.keysym.sym == SDLK_F9)
|
||||
{
|
||||
capture_screen(window);
|
||||
}
|
||||
else if (event.key.keysym.sym == SDLK_F10)
|
||||
{
|
||||
if (!is_recording)
|
||||
{
|
||||
start_recording(&is_recording);
|
||||
}
|
||||
else
|
||||
{
|
||||
end_recording(frames, &is_recording);
|
||||
}
|
||||
}
|
||||
else if (event.key.keysym.sym == SDLK_F11)
|
||||
{
|
||||
if (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN)
|
||||
{
|
||||
SDL_SetWindowFullscreen(window, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
|
||||
}
|
||||
}
|
||||
else if (event.key.keysym.sym == SDLK_f && SDL_GetModState() & KMOD_CTRL)
|
||||
{
|
||||
show_framerate = !show_framerate;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (is_recording && ticks - last_capture_timestamp + capture_time_overflow >
|
||||
recording_capture_framerate)
|
||||
{
|
||||
frames.push_back(get_screen_surface(window));
|
||||
printf("added frame at %i\n", ticks);
|
||||
capture_time_overflow = ticks - last_capture_timestamp + capture_time_overflow -
|
||||
recording_capture_framerate;
|
||||
last_capture_timestamp = ticks;
|
||||
for (int ii = 1; capture_time_overflow > recording_capture_framerate;
|
||||
ii++, capture_time_overflow -= recording_capture_framerate)
|
||||
{
|
||||
fprintf(stderr, "lost %i frame(s) during capture\n", ii);
|
||||
}
|
||||
}
|
||||
// glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
|
||||
// glBufferData(GL_ARRAY_BUFFER, 8 * sizeof(GLfloat), diamond, GL_STATIC_DRAW);
|
||||
// glClearColor((sin(r) + 1) / 2, (sin(r) + 1) / 2, .5, 1);
|
||||
glClearColor(.7, .7, .5, 1);
|
||||
r += .025;
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
glUseProgram(flat_program);
|
||||
glBindTexture(GL_TEXTURE_2D, fake_texture_id);
|
||||
glDisableVertexAttribArray(1);
|
||||
glEnableVertexAttribArray(2);
|
||||
glDrawArrays(GL_TRIANGLES, 36, 6);
|
||||
if (show_framerate)
|
||||
{
|
||||
glBindTexture(GL_TEXTURE_2D, framerate_texture_id);
|
||||
glDisableVertexAttribArray(2);
|
||||
glEnableVertexAttribArray(1);
|
||||
glVertexAttrib3f(2, 1, 1, 1);
|
||||
glDrawArrays(GL_TRIANGLES, 42, 6);
|
||||
}
|
||||
// printf("%s\n", glm::to_string(model).c_str());
|
||||
model = glm::rotate(model, .0005f * frame_length, glm::vec3(0.0f, 1.0f, 0.0f));
|
||||
mvp = projection * view * model;
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glUseProgram(world_program);
|
||||
glUniformMatrix4fv(m_id, 1, GL_FALSE, &mvp[0][0]);
|
||||
glBindTexture(GL_TEXTURE_2D, t_id);
|
||||
glEnableVertexAttribArray(1);
|
||||
glDisableVertexAttribArray(2);
|
||||
glVertexAttrib3f(2, 1, 1, 1);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||
//glFlush();
|
||||
//SDL_Rect rect = {x++, 0, 240, 160};
|
||||
//SDL_RenderCopy(renderer, texture, NULL, &rect);
|
||||
//SDL_RenderPresent(renderer);
|
||||
SDL_GL_SwapWindow(window);
|
||||
}
|
||||
if (ticks - frame_count_timestamp >= 1000)
|
||||
{
|
||||
frame_count_timestamp = ticks;
|
||||
if (show_framerate)
|
||||
{
|
||||
set_framerate_indicator(frame_count, framerate_texture_id);
|
||||
}
|
||||
frame_count = 0;
|
||||
}
|
||||
SDL_Delay(15);
|
||||
fprintf(stderr, "Could not load image! SDL_Error: %s\n", SDL_GetError());
|
||||
}
|
||||
SDL_GL_DeleteContext(glcontext);
|
||||
SDL_DestroyTexture(texture);
|
||||
else
|
||||
{
|
||||
TTF_Init();
|
||||
SDL_GLContext glcontext = SDL_GL_CreateContext(window);
|
||||
printf("OpenGL %s, renderer %s, shading language %s\n",
|
||||
glGetString(GL_VERSION), glGetString(GL_RENDERER),
|
||||
glGetString(GL_SHADING_LANGUAGE_VERSION));
|
||||
SDL_Event event;
|
||||
int done = 0;
|
||||
float r = 0.0;
|
||||
GLuint vao, vbo;
|
||||
|
||||
// GLfloat diamond[4][2] = {
|
||||
// { 0.0, 1.0 },
|
||||
// { 1.0, 0.0 },
|
||||
// { 0.0, -1.0 },
|
||||
// { -1.0, 0.0 } };
|
||||
|
||||
// const GLfloat diamond_colors[4][3] = {
|
||||
// { 1.0, 0.0, 0.0 },
|
||||
// { 0.0, 1.0, 0.0 },
|
||||
// { 0.0, 0.0, 1.0 },
|
||||
// { 1.0, 1.0, 1.0 } };
|
||||
|
||||
/*
|
||||
v0-v1-v2 (front)
|
||||
v2-v3-v0
|
||||
v0-v3-v4 (right)
|
||||
v4-v5-v0
|
||||
v0-v5-v6 (top)
|
||||
v6-v1-v0
|
||||
v1-v6-v7 (left)
|
||||
v7-v2-v1
|
||||
v7-v4-v3 (bottom)
|
||||
v3-v2-v7
|
||||
v4-v7-v6 (back)
|
||||
v6-v5-v4
|
||||
*/
|
||||
std::array<glm::vec3, 36> cube = {{
|
||||
{1, 1, 1}, {-1, 1, 1}, {-1,-1, 1},
|
||||
{-1,-1, 1}, {1,-1, 1}, {1, 1, 1},
|
||||
|
||||
{1, 1, 1}, {1,-1, 1}, {1,-1,-1},
|
||||
{1,-1,-1}, {1, 1,-1}, {1, 1, 1},
|
||||
|
||||
{1, 1, 1}, {1, 1,-1}, {-1, 1,-1},
|
||||
{-1, 1,-1}, {-1, 1, 1}, {1, 1, 1},
|
||||
|
||||
{-1, 1, 1}, {-1, 1,-1}, {-1,-1,-1},
|
||||
{-1,-1,-1}, {-1,-1, 1}, {-1, 1, 1},
|
||||
|
||||
{-1,-1,-1}, {1,-1,-1}, {1,-1, 1},
|
||||
{1,-1, 1}, {-1,-1, 1}, {-1,-1,-1},
|
||||
|
||||
{1,-1,-1}, {-1,-1,-1}, {-1, 1,-1},
|
||||
{-1, 1,-1}, {1, 1,-1}, {1,-1,-1}
|
||||
}};
|
||||
// GLfloat cube_colors[36][3] = {
|
||||
// {1, 0, 0}, {1, 0, 0}, {1, 0, 0},
|
||||
// {1, 0, 0}, {1, 0, 0}, {1, 0, 0},
|
||||
|
||||
// {0, 1, 0}, {0, 1, 0}, {0, 1, 0},
|
||||
// {0, 1, 0}, {0, 1, 0}, {0, 1, 0},
|
||||
|
||||
// {0, 0, 1}, {0, 0, 1}, {0, 0, 1},
|
||||
// {0, 0, 1}, {0, 0, 1}, {0, 0, 1},
|
||||
|
||||
// {1, 1, 0}, {1, 1, 0}, {1, 1, 0},
|
||||
// {1, 1, 0}, {1, 1, 0}, {1, 1, 0},
|
||||
|
||||
// {0, 1, 1}, {0, 1, 1}, {0, 1, 1},
|
||||
// {0, 1, 1}, {0, 1, 1}, {0, 1, 1},
|
||||
|
||||
// {1, 0, 1}, {1, 0, 1}, {1, 0, 1},
|
||||
// {1, 0, 1}, {1, 0, 1}, {1, 0, 1}
|
||||
// };
|
||||
|
||||
std::array<glm::vec3, 6> background_vertices = {
|
||||
{
|
||||
{-1, 1, 0}, {1, 1, 0}, {-1, -1, 0},
|
||||
{1, 1, 0}, {1, -1, 0}, {-1, -1, 0}
|
||||
}};
|
||||
GLfloat background_colors[40][3] = {
|
||||
{.2, .6, .8}, {.2, .6, .8}, {1, 1, 0},
|
||||
{.2, .6, .8}, {1, 1, 0}, {1, 1, 0}
|
||||
};
|
||||
GLuint background_colors_buffer;
|
||||
glGenBuffers(1, &background_colors_buffer);
|
||||
|
||||
std::array<glm::vec3, 6> framerate_indicator_vertices = {
|
||||
{
|
||||
{.9, 1, 0}, {1, 1, 0}, {.9, .9, 0},
|
||||
{1, 1, 0}, {1, .9, 0}, {.9, .9, 0}
|
||||
}};
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
|
||||
std::vector<glm::vec3> vertices;
|
||||
vertices.reserve(cube.size() + background_vertices.size() + framerate_indicator_vertices.size());
|
||||
vertices.insert(vertices.begin(), cube.begin(), cube.end());
|
||||
vertices.insert(vertices.end(), background_vertices.begin(), background_vertices.end());
|
||||
vertices.insert(vertices.end(), framerate_indicator_vertices.begin(),
|
||||
framerate_indicator_vertices.end());
|
||||
|
||||
glm::mat4 projection = glm::perspective(glm::radians(45.0f),
|
||||
(float) sw / (float) sh, 0.1f, 100.0f);
|
||||
glm::mat4 view = glm::lookAt(
|
||||
glm::vec3(4, 3, 3),
|
||||
glm::vec3(0, 0, 0),
|
||||
glm::vec3(0, 1, 0));
|
||||
glm::mat4 model = glm::mat4(1.0f);
|
||||
glm::mat4 mvp;
|
||||
|
||||
SDL_Surface *surface = rotateSurface90Degrees(
|
||||
IMG_Load("tile.png"), 2);
|
||||
printf("bytes per pixel %i\n", surface->format->BytesPerPixel);
|
||||
GLuint t_id = get_gl_texture_from_surface(surface, GL_LINEAR);
|
||||
|
||||
std::array<glm::vec2, 6> framerate_indicator_uv = {
|
||||
{
|
||||
{0, 1}, {1, 1}, {0, 0},
|
||||
{1, 1}, {1, 0}, {0, 0}
|
||||
}};
|
||||
std::array<glm::vec2, 36> cube_uv = {
|
||||
{
|
||||
{1, 1}, {0, 1}, {0, 0},
|
||||
{0, 0}, {1, 0}, {1, 1},
|
||||
{0, 1}, {0, 0}, {1, 0},
|
||||
{1, 0}, {1, 1}, {0, 1},
|
||||
{0, 1}, {0, 0}, {1, 0},
|
||||
{1, 0}, {1, 1}, {0, 1},
|
||||
{1, 1}, {0, 1}, {0, 0},
|
||||
{0, 0}, {1, 0}, {1, 1},
|
||||
{0, 0}, {1, 0}, {1, 1},
|
||||
{1, 1}, {0, 1}, {0, 0},
|
||||
{0, 0}, {1, 0}, {1, 1},
|
||||
{1, 1}, {0, 1}, {0, 0}
|
||||
}};
|
||||
std::vector<glm::vec2> uv;
|
||||
uv.reserve(cube_uv.size() + background_vertices.size() + framerate_indicator_uv.size());
|
||||
std::copy(cube_uv.begin(), cube_uv.end(), uv.begin());
|
||||
std::copy(framerate_indicator_uv.begin(), framerate_indicator_uv.end(),
|
||||
uv.begin() + cube_uv.size() + background_vertices.size());
|
||||
// uv.insert(uv.end(), cube_uv.begin(), cube_uv.end());
|
||||
// uv.insert(uv.end(), framerate_indicator_uv.begin(),
|
||||
// framerate_indicator_uv.end());
|
||||
GLuint uvbuffer;
|
||||
glGenBuffers(1, &uvbuffer);
|
||||
|
||||
GLuint fake_texture_id;
|
||||
unsigned char fake_texture_color[4] = {255, 255, 255, 255};
|
||||
glCreateTextures(GL_TEXTURE_2D, 1, &fake_texture_id);
|
||||
glBindTexture(GL_TEXTURE_2D, fake_texture_id);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
|
||||
fake_texture_color);
|
||||
|
||||
// Allocate and assign a Vertex Array Object to our handle
|
||||
glGenVertexArrays(1, &vao);
|
||||
|
||||
// Bind our Vertex Array Object as the current used object
|
||||
glBindVertexArray(vao);
|
||||
|
||||
// Allocate and assign one Vertex Buffer Objects to our handle
|
||||
glGenBuffers(1, &vbo);
|
||||
|
||||
// Bind vbo as being the active buffer and storing vertex coordinates
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||
|
||||
// Copy vertex data into buffer
|
||||
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLfloat) * 3, &vertices.front(),
|
||||
GL_STATIC_DRAW);
|
||||
|
||||
// Specify that our coordinate data is going into attribute
|
||||
// index 0, and contains three floats per vertex
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
||||
|
||||
// Enable attribute index 0 as being used
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
// Bind our second VBO as being the active buffer and storing texture coordinates
|
||||
glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
|
||||
|
||||
// Copy the data from cube_uv to our buffer
|
||||
glBufferData(GL_ARRAY_BUFFER, uv.capacity() * sizeof(GLfloat) * 2, &uv.front(), GL_STATIC_DRAW);
|
||||
|
||||
// Specify that our texture coordinate data is going into attribute index 1
|
||||
// and contains two floats per vertex
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
|
||||
|
||||
// Enable attribute index 1
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
glEnableVertexAttribArray(2);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, background_colors_buffer);
|
||||
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLfloat) * 3, 0, GL_STATIC_DRAW);
|
||||
glBufferSubData(GL_ARRAY_BUFFER,
|
||||
(cube.size()) * sizeof(GLfloat) * 3,
|
||||
background_vertices.size() * sizeof(GLfloat) * 3, background_colors);
|
||||
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
||||
|
||||
GLuint vertex_shader = load_shader("triangle.vert", GL_VERTEX_SHADER);
|
||||
GLuint fragment_shader = load_shader("triangle.frag", GL_FRAGMENT_SHADER);
|
||||
GLuint flat_shader = load_shader("flat.vert", GL_VERTEX_SHADER);
|
||||
|
||||
GLuint world_program = glCreateProgram();
|
||||
glAttachShader(world_program, vertex_shader);
|
||||
glAttachShader(world_program, fragment_shader);
|
||||
glBindAttribLocation(world_program, 0, "in_Position");
|
||||
glBindAttribLocation(world_program, 1, "vertexUV");
|
||||
glBindAttribLocation(world_program, 2, "in_Color");
|
||||
link_shader(world_program);
|
||||
|
||||
GLuint flat_program = glCreateProgram();
|
||||
glAttachShader(flat_program, flat_shader);
|
||||
glAttachShader(flat_program, fragment_shader);
|
||||
glBindAttribLocation(flat_program, 0, "in_Position");
|
||||
glBindAttribLocation(flat_program, 1, "vertexUV");
|
||||
glBindAttribLocation(flat_program, 2, "in_Color");
|
||||
link_shader(flat_program);
|
||||
|
||||
GLuint m_id = glGetUniformLocation(world_program, "MVP");
|
||||
GLuint t_uniform_id = glGetUniformLocation(world_program, "myTextureSampler");
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, t_id);
|
||||
glUniform1i(t_uniform_id, 0);
|
||||
|
||||
glDepthFunc(GL_LESS);
|
||||
|
||||
int framerate = 60;
|
||||
float frame_length = 1000.0 / framerate;
|
||||
int last_frame_timestamp, frame_count_timestamp, last_capture_timestamp;
|
||||
last_frame_timestamp = frame_count_timestamp = last_capture_timestamp = SDL_GetTicks();
|
||||
bool is_recording = false, show_framerate = false;
|
||||
std::list<SDL_Surface*> frames;
|
||||
int recording_capture_framerate = 100, frame_time_overflow = 0, capture_time_overflow = 0,
|
||||
frame_count = 0, ticks;
|
||||
|
||||
GLuint framerate_texture_id = get_gl_texture_from_surface(
|
||||
get_framerate_indicator_surface(frame_count), GL_LINEAR);
|
||||
|
||||
while (!done)
|
||||
{
|
||||
ticks = SDL_GetTicks();
|
||||
if (ticks - last_frame_timestamp + frame_time_overflow >= frame_length)
|
||||
{
|
||||
frame_count++;
|
||||
frame_time_overflow = ticks - last_frame_timestamp +
|
||||
frame_time_overflow - frame_length;
|
||||
last_frame_timestamp = ticks;
|
||||
for (int ii = 1; frame_time_overflow > frame_length;
|
||||
ii++, frame_time_overflow -= frame_length)
|
||||
{
|
||||
fprintf(stderr, "lost %i frame(s)\n", ii);
|
||||
}
|
||||
while (SDL_PollEvent(&event))
|
||||
{
|
||||
if (event.type == SDL_QUIT)
|
||||
{
|
||||
done = 1;
|
||||
}
|
||||
else if (event.type == SDL_KEYDOWN)
|
||||
{
|
||||
if (event.key.keysym.sym == SDLK_F9)
|
||||
{
|
||||
capture_screen(window);
|
||||
}
|
||||
else if (event.key.keysym.sym == SDLK_F10)
|
||||
{
|
||||
if (!is_recording)
|
||||
{
|
||||
start_recording(&is_recording);
|
||||
}
|
||||
else
|
||||
{
|
||||
end_recording(frames, &is_recording);
|
||||
}
|
||||
}
|
||||
else if (event.key.keysym.sym == SDLK_F11)
|
||||
{
|
||||
if (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN)
|
||||
{
|
||||
SDL_SetWindowFullscreen(window, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
|
||||
}
|
||||
}
|
||||
else if (event.key.keysym.sym == SDLK_f && SDL_GetModState() & KMOD_CTRL)
|
||||
{
|
||||
show_framerate = !show_framerate;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (is_recording && ticks - last_capture_timestamp + capture_time_overflow >
|
||||
recording_capture_framerate)
|
||||
{
|
||||
frames.push_back(get_screen_surface(window));
|
||||
printf("added frame at %i\n", ticks);
|
||||
capture_time_overflow = ticks - last_capture_timestamp + capture_time_overflow -
|
||||
recording_capture_framerate;
|
||||
last_capture_timestamp = ticks;
|
||||
for (int ii = 1; capture_time_overflow > recording_capture_framerate;
|
||||
ii++, capture_time_overflow -= recording_capture_framerate)
|
||||
{
|
||||
fprintf(stderr, "lost %i frame(s) during capture\n", ii);
|
||||
}
|
||||
}
|
||||
// glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
|
||||
// glBufferData(GL_ARRAY_BUFFER, 8 * sizeof(GLfloat), diamond, GL_STATIC_DRAW);
|
||||
// glClearColor((sin(r) + 1) / 2, (sin(r) + 1) / 2, .5, 1);
|
||||
glClearColor(.7, .7, .5, 1);
|
||||
r += .025;
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
glUseProgram(flat_program);
|
||||
glBindTexture(GL_TEXTURE_2D, fake_texture_id);
|
||||
glDisableVertexAttribArray(1);
|
||||
glEnableVertexAttribArray(2);
|
||||
glDrawArrays(GL_TRIANGLES, 36, 6);
|
||||
if (show_framerate)
|
||||
{
|
||||
glBindTexture(GL_TEXTURE_2D, framerate_texture_id);
|
||||
glDisableVertexAttribArray(2);
|
||||
glEnableVertexAttribArray(1);
|
||||
glVertexAttrib3f(2, 1, 1, 1);
|
||||
glDrawArrays(GL_TRIANGLES, 42, 6);
|
||||
}
|
||||
// printf("%s\n", glm::to_string(model).c_str());
|
||||
model = glm::rotate(model, .0005f * frame_length, glm::vec3(0.0f, 1.0f, 0.0f));
|
||||
mvp = projection * view * model;
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glUseProgram(world_program);
|
||||
glUniformMatrix4fv(m_id, 1, GL_FALSE, &mvp[0][0]);
|
||||
glBindTexture(GL_TEXTURE_2D, t_id);
|
||||
glEnableVertexAttribArray(1);
|
||||
glDisableVertexAttribArray(2);
|
||||
glVertexAttrib3f(2, 1, 1, 1);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||
//glFlush();
|
||||
//SDL_Rect rect = {x++, 0, 240, 160};
|
||||
//SDL_RenderCopy(renderer, texture, NULL, &rect);
|
||||
//SDL_RenderPresent(renderer);
|
||||
SDL_GL_SwapWindow(window);
|
||||
}
|
||||
if (ticks - frame_count_timestamp >= 1000)
|
||||
{
|
||||
frame_count_timestamp = ticks;
|
||||
if (show_framerate)
|
||||
{
|
||||
set_framerate_indicator(frame_count, framerate_texture_id);
|
||||
}
|
||||
frame_count = 0;
|
||||
}
|
||||
SDL_Delay(15);
|
||||
}
|
||||
SDL_GL_DeleteContext(glcontext);
|
||||
SDL_DestroyTexture(texture);
|
||||
}
|
||||
SDL_DestroyRenderer(renderer);
|
||||
}
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
}
|
||||
SDL_DestroyWindow(window);
|
||||
}
|
||||
}
|
||||
SDL_Quit();
|
||||
SDL_Quit();
|
||||
}
|
||||
};
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
new Game();
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue