我想在OpenGL中呈現棋盤,但是我得到和輸出不正確。 [棋盤] http://s9.postimg.org/g3wk1py4f/large_checkerboard.png「大棋盤」不能使用像素精度的OpenGL(不使用固定功能管道)
我試圖縮小這個問題,我注意到當我嘗試繪製11乘11網格時,我只在垂直方向上獲得10個像素[小棋盤] http://s12.postimg.org/olu7575jd/small_checkerboard.png「小棋盤」
紋理本身是確定的。我懷疑問題是頂點座標。下面的代碼:
紋理製作:
const int size = 11;
unsigned char buffer[size * size * 4];
for (size_t i = 0; i < size; i++)
{
for (size_t j = 0; j < size; j++)
{
unsigned char rgb = i % 2 == 1 || j % 2 == 1 ? 255 : 0;
int index = (i * size + j) * 4;
buffer[index] = rgb;
buffer[index + 1] = rgb;
buffer[index + 2] = rgb;
buffer[index + 3] = 255;
}
}
紋理參數:
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
頂點:(屏幕尺寸爲1024 * 768,我畫不右上角季度的任何轉換)
float w = size/512.0f;
float h = size/384.0f;
GLfloat vertices[] =
{
0.0f, h, 0.0f, 1.0f,
0.0f, 0.0f, 0.0f, 0.0f,
w, h, 1.0f, 1.0f,
w, 0.0f, 1.0f, 0.0f
};
int positionLocation = glGetAttribLocation(program.getId(), "a_position");
int textureLocation = glGetAttribLocation(program.getId(), "a_texCoord");
glEnableVertexAttribArray(positionLocation);
glVertexAttribPointer(positionLocation, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), vertices);
glEnableVertexAttribArray(textureLocation);
glVertexAttribPointer(textureLocation, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), vertices + 2);
glUniform1i(textureLocation, 0);
圖紙:
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
和着色器:
const char* VertexShaderCode =
"attribute vec4 a_position; \n"
"attribute vec2 a_texCoord; \n"
"varying vec2 v_texCoord; \n"
"void main() \n"
"{ \n"
" gl_Position = a_position; \n"
" v_texCoord = a_texCoord; \n"
"}";
const char* FragmentShaderCode =
"uniform sampler2D s_texture; \n"
"varying vec2 v_texCoord; \n"
"void main() \n"
"{ \n"
" gl_FragColor = texture2D(s_texture, v_texCoord); \n"
"}";
此外,您將'vec2'傳遞給'a_position',但它在着色器中聲明爲'vec4'。另外,不應該檢查模式爲'(i%2!= j%2)? 255:0;'? – PeterT
那大概不應該叫做棋盤。我只是使用這種模式來驗證紋理映射。 –
[opengl中完美(3D)紋理映射的可能重複](http://stackoverflow.com/questions/17422689/perfect-3d-texture-mapping-in-opengl) –