-
Notifications
You must be signed in to change notification settings - Fork 4
/
glview.cpp
159 lines (132 loc) · 4.5 KB
/
glview.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "glview.h"
#include <glm/common.hpp>
#include <glm/matrix.hpp>
#include <glm/trigonometric.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <iostream>
GLView::GLView(int textureWidth, int textureHeight, std::function<void(GLView *)> initCallback) : QOpenGLWidget(), _textureWidth(textureWidth), _textureHeight(textureHeight)
{
#if __APPLE__
QSurfaceFormat glFormat;
glFormat.setVersion( 3, 3 );
glFormat.setProfile(QSurfaceFormat::OpenGLContextProfile::CompatibilityProfile);
glFormat.setSwapBehavior(QSurfaceFormat::SwapBehavior::SingleBuffer);
glFormat.setSwapInterval(0);
setFormat(glFormat);
#endif
_initCallback = initCallback;
}
GLuint GLView::createTexture(unsigned int width, unsigned int height)
{
GLuint textureId;
glGenTextures(1, &textureId);
glBindTexture(GL_TEXTURE_2D, textureId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
return textureId;
}
void GLView::setPaintCallback(std::function<void(GLView*)> callback)
{
_paintCallback = callback;
}
void GLView::setBaseTexture(GLuint newTextureId)
{
_glTexture = newTextureId;
}
GLuint GLView::getBaseTexture() const
{
return _glTexture;
}
void GLView::paintGL()
{
_checkErrors("before draw");
// glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glBindTexture(GL_TEXTURE_2D, _glTexture);
glEnable(GL_TEXTURE_2D);
if(_paintCallback)
{
_paintCallback(this);
}
glBegin(GL_QUADS);
{
glTexCoord2f(0.0f, 1.0f); glVertex2f(-1.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex2f(-1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex2f( 1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex2f( 1.0f, 1.0f);
}
glEnd();
glDisable(GL_TEXTURE_2D);
_checkErrors("after draw");
}
void GLView::initializeGL()
{
// Initialize OpenGL Functions
initializeOpenGLFunctions();
glViewport(0, 0, width(), height());
_checkErrors("Before initializeGL");
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
glGenTextures(1, &_glTexture);
// In windows, here width and height
if(_glTexture)
{
// Chess dummy texture
int totalBytes = _textureWidth*_textureHeight*4;
unsigned char * dummyTexture = new unsigned char[totalBytes];
for(int i = 0 ; i < _textureHeight; i++)
{
for(int j = 0 ; j < _textureWidth; j++)
{
int tilex = i / 40 ;
int tiley = j / 40 ;
int index = i* (_textureWidth*4) + (j*4);
if((tilex % 2 == 0 && tiley % 2 == 0) || (tilex % 2 != 0 && tiley % 2 !=0))
{
dummyTexture[index +0] = 128;
dummyTexture[index +1] = 128;
dummyTexture[index +2] = 128;
dummyTexture[index +3] = 255;
}
else
{
dummyTexture[index +0] = 0;
dummyTexture[index +1] = 0;
dummyTexture[index +2] = 128;
dummyTexture[index +3] = 255;
}
}
}
glBindTexture(GL_TEXTURE_2D, _glTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, _textureWidth, _textureHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, dummyTexture);
delete [] dummyTexture;
}
else
{
std::cout << "Error loading texture " << std::endl;
}
if(_initCallback)
{
_initCallback(this);
}
_checkErrors("After initializeGL");
}
void GLView::_checkErrors(const std::string & snippet = "")
{
for(GLenum currError = glGetError(); currError != GL_NO_ERROR; currError = glGetError())
{
std::cout << "Error '" << snippet << "' " << currError << std::endl;
}
}