void Mesh::build() { GLuint points_vbo = create_vbo(vp, point_count * 3); GLuint normals_vbo = create_vbo(vn, point_count * 3); GLuint text_coord_vbo = create_vbo(vt, point_count * 2); vao = create_vao({ points_vbo, normals_vbo, text_coord_vbo }); model_uniform = Uniform::createUniformMatrix4fv("model", model.m); }
Shape create_rounded_rectangle(CSize size, float radius, int round_count, const vec4 color) { int real_vertex_count = size_of_rounded_rectangle_in_vertices(round_count); Params params = default_params(); params.const_params.datasize = sizeof(CPoint)*real_vertex_count*2; params.const_params.round_count=round_count; params.const_params.triangle_mode = GL_TRIANGLE_FAN; params.var_params.size=size; params.var_params.radius=radius; CPoint *data = malloc(params.const_params.datasize); gen_rounded_rectangle(data, params.var_params.size, params.var_params.radius, params.const_params.round_count); /* char str[150]; sprintf(str, "rounded_rectangle_data_size(%d) = %d", real_vertex_count, rounded_rectangle_data_size(real_vertex_count)); DEBUG_LOG_WRITE_D("fps>",str); */ return (Shape) { {color[0], color[1], color[2], color[3]}, data, create_vbo(params.const_params.datasize, data, GL_DYNAMIC_DRAW), real_vertex_count, params }; }
void init_gl(graphics_context_t *gc) { init_shader(gc, VShader1, FShader1); gc->vbo_pos = create_vbo(GL_ARRAY_BUFFER, vtx_model, sizeof(vtx_model)); gc->vbo_nor = create_vbo(GL_ARRAY_BUFFER, nor_model, sizeof(nor_model)); gc->vbo_tex = create_vbo(GL_ARRAY_BUFFER, tex_model, sizeof(tex_model)); gc->vbo_ind = create_vbo(GL_ELEMENT_ARRAY_BUFFER, ind_model, sizeof(ind_model)); gc->vloc_pos = glGetAttribLocation(gc->program, "pos"); gc->vloc_nor = glGetAttribLocation(gc->program, "nor"); gc->vloc_tex = glGetAttribLocation(gc->program, "tex"); gc->uloc_model = glGetUniformLocation(gc->program, "model_mat"); gc->uloc_nor = glGetUniformLocation(gc->program, "nor_mat"); gc->uloc_light = glGetUniformLocation(gc->program, "light_pos"); glBindBuffer(GL_ARRAY_BUFFER, gc->vbo_pos); glVertexAttribPointer(gc->vloc_pos, g_vsize_pos, GL_FLOAT, GL_FALSE, g_stride_pos, (GLfloat *)0); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, gc->vbo_nor); glVertexAttribPointer(gc->vloc_nor, g_vsize_nor, GL_FLOAT, GL_FALSE, g_stride_nor, (GLfloat *)0); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, gc->vbo_tex); glVertexAttribPointer(gc->vloc_tex, g_vsize_tex, GL_FLOAT, GL_FALSE, g_stride_tex, (GLfloat *)0); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindFramebuffer(GL_FRAMEBUFFER,0); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glViewport(0, 0, gc->screen_width, gc->screen_height); glUseProgram(gc->program); gc->texture = create_texture("texture1.bmp"); glDisable(GL_BLEND); glEnable(GL_CULL_FACE); glEnable(GL_DEPTH_TEST); }
Shape create_rectangle(CSize size, const vec4 color) { int real_vertex_count = 4; Params params = default_params(); params.const_params.datasize = sizeof(CPoint)*real_vertex_count; params.const_params.triangle_mode = GL_TRIANGLE_STRIP; CPoint *data = malloc(params.const_params.datasize); gen_rectangle(data, size); return (Shape) { {color[0], color[1], color[2], color[3]}, data, create_vbo(params.const_params.datasize, data, GL_DYNAMIC_DRAW), real_vertex_count, params }; }
Shape create_segmented_circle(float radius, int vertex_count, float start_angle, float angle, const vec4 color) { int real_vertex_count = size_of_segmented_circle_in_vertices(vertex_count); Params params=default_params(); params.const_params.datasize = sizeof(CPoint)*real_vertex_count; params.const_params.triangle_mode=GL_TRIANGLE_FAN; params.const_params.round_count=vertex_count; CPoint *data = malloc(params.const_params.datasize); gen_segmented_circle(data, radius, start_angle, angle, vertex_count); return (Shape) { {color[0], color[1], color[2], color[3]}, data, create_vbo(params.const_params.datasize, data, GL_DYNAMIC_DRAW), real_vertex_count, params }; }
TexturedShape create_textured_rectangle(CSize size, GLuint texture) { int real_vertex_count = 4; Params params = default_params(); params.const_params.datasize = sizeof(CPoint) * real_vertex_count * 2; params.const_params.triangle_mode = GL_TRIANGLE_STRIP; CPoint *data = malloc(params.const_params.datasize); gen_textured_rectangle(data, size); return (TexturedShape) { texture, data, create_vbo(params.const_params.datasize, data, GL_STATIC_DRAW), real_vertex_count, params }; }
TexturedShape create_segmented_square(float side_length, float start_angle, float end_angle, GLuint texture) { int real_vertex_count = size_of_segmented_square_in_vertices(); Params params = default_params(); params.const_params.datasize = sizeof(CPoint) * real_vertex_count * 2 * 2; params.const_params.triangle_mode = GL_TRIANGLE_FAN; CPoint *data = malloc(params.const_params.datasize); gen_segmented_square(data, side_length, start_angle, end_angle); return (TexturedShape) { texture, data, create_vbo(params.const_params.datasize, data, GL_DYNAMIC_DRAW), real_vertex_count, params }; }
Shape create_ribbon(float length, const vec4 color) { int real_vertex_count = 4; Params params=default_params(); params.const_params.datasize = sizeof(CPoint)*real_vertex_count; params.const_params.triangle_mode = GL_TRIANGLE_STRIP; params.var_params.side_length=length; CPoint *data = malloc(params.const_params.datasize); gen_ribbon(data, length); return (Shape) { {color[0], color[1], color[2], color[3]}, data, create_vbo(params.const_params.datasize, data, GL_DYNAMIC_DRAW), real_vertex_count, params }; }
int main(void){ if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) != 0){ fprintf(stderr, "Failed to initialize SDL: %s\n", SDL_GetError()); return 1; } SDL_WM_SetCaption("OpenGL test window", "OpenGL"); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_Surface *surface = SDL_SetVideoMode(400, 400, 32, SDL_OPENGL); if (surface == NULL) { fprintf(stderr, "Failed to initialize OpenGL: %s\n", SDL_GetError()); return 1; } GLenum err = glewInit(); if (GLEW_OK != err) { /* Problem: glewInit failed, something is seriously wrong. */ fprintf(stderr, "Failed to initialize GLEW: %s\n", glewGetErrorString(err)); return 1; } printf("OpenGL Version is %s\n", glGetString(GL_VERSION)); setup_rendering(); buffer_t buffer = create_vbo(10, 10); while(1) { //glScalef(1.0, 1.0, 1.1f); glRotatef(10, 0,0,1); glColor3f(1.0f, 0.0f, 0.0f); draw_vbo(buffer); glColor3f(1.0f, 1.0f, 0.0f); draw_vbo_raw(buffer); SDL_GL_SwapBuffers(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); SDL_Delay(500); if(handle_events() == 1) { destroy_vbo(buffer); SDL_Quit(); exit(0); } } }
Shape create_rounded_rectangle_stroked(CSize size, float radius, float stroke_width, int round_count, const vec4 color) { // round_count == 10 : polygons fall out int real_vertex_count = size_of_rounded_rectangle_stroked_in_vertices(round_count); Params params = default_params(); params.const_params.round_count=round_count; params.const_params.datasize = sizeof(CPoint)*real_vertex_count*2; params.var_params.size=size; params.var_params.radius=radius; params.var_params.width=stroke_width; CPoint *data = malloc(params.const_params.datasize); gen_rounded_rectangle_stroked(data, params.var_params.size, params.var_params.radius, params.var_params.width, params.const_params.round_count); params.const_params.triangle_mode = GL_TRIANGLE_STRIP; return (Shape) { {color[0], color[1], color[2], color[3]}, data, create_vbo(params.const_params.datasize, data, GL_DYNAMIC_DRAW), real_vertex_count, params }; }
Shape create_infinity(float width, float angle, int segment_count, const vec4 color) { int real_vertex_count = size_of_infinity_in_vertices(segment_count); Params params=default_params(); params.const_params.datasize = sizeof(CPoint)*real_vertex_count; params.const_params.triangle_mode=GL_TRIANGLE_STRIP; params.const_params.round_count=segment_count; params.var_params.width = width; params.var_params.angle = angle; CPoint *data = malloc(params.const_params.datasize); gen_infinity(data, width, angle, segment_count); return (Shape) { {color[0], color[1], color[2], color[3]}, data, create_vbo(params.const_params.datasize, data, GL_DYNAMIC_DRAW), real_vertex_count, params }; }
// create a cube model that looks like a wavefront model, MODEL_T cube_wavefront() { static const float qv[] = { -0.5f, -0.5f, 0.5f, -0.5f, -0.5f, -0.5f, 0.5f, -0.5f, -0.5f, 0.5f, -0.5f, 0.5f, -0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, -0.5f, -0.5f, 0.5f, -0.5f, }; static const float qn[] = { 0.0f, -1.0f, -0.0f, 0.0f, 1.0f, -0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, -0.0f, 0.0f, 0.0f, -1.0f, -1.0f, 0.0f, -0.0f, }; static const float qt[] = { 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, }; static const unsigned short qf[] = { 1,1,1, 2,2,1, 3,3,1, 3,3,1, 4,4,1, 1,1,1, 5,4,2, 6,1,2, 7,2,2, 7,2,2, 8,3,2, 5,4,2, 1,4,3, 4,1,3, 6,2,3, 6,2,3, 5,3,3, 1,4,3, 4,4,4, 3,1,4, 7,2,4, 7,2,4, 6,3,4, 4,4,4, 3,4,5, 2,1,5, 8,2,5, 8,2,5, 7,3,5, 3,4,5, 2,4,6, 1,1,6, 5,2,6, 5,2,6, 8,3,6, 2,4,6, }; WAVEFRONT_MODEL_T *model = malloc(sizeof *model); if (model) { WAVEFRONT_MATERIAL_T *mat = model->material; float *temp; const int offset = 0; memset(model, 0, sizeof *model); temp = allocbuffer(3*MAX_VERTICES*sizeof *temp); //mat->numverts = countof(qf)/3; mat->numverts = sizeof(qf)/sizeof(short)/3; // vertex, texture, normal deindex(temp, qv, qf+3*offset+0, 3, mat->numverts); create_vbo(GL_ARRAY_BUFFER, mat->vbo+VBO_VERTEX, 3 * mat->numverts * sizeof *qv, temp); // 3 deindex(temp, qt, qf+3*offset+1, 2, mat->numverts); create_vbo(GL_ARRAY_BUFFER, mat->vbo+VBO_TEXTURE, 2 * mat->numverts * sizeof *qt, temp); // 2 deindex(temp, qn, qf+3*offset+2, 3, mat->numverts); create_vbo(GL_ARRAY_BUFFER, mat->vbo+VBO_NORMAL, 3 * mat->numverts * sizeof *qn, temp); // 3 freebuffer(temp); model->num_materials = 1; } return (MODEL_T)model; }
MODEL_T load_wavefront(const char *modelname, const char *texturename) { WAVEFRONT_MODEL_T *model; float *temp, *qv, *qt, *qn; unsigned short *qf; int i; int numverts = 0, offset = 0; struct wavefront_model_loading_s *m; int s=-1; char modelname_obj[128]; model = malloc(sizeof *model); if (!model || !modelname) return NULL; memset (model, 0, sizeof *model); model->texture = 0; //load_texture(texturename); m = allocbuffer(sizeof *m + sizeof(float)*(3+2+3)*MAX_VERTICES + // 3 vertices + 2 textures + 3 normals sizeof(unsigned short)*3*MAX_VERTICES); //each face has 9 vertices if (!m) return 0; if (strlen(modelname) + 5 <= sizeof modelname_obj) { strcpy(modelname_obj, modelname); strcat(modelname_obj, ".dat"); s = load_wavefront_dat(modelname_obj, model, m); } if (s==0) {} else if (strncmp(modelname + strlen(modelname) - 4, ".obj", 4) == 0) { #ifdef DUMP_OBJ_DAT int size; FILE *fp; #endif s = load_wavefront_obj(modelname, model, m); #ifdef DUMP_OBJ_DAT strcpy(modelname_obj, modelname); strcat(modelname_obj, ".dat"); size = sizeof *m + sizeof(float)*(3*m->numv+2*m->numt+3*m->numn) + // 3 vertices + 2 textures + 3 normals sizeof(unsigned short)*3*m->numf; //each face has 9 vertices fp = host_file_open(modelname_obj, "w"); fwrite(m, 1, size, fp); fclose(fp); #endif } else if (strncmp(modelname + strlen(modelname) - 4, ".dat", 4) == 0) { s = load_wavefront_dat(modelname, model, m); } if (s != 0) return 0; qv = (float *)(m->data); qt = (float *)(m->data + m->numv); qn = (float *)(m->data + m->numv + m->numt); qf = (unsigned short *)(m->data + m->numv + m->numt + m->numn); numverts = m->numf/3; vc_assert(numverts <= MAX_VERTICES); temp = allocbuffer(3*numverts*sizeof *temp); for (i=0; i<m->num_materials; i++) { WAVEFRONT_MATERIAL_T *mat = model->material + i; mat->numverts = i < m->num_materials-1 ? m->material_index[i+1]-m->material_index[i] : numverts - m->material_index[i]; // vertex, texture, normal deindex(temp, qv, qf+3*offset+0, 3, mat->numverts); create_vbo(GL_ARRAY_BUFFER, mat->vbo+VBO_VERTEX, 3 * mat->numverts * sizeof *qv, temp); // 3 deindex(temp, qt, qf+3*offset+1, 2, mat->numverts); create_vbo(GL_ARRAY_BUFFER, mat->vbo+VBO_TEXTURE, 2 * mat->numverts * sizeof *qt, temp); // 2 deindex(temp, qn, qf+3*offset+2, 3, mat->numverts); create_vbo(GL_ARRAY_BUFFER, mat->vbo+VBO_NORMAL, 3 * mat->numverts * sizeof *qn, temp); // 3 offset += mat->numverts; mat->texture = model->texture; } model->num_materials = m->num_materials; vc_assert(offset == numverts); freebuffer(temp); freebuffer(m); return (MODEL_T)model; }