forked from andy-thomason/gles2-triangle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtriangle.cpp
More file actions
89 lines (75 loc) · 2.2 KB
/
Copy pathtriangle.cpp
File metadata and controls
89 lines (75 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// complete OpenGL triangle example.
#ifdef WIN32
// This is the "GL extension wrangler"
// Note: you will need to use the right version of glew32s.lib (32 bit vs 64 bit)
#define GLEW_STATIC
#include "GL/glew.h"
#pragma comment(lib, "glew32s.lib")
// This is the "GL utilities" framework for drawing with OpenGL
#define FREEGLUT_STATIC
//#define FREEGLUT_LIB_PRAGMAS 0
#include "GL/glut.h"
#else // (osx?)
#include "GLUT/glut.h"
#endif
// draw a triangle once!
class triangle
{
public:
static void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// corners of the triangle
static const float vertices[] = {
0.5f, 0.5f, // 2, GL_FLOAT (ie. vec2)
-0.5f, 0.5f,
0.5f, -0.5f
};
// set the attributes (assume pos=0)
glVertexAttribPointer(
0, 2, GL_FLOAT, GL_FALSE,
2*sizeof(float), (GLvoid*)vertices
);
glEnableVertexAttribArray(0);
// draw the triangle
glDrawArrays(GL_TRIANGLES, 0, 3);
glutSwapBuffers();
}
static void reshape(int w, int h)
{
glViewport(0, 0, w, h);
}
};
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutCreateWindow("triangle");
#ifdef WIN32
// On windows, we need to do this to get modern OpenGL. Thanks, Microsoft.
glewInit();
if (!glewIsSupported("GL_VERSION_2_0") )
{
return 1;
}
#endif
// vertex shader copies pos to glPosition
const char *vs = "attribute vec2 pos; void main() { gl_Position = vec4(pos, 0, 1); }";
GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader, 1, &vs, nullptr);
glCompileShader(vertex_shader);
// fragment shader draws in red
const char *fs = "void main() { gl_FragColor = vec4(1, 0, 0, 1); }";
GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_shader, 1, &fs, nullptr);
glCompileShader(fragment_shader);
// combine fragment and vertex shader
GLuint program = glCreateProgram();
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
glLinkProgram(program);
glUseProgram(program);
glutReshapeFunc(triangle::reshape);
glutDisplayFunc(triangle::display);
glutMainLoop();
return 0;
}