94 lines
2.2 KiB
C
94 lines
2.2 KiB
C
#include <stdio.h>
|
|
#include <GL/glew.h>
|
|
#define GLFW_DLL
|
|
#include <GLFW/glfw3.h>
|
|
|
|
#define eprint(fmt, ...) fprintf(stderr, "ERROR: " fmt "\n" #__VA_ARGS__)
|
|
|
|
const char *vertex_shader =
|
|
"#version 330 core\n"
|
|
"in vec3 vp;\n"
|
|
"void main() {\n"
|
|
" gl_Position = vec4(vp, 1.0);\n"
|
|
"}\n";
|
|
|
|
const char *fragment_shader =
|
|
"#version 330 core\n"
|
|
"out vec4 fragColor;\n"
|
|
"void main() {\n"
|
|
" fragColor = vec4(0.5, 0.0, 0.5, 1.0);\n"
|
|
"}\n";
|
|
|
|
int main() {
|
|
if (!glfwInit()) {
|
|
eprint("Could not start glfw");
|
|
return -1;
|
|
}
|
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
|
|
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
|
|
|
GLFWwindow *window = glfwCreateWindow(640, 480, "Hello triangle", NULL, NULL);
|
|
if (!window) {
|
|
eprint("Could not open window with glfw");
|
|
glfwTerminate();
|
|
return -1;
|
|
}
|
|
glfwMakeContextCurrent(window);
|
|
|
|
glewExperimental = GL_TRUE;
|
|
glewInit();
|
|
|
|
printf("Renderer: %s\n", glGetString(GL_RENDERER));
|
|
printf("OpenGL version supported: %s\n", glGetString(GL_VERSION));
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
glDepthFunc(GL_LESS);
|
|
|
|
float points[] = {
|
|
0.0f, 0.5f, 0.0f,
|
|
0.5f, -0.5f, 0.0f,
|
|
-0.5f, -0.5f, 0.0f,
|
|
};
|
|
|
|
GLuint vbo = 0;
|
|
glGenBuffers(1, &vbo);
|
|
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
|
glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(float), points, GL_STATIC_DRAW);
|
|
|
|
GLuint vao = 0;
|
|
glGenVertexArrays(1, &vao);
|
|
glBindVertexArray(vao);
|
|
glEnableVertexAttribArray(0);
|
|
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
|
|
|
|
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
|
|
glShaderSource(vs, 1, &vertex_shader, NULL);
|
|
glCompileShader(vs);
|
|
|
|
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
|
|
glShaderSource(fs, 1, &fragment_shader, NULL);
|
|
glCompileShader(fs);
|
|
|
|
GLuint shader_program = glCreateProgram();
|
|
glAttachShader(shader_program, fs);
|
|
glAttachShader(shader_program, vs);
|
|
glLinkProgram(shader_program);
|
|
|
|
while (!glfwWindowShouldClose(window)) {
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
glUseProgram(shader_program);
|
|
glBindVertexArray(vao);
|
|
glDrawArrays(GL_TRIANGLES, 0, 3);
|
|
glfwPollEvents();
|
|
glfwSwapBuffers(window);
|
|
}
|
|
|
|
|
|
glfwTerminate();
|
|
return 0;
|
|
}
|