-
Notifications
You must be signed in to change notification settings - Fork 18
/
example_beginend_shader_1.cpp
140 lines (108 loc) · 3.02 KB
/
example_beginend_shader_1.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <stdafx.h>
// OpenGL
#include <GL/glew.h>
#include <GL/gl.h>
#include <GL/glu.h>
// glm
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
// glfw
#include <GLFW/glfw3.h>
// stl
#include <vector>
#include <stdexcept>
#include <array>
#include <map>
#include <tuple>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#define _USE_MATH_DEFINES
#include <math.h>
// project includes
#include <gl/gl_debug.h>
#include <gl/gl_shader.h>
// preprocessor definitions
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
std::string sh_vert = R"(
#version 120
varying vec2 texcoord;
void main(void)
{
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
texcoord = gl_MultiTexCoord0.xy;
}
)";
std::string sh_frag = R"(
#version 120
varying vec2 texcoord;
uniform sampler2D texture;
void main(void)
{
vec4 texcolor = texture2D(texture, texcoord);
gl_FragColor = texcolor.gbra;
}
)";
int main(void)
{
if ( glfwInit() == 0 )
throw std::runtime_error( "error initializing glfw" );
GLFWwindow *wnd = glfwCreateWindow( 800, 600, "OGL window", nullptr, nullptr );
if ( wnd == nullptr )
{
glfwTerminate();
throw std::runtime_error( "error initializing window" );
}
glfwMakeContextCurrent(wnd);
if ( glewInit() != GLEW_OK )
throw std::runtime_error( "error initializing glew" );
OpenGL::CContext::TDebugLevel debug_level = OpenGL::CContext::TDebugLevel::all;
OpenGL::CContext context;
context.Init( debug_level );
OpenGL::ShaderProgramSimple prog(
{
{ sh_vert, GL_VERTEX_SHADER },
{ sh_frag, GL_FRAGMENT_SHADER }
} );
GLint tex_loc = glGetUniformLocation( prog.Prog(), "texture" );
prog.Use();
glUniform1i(tex_loc, 0);
GLuint tex_obj;
glGenTextures( 1, &tex_obj );
unsigned char tex_data[] = { 255, 0, 0, 255, 0, 255, 0, 255, 0, 0, 255, 255, 255, 255, 0, 255 };
glBindTexture( GL_TEXTURE_2D, tex_obj );
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex_data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
//glEnable( GL_TEXTURE_2D );
while (!glfwWindowShouldClose(wnd))
{
glBegin( GL_QUADS );
glTexCoord2d( 0.0, 0.0 );
glVertex2d( -0.5, -0.5 );
glTexCoord2d( 0.0, 1.0 );
glVertex2d( -0.5, 0.5 );
glTexCoord2d( 1.0, 1.0 );
glVertex2d( 0.5, 0.5 );
glTexCoord2d( 1.0, 0.0 );
glVertex2d( 0.5, -0.5 );
glEnd();
glfwSwapBuffers(wnd);
glfwPollEvents();
}
glfwDestroyWindow( wnd );
wnd = nullptr;
glfwTerminate();
return 0;
}