-1
我們必須製作一款球類比賽。但是,在將代碼應用到圖形中之前,我們爲3種圖形提供了3個代碼。一個代碼立方體,一個隨機球體代碼不移動,一個移動立方體代碼,如果我沒有錯(也許我是)。如何使用OpenGL創建一個或多個球體?
多維數據集第一個代碼如下:
#include "vue_opengl.h"
#include "vertex_shader.h"
#include "contenu.h"
// ======================================================================
void VueOpenGL::dessine(Contenu const& a_dessiner)
{
Q_UNUSED(a_dessiner);
draw first cube (at origin)
dessineCube();
QMatrix4x4 matrice;
// Dessine le 2e cube
matrice.translate(0.0, 1.5, 0.0);
matrice.scale(0.25);
dessineCube(matrice);
// Dessine le 3e cube
matrice.setToIdentity();
matrice.translate(0.0, 0.0, 1.5);
matrice.scale(0.25);
matrice.rotate(45.0, 0.0, 1.0, 0.0);
dessineCube(matrice);
}
// ======================================================================
void VueOpenGL::init()
{
prog.addShaderFromSourceFile(QGLShader::Vertex, ":/vertex_shader.glsl");
prog.addShaderFromSourceFile(QGLShader::Fragment, ":/fragment_shader.glsl");
prog.bindAttributeLocation("sommet", SommetId);
prog.bindAttributeLocation("couleur", CouleurId);
// Compilation of the shader openGL
prog.link();
// Activation of the shader
prog.bind();
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
initializePosition();
}
// ======================================================================
void VueOpenGL::initializePosition()
{
// position initiale
matrice_vue.setToIdentity();
matrice_vue.translate(0.0, 0.0, -4.0);
matrice_vue.rotate(60.0, 0.0, 1.0, 0.0);
matrice_vue.rotate(45.0, 0.0, 0.0, 1.0);
}
// ======================================================================
void VueOpenGL::translate(double x, double y, double z)
{
QMatrix4x4 translation_supplementaire;
translation_supplementaire.translate(x, y, z);
matrice_vue = translation_supplementaire * matrice_vue;
}
// ======================================================================
void VueOpenGL::rotate(double angle, double dir_x, double dir_y, double dir_z)
{// Multiplie la matrice de vue par LA GAUCHE
QMatrix4x4 rotation_supplementaire;
rotation_supplementaire.rotate(angle, dir_x, dir_y, dir_z);
matrice_vue = rotation_supplementaire * matrice_vue;
}
// ======================================================================
void VueOpenGL::dessineCube (QMatrix4x4 const& point_de_vue)
{
prog.setUniformValue("vue_modele", matrice_vue * point_de_vue);
glBegin(GL_QUADS);
// side X = +1
prog.setAttributeValue(CouleurId, 1.0, 0.0, 0.0); // red
prog.setAttributeValue(SommetId, +1.0, -1.0, -1.0);
prog.setAttributeValue(SommetId, +1.0, +1.0, -1.0);
prog.setAttributeValue(SommetId, +1.0, +1.0, +1.0);
prog.setAttributeValue(SommetId, +1.0, -1.0, +1.0);
// sides X = -1
prog.setAttributeValue(CouleurId, 0.0, 1.0, 0.0); // green
prog.setAttributeValue(SommetId, -1.0, -1.0, -1.0);
prog.setAttributeValue(SommetId, -1.0, -1.0, +1.0);
prog.setAttributeValue(SommetId, -1.0, +1.0, +1.0);
prog.setAttributeValue(SommetId, -1.0, +1.0, -1.0);
// side Y = +1
prog.setAttributeValue(CouleurId, 0.0, 0.0, 1.0); // blue
prog.setAttributeValue(SommetId, -1.0, +1.0, -1.0);
prog.setAttributeValue(SommetId, -1.0, +1.0, +1.0);
prog.setAttributeValue(SommetId, +1.0, +1.0, +1.0);
prog.setAttributeValue(SommetId, +1.0, +1.0, -1.0);
//side Y = -1
prog.setAttributeValue(CouleurId, 0.0, 1.0, 1.0); // cyan
prog.setAttributeValue(SommetId, -1.0, -1.0, -1.0);
prog.setAttributeValue(SommetId, +1.0, -1.0, -1.0);
prog.setAttributeValue(SommetId, +1.0, -1.0, +1.0);
prog.setAttributeValue(SommetId, -1.0, -1.0, +1.0);
// side Z = +1
prog.setAttributeValue(CouleurId, 1.0, 1.0, 0.0); // yellow
prog.setAttributeValue(SommetId, -1.0, -1.0, +1.0);
prog.setAttributeValue(SommetId, +1.0, -1.0, +1.0);
prog.setAttributeValue(SommetId, +1.0, +1.0, +1.0);
prog.setAttributeValue(SommetId, -1.0, +1.0, +1.0);
// side Z = -1
prog.setAttributeValue(CouleurId, 1.0, 0.0, 1.0); // magenta
prog.setAttributeValue(SommetId, -1.0, -1.0, -1.0);
prog.setAttributeValue(SommetId, -1.0, +1.0, -1.0);
prog.setAttributeValue(SommetId, +1.0, +1.0, -1.0);
prog.setAttributeValue(SommetId, +1.0, -1.0, -1.0);
glEnd();
}
現在,他們給我們一個球體代碼:
#include "vue_opengl.h"
#include "vertex_shader.h"
#include "contenu.h"
// ======================================================================
void VueOpenGL::dessine(Contenu const& a_dessiner)
{
Q_UNUSED(a_dessiner); //
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
QMatrix4x4 matrice;
matrice.translate(-0.5, 0.0, -2.0);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // passe en mode "fil de fer"
dessineSphere(matrice, 0.0, 0.0); // bleu
matrice.scale(1.5); // taille des axes (1.5 pour qu'ils dépassent un peu)
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); // repasse en mode "plein"
}
// ======================================================================
void VueOpenGL::init()
{
prog.addShaderFromSourceFile(QGLShader::Vertex, ":/vertex_shader.glsl");
prog.addShaderFromSourceFile(QGLShader::Fragment, ":/fragment_shader.glsl");
prog.bindAttributeLocation("sommet", SommetId);
prog.bindAttributeLocation("couleur", CouleurId);
prog.link();
// Activation du shader
prog.bind();
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
sphere.initialize(); // initialise la sphère
initializePosition();
}
// ======================================================================
void VueOpenGL::initializePosition()
{
// position initial
matrice_vue.setToIdentity();
matrice_vue.translate(0.0, 0.0, -4.0);
matrice_vue.rotate(60.0, 0.0, 1.0, 0.0);
matrice_vue.rotate(45.0, 0.0, 0.0, 1.0);
}
// ======================================================================
void VueOpenGL::translate(double x, double y, double z)
{
QMatrix4x4 translation_supplementaire;
translation_supplementaire.translate(x, y, z);
matrice_vue = translation_supplementaire * matrice_vue;
}
// ======================================================================
void VueOpenGL::rotate(double angle, double dir_x, double dir_y, double dir_z)
{
//
QMatrix4x4 rotation_supplementaire;
rotation_supplementaire.rotate(angle, dir_x, dir_y, dir_z);
matrice_vue = rotation_supplementaire * matrice_vue;
}
// ======================================================================
void VueOpenGL::dessineCube (QMatrix4x4 const& point_de_vue)
{
prog.setUniformValue("vue_modele", matrice_vue * point_de_vue);
glBegin(GL_QUADS);
// side X = +1
prog.setAttributeValue(CouleurId, 1.0, 0.0, 0.0); // rouge
prog.setAttributeValue(SommetId, +1.0, -1.0, -1.0);
prog.setAttributeValue(SommetId, +1.0, +1.0, -1.0);
prog.setAttributeValue(SommetId, +1.0, +1.0, +1.0);
prog.setAttributeValue(SommetId, +1.0, -1.0, +1.0);
// side X = -1
prog.setAttributeValue(CouleurId, 0.0, 1.0, 0.0); // vert
prog.setAttributeValue(SommetId, -1.0, -1.0, -1.0);
prog.setAttributeValue(SommetId, -1.0, -1.0, +1.0);
prog.setAttributeValue(SommetId, -1.0, +1.0, +1.0);
prog.setAttributeValue(SommetId, -1.0, +1.0, -1.0);
// face coté Y = +1
prog.setAttributeValue(CouleurId, 0.0, 0.0, 1.0); // bleu
prog.setAttributeValue(SommetId, -1.0, +1.0, -1.0);
prog.setAttributeValue(SommetId, -1.0, +1.0, +1.0);
prog.setAttributeValue(SommetId, +1.0, +1.0, +1.0);
prog.setAttributeValue(SommetId, +1.0, +1.0, -1.0);
// side Y = -1
prog.setAttributeValue(CouleurId, 0.0, 1.0, 1.0); // cyan
prog.setAttributeValue(SommetId, -1.0, -1.0, -1.0);
prog.setAttributeValue(SommetId, +1.0, -1.0, -1.0);
prog.setAttributeValue(SommetId, +1.0, -1.0, +1.0);
prog.setAttributeValue(SommetId, -1.0, -1.0, +1.0);
// side Z = +1
prog.setAttributeValue(CouleurId, 1.0, 1.0, 0.0); // jaune
prog.setAttributeValue(SommetId, -1.0, -1.0, +1.0);
prog.setAttributeValue(SommetId, +1.0, -1.0, +1.0);
prog.setAttributeValue(SommetId, +1.0, +1.0, +1.0);
prog.setAttributeValue(SommetId, -1.0, +1.0, +1.0);
//side Z = -1
prog.setAttributeValue(CouleurId, 1.0, 0.0, 1.0); // magenta
prog.setAttributeValue(SommetId, -1.0, -1.0, -1.0);
prog.setAttributeValue(SommetId, -1.0, +1.0, -1.0);
prog.setAttributeValue(SommetId, +1.0, +1.0, -1.0);
prog.setAttributeValue(SommetId, +1.0, -1.0, -1.0);
glEnd();
}
// ======================================================================
void VueOpenGL::dessineSphere (QMatrix4x4 const& point_de_vue,
double rouge, double vert, double bleu)
{
prog.setUniformValue("vue_modele", matrice_vue * point_de_vue);
prog.setAttributeValue(CouleurId, rouge, vert, bleu); // met la couleur
sphere.draw(prog, SommetId); // dessine la sphère
}
// ======================================================================
void VueOpenGL::dessineAxes (QMatrix4x4 const& point_de_vue, bool en_couleur)
{
prog.setUniformValue("vue_modele", matrice_vue * point_de_vue);
glBegin(GL_LINES);
// axe X
if (en_couleur) {
prog.setAttributeValue(CouleurId, 1.0, 0.0, 0.0); // rouge
} else {
prog.setAttributeValue(CouleurId, 1.0, 1.0, 1.0); // blanc
}
prog.setAttributeValue(SommetId, 0.0, 0.0, 0.0);
prog.setAttributeValue(SommetId, 1.0, 0.0, 0.0);
// axe Y
if (en_couleur) prog.setAttributeValue(CouleurId, 0.0, 1.0, 0.0); // green
prog.setAttributeValue(SommetId, 0.0, 0.0, 0.0);
prog.setAttributeValue(SommetId, 0.0, 1.0, 0.0);
// axe Z
if (en_couleur) prog.setAttributeValue(CouleurId, 0.0, 0.0, 1.0); // blue
prog.setAttributeValue(SommetId, 0.0, 0.0, 0.0);
prog.setAttributeValue(SommetId, 0.0, 0.0, 1.0);
glEnd();
}
LAST CODE:移動立方體: - 我認爲我們必須把在這個代碼球,以便它移動:
#include "vue_opengl.h"
#include "vertex_shader.h" // Identifiants Qt de nos différents attributs
#include "contenu.h"
// ======================================================================
void VueOpenGL::dessine(Contenu const& a_dessiner)
{
// Dessine le 1er cube (à l'origine)
dessineCube();
QMatrix4x4 matrice;
// Dessine le 4e cube
matrice.setToIdentity();
matrice.rotate(a_dessiner.infos(), 1.0, 0.0, 0.0);
matrice.translate(0.0, 2.3, 0.0);
matrice.scale(0.2);
dessineCube(matrice);
}
// ======================================================================
void VueOpenGL::init()
{
prog.addShaderFromSourceFile(QGLShader::Vertex, ":/vertex_shader.glsl");
prog.addShaderFromSourceFile(QGLShader::Fragment, ":/fragment_shader.glsl");
prog.bindAttributeLocation("sommet", SommetId);
prog.bindAttributeLocation("couleur", CouleurId);
// Compilation du shader OpenGL
prog.link();
// Activation du shader
prog.bind();
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
initializePosition();
}
// ======================================================================
void VueOpenGL::initializePosition()
{
// position initiale
matrice_vue.setToIdentity();
matrice_vue.translate(0.0, 0.0, -4.0);
matrice_vue.rotate(60.0, 0.0, 1.0, 0.0);
matrice_vue.rotate(45.0, 0.0, 0.0, 1.0);
}
// ======================================================================
void VueOpenGL::translate(double x, double y, double z)
{
QMatrix4x4 translation_supplementaire;
translation_supplementaire.translate(x, y, z);
matrice_vue = translation_supplementaire * matrice_vue;
}
// ======================================================================
void VueOpenGL::rotate(double angle, double dir_x, double dir_y, double dir_z)
{
QMatrix4x4 rotation_supplementaire;
rotation_supplementaire.rotate(angle, dir_x, dir_y, dir_z);
matrice_vue = rotation_supplementaire * matrice_vue;
}
// ======================================================================
void VueOpenGL::dessineCube (QMatrix4x4 const& point_de_vue)
{
prog.setUniformValue("vue_modele", matrice_vue * point_de_vue);
glBegin(GL_QUADS);
// face coté X = +1
prog.setAttributeValue(CouleurId, 1.0, 0.0, 0.0); // rouge
prog.setAttributeValue(SommetId, +1.0, -1.0, -1.0);
prog.setAttributeValue(SommetId, +1.0, +1.0, -1.0);
prog.setAttributeValue(SommetId, +1.0, +1.0, +1.0);
prog.setAttributeValue(SommetId, +1.0, -1.0, +1.0);
// face coté X = -1
prog.setAttributeValue(CouleurId, 0.0, 1.0, 0.0); // vert
prog.setAttributeValue(SommetId, -1.0, -1.0, -1.0);
prog.setAttributeValue(SommetId, -1.0, -1.0, +1.0);
prog.setAttributeValue(SommetId, -1.0, +1.0, +1.0);
prog.setAttributeValue(SommetId, -1.0, +1.0, -1.0);
// face coté Y = +1
prog.setAttributeValue(CouleurId, 0.0, 0.0, 1.0); // bleu
prog.setAttributeValue(SommetId, -1.0, +1.0, -1.0);
prog.setAttributeValue(SommetId, -1.0, +1.0, +1.0);
prog.setAttributeValue(SommetId, +1.0, +1.0, +1.0);
prog.setAttributeValue(SommetId, +1.0, +1.0, -1.0);
// face coté Y = -1
prog.setAttributeValue(CouleurId, 0.0, 1.0, 1.0); // cyan
prog.setAttributeValue(SommetId, -1.0, -1.0, -1.0);
prog.setAttributeValue(SommetId, +1.0, -1.0, -1.0);
prog.setAttributeValue(SommetId, +1.0, -1.0, +1.0);
prog.setAttributeValue(SommetId, -1.0, -1.0, +1.0);
// face coté Z = +1
prog.setAttributeValue(CouleurId, 1.0, 1.0, 0.0); // jaune
prog.setAttributeValue(SommetId, -1.0, -1.0, +1.0);
prog.setAttributeValue(SommetId, +1.0, -1.0, +1.0);
prog.setAttributeValue(SommetId, +1.0, +1.0, +1.0);
prog.setAttributeValue(SommetId, -1.0, +1.0, +1.0);
// face coté Z = -1
prog.setAttributeValue(CouleurId, 1.0, 0.0, 1.0); // magenta
prog.setAttributeValue(SommetId, -1.0, -1.0, -1.0);
prog.setAttributeValue(SommetId, -1.0, +1.0, -1.0);
prog.setAttributeValue(SommetId, +1.0, +1.0, -1.0);
prog.setAttributeValue(SommetId, +1.0, -1.0, -1.0);
glEnd();
}
我如何實現所有3這些代碼來顯示一個球體正在移動,並且是一個純色?
道歉代碼的一部分是在法語,但dessine裝置繪製 – johabachi
我的答案在這裏解釋了繪製球體的幾個選項:http://stackoverflow.com/questions/24137 198/OpenGL的ES-2-0-球體。 –