22
33// ////////////////////////////////////////////////////////////////////
44// Publics
5- BasicWidget::BasicWidget (QWidget* parent) : QOpenGLWidget(parent), vbo_(QOpenGLBuffer::VertexBuffer), cbo_(QOpenGLBuffer::VertexBuffer), ibo_(QOpenGLBuffer::IndexBuffer), logger_(this )
5+ BasicWidget::BasicWidget (QWidget* parent) : QOpenGLWidget(parent), vbo_(QOpenGLBuffer::VertexBuffer), ibo_(QOpenGLBuffer::IndexBuffer), logger_(this )
66{
77 setFocusPolicy (Qt::StrongFocus);
88}
@@ -11,10 +11,6 @@ BasicWidget::~BasicWidget()
1111{
1212 vbo_.release ();
1313 vbo_.destroy ();
14- // TODO: Remove the CBO
15- cbo_.release ();
16- cbo_.destroy ();
17- // End TODO
1814 ibo_.release ();
1915 ibo_.destroy ();
2016 vao_.release ();
@@ -38,9 +34,8 @@ QString BasicWidget::vertexShaderString() const
3834
3935 " void main()\n "
4036 " {\n "
41- // TODO: gl_Position must be updated!
42- " gl_Position = vec4(position, 1.0);\n "
43- // END TODO
37+ // " gl_Position = vec4(position, 1.0);\n"
38+ " gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(position, 1.0);\n "
4439 " vertColor = color;\n "
4540 " }\n " ;
4641 return str;
@@ -85,9 +80,15 @@ void BasicWidget::keyReleaseEvent(QKeyEvent* keyEvent)
8580 // Handle key events here.
8681 if (keyEvent->key () == Qt::Key_Left) {
8782 qDebug () << " Left Arrow Pressed" ;
83+ model_.rotate (-10 .0f , 0 .0f , 1 .0f , 0 .0f );
84+ shaderProgram_.bind ();
85+ shaderProgram_.setUniformValue (" modelMatrix" , model_);
8886 update (); // We call update after we handle a key press to trigger a redraw when we are ready
8987 } else if (keyEvent->key () == Qt::Key_Right) {
9088 qDebug () << " Right Arrow Pressed" ;
89+ model_.rotate (10 .0f , 0 .0f , 1 .0f , 0 .0f );
90+ shaderProgram_.bind ();
91+ shaderProgram_.setUniformValue (" modelMatrix" , model_);
9192 update (); // We call update after we handle a key press to trigger a redraw when we are ready
9293 } else {
9394 qDebug () << " You Pressed an unsupported Key!" ;
@@ -126,19 +127,21 @@ void BasicWidget::initializeGL()
126127 createShader ();
127128
128129 // Define our verts
129- static const GLfloat verts[9 ] =
130+ static const GLfloat verts[] =
130131 {
131- 0 .0f , 0 .0f , 0 .0f , // Center vertex position
132- 1 .0f , 1 .0f , 0 .0f , // Top right vertex position
133- -1 .0f , 1 .0f , 0 .0f // Top left vertex position
132+ 0 .0f , 0 .0f , 0 .0f , 1 . 0f , 0 . 0f , 0 . 0f , 1 . 0f , // Center vertex position
133+ 1 .0f , 1 .0f , 0 .0f , 0 . 0f , 1 . 0f , 0 . 0f , 1 . 0f , // Top right vertex position
134+ -1 .0f , 1 .0f , 0 .0f , 0 . 0f , 0 . 0f , 1 . 0f , 1 . 0f , // Top left vertex position
134135 };
136+
135137 // Define our vert colors
136- static const GLfloat colors[12 ] =
137- {
138- 1 .0f , 0 .0f , 0 .0f , 1 .0f , // red
139- 0 .0f , 1 .0f , 0 .0f , 1 .0f , // green
140- 0 .0f , 0 .0f , 1 .0f , 1 .0f // blue
141- };
138+ // static const GLfloat colors[12] =
139+ // {
140+ // 1.0f, 0.0f, 0.0f, 1.0f, // red
141+ // 0.0f, 1.0f, 0.0f, 1.0f, // green
142+ // 0.0f, 0.0f, 1.0f, 1.0f // blue
143+ // };
144+
142145 // Define our indices
143146 static const GLuint idx[3 ] =
144147 {
@@ -153,14 +156,7 @@ void BasicWidget::initializeGL()
153156 vbo_.create ();
154157 vbo_.setUsagePattern (QOpenGLBuffer::StaticDraw);
155158 vbo_.bind ();
156- vbo_.allocate (verts, 3 * 3 * sizeof (GL_FLOAT ));
157- // END TODO
158-
159- // TODO: Remove the cbo_
160- cbo_.create ();
161- cbo_.setUsagePattern (QOpenGLBuffer::StaticDraw);
162- cbo_.bind ();
163- cbo_.allocate (colors, 3 * 4 * sizeof (GL_FLOAT ));
159+ vbo_.allocate (verts, 3 * 7 * sizeof (GL_FLOAT ));
164160 // END TODO
165161
166162 // TODO: Generate our index buffer
@@ -178,10 +174,9 @@ void BasicWidget::initializeGL()
178174 // Note: Remember that Offset and Stride are expressed in terms
179175 // of bytes!
180176 shaderProgram_.enableAttributeArray (0 );
181- shaderProgram_.setAttributeBuffer (0 , GL_FLOAT , 0 , 3 );
182- cbo_.bind ();
177+ shaderProgram_.setAttributeBuffer (0 , GL_FLOAT , 0 , 3 , sizeof (GL_FLOAT )*7 );
183178 shaderProgram_.enableAttributeArray (1 );
184- shaderProgram_.setAttributeBuffer (1 , GL_FLOAT , 0 , 4 );
179+ shaderProgram_.setAttributeBuffer (1 , GL_FLOAT , sizeof ( GL_FLOAT )* 3 , 4 , sizeof ( GL_FLOAT )* 7 );
185180 // END TODO
186181
187182 ibo_.bind ();
@@ -195,8 +190,19 @@ void BasicWidget::initializeGL()
195190void BasicWidget::resizeGL (int w, int h)
196191{
197192 glViewport (0 , 0 , w, h);
193+
198194 // TODO: Set up the model, view, and projection matrices
195+ model_.rotate (30 .0f , 0 .5f , 0 .5f , 0 .5f );
196+ view_.translate (0 .0f , 0 .0f , -5 .0f );
197+ view_.rotate (180 .0f , 0 .0f , 0 .0f , -1 .0f );
198+ projection_.perspective (70 .0f , 1 .5f , 0 .0f , 100 .0f );
199199 // END TODO
200+
201+ shaderProgram_.bind ();
202+
203+ shaderProgram_.setUniformValue (" modelMatrix" , model_);
204+ shaderProgram_.setUniformValue (" viewMatrix" , view_);
205+ shaderProgram_.setUniformValue (" projectionMatrix" , projection_);
200206}
201207
202208void BasicWidget::paintGL ()
0 commit comments