在我的OpenGL旅程中,當我遇到這個問題時,我試圖製作一個紋理立方體: 只有部分我的立方體被渲染。應該有12個三角形,但只有3個正在渲染。我看過很多教程,但似乎無法找到我們的代碼之間的問題/主要區別。這裏是我的:C++ OpenGL紋理立方體缺失三角形
...
int main(int argc, char* argv[]) {
if (!setupGLFW()) return 1;
setupApple();
glfwWindowHint(GLFW_SAMPLES, 4);
GLFWwindow* window = glfwCreateWindow(WIN_WIDTH, WIN_HEIGHT, "Textured Cube", NULL, NULL);
if (!window) {
log_msg(LOG_ERROR, "Could not open a GLFW3 window!\n");
return 1;
}
glfwMakeContextCurrent(window);
if (!setupGLEW()) return 1;
glClearColor(0.5, 0.5, 0.5, 1.0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
static const int vertices = 12 * 3;
static const GLfloat points[] = {
-1.0, -1.0, -1.0,
-1.0, -1.0, 1.0,
-1.0, 1.0, 1.0,
1.0, 1.0, -1.0,
-1.0, -1.0, -1.0,
-1.0, 1.0, -1.0,
1.0, -1.0, 1.0,
-1.0, -1.0, -1.0,
1.0, -1.0, -1.0,
1.0, 1.0, -1.0,
1.0, -1.0, -1.0,
-1.0, -1.0, -1.0,
-1.0, -1.0, -1.0,
-1.0, 1.0, 1.0,
-1.0, 1.0, -1.0,
1.0, -1.0, 1.0,
-1.0, -1.0, 1.0,
-1.0, -1.0, -1.0,
-1.0, 1.0, 1.0,
-1.0, -1.0, 1.0,
1.0, -1.0, 1.0,
1.0, 1.0, 1.0,
1.0, -1.0, -1.0,
1.0, 1.0, -1.0,
1.0, -1.0, -1.0,
1.0, 1.0, 1.0,
1.0, -1.0, 1.0,
1.0, 1.0, 1.0,
1.0, 1.0, -1.0,
-1.0, 1.0, -1.0,
1.0, 1.0, 1.0,
-1.0, 1.0, -1.0,
-1.0, 1.0, 1.0,
1.0, 1.0, 1.0,
-1.0, 1.0, 1.0,
1.0, -1.0, 1.0
};
static const GLfloat textureCoords[] = {
0, 0,
1, 0,
0, 1,
0, 1,
1, 0,
1, 1,
0, 0,
1, 0,
0, 1,
0, 1,
1, 0,
1, 1,
0, 0,
1, 0,
0, 1,
0, 1,
1, 0,
1, 1,
0, 0,
1, 0,
0, 1,
0, 1,
1, 0,
1, 1,
0, 0,
1, 0,
0, 1,
0, 1,
1, 0,
1, 1,
0, 0,
1, 0,
0, 1,
0, 1,
1, 0,
1, 1,
};
GLuint pointBuffer;
glGenBuffers(1, &pointBuffer);
glBindBuffer(GL_ARRAY_BUFFER, pointBuffer);
glBufferData(GL_ARRAY_BUFFER, vertices * 3, points, GL_STATIC_DRAW);
GLuint textureBuffer;
glGenBuffers(1, &textureBuffer);
glBindBuffer(GL_ARRAY_BUFFER, textureBuffer);
glBufferData(GL_ARRAY_BUFFER, vertices * 2, textureCoords, GL_STATIC_DRAW);
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, pointBuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glBindBuffer(GL_ARRAY_BUFFER, textureBuffer);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
GLuint program = getProgramFromFiles("text_cube.vs.glsl", "text_cube.fs.glsl");
glm::mat4 MVP = calculateMVP();
GLuint texture = loadBMP("uvtemplate.bmp");
if (!program) {
log_msg(LOG_ERROR, "There was a problem opening shader.\n");
// clean up
return 1;
}
if (!texture) {
log_msg(LOG_ERROR, "There was a problem opening shader.\n");
// clean up
return 1;
}
glUseProgram(program);
GLuint MVPID = glGetUniformLocation(program, "MVP");
GLuint textureID = glGetUniformLocation(program, "cube_texture");
glUniformMatrix4fv(MVPID, 1, GL_FALSE, &MVP[0][0]);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glUniform1i(textureID, 0);
while (!glfwWindowShouldClose(window) && glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, vertices);
glfwPollEvents();
glfwSwapBuffers(window);
}
// clean up
return 0;
}
只要你知道,錯誤的是不與setupGLFW()
,setupApple()
,setupGLEW()
,getProgramFromFiles()
,calculateMVP()
,loadBMP()
,或者我的頂點和片段着色器,這是在不同的文件。編譯沒有問題,因爲我有GLFW
和GLEW
從myglutils.h
這是uvtemplate.bmp
,其512×512的圖像:
如果你們能幫助我,那將是極大的讚賞。謝謝!
哇。我不相信我犯了這個錯誤!我已經爲OpenGL中的所有其他程序完成了這個任務,我不知道爲什麼我的大腦在那裏滑落。非常感謝! – Jerfov2