#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdarg.h>
#include <stdbool.h>
#include <assert.h>
static const GLint WIDTH = 800, HEIGHT = 600;
static const char OPENGL_LOG_FILE[11] = "opengl.log";
bool start_opengl_log_file() {
FILE* file = fopen(OPENGL_LOG_FILE, "w");
if (!file) {
fprintf(stderr, "ERROR: could not open: %s\n", OPENGL_LOG_FILE);
return false;
}
time_t now = time(NULL);
char* date = ctime(&now);
fprintf(file, "time: %s\n", date);
fclose(file);
return true;
}
bool write_to_opengl_log(const char* message, ...) {
va_list valist;
FILE* file = fopen(OPENGL_LOG_FILE, "a");
if (!file) {
fprintf(stderr, "ERROR: could not open: %s\n", OPENGL_LOG_FILE);
return false;
}
va_start(valist, message);
vfprintf(file, message, valist);
va_end(valist);
fclose(file);
return true;
}
bool write_error_to_opengl_log_file(const char* message, ...) {
va_list valist;
FILE* file = fopen(OPENGL_LOG_FILE, "a");
if (!file) {
fprintf(stderr, "ERROR: could not open: %s\n", OPENGL_LOG_FILE);
return false;
}
va_start(valist, message);
vfprintf(file, message, valist);
va_end(valist);
va_start(valist, message);
vfprintf(stderr, message, valist);
va_end(valist);
fclose(file);
return true;
}
void write_opengl_parameters_to_opengl_log_file() {
GLenum params[] = {
GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS,
GL_MAX_CUBE_MAP_TEXTURE_SIZE,
GL_MAX_DRAW_BUFFERS,
GL_MAX_FRAGMENT_UNIFORM_COMPONENTS,
GL_MAX_TEXTURE_IMAGE_UNITS,
GL_MAX_TEXTURE_SIZE,
GL_MAX_VARYING_FLOATS,
GL_MAX_VERTEX_ATTRIBS,
GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS,
GL_MAX_VERTEX_UNIFORM_COMPONENTS,
GL_MAX_VIEWPORT_DIMS,
GL_STEREO,
};
const char* names[] = {
"GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS",
"GL_MAX_CUBE_MAP_TEXTURE_SIZE",
"GL_MAX_DRAW_BUFFERS",
"GL_MAX_FRAGMENT_UNIFORM_COMPONENTS",
"GL_MAX_TEXTURE_IMAGE_UNITS",
"GL_MAX_TEXTURE_SIZE",
"GL_MAX_VARYING_FLOATS",
"GL_MAX_VERTEX_ATTRIBS",
"GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS",
"GL_MAX_VERTEX_UNIFORM_COMPONENTS",
"GL_MAX_VIEWPORT_DIMS",
"GL_STEREO",
};
write_to_opengl_log("OpenGL Context Parameters:\n");
for (int i = 0; i < 10; i++) {
int val = 0;
glGetIntegerv(params[i], &val);
write_to_opengl_log("%s %i\n", names[i], val);
}
int valarr[2];
valarr[0] = valarr[1] = 0;
glGetIntegerv(params[10], valarr);
write_to_opengl_log("%s %i %i\n", names[10], valarr[0], valarr[1]);
unsigned char val = 0;
glGetBooleanv(params[11], &val);
write_to_opengl_log("%s %u\n", names[11], (unsigned int)val);
}
void glfw_error_callback(int error, const char* description) {
write_error_to_opengl_log_file("GLFW ERROR: code %i msg: %s\n", error, description);
}
int main(void)
{
assert(start_opengl_log_file());
write_to_opengl_log("GLFW version: %s\n\n", glfwGetVersionString());
glfwSetErrorCallback(glfw_error_callback);
if (!glfwInit()) {
fprintf(stderr, "glfwInit() Error\n");
return 1;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_SAMPLES, 4);
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "window title", NULL, NULL);
if (!window) {
printf("glfwCreateWindow() Error");
glfwTerminate();
return 1;
}
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK) {
printf("glewInit() Error");
glfwDestroyWindow(window);
glfwTerminate();
}
write_opengl_parameters_to_opengl_log_file();
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
glfwSetWindowShouldClose(window, 1);
}
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
/*
run:
time: Mon Mar 21 18:27:14 2022
GLFW version: 3.3.6 Win32 WGL EGL OSMesa VisualC
OpenGL Context Parameters:
GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS 192
GL_MAX_CUBE_MAP_TEXTURE_SIZE 32768
GL_MAX_DRAW_BUFFERS 8
GL_MAX_FRAGMENT_UNIFORM_COMPONENTS 4096
GL_MAX_TEXTURE_IMAGE_UNITS 32
GL_MAX_TEXTURE_SIZE 32768
GL_MAX_VARYING_FLOATS 0
GL_MAX_VERTEX_ATTRIBS 16
GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS 32
GL_MAX_VERTEX_UNIFORM_COMPONENTS 4096
GL_MAX_VIEWPORT_DIMS 32768 32768
GL_STEREO 0
*/