C++ OpenGL Color at each Vertex not drawn -



C++ OpenGL Color at each Vertex not drawn -

i'm new opengl , @ moment i'm trying understand vao , vbo.

the vao collection of vbo.

each vbo attribute of object, coordinates of vertices of object, color @ each object's vertex, etc.

in code below, vertex struct define 2 properties of vertex, position , color of vertex. therefore, 2 vbos needed store these information.

my problem is: i'm able draw triangle on screen, color @ each vertex doesn't seem drawn. why happen?

#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, set resulting identifier in vertex buffer glgenbuffers(1, &vertexbuffer); glbindbuffer(gl_array_buffer, vertexbuffer); // give our vertices 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)); // newly created vao disables array access attributes // array access enabled binding vao in setupgeometry , 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 windowed mode window , opengl context */ glfwwindow* window; if( !glfwinit() ) { printf("failed start glfw\n"); exit( exit_failure ); } window = glfwcreatewindow(640, 480, "hello world", null, null); if (!window) { glfwterminate(); printf("glfw failed start\n"); homecoming -1; } /* create window's context current */ glfwmakecontextcurrent(window); // important: must done glew recognises opengl glfwwindowhint(glfw_samples, 4); int err = glewinit(); if (glewinit() != glew_ok) { /* problem: glewinit failed, wrong. */ fprintf(stderr, "error initializing glew: %s\n", glewgeterrorstring(err)); } fprintf(stderr, "glew done\n"); fprintf(stderr, "gl version info %s\n", glgetstring(gl_version)); glfwsetkeycallback(window, key_callback); setupgeometry(); while(!glfwwindowshouldclose(window)){ render(0); glfwswapbuffers(window); glfwpollevents(); } }

you don't seem have included shaders. you'll need utilize fragment shader set colour values triangle. this link should help you.

c++ opengl

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -