-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
115 lines (89 loc) · 2.33 KB
/
main.cpp
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <OpenGL/gl3.h>
#include <OpenGL/gl3ext.h>
#include <SDL.h>
#include <iostream>
#include <cstdlib>
#include "GLVis.h"
#include "GLUtils.h"
void init();
enum VAO_IDs { Triangles, NumVAOs };
enum VBO_IDs { ArrayBuffer, NumVBOs };
enum Attrib_IDs { vPosition = 0 };
GLuint VAOs[NumVAOs];
GLuint VBOs[NumVBOs];
const GLuint NumVertices = 6;
int GLVis_main()
{
for(float f = 0.0f; f <= 1.0f; f += 0.1)
{
std::cout << f << std::endl;
}
if( SDL_Init(SDL_INIT_VIDEO) )
{
std::cerr << "SDL_Init Error: "
<< SDL_GetError() << std::endl;
return 1;
}
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_Window* window =
SDL_CreateWindow( "Hello World!", 100, 100, 640, 480, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN );
if( window == nullptr ) {
std::cout << "SDL_CreateWindow Error: "
<< SDL_GetError() << std::endl;
return 1;
}
SDL_GLContext glContext = SDL_GL_CreateContext(window);
if( glContext == nullptr ) {
std::cout << "SDL_GL_CreateContext Error: "
<< SDL_GetError() << std::endl;
return 1;
}
init();
bool bQuit = false;
while( !bQuit )
{
SDL_Event e;
while( SDL_PollEvent(&e) )
{
switch(e.type)
{
case SDL_QUIT: { bQuit = true; break; }
}
}
glClearColor(1.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glBindVertexArray(VAOs[Triangles]);
glDrawArrays(GL_TRIANGLES, 0, NumVertices);
SDL_GL_SwapWindow(window);
}
SDL_DestroyWindow(window);
atexit( SDL_Quit );
return 0;
}
void init()
{
glGenVertexArrays(NumVAOs, VAOs);
glBindVertexArray(VAOs[Triangles]);
GLfloat vertices[NumVertices][2] = {
{ -0.90, -0.90 }, // Triangle 1
{ 0.85, -0.90 },
{ -0.90, 0.85 },
{ 0.90, -0.85 }, // Triangle 2
{ 0.90, 0.90 },
{ -0.85, 0.90 }
};
glGenBuffers(NumVBOs, VBOs);
glBindBuffer(GL_ARRAY_BUFFER, VBOs[ArrayBuffer]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
ShaderInfo shaders[] = {
{ GL_VERTEX_SHADER, "triangles.vert" },
{ GL_FRAGMENT_SHADER, "triangles.frag" },
{ GL_NONE, nullptr }
};
GLuint program = LoadShaders(shaders);
glUseProgram(program);
glVertexAttribPointer(vPosition, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glEnableVertexAttribArray(vPosition);
}