/** * @brief Renderer::initialize_pointsprites internally used to initialize point-sprite rendering */ void Renderer::initialize_pointsprites(){ glEnable(GL_POINT_SPRITE); // needed so that fragment shader can use gl_PointCoord glEnable(GL_VERTEX_PROGRAM_POINT_SIZE); // needed so vertex stage can set point size glPointParameteri(GL_POINT_SPRITE_COORD_ORIGIN, GL_UPPER_LEFT); glPointParameterf(GL_POINT_FADE_THRESHOLD_SIZE, 4); glGenBuffers(1, &pointsprites_vbo); glBindBuffer(GL_ARRAY_BUFFER, pointsprites_vbo); glBufferData(GL_ARRAY_BUFFER, 3 /* size of a vertex in elements */ * dataset.bodycount /* number of vertices */ * sizeof(float), /* lastly, convert to bytes */ dataset.type_adapter_get(), GL_DYNAMIC_DRAW); glEnableVertexAttribArray(0); // xyz position glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float)*3 /* 3 components */, 0); // xyz positions if(use_fancy_shaders){ pointsprites_shader = new Shader(SHADER_RESOURCE_PATH + "/pointsprite-fancier.vsh", SHADER_RESOURCE_PATH + "/pointsprite-fancier.fsh"); } else { pointsprites_shader = new Shader(SHADER_RESOURCE_PATH + "/pointsprite.vsh", SHADER_RESOURCE_PATH + "/pointsprite-simple.fsh"); } }
bool begin() { bool Validated = true; if(Validated) Validated = initTexture(); if(Validated) Validated = initProgram(); if(Validated) Validated = initBuffer(); if(Validated) Validated = initVertexArray(); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //glEnable(GL_DEPTH_TEST); //glDepthFunc(GL_LESS); glEnable(GL_PROGRAM_POINT_SIZE); //glPointParameteri(GL_POINT_SPRITE_COORD_ORIGIN, GL_LOWER_LEFT); glPointParameteri(GL_POINT_SPRITE_COORD_ORIGIN, GL_UPPER_LEFT); return Validated; }
inline void VL_glPointParameteri( GLenum pname, GLenum param) { if (glPointParameteri) glPointParameteri(pname,param); else if (glPointParameteriNV) glPointParameteriNV(pname,param); else VL_UNSUPPORTED_FUNC(); }
void StateSystem::RasterState::applyGL() const { //glFrontFace(frontFace); glCullFace(cullFace); //glPolygonOffset(polyOffsetFactor,polyOffsetUnits); glPolygonMode(GL_FRONT_AND_BACK,polyMode); //glLineWidth(lineWidth); glPointSize(pointSize); glPointParameterf(GL_POINT_FADE_THRESHOLD_SIZE,pointFade); glPointParameteri(GL_POINT_SPRITE_COORD_ORIGIN,pointSpriteOrigin); }
bool begin() { bool Validated = true; if(Validated) Validated = initProgram(); if(Validated) Validated = initBuffer(); if(Validated) Validated = initVertexArray(); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); glEnable(GL_PROGRAM_POINT_SIZE); //glPointParameteri(GL_POINT_SPRITE_COORD_ORIGIN, GL_LOWER_LEFT); glPointParameteri(GL_POINT_SPRITE_COORD_ORIGIN, GL_UPPER_LEFT); return Validated && this->checkError("begin"); }
void Surface::drawBillboard(Vector3<float> position, float pointSize) { /* テクスチャ環境 */ glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE); glPointParameteri(GL_POINT_SPRITE_COORD_ORIGIN, GL_LOWER_LEFT); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBindTexture(GL_TEXTURE_2D , texture[0]); glEnable(GL_TEXTURE_2D); glEnable(GL_POINT_SPRITE); glColor3d(1.0, 1.0, 1.0); glPointSize(pointSize); glBegin(GL_POINTS); //glRotatef(90, 0, 0, 1); glVertex3f(position.x,position.y,position.z); glEnd(); glDisable(GL_POINT_SPRITE); glDisable(GL_TEXTURE_2D); }
bool begin() { bool Validated(true); glEnable(GL_PROGRAM_POINT_SIZE); glPointParameteri(GL_POINT_SPRITE_COORD_ORIGIN, GL_LOWER_LEFT); //glPointParameteri(GL_POINT_SPRITE_COORD_ORIGIN, GL_UPPER_LEFT); if(Validated) Validated = initProgram(); if(Validated) Validated = initBuffer(); if(Validated) Validated = initVertexArray(); if(Validated) Validated = initTexture(); if(Validated) Validated = initFramebuffer(); return Validated; }
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_GL14_nglPointParameteri(JNIEnv *env, jclass clazz, jint pname, jint param, jlong function_pointer) { glPointParameteriPROC glPointParameteri = (glPointParameteriPROC)((intptr_t)function_pointer); glPointParameteri(pname, param); }
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_GL14C_glPointParameteri(JNIEnv *__env, jclass clazz, jint pname, jint param) { glPointParameteriPROC glPointParameteri = (glPointParameteriPROC)tlsGetFunction(429); UNUSED_PARAM(clazz) glPointParameteri(pname, param); }
void DemoInputInterpreter::interpretInput(Keyboard* keyboard) { // Camera* mainCamera = // URE_INSTANCE->getSimulator(VISUAL_SIM_DOMAIN)->toLightingSimulator()->getMainCamera(); static float polygonOffSetFactor =2.5f; static float polyGonOffsetUnit = 5.8f; const float increment = 0.1f; if(keyboard->getRecentStatus() == GLFW_PRESS) { if( (keyboard->getRecentKey()) == GLFW_KEY_UP) { polygonOffSetFactor += increment; } if( (keyboard->getRecentKey()) == GLFW_KEY_DOWN) { polygonOffSetFactor -= increment; } if( (keyboard->getRecentKey()) == GLFW_KEY_LEFT) { polyGonOffsetUnit += increment; } if( (keyboard->getRecentKey()) == GLFW_KEY_RIGHT) { polyGonOffsetUnit -= increment; } // LOG<<DEBUG_LOG_LEVEL<<"polygonOffSetFactor: "<<polygonOffSetFactor // <<"; polyGonOffsetUnit: "<<polyGonOffsetUnit<<";\n"; GUARD(glPolygonOffset(polygonOffSetFactor,polyGonOffsetUnit)); } if(keyboard->getRecentKey() == GLFW_KEY_ENTER) { if(keyboard->getRecentStatus() == GLFW_PRESS) { LOG<<DEBUG_LOG_LEVEL<<"enter pressed;\n"; Vector2Dui currentRes = WindowManager::getInstance().getWindowResolution(); LOG<<INFO_LOG_LEVEL<<"Resolution is now ("<<currentRes.x<<","<<currentRes.y<<");\n"; } else { LOG<<DEBUG_LOG_LEVEL<<"enter released;\n"; } } if( (keyboard->getRecentKey()) == 'W') { if(keyboard->getRecentStatus() == GLFW_PRESS){ mMovementState[MOVEMENT_DIRECTION_FRONT_BACK] = MOVEMENT_STATE_FORWARD; } else{ mMovementState[MOVEMENT_DIRECTION_FRONT_BACK] = MOVEMENT_STATE_STILL; } } if( (keyboard->getRecentKey()) == 'S') { if(keyboard->getRecentStatus() == GLFW_PRESS){ mMovementState[MOVEMENT_DIRECTION_FRONT_BACK] = MOVEMENT_STATE_BACKWARD; } else{ mMovementState[MOVEMENT_DIRECTION_FRONT_BACK] = MOVEMENT_STATE_STILL; } } if( (keyboard->getRecentKey()) == 'D') { if(keyboard->getRecentStatus() == GLFW_PRESS){ mMovementState[MOVEMENT_DIRECTION_RIGHT_LEFT] = MOVEMENT_STATE_FORWARD; } else{ mMovementState[MOVEMENT_DIRECTION_RIGHT_LEFT] = MOVEMENT_STATE_STILL; } } if( (keyboard->getRecentKey()) == 'A') { if(keyboard->getRecentStatus() == GLFW_PRESS){ mMovementState[MOVEMENT_DIRECTION_RIGHT_LEFT] = MOVEMENT_STATE_BACKWARD; } else{ mMovementState[MOVEMENT_DIRECTION_RIGHT_LEFT] = MOVEMENT_STATE_STILL; } } if( (keyboard->getRecentKey()) == 'Q') { if(keyboard->getRecentStatus() == GLFW_PRESS){ mMovementState[MOVEMENT_DIRECTION_UP_DOWN] = MOVEMENT_STATE_BACKWARD; } else{ mMovementState[MOVEMENT_DIRECTION_UP_DOWN] = MOVEMENT_STATE_STILL; } } if( (keyboard->getRecentKey()) == 'E') { if(keyboard->getRecentStatus() == GLFW_PRESS){ mMovementState[MOVEMENT_DIRECTION_UP_DOWN] = MOVEMENT_STATE_FORWARD; } else{ mMovementState[MOVEMENT_DIRECTION_UP_DOWN] = MOVEMENT_STATE_STILL; } } //---------------------------------------------------------------------------- if(keyboard->getRecentKey() == GLFW_KEY_F2) { GUARD(glPolygonMode(GL_FRONT_AND_BACK, GL_POINT)); GUARD(glPointParameteri(GL_POINT_SPRITE_COORD_ORIGIN, GL_LOWER_LEFT)); GUARD(glEnable(GL_PROGRAM_POINT_SIZE)); } if(keyboard->getRecentKey() == GLFW_KEY_F3) { GUARD(glDisable(GL_PROGRAM_POINT_SIZE)); GUARD(glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)); } if(keyboard->getRecentKey() == GLFW_KEY_F4) { GUARD(glDisable(GL_PROGRAM_POINT_SIZE)); GUARD(glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)); } if( (keyboard->getRecentKey() == GLFW_KEY_TAB) && (keyboard->getRecentStatus() == GLFW_PRESS) ) { URE_INSTANCE->toggleDoDebugDraw(); } if((keyboard->getRecentKey() == GLFW_KEY_SPACE) && (keyboard->getRecentStatus() == GLFW_PRESS)) { mHideMouse = !mHideMouse; } //--------------------------------------------------------------------------- //static ShadingFeatures currentlyEnabledFeatures = ShaderManager::getInstance().getEnabledShadingFeatures(); if(keyboard->getRecentStatus() == GLFW_PRESS) { ShadingFeatures sfToChange = SHADING_FEATURE_NONE; switch (keyboard->getRecentKey()) { case 'N': sfToChange = SHADING_FEATURE_DIFFUSE_TEXTURING; break; case 'M': sfToChange = SHADING_FEATURE_DIRECT_LIGHTING; break; case ',': sfToChange = SHADING_FEATURE_NORMAL_MAPPING; break; case '.': sfToChange = SHADING_FEATURE_CUBE_MAPPING; break; case '-': sfToChange = SHADING_FEATURE_TESSELATION; break; default: break; } if( sfToChange != SHADING_FEATURE_NONE) { ShaderManager::getInstance().setEnableShadingFeatures( sfToChange, //toggle current activations state by querying, negating and setting it ! ShaderManager::getInstance().shadingFeaturesAreEnabled(sfToChange) ); } } // static bool enableTess =ShaderManager::getInstance().tesselationIsEnabled(); // if((keyboard->getRecentKey() == GLFW_KEY_TAB) && (keyboard->getRecentStatus() == GLFW_PRESS)) // { // enableTess = !enableTess; // ShaderManager::getInstance().setEnableTesselation(enableTess); // } //-------------------------------------------------------------------- if(keyboard->getRecentKey() == GLFW_KEY_ESC) { LOG<<DEBUG_LOG_LEVEL<<"escape pressed, shutting down;(;\n"; InputManager::getInstance().setMouseHidden(false); URE_INSTANCE->requestMainLoopQuit(); } }
static bool test_pointsprite_ps(void) { static const float vertArray[ATTR_SIZE * NUM_ATTRS] = { 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, }; const unsigned int numPixels = WIDTH * HEIGHT; GLuint texData[WIDTH * HEIGHT]; GLuint i, texFbo, fbo, vertexArray, vertexBuf; GLint attrLoc; const float pointSize = WIDTH; const unsigned int expectedTexelColor = 0xFFFFFFFF; for (i = 0; i < numPixels; ++i) { texData[i] = COLOR_GRAY; } /* Create 2D textures */ glGenTextures(1, &texFbo); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texFbo); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, WIDTH, HEIGHT, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, texData); if (!piglit_check_gl_error(GL_NO_ERROR)) return false; /* Setup the vertex attributes */ glGenVertexArrays(1, &vertexArray); glBindVertexArray(vertexArray); glGenBuffers(1, &vertexBuf); glBindBuffer(GL_ARRAY_BUFFER, vertexBuf); glBufferData(GL_ARRAY_BUFFER, sizeof(vertArray), vertArray, GL_STATIC_DRAW); for (i = 0; i < NUM_ATTRS; i++) { const GLvoid *offset = (const GLvoid *)(i * ATTR_SIZE * sizeof(float)); GLchar name[8]; snprintf(name, sizeof name, "Attr%d", i); attrLoc = glGetAttribLocation(prog, name); glEnableVertexAttribArray(attrLoc); glVertexAttribPointer(attrLoc, ATTR_SIZE, GL_FLOAT, GL_FALSE, ATTR_SIZE * sizeof(float), offset); } if (!piglit_check_gl_error(GL_NO_ERROR)) return false; /* Setup the FBO */ glGenFramebuffers(1, &fbo); glBindFramebuffer(GL_FRAMEBUFFER, fbo); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texFbo, 0); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { printf("incomplete framebuffer at line %d\n", __LINE__); return false; } glDrawBuffer(GL_COLOR_ATTACHMENT0); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { printf("incomplete framebuffer at line %d\n", __LINE__); return false; } /* Clear and draw */ glViewport(0, 0, WIDTH, HEIGHT); glClearColor(((CLEAR_COLOR >> 24) & 0xFF) / 255.0f, ((CLEAR_COLOR >> 16) & 0xFF) / 255.0f, ((CLEAR_COLOR >> 8) & 0xFF) / 255.0f, ((CLEAR_COLOR) & 0xFF) / 255.0f); glClear(GL_COLOR_BUFFER_BIT); glPointSize(pointSize); glPointParameteri(GL_POINT_SPRITE_COORD_ORIGIN, GL_LOWER_LEFT); glDrawArrays(GL_POINTS, 0, 1); /* Read back */ glReadBuffer(GL_COLOR_ATTACHMENT0); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { printf("incomplete framebuffer at line %d\n", __LINE__); return false; } /* read color buffer */ glPixelStorei(GL_PACK_ROW_LENGTH, WIDTH); glPixelStorei(GL_PACK_ALIGNMENT, 1); memset(texData, 0, sizeof(texData)); glReadPixels(0, 0, WIDTH, HEIGHT, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, texData); if (texData[0] != expectedTexelColor) { printf("At pixel (0,0) expected 0x%x but found 0x%x\n", expectedTexelColor, texData[0]); /* clean up */ glDeleteTextures(1, &texFbo); glDeleteFramebuffers(1, &fbo); return false; } if (!piglit_check_gl_error(GL_NO_ERROR)) return false; glDeleteTextures(1, &texFbo); glDeleteFramebuffers(1, &fbo); return true; }
inline void pointCoordOrigin(PointCoordOrigin p) { glPointParameteri( GL_POINT_SPRITE_COORD_ORIGIN, GLint(p) ); }
/* ARGSUSED1 */ static void key(unsigned char c, int x, int y) { switch (c) { case 13: animate = 1 - animate; /* toggle. */ if (animate && (motion || spin)) { glutIdleFunc(idle); } else { glutIdleFunc(NULL); } break; case ' ': animate = 1; makePointList(); glutIdleFunc(idle); break; case 'o': case 'O': org ^= 1; #ifdef GL_VERSION_2_0 #ifdef GL_ARB_point_parameters glPointParameteri(GL_POINT_SPRITE_COORD_ORIGIN, org ? GL_LOWER_LEFT : GL_UPPER_LEFT); #endif #endif glutPostRedisplay(); break; case 't': case 'T': sprite ^= 1; glutPostRedisplay(); break; case 's': case 'S': (smooth ^= 1) ? glEnable(GL_POINT_SMOOTH) : glDisable(GL_POINT_SMOOTH); glutPostRedisplay(); break; case 'f': case 'F': if (HaveShaders) { UseFragShader = !UseFragShader; glutPostRedisplay(); } break; case '0': glPointSize(1.0); glutPostRedisplay(); break; case '1': glPointSize(16.0); glutPostRedisplay(); break; case '2': glPointSize(32.0); glutPostRedisplay(); break; case '3': glPointSize(64.0); glutPostRedisplay(); break; case '4': glPointSize(128.0); glutPostRedisplay(); break; case 27: exit(0); } }
int main () { restart_gl_log (); // use GLFW and GLEW to start GL context. see gl_utils.cpp for details start_gl (); /* create buffer of particle initial attributes and a VAO */ GLuint vao = gen_particles (); GLuint shader_programme = create_programme_from_files ( "test_vs.glsl", "test_fs.glsl"); #define ONE_DEG_IN_RAD (2.0 * M_PI) / 360.0 // 0.017444444 // input variables float near = 0.1f; // clipping plane float far = 100.0f; // clipping plane float fov = 67.0f * ONE_DEG_IN_RAD; // convert 67 degrees to radians float aspect = (float)g_gl_width / (float)g_gl_height; // aspect ratio // matrix components float range = tan (fov * 0.5f) * near; float Sx = (2.0f * near) / (range * aspect + range * aspect); float Sy = near / range; float Sz = -(far + near) / (far - near); float Pz = -(2.0f * far * near) / (far - near); GLfloat proj_mat[] = { Sx, 0.0f, 0.0f, 0.0f, 0.0f, Sy, 0.0f, 0.0f, 0.0f, 0.0f, Sz, -1.0f, 0.0f, 0.0f, Pz, 0.0f }; float cam_speed = 1.0f; // 1 unit per second float cam_yaw_speed = 10.0f; // 10 degrees per second float cam_pos[] = {0.0f, 0.0f, 2.0f}; // don't start at zero, or we will be too close float cam_yaw = 0.0f; // y-rotation in degrees mat4 T = translate (identity_mat4 (), vec3 (-cam_pos[0], -cam_pos[1], -cam_pos[2])); mat4 R = rotate_y_deg (identity_mat4 (), -cam_yaw); mat4 view_mat = R * T; /* make up a world position for the emitter */ vec3 emitter_world_pos (0.0f, 0.0f, 0.0f); // locations of view and projection matrices int V_loc = glGetUniformLocation (shader_programme, "V"); assert (V_loc > -1); int P_loc = glGetUniformLocation (shader_programme, "P"); assert (P_loc > -1); int emitter_pos_wor_loc = glGetUniformLocation (shader_programme, "emitter_pos_wor"); assert (emitter_pos_wor_loc > -1); int elapsed_system_time_loc = glGetUniformLocation (shader_programme, "elapsed_system_time"); assert (elapsed_system_time_loc > -1); glUseProgram (shader_programme); glUniformMatrix4fv (V_loc, 1, GL_FALSE, view_mat.m); glUniformMatrix4fv (P_loc, 1, GL_FALSE, proj_mat); glUniform3f (emitter_pos_wor_loc, emitter_world_pos.v[0], emitter_world_pos.v[1], emitter_world_pos.v[2]); // load texture GLuint tex; if (!load_texture ("Droplet.png", &tex)) { gl_log_err ("ERROR: loading Droplet.png texture\n"); return 1; } glEnable (GL_CULL_FACE); // cull face glCullFace (GL_BACK); // cull back face glFrontFace (GL_CCW); // GL_CCW for counter clock-wise glDepthFunc (GL_LESS); // depth-testing interprets a smaller value as "closer" glEnable (GL_DEPTH_TEST); // enable depth-testing glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glClearColor (0.2, 0.2, 0.2, 1.0); /* MUST use this is in compatibility profile. doesn't exist in core glEnable(GL_POINT_SPRITE); */ while (!glfwWindowShouldClose (g_window)) { static double previous_seconds = glfwGetTime (); double current_seconds = glfwGetTime (); double elapsed_seconds = current_seconds - previous_seconds; previous_seconds = current_seconds; _update_fps_counter (g_window); // wipe the drawing surface clear glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glViewport (0, 0, g_gl_width, g_gl_height); /* Render Particles. Enabling point re-sizing in vertex shader */ glEnable (GL_PROGRAM_POINT_SIZE); glPointParameteri (GL_POINT_SPRITE_COORD_ORIGIN, GL_LOWER_LEFT); glEnable (GL_BLEND); glDepthMask (GL_FALSE); glActiveTexture (GL_TEXTURE0); glBindTexture (GL_TEXTURE_2D, tex); glUseProgram (shader_programme); /* update time in shaders */ glUniform1f (elapsed_system_time_loc, (GLfloat)current_seconds); glBindVertexArray (vao); // draw points 0-3 from the currently bound VAO with current in-use shader glDrawArrays (GL_POINTS, 0, PARTICLE_COUNT); glDisable (GL_BLEND); glDepthMask (GL_TRUE); glDisable (GL_PROGRAM_POINT_SIZE); // update other events like input handling glfwPollEvents (); // control keys bool cam_moved = false; if (glfwGetKey (g_window, GLFW_KEY_A)) { cam_pos[0] -= cam_speed * elapsed_seconds; cam_moved = true; } if (glfwGetKey (g_window, GLFW_KEY_D)) { cam_pos[0] += cam_speed * elapsed_seconds; cam_moved = true; } if (glfwGetKey (g_window, GLFW_KEY_PAGE_UP)) { cam_pos[1] += cam_speed * elapsed_seconds; cam_moved = true; } if (glfwGetKey (g_window, GLFW_KEY_PAGE_DOWN)) { cam_pos[1] -= cam_speed * elapsed_seconds; cam_moved = true; } if (glfwGetKey (g_window, GLFW_KEY_W)) { cam_pos[2] -= cam_speed * elapsed_seconds; cam_moved = true; } if (glfwGetKey (g_window, GLFW_KEY_S)) { cam_pos[2] += cam_speed * elapsed_seconds; cam_moved = true; } if (glfwGetKey (g_window, GLFW_KEY_LEFT)) { cam_yaw += cam_yaw_speed * elapsed_seconds; cam_moved = true; } if (glfwGetKey (g_window, GLFW_KEY_RIGHT)) { cam_yaw -= cam_yaw_speed * elapsed_seconds; cam_moved = true; } // update view matrix if (cam_moved) { mat4 T = translate (identity_mat4 (), vec3 (-cam_pos[0], -cam_pos[1], -cam_pos[2])); // cam translation mat4 R = rotate_y_deg (identity_mat4 (), -cam_yaw); // mat4 view_mat = R * T; glUniformMatrix4fv (V_loc, 1, GL_FALSE, view_mat.m); } if (GLFW_PRESS == glfwGetKey (g_window, GLFW_KEY_ESCAPE)) { glfwSetWindowShouldClose (g_window, 1); } // put the stuff we've been drawing onto the display glfwSwapBuffers (g_window); } // close GL context and any other GLFW resources glfwTerminate(); return 0; }
int main () { GLFWwindow* window = NULL; const GLubyte* renderer; const GLubyte* version; GLuint cube_sp, knot_sp; GLuint vao; // // Start OpenGL using helper libraries // -------------------------------------------------------------------------- if (!glfwInit ()) { fprintf (stderr, "ERROR: could not start GLFW3\n"); return 1; } /* change to 3.2 if on Apple OS X glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 0); glfwWindowHint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); glfwWindowHint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); */ glfwWindowHint (GLFW_SAMPLES, msaa); window = glfwCreateWindow (gl_width, gl_height, "{quadratic bezier}", NULL, NULL); if (!window) { fprintf (stderr, "ERROR: opening OS window\n"); return 1; } glfwMakeContextCurrent (window); glewExperimental = GL_TRUE; glewInit (); /* get version info */ renderer = glGetString (GL_RENDERER); /* get renderer string */ version = glGetString (GL_VERSION); /* version as a string */ printf ("Renderer: %s\n", renderer); printf ("OpenGL version supported %s\n", version); int point_count = 0; // // Set up vertex buffers and vertex array object // -------------------------------------------------------------------------- { GLfloat* vp = NULL; // array of vertex points GLfloat* vn = NULL; // array of vertex normals (we haven't used these yet) GLfloat* vt = NULL; // array of texture coordinates (or these) assert (load_obj_file ("smcube.obj", vp, vt, vn, point_count)); GLuint points_vbo, texcoord_vbo, normal_vbo; glGenBuffers (1, &points_vbo); glBindBuffer (GL_ARRAY_BUFFER, points_vbo); glBufferData (GL_ARRAY_BUFFER, sizeof (float) * 3 * point_count, vp, GL_STATIC_DRAW); glGenBuffers (1, &texcoord_vbo); glBindBuffer (GL_ARRAY_BUFFER, texcoord_vbo); glBufferData (GL_ARRAY_BUFFER, sizeof (float) * 2 * point_count, vt, GL_STATIC_DRAW); glGenBuffers (1, &normal_vbo); glBindBuffer (GL_ARRAY_BUFFER, normal_vbo); glBufferData (GL_ARRAY_BUFFER, sizeof (float) * 3 * point_count, vn, GL_STATIC_DRAW); glGenVertexArrays (1, &vao); glBindVertexArray (vao); glEnableVertexAttribArray (0); glBindBuffer (GL_ARRAY_BUFFER, points_vbo); glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 0, NULL); glEnableVertexAttribArray (1); glBindBuffer (GL_ARRAY_BUFFER, texcoord_vbo); glVertexAttribPointer (1, 2, GL_FLOAT, GL_FALSE, 0, NULL); glEnableVertexAttribArray (2); glBindBuffer (GL_ARRAY_BUFFER, normal_vbo); glVertexAttribPointer (2, 3, GL_FLOAT, GL_FALSE, 0, NULL); free (vp); free (vn); free (vt); } // // Load shaders from files // -------------------------------------------------------------------------- { char* vertex_shader_str; char* fragment_shader_str; // allocate some memory to store shader strings vertex_shader_str = (char*)malloc (81920); fragment_shader_str = (char*)malloc (81920); // load shader strings from text files assert (parse_file_into_str ("cube.vert", vertex_shader_str, 81920)); assert (parse_file_into_str ("cube.frag", fragment_shader_str, 81920)); GLuint vs, fs; vs = glCreateShader (GL_VERTEX_SHADER); fs = glCreateShader (GL_FRAGMENT_SHADER); glShaderSource (vs, 1, (const char**)&vertex_shader_str, NULL); glShaderSource (fs, 1, (const char**)&fragment_shader_str, NULL); // free memory free (vertex_shader_str); free (fragment_shader_str); glCompileShader (vs); glCompileShader (fs); cube_sp = glCreateProgram (); glAttachShader (cube_sp, fs); glAttachShader (cube_sp, vs); glBindAttribLocation (cube_sp, 0, "vp"); glBindAttribLocation (cube_sp, 1, "vt"); glBindAttribLocation (cube_sp, 2, "vn"); glLinkProgram (cube_sp); } { char* vertex_shader_str; char* fragment_shader_str; // allocate some memory to store shader strings vertex_shader_str = (char*)malloc (81920); fragment_shader_str = (char*)malloc (81920); // load shader strings from text files assert (parse_file_into_str ("knot.vert", vertex_shader_str, 81920)); assert (parse_file_into_str ("knot.frag", fragment_shader_str, 81920)); GLuint vs, fs; vs = glCreateShader (GL_VERTEX_SHADER); fs = glCreateShader (GL_FRAGMENT_SHADER); glShaderSource (vs, 1, (const char**)&vertex_shader_str, NULL); glShaderSource (fs, 1, (const char**)&fragment_shader_str, NULL); // free memory free (vertex_shader_str); free (fragment_shader_str); glCompileShader (vs); glCompileShader (fs); knot_sp = glCreateProgram (); glAttachShader (knot_sp, fs); glAttachShader (knot_sp, vs); glLinkProgram (knot_sp); } // // Create some matrices // -------------------------------------------------------------------------- mat4 M, V, P; M = identity_mat4 ();//scale (identity_mat4 (), vec3 (0.05, 0.05, 0.05)); vec3 cam_pos (0.0, 0.0, 15.0); vec3 targ_pos (0.0, 0.0, 0.0); vec3 up = normalise (vec3 (0.0, 1.0, 0.0)); V = look_at (cam_pos, targ_pos, up); P = perspective (67.0f, (float)gl_width / (float)gl_height, 0.1, 1000.0); int M_loc = glGetUniformLocation (cube_sp, "M"); int V_loc = glGetUniformLocation (cube_sp, "V"); int P_loc = glGetUniformLocation (cube_sp, "P"); int A_loc = glGetUniformLocation (cube_sp, "A"); int B_loc = glGetUniformLocation (cube_sp, "B"); int C_loc = glGetUniformLocation (cube_sp, "C"); int t_loc = glGetUniformLocation (cube_sp, "t"); // send matrix values to shader immediately glUseProgram (cube_sp); glUniformMatrix4fv (M_loc, 1, GL_FALSE, M.m); glUniformMatrix4fv (V_loc, 1, GL_FALSE, V.m); glUniformMatrix4fv (P_loc, 1, GL_FALSE, P.m); // // specific knots for bezier here A, C are start, end, B is control point // vec3 A = vec3 (-7.0f, -5.0f, 0.0f); vec3 B = vec3 (0.0f, 8.0f, 0.0f); vec3 C = vec3 (7.0f, -5.0f, 0.0f); glUniform3fv (A_loc, 1, A.v); glUniform3fv (B_loc, 1, B.v); glUniform3fv (C_loc, 1, C.v); int knot_loc = glGetUniformLocation (knot_sp, "pos"); int knotP_loc = glGetUniformLocation (knot_sp, "P"); int knotV_loc = glGetUniformLocation (knot_sp, "V"); glUseProgram (knot_sp); glUniformMatrix4fv (knotV_loc, 1, GL_FALSE, V.m); glUniformMatrix4fv (knotP_loc, 1, GL_FALSE, P.m); // // Start rendering // -------------------------------------------------------------------------- // tell GL to only draw onto a pixel if the fragment is closer to the viewer glEnable (GL_DEPTH_TEST); // enable depth-testing glDepthFunc (GL_LESS); // depth-testing interprets a smaller value as "closer" glDepthFunc (GL_LESS); // depth-testing is to use a "less than" function glEnable (GL_CULL_FACE); // enable culling of faces glCullFace (GL_BACK); glFrontFace (GL_CCW); glClearColor (0.04, 0.04, 0.75, 1.0); /* Render Points, allow resize in vertex shader */ glEnable (GL_PROGRAM_POINT_SIZE); glPointParameteri (GL_POINT_SPRITE_COORD_ORIGIN, GL_LOWER_LEFT); float t = 0.0f; float speed = 0.5f; double prev = glfwGetTime (); while (!glfwWindowShouldClose (window)) { glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // just the default viewport, covering the whole render area glViewport (0, 0, gl_width, gl_height); double curr = glfwGetTime (); double elapsed = curr - prev; prev = curr; // // move along spline if (GLFW_PRESS == glfwGetKey (window, GLFW_KEY_LEFT)) { t -= elapsed * speed; if (t < 0.0f) { t = 0.0f; } } if (GLFW_PRESS == glfwGetKey (window, GLFW_KEY_RIGHT)) { t += elapsed * speed; if (t > 1.0f) { t = 1.0f; } } // // render 3 knots glEnable (GL_PROGRAM_POINT_SIZE); glUseProgram (knot_sp); glUniform3fv (knot_loc, 1, A.v); glDrawArrays (GL_POINTS, 0, 1); glUseProgram (knot_sp); glUniform3fv (knot_loc, 1, B.v); glDrawArrays (GL_POINTS, 0, 1); glUseProgram (knot_sp); glUniform3fv (knot_loc, 1, C.v); glDrawArrays (GL_POINTS, 0, 1); glDisable (GL_PROGRAM_POINT_SIZE); glUseProgram (cube_sp); glBindVertexArray (vao); M = identity_mat4 ();//rotate_y_deg (identity_mat4 (), a); glUniformMatrix4fv (M_loc, 1, GL_FALSE, M.m); glUniform1f (t_loc, t); glDrawArrays (GL_TRIANGLES, 0, point_count); /* this just updates window events and keyboard input events (not used yet) */ glfwPollEvents (); glfwSwapBuffers (window); } return 0; }