两个原始纹理
混合后的效果图
顶点shader
attribute vec3 pos;
attribute vec2 texcoord;
attribute vec3 normal;
uniform mat4 M;
uniform mat4 P;
uniform mat4 V;
varying vec2 V_Texcoord;
void main()
{
V_Texcoord=texcoord;
gl_Position=P*V*M*vec4(pos,1.0);
}
片元shader
varying vec2 V_Texcoord;
uniform sampler2D U_MainTexture;
uniform sampler2D U_Wood;
void main()
{
gl_FragColor=texture2D(U_MainTexture,V_Texcoord)*0.8+texture2D(U_Wood,V_Texcoord)*0.8;
}
程序入口
#include <windows.h>
#include "glew.h"
#include <stdio.h>
#include <math.h>
#include "utils.h"
#include "GPUProgram.h"
#include "ObjModel.h"
#include "Glm/glm.hpp"
#include "Glm/ext.hpp"
#pragma comment(lib,"opengl32.lib")
#pragma comment(lib,"glew32.lib")
LRESULT CALLBACK GLWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_CLOSE:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd,msg,wParam,lParam);
}
float* CreatePerspective(float fov, float aspect, float zNear, float zFar)
{
float *matrix = new float[16];
float half = fov / 2.0f;
float randiansOfHalf = (half / 180.0f)*3.14f;
float yscale = cosf(randiansOfHalf) / sinf(randiansOfHalf);
float xscale = yscale / aspect;
memset(matrix, 0, sizeof(float) * 16);
matrix[0] = xscale;
matrix[5] = yscale;
matrix[10] = (zNear + zFar) / (zNear - zFar);
matrix[11] = -1.0f;
matrix[14] = (2.0f*zNear*zFar) / (zNear - zFar);
return matrix;
}
INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
{
WNDCLASSEX wndClass;
wndClass.cbClsExtra = 0;
wndClass.cbSize = sizeof(WNDCLASSEX);
wndClass.cbWndExtra = 0;
wndClass.hbrBackground = NULL;
wndClass.hCursor = LoadCursor(NULL,IDC_ARROW);
wndClass.hIcon = NULL;
wndClass.hIconSm = NULL;
wndClass.hInstance = hInstance;
wndClass.lpfnWndProc=GLWindowProc;
wndClass.lpszClassName = L"OpenGL";
wndClass.lpszMenuName = NULL;
wndClass.style = CS_VREDRAW | CS_HREDRAW;
ATOM atom = RegisterClassEx(&wndClass);
HWND hwnd = CreateWindowEx(NULL, L"OpenGL", L"RenderWindow", WS_OVERLAPPEDWINDOW, 100, 100, 800, 600, NULL, NULL, hInstance, NULL);
HDC dc = GetDC(hwnd);
PIXELFORMATDESCRIPTOR pfd;
memset(&pfd, 0, sizeof(PI