我的代码中有一个非常奇怪的问题,添加了名为“coord”的vec2以包含纹理坐标,这些坐标应该传递到我的顶点着色器,传递到几何体着色器,最后传递到片段着色器,在那里它将用于纹理映射。但出于某种原因,GLSL编译器正在优化vec2输入,使用
glVertexAttribPointer
若要为其指定网络无结果,请使用
glGetUniformLocation(gShaderProgram, "coord");
返回-1,这表示GLSL编译器已经对其进行了优化,为什么?据我所知,编译器只会优化那些对着色器输出没有贡献的值,但会在每个着色器阶段将其用作输出。这是一项大学作业,所以这对我来说几乎是全新的。
顶点著色引擎
layout(location = 0) in vec3 vertex_position;
layout(location = 1) in vec3 vertex_color;
layout(location = 2) in vec2 coord;
layout(location = 20) uniform vec4 test;
layout(binding = 3, std140) uniform uniformBlock
{
float v1;
float v2;
float v3;
float v4;
};
uniform mat4 m;
uniform mat4 p;
uniform mat4 v;
out vData
{
mat4 m;
mat4 p;
mat4 v;
vec4 test;
vec4 color;
vec2 tex_coord;
}vertices;
void main() {
vertices.m = m;
vertices.p = p;
vertices.v = v;
vertices.test = test;
vertices.tex_coord = coord;
vertices.color = vec4(vertex_color, 1.0);
gl_Position = vec4(vertex_position, 1.0);
}
几何著色器
layout (triangles) in;
layout (triangle_strip, max_vertices = 6) out;
in vData
{
mat4 m;
mat4 p;
mat4 v;
vec4 test;
vec4 color;
vec2 tex_coord;
} vertices[];
out fData
{
vec3 normal;
vec4 color;
mat4 v;
vec2 tex_coord;
} frag;
void main()
{
vec3 A = gl_in[2].gl_Position.xyz - gl_in[0].gl_Position.xyz;
vec3 B = gl_in[1].gl_Position.xyz - gl_in[0].gl_Position.xyz;
vec3 normal = normalize(cross(A,B));
int i;
for(i = 0;i < gl_in.length();i++)
{
gl_Position = (vertices[i].p * vertices[i].v * vertices[i].m) * gl_in[i].gl_Position;
frag.normal = normalize(vec3(vertices[i].m * vec4(normal,0)));
frag.color = vertices[i].color;
frag.v = vertices[i].v;
frag.tex_coord = vertices[i].tex_coord;
EmitVertex();
}
EndPrimitive();
for(i = 0;i < gl_in.length();i++)
{
gl_Position = vertices[i].p * vertices[i].v * vertices[i].m * (gl_in[i].gl_Position + vec4(normal,0));
frag.normal = normalize(vec3(vertices[i].m * vec4(normal,0)));
frag.color = vertices[i].color;
frag.v = vertices[i].v;
frag.tex_coord = vertices[i].tex_coord;
EmitVertex();
}
EndPrimitive();
}
片段着色器
#version 400
out vec4 fragment_color;
const float PI = 3.14159265;
uniform sampler2D texture0;
in fData
{
vec3 normal;
vec4 color;
mat4 v;
vec2 tex_coord;
} frag;
void main () {
vec3 n = normalize(frag.normal);
float intensity = min(max(dot(n, vec3(0,0,-1)), 0.0), 1.0);
fragment_color = texture2D(texture0, frag.tex_coord.st);
}
主要的cpp公司
#include <vector>
#include <windows.h>
#include <iostream>
#include <string>
#include <fstream>
#include <streambuf>
#include <chrono>
#include <gl/glew.h>
#include <gl/GL.h>
# define M_PI 3.14159265358979323846
#include "glm\glm.hpp"
#include "glm\gtc\matrix_transform.hpp"
#include "bth_image.h"
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glew32.lib")
using namespace std;
using namespace glm;
HWND InitWindow(HINSTANCE hInstance);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HGLRC CreateOpenGLContext(HWND wndHandle);
GLuint gVertexBuffer = 0;
GLuint gVertexAttribute = 0;
GLuint gShaderProgram = 0;
GLuint textures[1];
mat4x4 view;
mat4x4 world;
mat4x4 projection;
float DT;
struct CPUvalues
{
float v1;
float v2;
float v3;
float v4;
};
CPUvalues Gv = { 0.5, 0, 0, 0 };
GLuint gu = 0;
#define BUFFER_OFFSET(i) ((char *)nullptr + (i))
void CreateShaders()
{
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
ifstream shaderFile("VertexShader.glsl");
std::string shaderText((std::istreambuf_iterator<char>(shaderFile)), std::istreambuf_iterator<char>());
shaderFile.close();
const char* shaderTextPtr = shaderText.c_str();
glShaderSource(vs, 1, &shaderTextPtr, nullptr);
glCompileShader(vs);
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
shaderFile.open("Fragment.glsl");
shaderText.assign((std::istreambuf_iterator<char>(shaderFile)), std::istreambuf_iterator<char>());
shaderFile.close();
shaderTextPtr = shaderText.c_str();
glShaderSource(fs, 1, &shaderTextPtr, nullptr);
glCompileShader(fs);
GLuint gs = glCreateShader(GL_GEOMETRY_SHADER);
shaderFile.open("GMshader.glsl");
shaderText.assign((std::istreambuf_iterator<char>(shaderFile)), std::istreambuf_iterator<char>());
shaderFile.close();
shaderTextPtr = shaderText.c_str();
glShaderSource(gs, 1, &shaderTextPtr, nullptr);
glCompileShader(gs);
GLint success = 0;
glGetShaderiv(gs, GL_COMPILE_STATUS, &success);
if (success == GL_FALSE)
{
GLint logSize = 0;
glGetShaderiv(gs, GL_INFO_LOG_LENGTH, &logSize);
std::vector<GLchar> errorLog(logSize);
glGetShaderInfoLog(gs, logSize, &logSize, &errorLog[0]);
for (int i = 0; i < errorLog.size(); i++)
{
cout << errorLog.at(i);
}
}
gShaderProgram = glCreateProgram();
glAttachShader(gShaderProgram, fs);
glAttachShader(gShaderProgram, gs);
glAttachShader(gShaderProgram, vs);
glBindAttribLocation(gShaderProgram, 1, "vertices");
glEnable(GL_TEXTURE_2D);
glGenTextures(1, textures);
glBindTexture(GL_TEXTURE_2D, textures[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, BTH_IMAGE_WIDTH, BTH_IMAGE_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*)BTH_IMAGE_DATA);
glLinkProgram(gShaderProgram);
GLint isLinked = 0;
glGetProgramiv(gShaderProgram, GL_LINK_STATUS, &isLinked);
if (isLinked == GL_FALSE)
{
GLint maxLength = 0;
glGetProgramiv(gShaderProgram, GL_INFO_LOG_LENGTH, &maxLength);
std::vector<GLchar> infoLog(maxLength);
glGetProgramInfoLog(gShaderProgram, maxLength, &maxLength, &infoLog[0]);
for (GLint i = 0; i < maxLength; i++)
{
cout << infoLog.at(i);
}
}
}
void CreateTriangleData()
{
struct TriangleVertex
{
float x, y, z;
float r, g, b;
float s, t;
};
TriangleVertex triangleVertices[6] =
{
{ -0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.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, 1.0f, 0.0f },
{ 0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f },
{ 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f },
{ -0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f }
};
glGenVertexArrays(1, &gVertexAttribute);
glBindVertexArray(gVertexAttribute);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glGenBuffers(1, &gVertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, gVertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(triangleVertices), triangleVertices, GL_STATIC_DRAW);
GLint vertexPos = glGetAttribLocation(gShaderProgram, "vertex_position");
glVertexAttribPointer(vertexPos, 3, GL_FLOAT, GL_FALSE, sizeof(TriangleVertex), BUFFER_OFFSET(0));
GLint vertexColor = glGetAttribLocation(gShaderProgram, "vertex_color");
cout << "vertex pos " << vertexPos << endl;
cout << "vertex color " << vertexColor << endl;
glVertexAttribPointer(vertexColor, 3, GL_FLOAT, GL_FALSE, sizeof(TriangleVertex), BUFFER_OFFSET(sizeof(float)*3));
cout << glGetError() << endl;
GLint tex_coord = glGetUniformLocation(gShaderProgram, "coord");
cout << "coord " << tex_coord << endl;
glVertexAttribPointer(tex_coord, 2, GL_FLOAT, GL_FALSE, sizeof(TriangleVertex), BUFFER_OFFSET(sizeof(float) * 6));
cout << glGetError() << endl;
}
void SetViewport()
{
glViewport(0, 0, 640, 480);
}
void Render()
{
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(gShaderProgram);
glBindVertexArray(gVertexAttribute);
glGenBuffers(1, &gu);
glBindBuffer(GL_UNIFORM_BUFFER, gu);
glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(CPUvalues), &Gv);
GLint OG_loc = glGetUniformLocation(gShaderProgram, "test");
GLint _p = glGetUniformLocation(gShaderProgram, "p");
GLint _m = glGetUniformLocation(gShaderProgram, "m");
GLint _v = glGetUniformLocation(gShaderProgram, "v");
GLuint unit = 0;
GLint texture0 = glGetUniformLocation(gShaderProgram, "texture0");
glActiveTexture(GL_TEXTURE0 + unit);
glBindTexture(GL_TEXTURE_2D, textures[0]);
glUniform1i(texture0, unit);
glUniformMatrix4fv(_p, 1, GL_FALSE, &projection[0][0]);
glUniformMatrix4fv(_m, 1, GL_FALSE, &world[0][0]);
glUniformMatrix4fv(_v, 1, GL_FALSE, &view[0][0]);
glUniform4f(OG_loc, Gv.v1, Gv.v2, Gv.v3, Gv.v4);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glFrontFace(GL_CW);
glCullFace(GL_BACK);
glDrawArrays(GL_TRIANGLES, 0, 6);
}
int WINAPI wWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow )
{
AllocConsole();
freopen("CONOUT$", "w", stdout);
DT = 0.016;
MSG msg = { 0 };
HWND wndHandle = InitWindow(hInstance);
if (wndHandle)
{
HDC hDC = GetDC(wndHandle);
HGLRC hRC = CreateOpenGLContext(wndHandle);
glewInit();
SetViewport();
CreateShaders();
CreateTriangleData();
ShowWindow(wndHandle, nCmdShow);
view = lookAt(vec3(0, 0, -2), vec3(0, 0, 0), vec3(0, 1, 0));
mat4x4 sm;
sm = scale(sm, vec3(1, 1, 1));
mat4x4 tm;
tm = translate(tm, vec3(0, 0, 0));
mat4x4 rm;
rm = mat4x4(1);
projection = perspective<float>(M_PI*0.45, 640 / 480, 0.1, 20);
while (WM_QUIT != msg.message)
{
auto start_time = chrono::high_resolution_clock::now();
Gv.v1 += 1 * DT;
world = tm * rotate(rm, Gv.v1, vec3(0, 1, 0)) * sm;
if (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
Render();
SwapBuffers(hDC);
}
auto final_time = chrono::high_resolution_clock::now() - start_time;
DT = chrono::duration_cast<std::chrono::milliseconds>(final_time).count() / (double)1000;
}
wglMakeCurrent(NULL, NULL);
ReleaseDC(wndHandle, hDC);
wglDeleteContext(hRC);
DestroyWindow(wndHandle);
}
return (int) msg.wParam;
}
HWND InitWindow(HINSTANCE hInstance)
{
WNDCLASSEX wcex = { 0 };
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.hInstance = hInstance;
wcex.lpszClassName = L"BTH_GL_DEMO";
if( !RegisterClassEx(&wcex) )
return false;
RECT rc = { 0, 0, 640, 480 };
AdjustWindowRect( &rc, WS_OVERLAPPEDWINDOW, FALSE );
HWND handle = CreateWindow(
L"BTH_GL_DEMO",
L"BTH OpenGL Demo",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
rc.right - rc.left,
rc.bottom - rc.top,
nullptr,
nullptr,
hInstance,
nullptr);
return handle;
}
LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
HGLRC CreateOpenGLContext(HWND wndHandle)
{
HDC hDC = GetDC(wndHandle);
static PIXELFORMATDESCRIPTOR pixelFormatDesc =
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
32,
0, 0, 0, 0, 0, 0,
0,
0,
0,
0, 0, 0, 0,
0,
0,
0,
PFD_MAIN_PLANE,
0,
0, 0, 0
};
int pixelFormat = ChoosePixelFormat(hDC, &pixelFormatDesc);
SetPixelFormat(hDC, pixelFormat, &pixelFormatDesc);
HGLRC hRC = wglCreateContext(hDC);
wglMakeCurrent(hDC, hRC);
return hRC;
}