done hellow triangle.
This commit is contained in:
		
							parent
							
								
									437b1ccd44
								
							
						
					
					
						commit
						56bc859562
					
				
					 2 changed files with 89 additions and 44 deletions
				
			
		
							
								
								
									
										127
									
								
								mybox.cpp
									
										
									
									
									
								
							
							
						
						
									
										127
									
								
								mybox.cpp
									
										
									
									
									
								
							| 
						 | 
					@ -67,27 +67,52 @@ int main(void) {
 | 
				
			||||||
	// Create the fragment shader
 | 
						// Create the fragment shader
 | 
				
			||||||
	GLchar *shdr_src_fragment;
 | 
						GLchar *shdr_src_fragment;
 | 
				
			||||||
	readshader("fragment.frag", &shdr_src_fragment);
 | 
						readshader("fragment.frag", &shdr_src_fragment);
 | 
				
			||||||
	u32 fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
 | 
						u32 fragmentShader[2] = {
 | 
				
			||||||
	glShaderSource(fragmentShader, 1, &shdr_src_fragment, NULL);
 | 
							glCreateShader(GL_FRAGMENT_SHADER),
 | 
				
			||||||
	glCompileShader(fragmentShader);
 | 
							glCreateShader(GL_FRAGMENT_SHADER)
 | 
				
			||||||
	glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
 | 
						};
 | 
				
			||||||
 | 
						glShaderSource(fragmentShader[0], 1, &shdr_src_fragment, NULL);
 | 
				
			||||||
 | 
						glCompileShader(fragmentShader[0]);
 | 
				
			||||||
 | 
						glGetShaderiv(fragmentShader[0], GL_COMPILE_STATUS, &success);
 | 
				
			||||||
	freeshader(shdr_src_fragment);
 | 
						freeshader(shdr_src_fragment);
 | 
				
			||||||
	if (!success) {
 | 
						if (!success) {
 | 
				
			||||||
		glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
 | 
							glGetShaderInfoLog(fragmentShader[0], 512, NULL, infoLog);
 | 
				
			||||||
 | 
							std::cout << "ERROR SHADER::FRAGMENT::Compilation Failed\n" << 
 | 
				
			||||||
 | 
								infoLog << std::endl;
 | 
				
			||||||
 | 
							return -1;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						readshader("fragment2.frag", &shdr_src_fragment);
 | 
				
			||||||
 | 
						glShaderSource(fragmentShader[1], 1, &shdr_src_fragment, NULL);
 | 
				
			||||||
 | 
						glCompileShader(fragmentShader[1]);
 | 
				
			||||||
 | 
						glGetShaderiv(fragmentShader[1], GL_COMPILE_STATUS, &success);
 | 
				
			||||||
 | 
						freeshader(shdr_src_fragment);
 | 
				
			||||||
 | 
						if (!success) {
 | 
				
			||||||
 | 
							glGetShaderInfoLog(fragmentShader[1], 512, NULL, infoLog);
 | 
				
			||||||
		std::cout << "ERROR SHADER::FRAGMENT::Compilation Failed\n" << 
 | 
							std::cout << "ERROR SHADER::FRAGMENT::Compilation Failed\n" << 
 | 
				
			||||||
			infoLog << std::endl;
 | 
								infoLog << std::endl;
 | 
				
			||||||
		return -1;
 | 
							return -1;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Create the shader program
 | 
						// Create the shader program
 | 
				
			||||||
	u32 shaderProgram = glCreateProgram();
 | 
						u32 shaderProgram[2] = {glCreateProgram(), glCreateProgram()};
 | 
				
			||||||
	glAttachShader(shaderProgram, vertexShader);
 | 
						glAttachShader(shaderProgram[0], vertexShader);
 | 
				
			||||||
	glAttachShader(shaderProgram, fragmentShader);
 | 
						glAttachShader(shaderProgram[1], vertexShader);
 | 
				
			||||||
	glLinkProgram(shaderProgram);
 | 
						glAttachShader(shaderProgram[0], fragmentShader[0]);
 | 
				
			||||||
 | 
						glAttachShader(shaderProgram[1], fragmentShader[1]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
 | 
						glLinkProgram(shaderProgram[0]);
 | 
				
			||||||
 | 
						glGetProgramiv(shaderProgram[0], GL_LINK_STATUS, &success);
 | 
				
			||||||
	if (!success) {
 | 
						if (!success) {
 | 
				
			||||||
		glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
 | 
							glGetProgramInfoLog(shaderProgram[0], 512, NULL, infoLog);
 | 
				
			||||||
 | 
							std::cout << "ERROR SHADER::PROGRAM::Linking Failed\n" << 
 | 
				
			||||||
 | 
								infoLog << std::endl;
 | 
				
			||||||
 | 
							return -1;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						glLinkProgram(shaderProgram[1]);
 | 
				
			||||||
 | 
						glGetProgramiv(shaderProgram[1], GL_LINK_STATUS, &success);
 | 
				
			||||||
 | 
						if (!success) {
 | 
				
			||||||
 | 
							glGetProgramInfoLog(shaderProgram[1], 512, NULL, infoLog);
 | 
				
			||||||
		std::cout << "ERROR SHADER::PROGRAM::Linking Failed\n" << 
 | 
							std::cout << "ERROR SHADER::PROGRAM::Linking Failed\n" << 
 | 
				
			||||||
			infoLog << std::endl;
 | 
								infoLog << std::endl;
 | 
				
			||||||
		return -1;
 | 
							return -1;
 | 
				
			||||||
| 
						 | 
					@ -95,45 +120,56 @@ int main(void) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Free the old shader bits
 | 
						// Free the old shader bits
 | 
				
			||||||
	glDeleteShader(vertexShader);
 | 
						glDeleteShader(vertexShader);
 | 
				
			||||||
	glDeleteShader(fragmentShader);
 | 
						glDeleteShader(fragmentShader[0]);
 | 
				
			||||||
 | 
						glDeleteShader(fragmentShader[1]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/* ====================================================================== */
 | 
						/* ====================================================================== */
 | 
				
			||||||
	/* ====================================================================== */
 | 
						/* ====================================================================== */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Define the vertex shader dat.
 | 
						// Define the vertex shader dat.
 | 
				
			||||||
	f32 vertices[] = {
 | 
						f32 vertices_1[] = {
 | 
				
			||||||
		 0.5f,  0.5f, 0.0f,	// tr
 | 
							-0.9f, -0.66f, 0.0f,
 | 
				
			||||||
		 0.5f, -0.5f, 0.0f,	// br
 | 
							-0.1f, -0.66f, 0.0f,
 | 
				
			||||||
		-0.5f, -0.5f, 0.0f,	// bl
 | 
							-0.50f, +0.66f, 0.0f
 | 
				
			||||||
		-0.5f,  0.5f, 0.0f 	// tl
 | 
					 | 
				
			||||||
	};
 | 
						};
 | 
				
			||||||
 | 
						f32 vertices_2[sizeof(vertices_1)/sizeof(f32)];
 | 
				
			||||||
 | 
						for (u32 i = 0; i < sizeof(vertices_1)/sizeof(f32); i++) {
 | 
				
			||||||
 | 
							if (i % 3 == 0) {
 | 
				
			||||||
 | 
								vertices_2[i] = -1*vertices_1[i];
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								vertices_2[i] = vertices_1[i];
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	u32 indicies[] {
 | 
						u32 indicies[] {
 | 
				
			||||||
		0, 1, 3,	// t1
 | 
							0, 1, 2,	// t1
 | 
				
			||||||
		2, 1, 3		// t2
 | 
					 | 
				
			||||||
	};
 | 
						};
 | 
				
			||||||
	u32 VBO; // Vertex Buffer Object refernce number
 | 
						u32 VAO[2]; // Vertex Array Object refernce number
 | 
				
			||||||
	glGenBuffers(1, &VBO); // ask the GPU for a reference for one buffer
 | 
						glGenVertexArrays(2, VAO); // ask the GPU for a reference for one buffer
 | 
				
			||||||
	u32 VAO; // Vertex Array Object refernce number
 | 
						u32 VBO[2]; // Vertex Buffer Object refernce number
 | 
				
			||||||
	glGenVertexArrays(1, &VAO); // ask the GPU for a reference for one buffer
 | 
						glGenBuffers(2, VBO); // ask the GPU for a reference for one buffer
 | 
				
			||||||
	u32 EBO; // 
 | 
						u32 EBO; // 
 | 
				
			||||||
	glGenBuffers(1, &EBO); // ask the gpu for a reference for one buffer
 | 
						glGenBuffers(1, &EBO); // ask the gpu for a reference for one buffer
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
	// Bind VAO, meaning we 
 | 
						// Bind VAO, meaning we 
 | 
				
			||||||
	glBindVertexArray(VAO); 
 | 
						glBindVertexArray(VAO[0]); 
 | 
				
			||||||
	// Tell the GPU subsequent calls to modify buffers apply to this buffer, 
 | 
						// Tell the GPU subsequent calls to modify buffers apply to this buffer, 
 | 
				
			||||||
	// namely, the VBO buffer.
 | 
						// namely, the VBO buffer.
 | 
				
			||||||
	glBindBuffer(GL_ARRAY_BUFFER, VBO);
 | 
						glBindBuffer(GL_ARRAY_BUFFER, VBO[0]);
 | 
				
			||||||
	// copy in data to the VBO. I.e. give it a pointer to some data, tell it
 | 
						glBufferData(GL_ARRAY_BUFFER, sizeof(vertices_1), vertices_1, GL_STATIC_DRAW);
 | 
				
			||||||
	// how the entire buffer will be. sizeof(*[]) evaluates the full allocated
 | 
					 | 
				
			||||||
	// size of the data object.
 | 
					 | 
				
			||||||
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
 | 
					 | 
				
			||||||
	// Next, tell the GPU how to use the GL_ELEMENT_ARRAY_BUfFER.
 | 
					 | 
				
			||||||
	// In this case, read from the 0th index, 3 elements at a time as 
 | 
					 | 
				
			||||||
	// off of the indirected GL_ARRAY_BUFFER as floating point numbers. Do not
 | 
					 | 
				
			||||||
	// re-normalize the values (i.e., they're already normalized), and use a
 | 
					 | 
				
			||||||
	// stride of 3xf32 as we walk through the data in the original GL_ARRAY_BUFFER
 | 
					 | 
				
			||||||
	// Target the Atrib pointer in the VAO to the VBO inside it.
 | 
					 | 
				
			||||||
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(f32), (void *)0);
 | 
						glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(f32), (void *)0);
 | 
				
			||||||
 | 
						glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
 | 
				
			||||||
 | 
						glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indicies), indicies, GL_STATIC_DRAW);
 | 
				
			||||||
 | 
						// For the current Generic Vertex Attirbute Array object (VAO), indicate
 | 
				
			||||||
 | 
						// what index we should be starting in for subsequent draw calls.
 | 
				
			||||||
 | 
						glEnableVertexAttribArray(0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						glBindVertexArray(VAO[1]); 
 | 
				
			||||||
 | 
						glBindBuffer(GL_ARRAY_BUFFER, VBO[1]);
 | 
				
			||||||
 | 
						glBufferData(GL_ARRAY_BUFFER, sizeof(vertices_2), vertices_2, GL_STATIC_DRAW);
 | 
				
			||||||
 | 
						glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(f32), (void *)0);
 | 
				
			||||||
 | 
						glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
 | 
				
			||||||
 | 
						glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indicies), indicies, GL_STATIC_DRAW);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// For the VAO array (theoretically, but we have an array of 1), start
 | 
						// For the VAO array (theoretically, but we have an array of 1), start
 | 
				
			||||||
	// at the 0th element of the VAO array.
 | 
						// at the 0th element of the VAO array.
 | 
				
			||||||
	glEnableVertexAttribArray(0);
 | 
						glEnableVertexAttribArray(0);
 | 
				
			||||||
| 
						 | 
					@ -141,8 +177,6 @@ int main(void) {
 | 
				
			||||||
	// Setup the indexing into the VBO data. The EBO is the indirection buffer
 | 
						// Setup the indexing into the VBO data. The EBO is the indirection buffer
 | 
				
			||||||
	// that tells the GPU where to look into it's known GL_ARRAY_BUFFER object
 | 
						// that tells the GPU where to look into it's known GL_ARRAY_BUFFER object
 | 
				
			||||||
	// for data.
 | 
						// for data.
 | 
				
			||||||
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
 | 
					 | 
				
			||||||
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indicies), indicies, GL_STATIC_DRAW);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
 | 
						glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
 | 
				
			||||||
	// ??
 | 
						// ??
 | 
				
			||||||
| 
						 | 
					@ -157,23 +191,28 @@ int main(void) {
 | 
				
			||||||
		glClearColor(0.0f, 0.1f, 0.3f, 1.0f);
 | 
							glClearColor(0.0f, 0.1f, 0.3f, 1.0f);
 | 
				
			||||||
		glClear(GL_COLOR_BUFFER_BIT);
 | 
							glClear(GL_COLOR_BUFFER_BIT);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		glUseProgram(shaderProgram);
 | 
							glUseProgram(shaderProgram[0]);
 | 
				
			||||||
		glBindVertexArray(VAO);
 | 
							glBindVertexArray(VAO[0]);
 | 
				
			||||||
		glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
 | 
							glDrawArrays(GL_TRIANGLES, 0, 3);
 | 
				
			||||||
 | 
							glUseProgram(shaderProgram[1]);
 | 
				
			||||||
 | 
							glBindVertexArray(VAO[1]);
 | 
				
			||||||
 | 
							glDrawArrays(GL_TRIANGLES, 0, 3);
 | 
				
			||||||
 | 
							//glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// Finish up
 | 
							// Finish up
 | 
				
			||||||
		glfwSwapBuffers(window);
 | 
							glfwSwapBuffers(window);
 | 
				
			||||||
		glfwPollEvents();
 | 
							glfwPollEvents();
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	glDeleteVertexArrays(1, &VAO);
 | 
						glDeleteVertexArrays(2, VAO);
 | 
				
			||||||
	glDeleteBuffers(1, &VBO);
 | 
						glDeleteBuffers(2, VBO);
 | 
				
			||||||
	glDeleteBuffers(1, &EBO);
 | 
						glDeleteBuffers(1, &EBO);
 | 
				
			||||||
	glDeleteProgram(shaderProgram);
 | 
						glDeleteProgram(shaderProgram[0]);
 | 
				
			||||||
 | 
						glDeleteProgram(shaderProgram[1]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	i32 xpos, ypos;
 | 
						i32 xpos, ypos;
 | 
				
			||||||
	glfwGetWindowPos(window, &xpos, &ypos);
 | 
						glfwGetWindowPos(window, &xpos, &ypos);
 | 
				
			||||||
	std::cout << "Closed window at position\n  (X,Y) = "
 | 
						std::cout << "Closed window at pos`ition\n  (X,Y) = "
 | 
				
			||||||
		<< xpos << ", " << ypos << "\n" << std::endl;
 | 
							<< xpos << ", " << ypos << "\n" << std::endl;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	glfwTerminate();
 | 
						glfwTerminate();
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										6
									
								
								res/shaders/fragment2.frag
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								res/shaders/fragment2.frag
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,6 @@
 | 
				
			||||||
 | 
					#version 420 core
 | 
				
			||||||
 | 
					out vec4 FragColor;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void main() {
 | 
				
			||||||
 | 
					    FragColor = vec4(0.0f, 1.0f, 0.2f, 1.0f);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue