Created
December 9, 2015 10:03
-
-
Save Einlander/6197db0329df2fdb171b to your computer and use it in GitHub Desktop.
Object Oriented Version
This file contains hidden or 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
| //https://en.wikibooks.org/wiki/OpenGL_Programming/Modern_OpenGL_Introduction | |
| /* | |
| Object Oriented Version | |
| By: Einlander | |
| */ | |
| import "ecere" | |
| #include "gl_core_3_3.h" | |
| class Example1_OO : Window | |
| { | |
| // shader programs | |
| GLuint program; | |
| // shader attribs and uniforms | |
| GLint attribute_coord2d; | |
| //3d objects | |
| Array<float> triangle_vertices {[ // EC-LANG !!! You MUST use float because it will not take GLfloat, it will give this error: | |
| 0.0f, 0.8f, // EC-LANG !!! triangle00.ec: In function '__ecereConstructor_Example1_OO': | |
| -0.8f, -0.8f, // EC-LANG !!! triangle00.ec:15:6: error: '__ecereClass_GLfloat' undeclared (first use in this function) | |
| 0.8f, -0.8f // EC-LANG !!! triangle00.ec:15:6: note: each undeclared identifier is reported only once for each function it appears in | |
| ]}; | |
| caption = $"OpenGL Example Triangle Object Oriented Version"; | |
| background = formColor; | |
| borderStyle = sizable; | |
| hasMaximize = true; | |
| hasMinimize = true; | |
| hasClose = true; | |
| clientSize = { 632, 438 }; | |
| displayDriver = "OpenGL"; | |
| 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; | |
| } | |
| bool OnLoadGraphics() | |
| { | |
| ogl_LoadFunctions(); | |
| if (!init_resources()) | |
| { | |
| return false; | |
| } | |
| return true; | |
| } | |
| void OnRedraw(Surface surface) | |
| { | |
| GLint prevProgram; | |
| //float triangle_vertices1[] // EC-LANG !!! Don't need this because it is already initialized | |
| glGetIntegerv(GL_CURRENT_PROGRAM, (GLint *)&prevProgram); | |
| glDisable(GL_BLEND); | |
| glClearColor(0.0, 0.0, 0.0, 0.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.array // pointer to the C array | |
| // EC-LANG !!! You can not simply pass the variable as in the example, you actually need to pass a POINTER !! | |
| ); | |
| /* 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) | |
| { | |
| return true; | |
| } | |
| void OnUnloadGraphics() | |
| { | |
| free_resources(); | |
| } | |
| void free_resources() | |
| { | |
| glDeleteProgram(program); | |
| } | |
| } | |
| Example1_OO exampls1_oo {}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment