PDA

View Full Version : I am new to Opengl I am getting a black screen pls help



WildFire
07-07-2018, 04:23 AM
I am new to opengl
Getting a blank window.
the triangles were being rendered while ago
cant figure out the mistake
Pls help

main.cpp


#include<iostream>
#include<GL/glew.h>
#include<GLFW/glfw3.h>
#include<string.h>
#include"Window.h"
#include"Shader.h"
#include<glm.hpp>
#include<gtc\type_ptr.hpp>
#include<gtc\matrix_transform.hpp>
int main() {
Shader shader;
glm::mat4 model(1.0);
glm::mat4 projection;

Window mainWindow = Window(800, 600);
mainWindow.InitWindow();
shader.CreateMesh();
shader.CompileShader();
projection = glm::perspective(45.0f, mainWindow.getAspectRatio(),0.1f , 100.f);
model = glm::translate(model, glm::vec3(0.0f,0.0f,-7.5f));
while (!mainWindow.getShouldClose()) {
glfwPollEvents();
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
shader.UseShader();
model = glm::rotate(model, (float)glm::radians(000.1f), glm::vec3(0.0f,1.0f,0.0f));
glUniformMatrix4fv(shader.getModelLocation(), 1,GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(shader.getProjectionLocation(), 1, GL_FALSE, glm::value_ptr(projection));
shader.RenderMesh();
shader.UnbindShader();
glfwSwapBuffers(mainWindow.mainWindow);
}
return 0;
}


Shader.cpp


#include"Shader.h"
Shader::Shader() {
ShaderProgram = NULL;
vShader = " \n\
#version 330 \n\
layout (location = 0) in vec3 pos; \n\
uniform mat4 model; \n\
uniform mat4 projection; \n\
out vec4 vColor;\n\
void main() {\n\
gl_Position =projection * model* vec4(0.4*pos.x ,0.4*pos.y, 0.4*pos.z, 1.0);\n\
vColor = vec4 (clamp(pos,0.0f,1.0f),1.0);\n\
}";
fShader = "\n\
#version 330\n\
in vec3 pos;\n\
in vec4 vColor; \n\
out vec4 color; \n\
void main() { \n\
color = vColor; \n\
}"
;
}
Shader::Shader(char* verthader,char* fragShader) {
vShader = verthader;
fShader = fragShader;
}
void Shader::AddShader(const char* shaderCode, GLuint theProgram, GLenum shaderType) {
GLuint theShader = glCreateShader(shaderType);
const GLchar* theCode[1];
theCode[0] = shaderCode;
GLint codeLength[1];
codeLength[0] = strlen(shaderCode);
glShaderSource(theShader, 1, theCode, codeLength);
glCompileShader(theShader);
GLint result = 0;
GLchar eLog[1024] = { 0 };
glGetShaderiv(theShader, GL_COMPILE_STATUS, &result);
if (!result)
{
glGetShaderInfoLog(theShader, 1024, NULL, eLog);
fprintf(stderr, "Error compiling the %d Shader: '%s'\n", shaderType, eLog);
return;
}
glAttachShader(theProgram, theShader);
}
void Shader::CompileShader() {
ShaderProgram = glCreateProgram();
if (!ShaderProgram)
{
printf("Failed to create Shader\n");
return;
}
AddShader(vShader, ShaderProgram, GL_VERTEX_SHADER);
AddShader(fShader, ShaderProgram, GL_FRAGMENT_SHADER);
GLint result = 0;
GLchar eLog[1024] = { 0 };
glLinkProgram(ShaderProgram);
glGetProgramiv(ShaderProgram, GL_LINK_STATUS, &result);
if (!result)
{
glGetProgramInfoLog(ShaderProgram, sizeof(eLog), NULL, eLog);
printf("Error linking program: '%s'\n", eLog);
return;
}
glValidateProgram(ShaderProgram);
glGetProgramiv(ShaderProgram, GL_VALIDATE_STATUS, &result);
if (!result)
{
glGetProgramInfoLog(ShaderProgram, sizeof(eLog), NULL, eLog);
printf("Error validating program: '%s'\n", eLog);
return;
}
locateUniforms();
}
void Shader::CreateMesh() {
GLfloat vertices[] = {
-1.0f, -1.0f, 0.0f,
0.0f, -1.0f, 1.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f
};
unsigned int indices[] = {
0, 3, 1,
1, 3, 2,
2, 3, 0,
0, 1, 2
};

glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glGenBuffers(1, &IBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,IBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,sizeof(vertices), 0);
glEnableVertexAttribArray(0);

glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
void Shader::RenderMesh() {
glBindVertexArray(VAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
glDrawElements(GL_TRIANGLES, 12, GL_UNSIGNED_INT, 0);

}
void Shader::UseShader()
{
glUseProgram(ShaderProgram);
}
void Shader::UnbindShader()
{
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glUseProgram(0);
}
GLuint Shader::getModelLocation()
{
return modelLocation;
}
GLuint Shader::getProjectionLocation() {
return projectionLocation;
}
void Shader::locateUniforms() {
modelLocation = glGetUniformLocation(ShaderProgram, "model");
projectionLocation = glGetUniformLocation(ShaderProgram, "projection");
}