我创建了这样的纹理:
GLuint tex;
unsigned char data[12] = {255, 255, 255,
255, 255, 255,
255, 255, 255,
255, 255, 255};
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Run Code Online (Sandbox Code Playgroud)
我得到它的位置:
GLuint texloc = glGetUniformLocation(program, "tex_map");
Run Code Online (Sandbox Code Playgroud)
然后在我的绘图循环中我有:
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex);
glUniform1i(texloc, 0);
Run Code Online (Sandbox Code Playgroud)
然后在我的片段着色器中,我有:
out vec3 color;
uniform sampler2D tex_map;
void main()
{
.
.
color = texture(tex_map, vec2(-0.1, -0.5));
}
Run Code Online (Sandbox Code Playgroud)
有时当我运行程序时,我绘制的对象变成随机的非白色,通常是红色.我以为我是通过使用来处理出界的情况GL_CLAMP_TO_EDGE.是什么导致了这种行为?
编辑:当vec2(_, _) …
嗨,我正在尝试在矩形上渲染纹理。我正在使用GL_CLAMP_TO_BORDER,因为我不希望纹理重复出现。
glTextureParameteri(id, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTextureParameteri(id, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
Run Code Online (Sandbox Code Playgroud)
如果启用混合功能,则在第2条中提到的区域应该可以解决,但是我没有获得第1区域的任何解决方案。我知道我没有共享任何代码,因为我确实不能共享,是否还需要其他gl调用来解决问题?
在我的着色器中,我喜欢使用这样的语法:
layout (location = 0) in vec3 aPos;
所以,我可以只使用索引0中glVertexAttribPointer和这样的,节省了努力glGetAttribLocation通话。我想对uniform价值观做同样的事情,但如果我这样做
layout (location = 2) uniform float offset;
Run Code Online (Sandbox Code Playgroud)
我的顶点着色器无法编译。有没有办法实现相同的行为而不使用glGetUniformLocation?
我正在使用带有SDL2的OpenGL 4.4.我试图渲染一个带有顶点(-1,-1,0),(1,-1,0),(0,1,0)的简单三角形.但是,当我认为我正确地做了一切时,没有任何东西被画出来.
我从项目中提取并重新组织了相关代码:
#include <cerrno>
#include <cstring>
#include <exception>
#include <fstream>
#include <iostream>
#include <string>
#include <GL/glew.h>
#include <GL/glu.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>
void init();
void cleanUp();
std::string loadShader(std::string filepath);
void checkShaderSuccess(GLuint shader);
SDL_Window* win;
SDL_GLContext glContext;
GLuint program, vertShader, fragShader, vao, vbo;
class GenError: public std::exception {
public:
GenError():
exception(), msg("") {}
GenError(const std::string& m):
exception(), msg(m) {}
virtual ~GenError() throw() {}
virtual const char* what() const throw() {
return msg.c_str();
}
private:
std::string msg;
};
int main() { …Run Code Online (Sandbox Code Playgroud)