- glDrawArrays przykład 1
//Opis modelu
//Tablica współrzędnych wierzchołków
float verts[]={
0, 1,0,1,
-1,-1,0,1,
1,-1,0,1
};
int vertexCount=3; //Liczba wierzchołków w tablicy
//Kod rysujący
spConstant->use();
glUniformMatrix4fv(spConstant->u("P"),1,false,glm::value_ptr(P));
glUniformMatrix4fv(spConstant->u("V"),1,false,glm::value_ptr(V));
glUniformMatrix4fv(spConstant->u("M"),1,false,glm::value_ptr(M));
glUniform4f(spConstant->u("color"),1,0,0,1);
glEnableVertexAttribArray(spConstant->a("vertex"));
glVertexAttribPointer(spConstant->a("vertex"),4,GL_FLOAT,false,0,verts);
glDrawArrays( GL_TRIANGLES, 0, vertexCount );
glDisableVertexAttribArray(spConstant->a("vertex"));
- glDrawArrays przykład 2
//Opis modelu
//Tablica współrzędnych wierzchołków
float verts[]={
0,4.08,0,1, 0,0,2.88,1,
0,4.08,0,1, 2.5,0,-1.44,1,
0,4.08,0,1, -2.5,0,-1.44,1,
0,0,2.88,1, -2.5,0,-1.44,1,
0,0,2.88,1, 2.5,0,-1.44,1,
-2.5,0,-1.44,1, 2.5,0,-1.44,1
};
int vertexCount=12;
///Kod rysujący
spConstant->use();
glUniformMatrix4fv(spConstant->u("P"),1,false,glm::value_ptr(P));
glUniformMatrix4fv(spConstant->u("V"),1,false,glm::value_ptr(V));
glUniformMatrix4fv(spConstant->u("M"),1,false,glm::value_ptr(M));
glUniform4f(spConstant->u("color"),0,1,0,1);
glEnableVertexAttribArray(spConstant->a("vertex"));
glVertexAttribPointer(spConstant->a("vertex"),4,GL_FLOAT,false,0,verts);
glDrawArrays( GL_LINES, 0, vertexCount );
glDisableVertexAttribArray(spConstant->a("vertex"));
- glDrawArrays przykład 3
//Opis modelu
//Tablica współrzędnych wierzchołków
float verts[]={
0,4.08,0,1, 0,0,2.88,1, -2.5,0,-1.44,1,
0,4.08,0,1, 0,0,2.88,1, 2.5,0,-1.44,1,
0,4.08,0,1, 2.5,0,-1.44,1, -2.5,0,-1.44,1,
2.5,0,-1.44,1, -2.5,0,-1.44,1, 0,0,2.88,1};
//Tablica kolorów wierzchołków
float colors[]={
1,0,0,1, 1,0,0,1, 1,0,0,1,
0,1,0,1, 0,1,0,1, 0,1,0,1,
0,0,1,1, 0,0,1,1, 0,0,1,1,
1,1,0,1, 1,1,0,1, 1,1,0,1};
int vertexCount=12;
//Kod rysujący
spColored->use();
glUniformMatrix4fv(spColored->u("P"),1,false,glm::value_ptr(P));
glUniformMatrix4fv(spColored->u("V"),1,false,glm::value_ptr(V));
glUniformMatrix4fv(spColored->u("M"),1,false,glm::value_ptr(M));
glEnableVertexAttribArray(spColored->a("vertex"));
glVertexAttribPointer(spColored->a("vertex"),4,GL_FLOAT,false,0,verts);
glEnableVertexAttribArray(spColored->a("color"));
glVertexAttribPointer(spColored->a("color"),4,GL_FLOAT,false,0,colors);
glDrawArrays( GL_TRIANGLES, 0, vertexCount );
glDisableVertexAttribArray(spColored->a("vertex"));
glDisableVertexAttribArray(spColored->a("color"));
- glDrawElements przykład
//Opis modelu
//Tablica współrzędnych wierzchołków
float verts[]={
0,4.08,0,1, 0,0,2.88,1, -2.5,0,-1.44,1, 2.5,0,-1.44,1};
//Tablica kolorów wierzchołków
float colors[]={
1,0,0,1, 0,1,0,1, 0,0,1,1, 1,1,0,1};
//Tablica indeksów
unsigned int indexes[] ={
0,1,2, 0,1,3, 0,2,3, 3,2,1};
int indexCount=12;
int vertexCount=4;
//Kod rysujący
spColored->use();
glUniformMatrix4fv(spColored->u("P"),1,false,glm::value_ptr(P));
glUniformMatrix4fv(spColored->u("V"),1,false,glm::value_ptr(V));
glUniformMatrix4fv(spColored->u("M"),1,false,glm::value_ptr(M));
glEnableVertexAttribArray(spColored->a("vertex"));
glVertexAttribPointer(spColored->a("vertex"),4,GL_FLOAT,false,0,verts);
glEnableVertexAttribArray(spColored->a("color"));
glVertexAttribPointer(spColored->a("color"),4,GL_FLOAT,false,0,colors);
glDrawElements(GL_TRIANGLES,indexCount, GL_UNSIGNED_INT,indexes);
glDisableVertexAttribArray(spColored->a("vertex"));
glDisableVertexAttribArray(spColored->a("color"));
- Wczytywanie tekstury
GLuint tex; //Uchwyt – deklaracja globalna
//Funkcja wczytująca teksturę
GLuint readTexture(char* filename) {
GLuint tex;
glActiveTexture(GL_TEXTURE0);
//Wczytanie do pamięci komputera
std::vector<unsigned char> image; //Alokuj wektor do wczytania obrazka
unsigned width, height; //Zmienne do których wczytamy wymiary obrazka
//Wczytaj obrazek
unsigned error = lodepng::decode(image, width, height, filename);
//Import do pamięci karty graficznej
glGenTextures(1,&tex); //Zainicjuj jeden uchwyt
glBindTexture(GL_TEXTURE_2D, tex); //Uaktywnij uchwyt
//Wczytaj obrazek do pamięci KG skojarzonej z uchwytem
glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, 0,
GL_RGBA, GL_UNSIGNED_BYTE, (unsigned char*) image.data());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
return tex;
}
//Wczytanie i import obrazka – w initOpenGLProgram
tex=readTexture("bricks.png");
//Usunięcie tekstury z pamięci karty graficznej – w freeOpenGLProgram
glDeleteTextures(1,&tex);
- Rysowanie oteksturowanego modelu
//Opis modelu
//Tablica współrzędnych wierzchołków
float verts[]={
1.0f,-1.0f,0.0f,1.0f, //A
-1.0f, 1.0f,0.0f,1.0f, //B
-1.0f,-1.0f,0.0f,1.0f, //C
1.0f,-1.0f,0.0f,1.0f, //A
1.0f, 1.0f,0.0f,1.0f, //D
-1.0f, 1.0f,0.0f,1.0f, //B
};
//Tablica współrzędnych teksturowania
float texCoords[]={
1.0f, 0.0f, //A
0.0f, 1.0f, //B
0.0f, 0.0f, //C
1.0f, 0.0f, //A
1.0f, 1.0f, //D
0.0f, 1.0f, //B
};
//Liczba wierzchołków w tablicy
int vertexCount=6;
//Kod rysujący
spTextured->use();
glUniformMatrix4fv(spTextured->u("P"),1,false,glm::value_ptr(P));
glUniformMatrix4fv(spTextured->u("V"),1,false,glm::value_ptr(V));
glUniformMatrix4fv(spTextured->u("M"),1,false,glm::value_ptr(M));
glEnableVertexAttribArray(spTextured->a("vertex"));
glVertexAttribPointer(spTextured->a("vertex"),4,GL_FLOAT,false,0,verts);
glEnableVertexAttribArray(spTextured->a("texCoord"));
glVertexAttribPointer(spTextured->a("texCoord"),2,GL_FLOAT,false,0,texCoords);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,tex);
glUniform1i(spLambertTextured->u("tex"),0);
glDrawArrays( GL_TRIANGLES, 0, vertexCount );
glDisableVertexAttribArray(spTextured->a("vertex"));
glDisableVertexAttribArray(spTextured->a("texCoord"));
- Rysowanie oteksturowanego i ocieniowanego modelu
//Opis modelu
//Tablica współrzędnych wierzchołków
float verts[]={
1.0f,-1.0f,0.0f,1.0f, //A
-1.0f, 1.0f,0.0f,1.0f, //B
-1.0f,-1.0f,0.0f,1.0f, //C
1.0f,-1.0f,0.0f,1.0f, //A
1.0f, 1.0f,0.0f,1.0f, //D
-1.0f, 1.0f,0.0f,1.0f}; //B
//Tablica współrzędnych teksturowania
float texCoords[]={
1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, //ABC
1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, //ADB
};
//Tablica wektorów normalnych
float normals[]={
0.0f,0.0f,1.0f,0.0f, //A
0.0f,0.0f,1.0f,0.0f, //B
0.0f,0.0f,1.0f,0.0f, //C
0.0f,0.0f,1.0f,0.0f, //A
0.0f,0.0f,1.0f,0.0f, //D
0.0f,0.0f,1.0f,0.0f, //B
};
//Liczba wierzchołków w tablicy
int vertexCount=6;
//Kod rysujący
glEnableVertexAttribArray(spLambertTextured->a("vertex"));
glVertexAttribPointer(spLambertTextured->a("vertex"),4,GL_FLOAT,false,0,verts);
glEnableVertexAttribArray(spLambertTextured->a("normal"));
glVertexAttribPointer(spLambertTextured->a("normal"),4,GL_FLOAT,false,0,normals);
glEnableVertexAttribArray(spLambertTextured->a("texCoord"));
glVertexAttribPointer(spLambertTextured->a("texCoord"),2,GL_FLOAT,false,0,texCoords);
glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D,tex);
glUniform1i(spLambertTextured->u("tex"),0);
glDrawArrays( GL_TRIANGLES, 0, vertexCount );
glDisableVertexAttribArray(spLambertTextured->a("vertex"));
glDisableVertexAttribArray(spLambertTextured->a("normal"));
glDisableVertexAttribArray(spLambertTextured->a("texCoord"));