Created
October 3, 2020 10:03
-
-
Save seungha-kim/a842ba79eca4108aaf3c609beeda1147 to your computer and use it in GitHub Desktop.
OpenGL ES 2.0 triangle on macOS using ANGLE
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
#import "MyOpenGLView.h" | |
#import <GLES2/gl2.h> | |
#import <GLES2/gl2ext.h> | |
#import <EGL/egl.h> | |
const GLchar vertexShaderSource[] = | |
"attribute vec3 aPosition;\n" | |
"void main() {\n" | |
" gl_Position = vec4(aPosition, 1.0);\n" | |
"}\n"; | |
const GLchar fragmentShaderSource[] = | |
"precision mediump float;\n" | |
"void main() {\n" | |
" gl_FragColor = vec4(1.0, 1.0, 0.0, 1.0);\n" | |
"}\n"; | |
GLuint getShader(GLenum type, const GLchar* shaderSource) | |
{ | |
GLuint shader; | |
GLint compiled; | |
shader = glCreateShader(type); | |
if (shader == 0) { | |
NSLog(@"cannot create shader"); | |
return 0; | |
} | |
glShaderSource(shader, 1, &shaderSource, NULL); | |
glCompileShader(shader); | |
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled); | |
if (!compiled) { | |
NSLog(@"cannot compile shader"); | |
return 0; | |
} | |
return shader; | |
} | |
GLuint createProgram() | |
{ | |
GLuint program = glCreateProgram(); | |
GLuint vertexShader = getShader(GL_VERTEX_SHADER, vertexShaderSource); | |
GLuint fragmentShader = getShader(GL_FRAGMENT_SHADER, fragmentShaderSource); | |
glAttachShader(program, vertexShader); | |
glAttachShader(program, fragmentShader); | |
glLinkProgram(program); | |
return program; | |
} | |
@implementation MyOpenGLView | |
- (void) drawRect: (NSRect) bounds | |
{ | |
NSLog(@"draw rect"); | |
// ********************** | |
// *** Initialize *** | |
// ********************** | |
EGLint majorVersion; | |
EGLint minorVersion; | |
EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY); | |
if (display == EGL_NO_DISPLAY) { | |
NSLog(@"no display"); | |
return; | |
} | |
if (!eglInitialize(display, &majorVersion, &minorVersion)) { | |
NSLog(@"can't initialize"); | |
return; | |
} | |
EGLNativeWindowType* nativeWindow = (__bridge EGLNativeWindowType)(self.layer); | |
// ********************** | |
// *** Choose config *** | |
// ********************** | |
EGLint configAttribList[] = { | |
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, | |
EGL_RED_SIZE, 5, | |
EGL_GREEN_SIZE, 6, | |
EGL_BLUE_SIZE, 5, | |
EGL_DEPTH_SIZE, 1, | |
EGL_NONE | |
}; | |
EGLConfig configs[10]; | |
EGLint numConfigs; | |
if (!eglChooseConfig(display, configAttribList, configs, 10, &numConfigs)) { | |
NSLog(@"no config"); | |
return; | |
} else { | |
NSLog(@"yes config! %d", numConfigs); | |
} | |
EGLConfig config = configs[0]; | |
// ********************** | |
// *** Create surface *** | |
// ********************** | |
EGLSurface window = eglCreateWindowSurface(display, config, nativeWindow, NULL); | |
if (window == EGL_NO_SURFACE) { | |
NSLog(@"no window"); | |
return; | |
} else { | |
NSLog(@"yes surface!"); | |
} | |
// ********************** | |
// *** Create context *** | |
// ********************** | |
const EGLint contextAttribList[] = { | |
EGL_CONTEXT_CLIENT_VERSION, 2, | |
EGL_NONE | |
}; | |
EGLContext context = eglCreateContext(display, config, EGL_NO_CONTEXT, contextAttribList); | |
if (context == EGL_NO_CONTEXT) { | |
NSLog(@"no context"); | |
return; | |
} else { | |
NSLog(@"yes context!"); | |
} | |
// ********************** | |
// *** Make current *** | |
// ********************** | |
if (!eglMakeCurrent(display, window, window, context)) { | |
NSLog(@"can't make current"); | |
return; | |
} | |
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); | |
// ********************** | |
// *** Draw triangle *** | |
// ********************** | |
GLuint program = createProgram(); | |
GLfloat vVertices[] = {0.0f, 0.5f, 0.0f, | |
-0.5f, -0.5f, 0.0f, | |
0.5f, -0.5f, 0.0f}; | |
glViewport(0, 0, self.bounds.size.width, self.bounds.size.height); | |
glClear(GL_COLOR_BUFFER_BIT); | |
glUseProgram(program); | |
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, vVertices); | |
glUniform3f(glGetUniformLocation(program, "uColor"), 1.0, 0.0, 0.0); | |
glEnableVertexAttribArray(0); | |
glDrawArrays(GL_TRIANGLES, 0, 3); | |
eglSwapBuffers(display, window); | |
NSLog(@"draw rect end"); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment