Created
July 6, 2023 20:23
-
-
Save fabjan/aa03df1fddd7369f33ed44e1f6f9139f to your computer and use it in GitHub Desktop.
Debugging
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// clang gltest.c -lglfw -lglew -framework OpenGL -o gltest | |
#include <GL/glew.h> | |
#include <GLFW/glfw3.h> | |
#include <stdio.h> | |
void glAssertNoError(const char *msg) { | |
int code = glGetError(); | |
if (code == GL_NO_ERROR) { | |
return; | |
} | |
fprintf(stderr, "OpenGL error: %d, %s\n", code, msg); | |
} | |
int main() { | |
// Initialize GLFW first | |
if (!glfwInit()) { | |
fprintf(stderr, "Failed to initialize GLFW\n"); | |
return -1; | |
} | |
// Then setup hints for OpenGL 4.1 | |
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); | |
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); | |
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); | |
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); | |
// Now we can create the window | |
GLFWwindow* window = glfwCreateWindow(800, 600, "OpenGL Program", NULL, NULL); | |
if (!window) { | |
fprintf(stderr, "Failed to create GLFW window\n"); | |
const char* description; | |
int code = glfwGetError(&description); | |
fprintf(stderr, "Error %d: %s\n", code, description); | |
glfwTerminate(); | |
return -1; | |
} | |
glfwMakeContextCurrent(window); | |
// and initialize GLEW | |
if (glewInit() != GLEW_OK) { | |
fprintf(stderr, "Failed to initialize GLEW\n"); | |
glfwTerminate(); | |
return -1; | |
} | |
fprintf(stderr, "OpenGL version: %s\n", glGetString(GL_VERSION)); | |
fprintf(stderr, "GLSL version: %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION)); | |
while (!glfwWindowShouldClose(window)) { | |
// white background | |
glClearColor(1.0, 1.0, 1.0, 1.0); | |
glAssertNoError("glClearColor"); | |
glClear(GL_COLOR_BUFFER_BIT); | |
glAssertNoError("glClear"); | |
// red triangle | |
glBegin(GL_TRIANGLES); | |
glAssertNoError("glBegin"); | |
glColor3f(1.0, 0.0, 0.0); | |
glAssertNoError("glColor3f"); | |
glVertex2f(-0.5, -0.5); | |
glAssertNoError("glVertex2f"); | |
glVertex2f(0.5, -0.5); | |
glAssertNoError("glVertex2f"); | |
glVertex2f(0.0, 0.5); | |
glAssertNoError("glVertex2f"); | |
glEnd(); | |
glAssertNoError("glEnd"); | |
glfwSwapBuffers(window); | |
glAssertNoError("glfwSwapBuffers"); | |
glfwPollEvents(); | |
glAssertNoError("glfwPollEvents"); | |
} | |
glfwTerminate(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment