Skip to content

Instantly share code, notes, and snippets.

@Einlander
Last active December 7, 2015 06:13
Show Gist options
  • Select an option

  • Save Einlander/b9a74690d43aa8b6fcc7 to your computer and use it in GitHub Desktop.

Select an option

Save Einlander/b9a74690d43aa8b6fcc7 to your computer and use it in GitHub Desktop.
test opengl
//https://en.wikibooks.org/wiki/OpenGL_Programming/Modern_OpenGL_Introduction
//https://gitlab.com/wikibooks-opengl/modern-tutorials/blob/master/tut01_intro/triangle.cpp
import "ecere"
#include "gl_core_3_3.h"
GLuint program;
GLint attribute_coord2d;
GLfloat triangle_vertices[] = {
0.0f, 0.8f,
-0.8f, -0.8f,
0.8f, -0.8f
};
bool init_resources(void)
{
// Vertex shader
const char *vs_source=
"#version 120\n"
"attribute vec2 coord2d;"
"void main(void) {"
" gl_Position = vec4(coord2d, 0.0, 1.0);"
"}";
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
// Fragment Shader
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
const char *fs_source =
"#version 120\n"
"void main(void) {"
" gl_FragColor[0] = 0.0;"
" gl_FragColor[1] = 0.0;"
" gl_FragColor[2] = 1.0;"
"}";
// attributes
const char* attribute_name = "coord2d";
//
GLint compile_ok = GL_FALSE;
GLint link_ok = GL_FALSE;
glShaderSource(vs, 1, &vs_source, null);
glCompileShader(vs);
glGetShaderiv(vs, GL_COMPILE_STATUS, &compile_ok);
if (!compile_ok) {
PrintLn("Error in vertex shader");
return false;
}
glShaderSource(fs, 1, &fs_source, null);
glCompileShader(fs);
glGetShaderiv(fs, GL_COMPILE_STATUS, &compile_ok);
if(!compile_ok){
PrintLn("Error in fragment shader");
return 0;
}
program = glCreateProgram();
glAttachShader(program, vs);
glAttachShader(program, fs);
glLinkProgram(program);
glGetProgramiv(program, GL_LINK_STATUS, &link_ok);
if (!link_ok) {
PrintLn("glLinkProgram:");
return 0;
}
attribute_coord2d = glGetAttribLocation(program, attribute_name);
if (attribute_coord2d == -1) {
PrintLn("Could not bind attribute %s\n", attribute_name);
return 0;
}
PrintLn("init_complete");
return true;
}
void free_resources()
{
glDeleteProgram(program);
}
class Example1 : Window
{
caption = $"OpenGL Example Triangle";
background = formColor;
borderStyle = sizable;
hasMaximize = true;
hasMinimize = true;
hasClose = true;
clientSize = { 632, 438 };
displayDriver = "OpenGL";
bool OnLoadGraphics()
{
ogl_LoadFunctions();
if (!init_resources())
{
return false;
}
return true;
}
void OnRedraw(Surface surface)
{
GLint prevProgram;
glGetIntegerv(GL_CURRENT_PROGRAM, (GLint *)&prevProgram);
GLfloat triangle_vertices[] = {
0.0f, 0.8f,
-0.8f, -0.8f,
0.8f, -0.8f
};
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(program);
glEnableVertexAttribArray(attribute_coord2d);
/* Describe our vertices array to OpenGL (it can't guess its format automatically) */
glVertexAttribPointer(
attribute_coord2d, // attribute
2, // number of elements per vertex, here (x,y)
GL_FLOAT, // the type of each element
GL_FALSE, // take our values as-is
0, // no extra data between each position
triangle_vertices // pointer to the C array
);
/* Push each element in buffer_vertices to the vertex shader */
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(attribute_coord2d);
// Restore normal Ecere program
glUseProgram( prevProgram );
//glutSwapBuffers();
}
bool OnClose(bool parentClosing)
{
free_resources();
return true;
}
}
Example1 example1 {};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment