'C++ opengl fragment shader not filling the screen with white
So basically I tried to fill the whole screen with white using the fragment shader, however it does not seem to work. P.S.- Im new to programming shaders in c++, I have done so sucessfully in java so its not like I dont understand anything, but its not like I understand everything either. P.S.S. Sorry for bad english.
main.cpp
#include<glad/glad.h>
#include<GLFW/glfw3.h>
#include "main.h"
#include <filesystem>
#include <string>
#include <fstream>
static const GLchar* readFromFile(const GLchar* pathToFile)
{
std::string content;
std::ifstream fileStream(pathToFile, std::ios::in);
if (!fileStream.is_open()) {
std::cerr << "Could not read file " << pathToFile << ". File does not exist." << std::endl;
return "";
}
std::string line = "";
while (!fileStream.eof()) {
std::getline(fileStream, line);
content.append(line + "\n");
}
fileStream.close();
std::cout << content << std::endl;
return content.c_str();
}
static void processInput(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_ENTER && action == GLFW_PRESS) {
glClearColor(1.06666666666667f, 0.168888888888889f, 0.306666666666667f, 1.0f);
}
}
int main()
{
const char* vertexShaderSource = readFromFile("vertex.glsl");
const char* fragmentShaderSource = readFromFile("frag.glsl");
//initialize GLFW
glfwInit();
//tell GLFW the openGl version
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
//tell GLFW that we will use the core profile
//witch means that we will only use the modern funcions and dont neeed the old ones
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
//create a window
GLFWwindow *window = glfwCreateWindow(800, 800, "GLFW", NULL, NULL);
//check if we created a widow succesfully
if (window == NULL)
{
std::cout << "Failed to create window" << std::endl;
glfwTerminate();
return -1;
}
//set the created window as the current context
glfwMakeContextCurrent(window);
//tell glad to configure openGL
gladLoadGL();
//set the viewport to go from x = 0, y = 0 (bottom left corner) to x = 800, y = 800 (top right corner)
glViewport(0, 0, 800, 800);
// vertex shader
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
// fragment shader
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
glUseProgram(shaderProgram);
//create references to the vertex array object, vertex buffer object and the element array buffer
GLuint VAO, VBO, EBO;
//generate them
glGenVertexArrays(1, &VAO);
//bind them
glBindVertexArray(VAO);
//configure the vertex attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
//enable it
glEnableVertexAttribArray(0);
//unbind not needed VBO's, VAO's and the EBO's
glBindVertexArray(0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
//specify the clear color and clean the back buffer
glClearColor(0.07f, 0.13f, 0.17f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
//swap the buffers
glfwSwapBuffers(window);
glfwSetKeyCallback(window, processInput);
while(!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
glfwPollEvents();
}
//dispose of all the stuff we made
glfwDestroyWindow(window);
glfwTerminate();
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &EBO);
//return the exit code 0 = succesfull
return 0;
}
Fragment shader
out vec4 gl_FragColor
void main()
{
gl_FragColor = vec4(1f, 0f, 0f, 1f);
}
EDIT: So turns out i totally forgot that you have to draw something on the screen for the fragment shader to take effect. Thanks Rabbid76
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
