int main(void) { Primative *edits[] = { primative_create(CUBE, ADDITIVE), primative_create(SPHERE, ADDITIVE), primative_create(SPHERE, SUBTRACTIVE), primative_create(SPHERE, ADDITIVE) }; primative_scale(edits[0], 0.4, 1.0, 1.5); primative_translate(edits[1], 0.2, 0, 0); primative_translate(edits[2], -0.3, 0, 0); primative_translate(edits[3], -0.5, 0, 0); primative_scale(edits[3], 0.5, 0.5, 0.5); edits[1]->blend = 0.2; sdf_args.edits = edits; sdf_args.num_edits = 4; vec3 origin = {0, 0, 0}; vec3 halfDim = {1, 1, 1}; OctTree *tree = octTree_create(origin, halfDim); octTree_populate(tree, distance); int num_verticies = octTree_count(tree); size_t vert_size = sizeof(Point); fprintf(stdout, "num verts %d\n", num_verticies); int vi = 0; Point *verticies = malloc(num_verticies * vert_size); octTree_get_points(tree, verticies, &vi); GLFWwindow* window; mat4x4 ident; mat4x4_identity(ident); glfwSetErrorCallback(error_callback); if (!glfwInit()) exit(EXIT_FAILURE); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); window = glfwCreateWindow(640, 480, "speed dream", NULL, NULL); if (!window) { glfwTerminate(); exit(EXIT_FAILURE); } glfwMakeContextCurrent(window); glfwSwapInterval(1); glEnable(GL_PROGRAM_POINT_SIZE); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); glfwSetKeyCallback(window, key_callback); mat4x4 proj; mat4x4 view; mat4x4 matrix; mat4x4_identity(view); mat4x4_identity(matrix); vec3_set(camera.pos, 0.0f, 0.0f, 0.0f); // Create Vertex Array Object GLuint vao; glGenVertexArrays(1, &vao); glBindVertexArray(vao); // Create a Vertex Buffer Object and copy the vertex data to it GLuint vbo; glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, num_verticies * vert_size, verticies, GL_STATIC_DRAW); // Create and compile the shaders GLuint vertexShader = loadShader(GL_VERTEX_SHADER, DREAM_VERT); GLuint fragmentShader = loadShader(GL_FRAGMENT_SHADER, DREAM_FRAG); // Link the vertex and fragment shader into a shader program GLuint shaderProgram = glCreateProgram(); glAttachShader(shaderProgram, vertexShader); glAttachShader(shaderProgram, fragmentShader); glBindFragDataLocation(shaderProgram, 0, "outColor"); glLinkProgram(shaderProgram); glUseProgram(shaderProgram); // Specify the layout of the vertex data GLint posAttrib = glGetAttribLocation(shaderProgram, "position"); glEnableVertexAttribArray(posAttrib); glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, vert_size, 0); GLint normAttrib = glGetAttribLocation(shaderProgram, "normal"); glEnableVertexAttribArray(normAttrib); glVertexAttribPointer(normAttrib, 3, GL_FLOAT, GL_FALSE, vert_size, (void*)(3 * sizeof(GLfloat))); GLint colorAttrib = glGetAttribLocation(shaderProgram, "color"); glEnableVertexAttribArray(colorAttrib); glVertexAttribPointer(colorAttrib, 3, GL_FLOAT, GL_FALSE, vert_size, (void*)(6 * sizeof(GLfloat))); GLint uniModel = glGetUniformLocation(shaderProgram, "model"); GLint uniView = glGetUniformLocation(shaderProgram, "view"); GLint uniProj = glGetUniformLocation(shaderProgram, "proj"); vec3 lightPos = {3.f, 0.f, 3.f}; GLint uniLightPos = glGetUniformLocation(shaderProgram, "lightPos"); glUniform3fv(uniLightPos, 1, (const GLfloat *)&lightPos); while (!glfwWindowShouldClose(window)) { float ratio; int width, height; glfwGetFramebufferSize(window, &width, &height); ratio = width / (float) height; glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); vec4 movement = {0, 0, 0, 1}; if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS) movement[0] -= 0.1; if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS) movement[0] += 0.1; if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS) movement[2] += 0.1; if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS) movement[2] -= 0.1; if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) camera.pitch -= 0.05; if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) camera.pitch += 0.05; if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) camera.yaw += 0.05; if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) camera.yaw -= 0.05; mat4x4 pitch_mat; mat4x4_identity(pitch_mat); mat4x4_rotate_X(pitch_mat, pitch_mat, camera.pitch); mat4x4 yaw_mat; mat4x4_identity(yaw_mat); mat4x4_rotate_Y(yaw_mat, yaw_mat, camera.yaw); mat4x4 inv_yaw_mat; mat4x4_invert(inv_yaw_mat, yaw_mat); vec4 rotated_movement; mat4x4_mul_vec4(rotated_movement, inv_yaw_mat, movement); camera.pos[0] += rotated_movement[0]; camera.pos[1] += rotated_movement[1]; camera.pos[2] += rotated_movement[2]; mat4x4 translation_mat; mat4x4_translate(translation_mat, camera.pos[0], camera.pos[1], camera.pos[2]); mat4x4_mul(view, pitch_mat, yaw_mat); mat4x4_mul(view, view, translation_mat); glUniformMatrix4fv(uniView, 1, GL_FALSE, (const GLfloat *)&view); mat4x4_perspective(proj, 60 * M_PI / 180, ratio, 0.1f, 10.0f); glUniformMatrix4fv(uniProj, 1, GL_FALSE, (const GLfloat *)&proj); glUniformMatrix4fv(uniModel, 1, GL_FALSE, (const GLfloat *)&matrix); glDrawArrays(GL_POINTS, 0, num_verticies); glfwSwapBuffers(window); glfwPollEvents(); } glDeleteProgram(shaderProgram); glDeleteShader(fragmentShader); glDeleteShader(vertexShader); glDeleteBuffers(1, &vbo); glDeleteVertexArrays(1, &vao); glfwDestroyWindow(window); free(verticies); glfwTerminate(); exit(EXIT_SUCCESS); }
void G_Tick() { BufferTextInput(); if (G_ContainsHeightMap(*((heightmap*)((object*)G_staticMeshes.first->value)->shape), G_camPos)) printf("\rYes\n"); vec3 dir = {0, 0, 0}; float moveSpeed = G_moveSpeed * (SYS_deltaMillis / 1000.0); float rotSpeed = G_rotSpeed * (SYS_deltaMillis / 1000.0); if (IN_IsKeyPressed(IN_W)) dir[2] -= moveSpeed; if (IN_IsKeyPressed(IN_A)) dir[0] -= moveSpeed; if (IN_IsKeyPressed(IN_S)) dir[2] += moveSpeed; if (IN_IsKeyPressed(IN_D)) dir[0] += moveSpeed; if (IN_IsKeyPressed(IN_UP)) dir[1] += moveSpeed; if (IN_IsKeyPressed(IN_DOWN)) dir[1] -= moveSpeed; if (IN_IsKeyPressed(IN_RIGHT)) G_camRot[1] += rotSpeed; if (IN_IsKeyPressed(IN_LEFT)) G_camRot[1] -= rotSpeed; if (IN_IsKeyPressed(IN_PITCH_UP)) G_camRot[0] += rotSpeed; if (IN_IsKeyPressed(IN_PITCH_DOWN)) G_camRot[0] -= rotSpeed; if (IN_IsKeyPressed(IN_ACTION)) { if (!actionHeld) { Shoot(); actionHeld = true; } }else actionHeld = false; if (IN_IsKeyPressed(IN_TOGGLE)) { if (!toggleHeld) { ads = ads ? false : true; lastAdsTime = SYS_TimeMillis() / 1000.0; toggleHeld = true; } }else toggleHeld = false; if (IN_IsKeyPressed(IN_CHAT)) { if (!chatHeld) { IN_StartTextInput(); C_console.inputActive = true; chatHeld = true; } }else chatHeld = false; if (IN_IsKeyPressed(IN_RELOAD)) { ReloadLevel(); } if (G_camRot[0] > PITCH_LIMIT) G_camRot[0] = PITCH_LIMIT; else if (G_camRot[0] < -PITCH_LIMIT) G_camRot[0] = -PITCH_LIMIT; float c = cos(G_camRot[1]); float s = sin(G_camRot[1]); G_camPos[0] += -dir[2] * s + dir[0] * c; G_camPos[2] += dir[2] * c + dir[0] * s; G_camPos[1] += dir[1]; mat4x4_translate(G_gunMat, G_camPos[0], G_camPos[1], G_camPos[2]); mat4x4_rotate_Y(G_gunMat, G_gunMat, G_camRot[1] + M_PI); mat4x4_rotate_X(G_gunMat, G_gunMat, -G_camRot[0]); double d, f; { termf terms[2] = {{2, 2}, {-1, 4}}; d = G_Valuef((function) {0, 1, 2, terms}, (SYS_TimeMillis() / 1000.0 - lastAdsTime) * 5); if (ads) d = 1 - d; } { termf terms[2] = {{1, 0.3}, {-1, 1}}; f = G_Valuef((function) {0, 1, 2, terms}, (SYS_TimeMillis() / 1000.0 - lastShootTime) * 5); } V_SetProj(fov->value * d + adsFov->value * (1 - d)); vec3 resultant = {0, 0, 0}; for (int i = 0; i < 3; i++) { resultant[i] += defGunPos[i]; resultant[i] += hipGunPos[i] * d; resultant[i] += fireGunPos[i] * f; resultant[i] += hipFireGunPos[i] * d * f; } resultant[0] += d * 0.01 * cos(SYS_TimeMillis() / 2000.0); resultant[1] += d * 0.005 * cos(SYS_TimeMillis() / 1500.0); mat4x4_translate_in_place(G_gunMat, resultant[0], resultant[1], resultant[2]); HandleSmoke(); if (IN_IsKeyPressed(IN_RELOAD)) V_reloadShaders = true; }
int main(void) { glfwSetErrorCallback(error_callback); if (!glfwInit()) exit(EXIT_FAILURE); GLFWmonitor *monitor = glfwGetPrimaryMonitor(); const GLFWvidmode *mode = glfwGetVideoMode(monitor); printf("monitor mode: %d, %d\n", mode->width, mode->height); // if DEBUG { // glfw.WindowHint(glfw.OpenGLDebugContext, gl.TRUE) // } glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, 1); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); GLFWwindow *window = glfwCreateWindow(mode->width, mode->height, "Hialin", NULL, NULL); if (!window) { glfwTerminate(); exit(EXIT_FAILURE); } glfwSetKeyCallback(window, key_callback); glfwMakeContextCurrent(window); glfwSwapInterval(0); p_glBindFragDataLocation = (glBindFragDataLocation_t)glfwGetProcAddress("glBindFragDataLocation"); if (!p_glBindFragDataLocation) { printf("\n failed glBindFragDataLocation"); glfwTerminate(); exit(EXIT_FAILURE); } p_glGenVertexArrays = (glGenVertexArrays_t)glfwGetProcAddress("glGenVertexArrays"); if (!p_glGenVertexArrays) { printf("\n failed glGenVertexArrays"); glfwTerminate(); exit(EXIT_FAILURE); } p_glBindVertexArray = (glBindVertexArray_t)glfwGetProcAddress("glBindVertexArray"); if (!p_glBindVertexArray) { printf("\n failed glBindVertexArray"); glfwTerminate(); exit(EXIT_FAILURE); } GLuint vsh = glCreateShader(GL_VERTEX_SHADER); { glShaderSource(vsh, 1, &vertex_shader_text, NULL); glCompileShader(vsh); int result; glGetShaderiv(vsh, GL_COMPILE_STATUS, &result ); if (result == GL_FALSE) { int logLength; glGetShaderiv(vsh, GL_INFO_LOG_LENGTH, &logLength); char log[10*1024] = {0}; glGetShaderInfoLog(vsh, logLength, NULL, log); printf("\nvertex shader compile: %s", log); glfwTerminate(); exit(EXIT_FAILURE); } } glCheck(); GLuint fsh = glCreateShader(GL_FRAGMENT_SHADER); { glShaderSource(fsh, 1, &fragment_shader_text, NULL); glCompileShader(fsh); int result; glGetShaderiv(fsh, GL_COMPILE_STATUS, &result); if (result == GL_FALSE) { int logLength; glGetShaderiv(fsh, GL_INFO_LOG_LENGTH, &logLength); char log[10*1024] = {0}; glGetShaderInfoLog(fsh, logLength, NULL, log); printf("\nfragment shader compile: %s", log); glfwTerminate(); exit(EXIT_FAILURE); } } glCheck(); GLuint program = glCreateProgram(); { glAttachShader(program, vsh); glAttachShader(program, fsh); glLinkProgram(program); int result; glGetProgramiv(program, GL_LINK_STATUS, &result); if (result == GL_FALSE) { int logLength; glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logLength); char log[10*1024] = {0}; glGetProgramInfoLog(program, logLength, NULL, log); printf("\nprogram link: \n%s", log); glfwTerminate(); exit(EXIT_FAILURE); } } glCheck(); glUseProgram(program); glCheck(); GLint projectionU = glGetUniformLocation(program, "projection"); mat4x4 camera; vec3 eye = {3, 3, 3}, center = {0, 0, 0}, up = {0, 1, 0}; mat4x4_look_at(camera, eye, center, up); GLint cameraU = glGetUniformLocation(program, "camera"); glUniformMatrix4fv(cameraU, 1, GL_FALSE, (const float*)camera); glCheck(); mat4x4 model; mat4x4_identity(model); GLint modelU = glGetUniformLocation(program, "model"); glUniformMatrix4fv(modelU, 1, GL_FALSE, (const float*)model); glCheck(); GLint texU = glGetUniformLocation(program, "tex"); glUniform1i(texU, 0); p_glBindFragDataLocation(program, 0, "outputColor"); glCheck(); // Load the texture // char *texturePath = "./Resources/code.png" // GLuint texture = MakeTexture(0, texturePath); GLuint texture = MakeTexture(0); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texture); glCheck(); // Configure the vertex data GLuint vao; p_glGenVertexArrays(1, &vao); p_glBindVertexArray(vao); glCheck(); GLuint vbo; glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(cube_vertices), cube_vertices, GL_STATIC_DRAW); glCheck(); GLuint val = glGetAttribLocation(program, "vert"); glEnableVertexAttribArray(val); glVertexAttribPointer(val, 3, GL_FLOAT, GL_FALSE, 5*4, (const void *)(0*4)); glCheck(); GLuint valTC = glGetAttribLocation(program, "vertTexCoord"); glEnableVertexAttribArray(valTC); glVertexAttribPointer(valTC, 2, GL_FLOAT, GL_FALSE, 5*4, (const void *)(3*4)); glCheck(); // Configure global settings glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); glClearColor(0.0, 0.0, 0.0, 1.0); glCheck(); long time0 = tick(); float angle = 0.01; int width = 0, height = 0; int i = 0; while (!glfwWindowShouldClose(window)) { int w, h; glfwGetFramebufferSize(window, &w, &h); if (w != width || h != height) { width = w; height = h; glViewport(0, 0, width, height); printf("buffer size: %d %d\n", w, h); } float ratio = width/(float)height; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); mat4x4_rotate_Y(model, model, angle); mat4x4 projection; mat4x4_perspective(projection, 0.785, ratio, 0.1, 10.0); // 45 degrees == 0.785 radians glUniformMatrix4fv(projectionU, 1, GL_FALSE, (const float*)projection); glUniformMatrix4fv(modelU, 1, GL_FALSE, (const float*)model); for (int i = 0; i < 1*1000; i++) { glDrawArrays(GL_TRIANGLES, 0, 6*2*3); // 12 triangles } i++; if (i == 100) { printf("time for 100 frames: %ld\n", tick()-time0); } else if (i == 1000) { printf("time for 1000 frames: %ld\n", tick()-time0); break; } glfwSwapBuffers(window); glfwPollEvents(); } glfwDestroyWindow(window); glfwTerminate(); exit(EXIT_SUCCESS); }