源码效果
C++源码
纹理图片
需下载stb_image.h这个解码图片的库,该库只有一个头文件。
具体代码:
vertexShader.glsl
#version 330 core
layout(location = 0) in vec3 aPos;
layout(location = 1) in vec3 aColor;
layout(location = 2) in vec2 aUV;
out vec4 outColor;
out vec2 outUV;
void main()
{
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
outColor = vec4(aColor, 1.0);
outUV = aUV;
}
fragmentShader.glsl
#version 330 core
out vec4 FragColor;
in vec4 outColor;
in vec2 outUV;
uniform sampler2D ourTexture;
void main()
{
// 使用色彩填充
// FragColor = outColor;
// 使用图片纹理
//FragColor = texture(ourTexture, outUV);
// 使用图片纹理及色彩混合
FragColor = texture(ourTexture, outUV)*outColor;
}
main.c
#include "OpenGLClass.h"
int main()
{
OpenGLClass opengl;
return 0;
}
ffImage.h
#pragma once
#include "Global.h"
class ffImage
{
private:
int m_width, m_height, m_picType;
ffRGBA *m_data;
public:
int getWidth()const;
int getHeight()const;
int getPicType()const;
ffRGBA *getData()const;
ffRGBA getColor(int x, int y)const;
ffImage(int _width = 0, int _height = 0, int _picType = 0, ffRGBA *_data = nullptr);
~ffImage();
static ffImage *readFromFile(const char *_file_Name);
};
ffImage.cpp
#include "ffImage.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
int ffImage::getWidth() const
{
return m_width;
}
int ffImage::getHeight() const
{
return m_height;
}
int ffImage::getPicType() const
{
return m_picType;
}
ffRGBA *ffImage::getData() const
{
return m_data;
}
ffRGBA ffImage::getColor(int x, int y) const
{
if (x<0 || x>m_width - 1 || y<0 || y>m_height - 1) {
return ffRGBA(0, 0, 0, 0); }
return m_data[y*m_width + x];
}
ffImage::ffImage(int _width, int _height, int _picType, ffRGBA *_data)
{
m_width = _width;
m_height = _height;
m_picType = _picType;
int _sumSize = m_width * m_height;
if (_data && _sumSize)
{
m_data = new ffRGBA[_sumSize];
memcpy(m_data, _data, sizeof(ffRGBA)*_sumSize);
}
else