未绘制每个顶点处的C++OpenGL颜色
C++ OpenGL Color at each Vertex not drawn
我是OpenGL的新手,目前我正在努力理解VAO和VBO。
VAO只是VBO的集合。
每个VBO都是对象的属性、对象顶点的坐标、每个对象顶点的颜色等。
在下面的代码中,"顶点"结构定义了顶点的两个属性,即顶点的位置和颜色。因此,需要2个VBO来存储这些信息。
我的问题是:我可以在屏幕上画三角形,但每个顶点的颜色似乎都没有画出来。为什么会发生这种情况?
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
// create vertex buffer
GLuint vertexbuffer;
struct Vertex{
GLdouble position[3];
GLfloat color[3];
};
static const GLfloat vertex_data[] = {
-0.7f, -0.7f, 0.0f,
0.7f, -0.7f, 0.0f,
0.0f, 1.0f, 0.0f
};
void SetupGeometry(){
const struct Vertex triangle[3] = {
{{-0.7, -0.7, 0.0}, {1.0f, 0.0f, 0.0f}},
{{0.7, -0.7, 0.0}, {0.0f, 1.0f, 0.0f}},
{{0.0, 1.0, 0.0}, {0.0f, 0.0f, 1.0f}}
};
//GLuint VertexArrayID;
//glGenVertexArrays(1, &VertexArrayID);
//glBindVertexArray(VertexArrayID);
//generate 1 buffer, put the resulting identifier in vertex buffer
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
// give our vertices to OpenGL
glBufferData(GL_ARRAY_BUFFER, 3*sizeof(struct Vertex), triangle, GL_STATIC_DRAW);
// GLuint index, GLuint size, GLenum type, GLboolean normalized, GLsizei stride, const void *offset
glVertexAttribPointer(0, 3, GL_DOUBLE, GL_FALSE, sizeof(struct Vertex), (void*) offsetof(struct Vertex, position));
// any newly created VAO disables array access for all attributes
// array access is enabled by binding the VAO in SetupGeometry and calling:
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(struct Vertex), (void*) offsetof(struct Vertex, color));
glEnableVertexAttribArray(1);
}
void SetupShaders(void){
}
void Render(int i){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawArrays(GL_LINE_LOOP, 0, 3);
}
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods){
if((key == GLFW_KEY_ESCAPE || key == GLFW_KEY_Q) && action != GLFW_PRESS){
glfwSetWindowShouldClose(window, GL_TRUE);
}
}
int main( void ) {
/* Create a windowed mode window and its OpenGL context */
GLFWwindow* window;
if( !glfwInit() ) {
printf("Failed to start GLFWn");
exit( EXIT_FAILURE );
}
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window) {
glfwTerminate();
printf("GLFW Failed to startn");
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window); // IMPORTANT: Must be done so glew recognises OpenGL
glfwWindowHint(GLFW_SAMPLES, 4);
int err = glewInit();
if (glewInit() != GLEW_OK) {
/* Problem: glewInit failed, something is seriously wrong. */
fprintf(stderr, "Error initializing GLEW: %sn", glewGetErrorString(err));
}
fprintf(stderr, "Glew donen");
fprintf(stderr, "GL VERSION INFO %sn", glGetString(GL_VERSION));
glfwSetKeyCallback(window, key_callback);
SetupGeometry();
while(!glfwWindowShouldClose(window)){
Render(0);
glfwSwapBuffers(window);
glfwPollEvents();
}
}
您似乎没有包含任何着色器。您需要使用片段着色器来设置三角形的颜色值。这个链接应该会对你有所帮助。
相关文章:
- 使用 OpenGL 4.5 更改所选顶点的颜色
- 为什么我的 LEGACY OPENGL 颜色反转了?
- 如何在 openGL-2D 中绘制不同颜色的线条?
- 从OpenGL应用程序中提取颜色/深度缓冲区
- OpenGL 不渲染颜色
- 在 OpenGL 中将深度映射到颜色
- Opengl 3+ 绘制不同颜色的线条
- 将 OpenGL 颜色附件存储在 constexpr GLenum 数组中
- 获取 OpenGL 纹理中特定像素的颜色
- OpenGL:正在读取颜色缓冲区
- 旋转三角形并在opengl中改变其颜色
- OpenGL不呈现颜色
- 每帧更改顶点的颜色 (OpenGL)
- OpenGL改变生成的纹理的颜色
- 未绘制每个顶点处的C++OpenGL颜色
- SDL2 OpenGL 4.2上下文,具有0个颜色位和0个深度位
- 如何使用glut和openGL更改颜色
- 绘制具有不同颜色顶点的OpenGL颜色矩形
- OpenGL颜色转换
- OpenGL颜色缓冲每帧添加太多的帧时间