着色器代码
//顶点着色器
#version 330 core
layout(location=0) in vec3 aPos;//顶点布局
layout(location=1) in vec3 aColor;//颜色布局
layout(location=2) in vec2 aTexCoord;//纹理布局
uniform float xOffset;
out vec4 vertexColor;
out vec3 ourPosition;
out vec2 TexCoord;
void main()
{
gl_Position=vec4(aPos.x+xOffset,aPos.y,aPos.z,1.0f);//定点坐标
vertexColor=vec4(aColor,1.0f);//颜色
TexCoord=aTexCoord;//纹理
}
//片段着色器
#version 330 core
out vec4 FragColor;
in vec4 vertexColor;//
in vec3 ourPosition;
in vec2 TexCoord;
uniform sampler2D textureWall;
uniform sampler2D textureWall2;
void main()
{
FragColor=mix(texture(textureWall2,TexCoord),texture(textureWall,TexCoord),0.5)*vertexColor;
}
cpu中代码
#include "sunopengl.h"
#include
sunOpengl::sunOpengl(QWidget *parent)
{
}
unsigned int VBO,VAO;
float vertices[]={
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f,1.0f,1.0f,
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f,1.0f,0.0f,
-0.5f,-0.5f, 0.0f, 0.0f, 0.0f, 1.0f,0.0f,0.0f,
-0.5f,0.5f, 0.0f, 0.5f, 0.5f, 0.5f,0.0f,1.0f
};
unsigned int indices[]={
0,1,3,
1,2,3,
};
unsigned int EBO;
sunOpengl::~sunOpengl()
{
delete textureWall;
makeCurrent();
glDeleteVertexArrays(1,&VAO);
glDeleteBuffers(1,&VBO);
glDeleteBuffers(1,&EBO);
//glDeleteProgram(shaderProgram);
doneCurrent();
}
void sunOpengl::initializeGL()
{
this->initializeOpenGLFunctions();
shaderProgram.addShaderFromSourceFile(QOpenGLShader::Vertex,":/shades/shapes.vert");
shaderProgram.addShaderFromSourceFile(QOpenGLShader::Fragment,":/shades/shapes.frag");
bool success=shaderProgram.link();
if(!success)
{
qDebug()<bind(0);
textureWall2->bind(1);
if(!isDraw)
return;
glBindVertexArray(VAO);
// glDrawArrays(GL_TRIANGLES,0,6);
glDrawElements(ShapeType,6,GL_UNSIGNED_INT,0);
glBindVertexArray(0);
}
void sunOpengl::drawrectangle()
{
ShapeType=GL_TRIANGLES;
isDraw=true;
update();
}
void sunOpengl::setPloygonMode(bool isWire)
{
makeCurrent();
if(isWire)
{
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
}
else
{
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
}
doneCurrent();
update();
}
void sunOpengl::clear()
{
isDraw=false;
update();
}
//定点着色器-》几何着色-》图元装配-》光栅化-》片段着色器-》测试与混合