c - Using `const` breaks program -
i wrote small test program using sdl 2.0 , opengl 1.1, , ran strange problem: making variables const
breaks program! can explain why happens?
code
static float vertexes[] = { // static uint8_t colors[] = { static float vertexes[] = { // no problem static const uint8_t colors[] = { static const float vertexes[] = { // bad static const uint8_t colors[] = { static const float vertexes[] = { // bad static uint8_t colors[] = {
respective screenshots
full (working) program
this program gives desired behaviour. comment out const
s view broken versions.
// gcc -o main main.c -std=c11 -pedantic -wall -i. -lm -lmingw32 -lsdl2main -lsdl2 -lopengl32 -lglu32 -mwindows #include <sdl.h> #include <gl/gl.h> #include <gl/glu.h> #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define arraysize(arr) (sizeof(arr) / sizeof(*arr)) typedef struct { float x; float y; float z; } vec3; bool running; int width, height; sdl_window *window; sdl_glcontext ctx; vec3 rotation; static /* const */ float vertexes[] = { 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, }; static /* const */ uint8_t colors[] = { 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x7f, 0x3f, 0x00, 0x7f, 0x3f, 0x00, 0x7f, 0x3f, 0x00, 0x7f, 0x3f, 0x00, 0x7f, 0x3f, 0x00, 0x7f, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0x00, 0xff, 0xff, 0x00, 0xff, 0xff, 0x00, 0xff, 0xff, 0x00, 0xff, 0xff, 0x00, 0xff, 0xb3, 0x00, 0xff, 0xb3, 0x00, 0xff, 0xb3, 0x00, 0xff, 0xb3, 0x00, 0xff, 0xb3, 0x00, 0xff, 0xb3, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, }; void onresize(void) { glviewport(0, 0, width, height); glmatrixmode(gl_projection); glloadidentity(); gluperspective(60, (float)width/(float)height, 1, -1); } void init(void); void fini(void); void loop(void); void handle(void); void render(void); void update(void); int main(int argc, char* argv[]) { init(); loop(); fini(); return 0; } void init(void) { sdl_displaymode mode; if (sdl_init(sdl_init_video)) return; sdl_getdesktopdisplaymode(0, &mode); width = mode.w; height = mode.h; window = sdl_createwindow( "game", sdl_windowpos_undefined, sdl_windowpos_undefined, width, height, sdl_window_opengl | sdl_window_fullscreen ); if (!window) return; ctx = sdl_gl_createcontext(window); if (!ctx) return; onresize(); glenable(gl_depth_test); running = true; } void fini(void) { if (ctx) sdl_gl_deletecontext(ctx); if (window) sdl_destroywindow(window); sdl_quit(); } void loop(void) { while (running) { handle(); render(); update(); } } void handle(void) { sdl_event event; while (sdl_pollevent(&event)) { switch (event.type) { case sdl_windowevent: switch (event.window.event) { case sdl_windowevent_resized: width = event.window.data1, height = event.window.data2; onresize(); break; } break; case sdl_quit: running = false; break; default: break; } } } void render(void) { glclearcolor(0.9f, 0.8f, 0.7f, 1.0f); glclear(gl_color_buffer_bit | gl_depth_buffer_bit); glmatrixmode(gl_modelview); glloadidentity(); glulookat(0, 0, -5, 0, 0, 0, 0, 1, 0); glrotatef(rotation.x, 1, 0, 0); glrotatef(rotation.y, 0, 1, 0); glrotatef(rotation.z, 0, 0, 1); glscalef(0.25f, 0.25f, 0.25f); glcolor4f(0.0f, 0.0f, 0.0f, 0.0f); glbegin(gl_lines); glvertex3f(0.0f, 0.0f, 0.0f); glvertex3f(9.0f, 0.0f, 0.0f); glvertex3f(0.0f, 0.0f, 0.0f); glvertex3f(0.0f, 9.0f, 0.0f); glvertex3f(0.0f, 0.0f, 0.0f); glvertex3f(0.0f, 0.0f, 9.0f); glend(); glenableclientstate(gl_vertex_array); glenableclientstate(gl_color_array); glvertexpointer(3, gl_float, 0, vertexes); glcolorpointer(3, gl_unsigned_byte, 0, colors); gldrawarrays(gl_triangles, 0, arraysize(vertexes)); glenableclientstate(gl_color_array); gldisableclientstate(gl_vertex_array); sdl_gl_swapwindow(window); } void update(void) { rotation.x++; rotation.y++; }
alternatively available here.
use arraysize(vertexes) / 3
instead of arraysize(vertexes)
in gldrawarrays()
call.
gldrawarrays()
's count
argument number of vertices, not number of floats. right you're telling opengl read off end of vertexes/colors arrays $diety
-knows-what. you're lucky isn't segfaulting.
const
/non-const
switching segment arrays ending in, providing different "garbage" vertices opengl try interpret.
Comments
Post a Comment