-
Notifications
You must be signed in to change notification settings - Fork 0
/
HW.cpp
130 lines (103 loc) · 3.96 KB
/
HW.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
// ===============================================================
// Computer Graphics Homework Solutions
// Copyright (C) 2017 by George Wolberg
//
// HW.cpp - HW class. Base class of homework solutions.
//
// Written by: George Wolberg, 2017
// ===============================================================
#include "HW.h"
QString GroupBoxStyle = "QGroupBox { \
border: 1px solid gray; \
border-radius: 9px; \
font-weight: bold; \
margin-top: 0.5em;} \
QGroupBox::title { \
subcontrol-origin: margin; \
left: 10px; \
padding: 0 3px 0 3px; \
}";
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// HW::HW:
//
// HW constructor.
// This is base class for homework solutions that will replace
// the control panel, reset function, and add homework solution.
//
HW::HW(const QGLFormat &glf, QWidget *parent) : QGLWidget (glf, parent)
{}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// HW::controlPanel:
//
// Create a control panel of widgets for homework solution.
//
QGroupBox*
HW::controlPanel()
{
return NULL;
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// HW::reset:
//
// Reset parameters in control panel.
//
void
HW::reset() {}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// HW::initShader:
//
// Initialize vertex and fragment shaders.
//
void
HW::initShader(int shaderID, QString vshaderName, QString fshaderName, UniformMap uniforms)
{
// due to bug in Qt, in order to use higher OpenGL version (>2.1), we need to add lines
// up to initShader() to render properly
uint vao;
typedef void (APIENTRY *_glGenVertexArrays) (GLsizei, GLuint*);
typedef void (APIENTRY *_glBindVertexArray) (GLuint);
_glGenVertexArrays glGenVertexArrays;
_glBindVertexArray glBindVertexArray;
glGenVertexArrays = (_glGenVertexArrays) QGLWidget::context()->getProcAddress("glGenVertexArrays");
glBindVertexArray = (_glBindVertexArray) QGLWidget::context()->getProcAddress("glBindVertexArray");
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
// compile vertex shader
bool flag = m_program[shaderID].addShaderFromSourceFile(QGLShader::Vertex, vshaderName);
if(!flag) {
QMessageBox::critical(0, "Error", "Vertex shader error: " + vshaderName + "\n" +
m_program[shaderID].log(), QMessageBox::Ok);
exit(-1);
}
// compile fragment shader
if(!m_program[shaderID].addShaderFromSourceFile(QGLShader::Fragment, fshaderName)) {
QMessageBox::critical(0, "Error", "Fragment shader error: " + fshaderName + "\n" +
m_program[shaderID].log(), QMessageBox::Ok);
exit(-1);
}
// bind the attribute variable in the glsl program with a generic vertex attribute index;
// values provided via ATTRIB_VERTEX will modify the value of "a_position")
glBindAttribLocation(m_program[shaderID].programId(), ATTRIB_VERTEX, "a_Position");
glBindAttribLocation(m_program[shaderID].programId(), ATTRIB_COLOR, "a_Color" );
glBindAttribLocation(m_program[shaderID].programId(), ATTRIB_TEXCOORD, "a_TexCoord");
glBindAttribLocation(m_program[shaderID].programId(), ATTRIB_NORMAL, "a_Normal" );
// link shader pipeline; attribute bindings go into effect at this point
if(!m_program[shaderID].link()) {
QMessageBox::critical(0, "Error", "Could not link shader: " + vshaderName + "\n" +
m_program[shaderID].log(), QMessageBox::Ok);
qDebug() << m_program[shaderID].log();
exit(-1);
}
// iterate over all uniform variables; map each uniform name to shader location ID
for(std::map<QString, GLuint>::iterator iter = uniforms.begin(); iter != uniforms.end(); ++iter) {
QString uniformName = iter->first;
GLuint uniformID = iter->second;
// get storage location
m_uniform[shaderID][uniformID]=glGetUniformLocation(m_program[shaderID].programId(),
uniformName.toStdString().c_str());
if(m_uniform[shaderID][uniformID] < 0) {
qDebug() << m_program[shaderID].log();
qDebug() << "Failed to get the storage location of " + uniformName;
}
}
}