Changeset ec4456b in opengl-game
- Timestamp:
- Aug 4, 2017, 5:51:59 PM (7 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- c40990d
- Parents:
- be246ad
- git-author:
- Dmitry Portnoy <dmp1488@…> (08/03/17 03:03:21)
- git-committer:
- Dmitry Portnoy <dmitry.portnoy@…> (08/04/17 17:51:59)
- Files:
-
- 2 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
new-game.cpp
rbe246ad rec4456b 6 6 #include <cstdio> 7 7 #include <iostream> 8 #include <fstream> 8 9 9 10 using namespace std; 11 12 GLuint loadShader(GLenum type, string file); 13 14 const bool FULLSCREEN = false; 15 16 void glfw_error_callback(int error, const char* description) { 17 gl_log_err("GLFW ERROR: code %i msg: %s\n", error, description); 18 } 10 19 11 20 int main(int argc, char* argv[]) { 12 21 cout << "New OpenGL Game" << endl; 13 22 14 restart_gl_log(); 23 if (!restart_gl_log()) {} 24 gl_log("starting GLFW\n%s\n", glfwGetVersionString()); 15 25 26 glfwSetErrorCallback(glfw_error_callback); 16 27 if (!glfwInit()) { 17 28 fprintf(stderr, "ERROR: could not start GLFW3\n"); … … 26 37 #endif 27 38 28 GLFWwindow* window = glfwCreateWindow(640, 480, "Hello Triangle", NULL, NULL); 39 glfwWindowHint(GLFW_SAMPLES, 4); 40 41 GLFWwindow* window = NULL; 42 43 int width = 640; 44 int height = 480; 45 46 if (FULLSCREEN) { 47 GLFWmonitor* mon = glfwGetPrimaryMonitor(); 48 const GLFWvidmode* vmode = glfwGetVideoMode(mon); 49 50 cout << "Fullscreen resolution " << vmode->width << "x" << vmode->height << endl; 51 window = glfwCreateWindow(vmode->width, vmode->height, "Extended GL Init", mon, NULL); 52 53 width = vmode->width; 54 height = vmode->height; 55 } else { 56 window = glfwCreateWindow(width, height, "Hello Triangle", NULL, NULL); 57 } 58 29 59 if (!window) { 30 60 fprintf(stderr, "ERROR: could not open window with GLFW3\n"); … … 35 65 glewExperimental = GL_TRUE; 36 66 glewInit(); 67 68 // glViewport(0, 0, width*2, height*2); 37 69 38 70 const GLubyte* renderer = glGetString(GL_RENDERER); … … 61 93 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL); 62 94 63 const char* vertex_shader = 64 "#version 410\n" 65 "in vec3 vp;" 66 "void main() {" 67 " gl_Position = vec4(vp, 1.0);" 68 "}"; 95 //string vertexShaderString = loadShader(); 69 96 70 GLuint vs = glCreateShader(GL_VERTEX_SHADER); 71 glShaderSource(vs, 1, &vertex_shader, NULL); 72 glCompileShader(vs); 73 74 const char* fragment_shader = 75 "#version 410\n" 76 "out vec4 frag_color;" 77 "void main() {" 78 " frag_color = vec4(0.5, 0.0, 0.5, 1.0);" 79 "}"; 80 81 GLuint fs = glCreateShader(GL_FRAGMENT_SHADER); 82 glShaderSource(fs, 1, &fragment_shader, NULL); 83 glCompileShader(fs); 97 GLuint vs = loadShader(GL_VERTEX_SHADER, "./test.vert"); 98 GLuint fs = loadShader(GL_FRAGMENT_SHADER, "./test.frag"); 84 99 85 100 GLuint shader_program = glCreateProgram(); … … 93 108 glBindVertexArray(vao); 94 109 glDrawArrays(GL_TRIANGLES, 0, 3); 110 95 111 glfwPollEvents(); 96 112 glfwSwapBuffers(window); 113 114 if (GLFW_PRESS == glfwGetKey(window, GLFW_KEY_ESCAPE)) { 115 glfwSetWindowShouldClose(window, 1); 116 } 97 117 } 98 118 … … 100 120 return 0; 101 121 } 122 123 GLuint loadShader(GLenum type, string file) { 124 cout << "Loading shader from file " << file << endl; 125 126 ifstream shaderFile(file); 127 GLuint shaderId = 0; 128 129 if (shaderFile.is_open()) { 130 string line, shaderString; 131 132 while(getline(shaderFile, line)) { 133 shaderString += line + "\n"; 134 } 135 shaderFile.close(); 136 const char* shaderCString = shaderString.c_str(); 137 138 shaderId = glCreateShader(type); 139 glShaderSource(shaderId, 1, &shaderCString, NULL); 140 glCompileShader(shaderId); 141 142 cout << "Loaded successfully" << endl; 143 } else { 144 cout << "Failed to loade the file" << endl; 145 } 146 147 return shaderId; 148 }
Note:
See TracChangeset
for help on using the changeset viewer.