OpenGL: Różnice pomiędzy wersjami

[wersja przejrzana][wersja przejrzana]
Usunięta treść Dodana treść
mNie podano opisu zmian
mNie podano opisu zmian
Linia 199:
</syntaxhighlight>
 
Plik '''mainwindow.h'''<syntaxhighlight lang="cpp-qt" line="1">
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
 
//#include <QMainWindow>
Plik '''mainwindow.cpp'''
#include <QOpenGLFunctions_3_3_Core>
#include <QOpenGLWidget>
 
class MainWindow : public QOpenGLWidget, protected QOpenGLFunctions_3_3_Core //public QMainWindow,
{
//Q_OBJECT
 
GLuint uchwytProg1; //unsigned int
 
GLuint VertexArrayO; // Vertex Array Object
int ilWiesz; //ilosc wierzcholkow
 
GLuint tex0;
 
 
public:
MainWindow();
~MainWindow();
 
void initializeGL();
void paintGL();
void resizeGL(int w, int h);
};
 
#endif // MAINWINDOW_H
</syntaxhighlight>
 
Plik '''mainwindow.cpp'''<syntaxhighlight lang="cpp-qt" line="1">
#include "mainwindow.h"
 
#include <QOpenGLFunctions_3_3_Core>
#include <QFile>
#include <Qdebug>
 
struct vec3 { float x,y,z; };
 
MainWindow::MainWindow() //QMainWindow(parent)
{ }
 
MainWindow::~MainWindow()
{ }
 
 
void MainWindow::initializeGL()
{
initializeOpenGLFunctions();
 
qDebug() << "initializeGL";
 
GLint status;
 
// program kopiujący teksture na ekran
uchwytProg1 = glCreateProgram();
 
QFile file("vshaderS.sh");
if(file.open(QFile::ReadOnly))
{
QTextStream stream(&file);
std::string vshader_zrodlo = stream.readAll().toStdString();
 
GLuint shader = glCreateShader(GL_VERTEX_SHADER);
GLchar* srcs[] = {(GLchar*)vshader_zrodlo.c_str()};
glShaderSource(shader, 1, srcs, NULL);
glCompileShader(shader);
 
glGetShaderiv(shader, GL_COMPILE_STATUS, &status); //skompilowany
if( status == true )
{
glAttachShader(uchwytProg1, shader);
glDeleteShader(shader);
} else { qDebug() << "Shadera nie skompilowano. V"; }
qDebug() << "BB" << vshader_zrodlo.c_str();
} else qDebug() << "Brak pliku";
file.close();
 
QFile file2("fshaderS.sh");
if(file2.open(QFile::ReadOnly))
{
QTextStream stream(&file2);
std::string vshader_zrodlo = stream.readAll().toStdString();
 
GLuint shader = glCreateShader(GL_FRAGMENT_SHADER);
GLchar* srcs[] = {(GLchar*)vshader_zrodlo.c_str()};
glShaderSource(shader, 1, srcs, NULL);
glCompileShader(shader);
glGetShaderiv(shader, GL_COMPILE_STATUS, &status); //skompilowany
if( status == true )
{
glAttachShader(uchwytProg1, shader);
glDeleteShader(shader);
} else {
qDebug() << "Błąd komp. szhadera. F";
GLchar infoLog[10240];
glGetShaderInfoLog(shader, 10240, NULL, infoLog);
qDebug() << infoLog << endl;
}
 
qDebug() << "AA" << srcs;
} else qDebug() << "Brak pliku";
file2.close();
 
glLinkProgram(uchwytProg1);
glGetProgramiv(uchwytProg1, GL_LINK_STATUS, &status);
if( status == true) { qDebug() << "Program przygotowany"; }
else qDebug() << "Problemy z programem.";
 
glUseProgram(uchwytProg1);
GLint polozenie = glGetUniformLocation(uchwytProg1, "textura");
qDebug() << "Polozenie, textura: " << polozenie;
 
// prostokat na cały ekran
vec3 wieszcholki[] = { {-1,-1,0}, {1,-1,0}, //Dwa trójkąty...
{1,1,0}, {1,1,0},
{-1,1,0}, {-1,-1,0} };
ilWiesz = 6;
 
glGenVertexArrays(1, &VertexArrayO);
glBindVertexArray(VertexArrayO);
glEnableVertexAttribArray(0); //włączenie obsługi konkretnej tablicy atrybutów,
 
//wygenerowanie nazwy bufora dla atrybutu i dodanie do mapy pod konkretnym indexem
GLuint buforVertex;
glGenBuffers(1, &buforVertex);
 
// przypisanie bufora do tablicy vao
glBindBuffer(GL_ARRAY_BUFFER, buforVertex);
 
// stworzenie bufora i skopiowanie do niego danych
glBufferData(GL_ARRAY_BUFFER, ilWiesz*sizeof(vec3), wieszcholki, GL_STATIC_DRAW);
 
// ustawienie odpowiednich typow i wielkosci bufora wierzcholkow vbo
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
 
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
 
glGenTextures(1, & tex0);
 
glActiveTexture(GL_TEXTURE0);
 
// tekstura wczytana z pliku
QImage img("lena.png");
if(img.width() == 0) qt_assert("QImage not loaded!", __FILE__, __LINE__);
 
glBindTexture(GL_TEXTURE_2D, tex0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img.width(), img.height(), 0, GL_BGRA, GL_UNSIGNED_BYTE, img.bits());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
 
glBindTexture(GL_TEXTURE_2D, 0);
}
 
void MainWindow::resizeGL(int w, int h)
{
qDebug() << "resizeGL";
}
 
void MainWindow::paintGL()
{
qDebug() << "PaintGL - rysuje";
 
GLuint polozenie;
 
/* niektore implementacje nie przypisuja domyslnego FB ekranu == 0
wtedy trzeba zapamietac samemu jego numer: */
GLint screenFB;
glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &screenFB);
qDebug() << "Numer dla ScreenFramebuffer: " << screenFB;
 
int tryb = 1;
 
if( tryb == 1 )
{
// rysowanie tekstury na domyslnym FrameBufferze
 
// glBindFramebuffer(GL_FRAMEBUFFER, 0);
// lub
glBindFramebuffer(GL_FRAMEBUFFER, screenFB);
glViewport(0,0,width(), height());
 
glClear(GL_COLOR_BUFFER_BIT);
 
glUseProgram(uchwytProg1);
 
// Ustawienie tekstury
glActiveTexture(GL_TEXTURE0);
polozenie = glGetUniformLocation(uchwytProg1, "textura");
if(polozenie != -1) glUniform1i(polozenie, 0); else qDebug() << "Brak zmiennej Uniform textura";
 
glBindTexture(GL_TEXTURE_2D, tex0);
 
glBindVertexArray(VertexArrayO);
glDrawArrays(GL_TRIANGLES, 0, ilWiesz);
glBindVertexArray(0);
 
glBindTexture(GL_TEXTURE_2D, tex0);
glViewport(width()/2.0, 0, width()/2.0, height());
 
glBindVertexArray(VertexArrayO);
glDrawArrays(GL_TRIANGLES, 0, ilWiesz);
glBindVertexArray(0);
 
glUseProgram(0);
}
}
</syntaxhighlight>
 
Plik vshader.glsl - '''Shader Vertexów - Shader wieschołków:'''<syntaxhighlight lang="cpp-qt">