我使用freeglut,GLEW 和魔鬼,以呈现纹理的茶壶使用顶点和片段着色器.这在Ubuntu 14.04上的OpenGL 2.0和GLSL 1.2中都运行良好.
现在,我想将凹凸贴图应用于茶壶.我的讲师显然不会酿造他自己的茶,所以不知道他们应该是顺利的.无论如何,我找到了一个关于老式凹凸贴图的漂亮教程,其中包括一个片段着色器,它开始于:
uniform sampler2D DecalTex; //The texture
uniform sampler2D BumpTex; //The bump-map
Run Code Online (Sandbox Code Playgroud)
他们没有提到的是如何将两个纹理首先传递给着色器.
以前我
//OpenGL cpp file
glBindTexture(GL_TEXTURE_2D, textureHandle);
//Vertex shader
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
//Fragment shader
gl_FragColor = color * texture2D(DecalTex,gl_TexCoord[0].xy);
Run Code Online (Sandbox Code Playgroud)
所以现在我
//OpenGL cpp file
glBindTexture(GL_TEXTURE_2D, textureHandle);
glBindTexture(GL_TEXTURE_2D, bumpHandle);
//Vertex shader
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1;
//Fragment shader
gl_FragColor = color * texture2D(BumpTex,gl_TexCoord[0].xy);
//no bump logic yet, just testing I …Run Code Online (Sandbox Code Playgroud) 我在这个例子中添加了一个顶点着色器:
void main()
{
gl_Position = ftransform();
}
Run Code Online (Sandbox Code Playgroud)
然后我得到这个图像:

我在这做错了什么?
我很难理解我的OpenGL应用程序和我的GLSL文件(.vert和.frag)之间的联系.我知道如何创建顶点或片段着色器,网上有很多例子,但我在我的应用程序中实际使用着色器时遇到了困难,特别是将(绑定?)纹理链接到我的着色器.我的问题是,我错过了哪些代码使用我使用着色器上传到我的应用程序中的纹理?
我当前的应用程序创建一个窗口,加载4个纹理(岩石,草,石头和混合图,因为我想用它来进行纹理喷溅),然后在主循环中绘制一个四边形.我的整个应用程序代码如下:
#include <windows.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <SFML/Graphics.hpp>
#include <glew.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include "textfile.h"
#include "textfile.cpp"
using namespace std;
class Scene {
public:
void resize( int w, int h ) {
// OpenGL Reshape
glViewport( 0, 0, w, h );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 120.0, (GLdouble)w/(GLdouble)h, 0.5, 500.0 );
glMatrixMode( GL_MODELVIEW );
}
};
///Shader
GLuint p, f, v;
void setShader() {
char *vs,*fs;
v = glCreateShader(GL_VERTEX_SHADER);
f = glCreateShader(GL_FRAGMENT_SHADER);
vs = textFileRead("shader.vert"); …Run Code Online (Sandbox Code Playgroud)