How to write 3D applications?
-
Good, let's render a green triangle on our blue background. Our triangle has 3 vertices:
std::array<float, 3 * 2> m_vertices = {{ // 3 vertices * 2 coordinates -0.75f, -0.75f, // vertex 1 : x, y -0.75f, 0.75f, // vertex 2 : x, y 0.75f, 0.75f // vertex 3 : x, y }};
We only give x and y coordinates here. We'll assume that z is zero and w is one. Also we don't provide any color information here as we'll assume that the triangle is uniformly green.
That's the data we'll feed to our OpenGL program. The program consists of two parts, both written in GLSL (= OpenGL Shading Language): a vertex shader and a fragment shader:
shader.vert
#version 330 in vec2 position; void main() { gl_Position = vec4(position, 0.0, 1.0); // x, y, z, w }
shader.frag
#version 330 out vec4 color; void main() { color = vec4(0.0, 1.0, 0.0, 1.0); // red, green, blue, alpha ; our triangle will be green }
-
And now we put all that together and get this:
my3dwidget.h
#ifndef MY3DWIDGET_H #define MY3DWIDGET_H #include <QOpenGLWidget> #include <QOpenGLFunctions> #include <array> #include <QOpenGLShaderProgram> #include <QOpenGLBuffer> #include <QOpenGLVertexArrayObject> class My3dWidget : public QOpenGLWidget , protected QOpenGLFunctions { Q_OBJECT public: explicit My3dWidget(QWidget *parent = nullptr); protected: virtual void initializeGL() override; virtual void resizeGL(int w, int h) override; virtual void paintGL() override; private: std::array<float, 3 * 2> m_vertices = {{ // 3 vertices * 2 coordinates -0.75f, -0.75f, // vertex 1 : x, y -0.75f, 0.75f, // vertex 2 : x, y 0.75f, 0.75f // vertex 3 : x, y }}; QOpenGLShaderProgram m_program; QOpenGLBuffer m_buffer; QOpenGLVertexArrayObject m_vao; }; #endif // MY3DWIDGET_H
my3dwidget.cpp
#include "my3dwidget.h" My3dWidget::My3dWidget(QWidget *parent) : QOpenGLWidget(parent) { } void My3dWidget::initializeGL() { initializeOpenGLFunctions(); glClearColor(0.0f, 0.0f, 1.0f, 1.0f); // blue m_program.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/shaders/shader.vert"); m_program.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/shaders/shader.frag"); m_program.link(); m_program.bind(); m_buffer.create(); m_buffer.bind(); m_buffer.allocate(m_vertices.data(), // pointer to first element of array sizeof(m_vertices)); // size of array in bytes m_vao.create(); m_vao.bind(); m_program.enableAttributeArray(0); // vertex array at location 0 m_program.setAttributeBuffer(0, // location 0 GL_FLOAT, // elements of the array are of type GL_FLOAT (= float) 0, // start at array position 0 2, // number of elements per vertex (= x, y) 0); // stride: densely packed (= no holes) m_vao.release(); m_buffer.release(); m_program.release(); } void My3dWidget::resizeGL(int w, int h) { Q_UNUSED(w) Q_UNUSED(h) } void My3dWidget::paintGL() { glClear(GL_COLOR_BUFFER_BIT); m_program.bind(); m_vao.bind(); glDrawArrays(GL_TRIANGLES, 0, sizeof(m_vertices)); m_vao.release(); m_program.release(); }
End of tutorial.
-
very very thanks friend.
so why i could not run qt 5 tutorial? link text
what about GLUT? can i use glut commands in Qt?
can i ask my questions if i had more on future?
UPDATED:
i can to run trent reed tutorial finally. of course in my laptop. but on my work pc not run. that get erros:link text[Edit: removed link, contains malware ~~ @Wieland]
-
very very thanks friend.
so why i could not run qt 5 tutorial? link text
what about GLUT? can i use glut commands in Qt?
can i ask my questions if i had more on future?
UPDATED:
i can to run trent reed tutorial finally. of course in my laptop. but on my work pc not run. that get erros:link text[Edit: removed link, contains malware ~~ @Wieland]
@parvizwpf said in How to write 3D applications?:
very very thanks friend
No worries. Please mark the thread as solved and vote-up the posts that were helpful.
can i ask my questions if i had more on future
Sure. Please start a new thread for each specific question.
i can to run it of course in my laptop. but on my work pc not run. that get erros: link text
The domain you linked to contains malware. Please use a different image hoster, e.g. http://postimages.org/
-
repaired error image link
what about GLUT? can i use glut commands in Qt?
whatever you help me to start opengl :-)))
"vote-up the posts that were helpful." how? -
GLUT is old and stinks. You don't want to touch it.
-
repaired error image link
what about GLUT? can i use glut commands in Qt?
whatever you help me to start opengl :-)))
"vote-up the posts that were helpful." how?@parvizwpf said in How to write 3D applications?:
"vote-up the posts that were helpful." how?
-
@parvizwpf said in How to write 3D applications?:
why? i see it in many web tutorials
because all these tutorials are outdated and written by n00bs
-
Edit: Oops, looks like you've been faster :-)
-
i can not see anything in your topic link.
how can i create a quote? i don't see it.
n00bs???? if those are n00bs ,so who am i?
do you tell me good tutorials ?but on my work pc not run.
Maybe your PC has a terribly outdated GPU or the driver has some problem.
do you tell me good tutorials ?
Actually I like Trent's tutorial for the Qt integration part. Anything else: get a book on GLSL.
-
but on my work pc not run.
Maybe your PC has a terribly outdated GPU or the driver has some problem.
do you tell me good tutorials ?
Actually I like Trent's tutorial for the Qt integration part. Anything else: get a book on GLSL.