I am trying to learn OpenGl using this tutorial series. It all works fine up to tutorial 4 (Textures) but after tutorial 5 (3D Motion), the triangle which I have drawn previously disappears.
I have seen the video twice to make sure that I have not done any spelling mistake or something like this.
Code for relevant parts is given below:
main.cpp
#include <iostream>
#include <GL/glew.h>
#include <glm/glm.hpp>
#include "display.h"
#include "shader.h"
#include "mesh.h"
#include "texture.h"
#include "transform.h"
using namespace std;
int main()
{
Display display(800, 600, "Hello World!");
Vertex vertices[] = {
Vertex(glm::vec3(-0.5, -0.5, 0), glm::vec2(0.0, 0.0)),
Vertex(glm::vec3(0.5, -0.5, 0), glm::vec2(1.0, 0.0)),
Vertex(glm::vec3(0, 0.5, 0), glm::vec2(0.5, 1.0)),
};
Mesh mesh(vertices, sizeof(vertices) / sizeof(vertices[0]));
Shader shader("./res/basicShader");
Texture texture("./res/bricks.jpg");
Transform transform;
while(!display.IsClosed())
{
display.Clear(0.0f, 0.1f, 0.3f, 1.0f);
shader.Bind();
texture.Bind(0);
shader.Update(transform);
mesh.Draw();
display.Update();
}
return 0;
}
shader.h
#ifndef SHADER_H
#define SHADER_H
#include "transform.h"
#include <string>
#include <GL/glew.h>
class Shader
{
public:
Shader(const std::string& fileName);
void Bind();
void Update(const Transform& transform);
virtual ~Shader();
protected:
private:
static const unsigned int NUM_SHADERS = 2;
Shader(const Shader& other){}
//Shader& operator=(const Shader& other);
void operator=(const Shader& other){}
enum
{
TRANSFORM_U,
NUM_UNIFORMS
};
GLuint m_program;
GLuint m_shaders[NUM_SHADERS];
GLuint m_uniforms[NUM_UNIFORMS];
};
#endif // SHADER_H
shader.cpp
#include "shader.h"
#include <iostream>
#include <fstream>
#include <string>
static void CheckShaderError(GLuint shader, GLuint flag, bool isProgram, const std::string& errorMessage);
static std::string LoadShader(const std::string& fileName);
static GLuint CreateShader(const std::string& text, GLenum shaderType);
Shader::Shader(const std::string& fileName)
{
//ctor
m_program = glCreateProgram();
m_shaders[0] = CreateShader(LoadShader(fileName + ".vs"), GL_VERTEX_SHADER);
m_shaders[1] = CreateShader(LoadShader(fileName + ".fs"), GL_FRAGMENT_SHADER);
for(unsigned int i = 0; i<NUM_SHADERS; ++i)
glAttachShader(m_program, m_shaders[i]);
glBindAttribLocation(m_program, 0, "position");
glBindAttribLocation(m_program, 1, "texCoord");
glLinkProgram(m_program);
CheckShaderError(m_program, GL_LINK_STATUS, true, "Error: Program linking failed: ");
glValidateProgram(m_program);
CheckShaderError(m_program, GL_VALIDATE_STATUS, true, "Error: Program is invalid: ");
m_uniforms[TRANSFORM_U] = glGetUniformLocation(m_program, "transform");
}
Shader::~Shader()
{
//dtor
for(unsigned int i = 0; i<NUM_SHADERS; ++i)
{
glDetachShader(m_program, m_shaders[i]);
glDeleteShader(m_shaders[i]);
}
glDeleteProgram(m_program);
}
static GLuint CreateShader(const std::string& text, GLenum shaderType)
{
GLuint shader = glCreateShader(shaderType);
if(shader == 0)
std::cerr<<"Error: Shader creation failed!"<<std::endl;
const GLchar* sourceShaderStrings[1];
GLint sourceShaderStringLengths[1];
sourceShaderStrings[0] = text.c_str();
sourceShaderStringLengths[0] = text.length();
glShaderSource(shader, 1, sourceShaderStrings, sourceShaderStringLengths);
glCompileShader(shader);
CheckShaderError(shader, GL_COMPILE_STATUS, false, "Error: Shader compilation failed: ");
return shader;
}
static std::string LoadShader(const std::string& fileName)
{
std::ifstream file;
file.open(fileName.c_str());
std::string output;
std::string line;
if(file.is_open())
{
while(file.good())
{
getline(file, line);
output.append(line + "\n");
}
}
else
{
std::cerr<<"Unable to load shader: "<<fileName<<std::endl;
}
return output;
}
static void CheckShaderError(GLuint shader, GLuint flag, bool isProgram, const std::string& errorMessage)
{
GLint success = 0;
GLchar error[1024] = {0};
if(isProgram)
glGetProgramiv(shader, flag, &success);
else
glGetShaderiv(shader, flag, &success);
if(success == GL_FALSE)
{
if(isProgram)
glGetProgramInfoLog(shader, sizeof(error), NULL, error);
else
glGetShaderInfoLog(shader, sizeof(error), NULL, error);
std::cerr<<errorMessage<<": '"<<error<<"' "<<std::endl;
}
}
void Shader::Bind()
{
glUseProgram(m_program);
}
void Shader::Update(const Transform& transform)
{
glm::mat4 model = transform.GetModel();
glUniformMatrix4fv(m_uniforms[TRANSFORM_U], 1, GL_FALSE, &model[0][0]);
}
transform.h
#ifndef TRANSFORM_H
#define TRANSFORM_H
#include <glm/glm.hpp>
#include <glm/gtx/transform.hpp>
class Transform
{
public:
Transform(const glm::vec3& pos = glm::vec3(), const glm::vec3& rot = glm::vec3(), const glm::vec3& scale = glm::vec3(1.0f, 1.0f, 1.0f)):
m_pos(pos),
m_rot(rot),
m_scale(scale)
{
}
inline glm::mat4 GetModel() const
{
glm::mat4 posMatrix = glm::translate(m_pos);
glm::mat4 rotXMatrix = glm::rotate(m_rot.x, glm::vec3(1, 0, 0));
glm::mat4 rotYMatrix = glm::rotate(m_rot.y, glm::vec3(0, 1, 0));
glm::mat4 rotZMatrix = glm::rotate(m_rot.z, glm::vec3(0, 0, 1));
glm::mat4 scaleMatrix = glm::scale(m_rot);
glm::mat4 rotMatrix = rotZMatrix * rotYMatrix * rotXMatrix;
return posMatrix * rotMatrix * scaleMatrix;
}
inline glm::vec3& GetPos(){return m_pos;}
inline glm::vec3& GetRot(){return m_rot;}
inline glm::vec3& GetScale(){return m_scale;}
inline void SetPos(const glm::vec3& pos){m_pos = pos;}
inline void SetRot(const glm::vec3& rot){m_rot = rot;}
inline void SetScale(const glm::vec3& scale){m_scale = scale;}
virtual ~Transform();
protected:
private:
Transform(const Transform& other){}
void operator=(const Transform& other){}
glm::vec3 m_pos;
glm::vec3 m_rot;
glm::vec3 m_scale;
};
#endif // TRANSFORM_H
Vertex Shader
Filename: basicShader.vs
#version 120
attribute vec3 position;
attribute vec2 texCoord;
varying vec2 texCoord0;
uniform mat4 transform;
void main()
{
//gl_Position = vec4(position, 1.0); // #1
gl_Position = transform * vec4(position, 1.0); // #2
// if I uncomment Statement #1 and put comment on #2, it works obviously I can not move the triangle.
texCoord0 = texCoord;
}
Frangment Shader
Filename: basicShader.fs
#version 120
uniform sampler2D diffuse;
varying vec2 texCoord0;
void main()
{
gl_FragColor = texture2D(diffuse, texCoord0); //vec2(0.2, 0.2)); //vec4(1.0, 0.0, 0.0, 1.0);
}
You use your rot vector to define your scale matrix.
glm::mat4 scaleMatrix = glm::scale(m_rot);
you should use your scale vector
glm::mat4 scaleMatrix = glm::scale(m_scale);
You were scalling by a 0 which set your point all of your point to the (0,0,0) position, they will collide and your face disapear
Related
My program did not render any objects and showed a blank screen. I have checked for any openGL errors and haven't been able to find any.
Header file (Display.h):
#include "glew.h"
#include "glfw3.h"
#include <iostream>
class Display
{
public:
Display() {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
if (window == nullptr)
{
window = glfwCreateWindow(649, 489, "GL_Lib", NULL, NULL);
}
glfwMakeContextCurrent(window);
glewInit();
};
static GLFWwindow* window;
void Create_Buffer(float f, float s, float t, float forth, float fifth, float sixth, unsigned int* ptr) {
float positions[6] = { f,s,t,forth,fifth,sixth };
glGenBuffers(1, ptr);
glBindBuffer(GL_ARRAY_BUFFER, *ptr);
glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), positions, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), 0);
return;
}
static unsigned int Create_Shader(const std::string& VertexShader, const std::string FragmentShader) {
unsigned int program = glCreateProgram();
unsigned int vs = Compile_Shader(VertexShader, GL_VERTEX_SHADER);
unsigned int fs = Compile_Shader(FragmentShader, GL_FRAGMENT_SHADER);
glAttachShader(program, vs);
glAttachShader(program, fs);
glLinkProgram(program);
glValidateProgram(program);
int status;
glGetProgramiv(program, GL_VALIDATE_STATUS, &status);
std::cout << status;
glDeleteShader(vs);
glDeleteShader(fs);
return program;
}
static unsigned int Compile_Shader(const std::string& source, unsigned int type)
{
unsigned int id = glCreateShader(type);
const char* src = source.c_str();
glShaderSource(id, 1, &src, nullptr);
glCompileShader(id);
//TODO: Error Handling
return id;
}
static void GLCheckError() {
while (GLenum error = glGetError())
{
std::cout << "Error (" << error << ")" << std::endl;
}
}
static void GLClearError()
{
while(glGetError != GL_NO_ERROR);
}
};
Cpp file (Main.cpp):
#pragma once
#include "glew.h"
#include "glfw3.h"
#include "Display.h"
#include <iostream>
int main()
{
Display* dis = new Display();
unsigned int* buffer = new unsigned int;
dis->Create_Buffer(-0.5f, -0.5f, 0.0f, -0.5f, 0.5f, -0.5f, buffer);
std::string vertexShader =
"#version 330 core\n"
"\n"
"layout(location = 0) in vec4 position;\n"
"\n"
"void main()\n"
"{\n"
"gl_Position = position;\n"
"}\n";
std::string fragmentShader =
"#version 330 core\n"
"\n"
"layout(location = 0) out vec4 color;\n"
"\n"
"void main()\n"
"{\n"
"color = vec4(1.0, 0.0, 0.0, 1.0);\n"
"}\n";
unsigned int shader = Display::Create_Shader(vertexShader, fragmentShader);
glBindBuffer(GL_ARRAY_BUFFER, 0);
while (!glfwWindowShouldClose(Display::window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(shader);
// draw points 0-3 from the currently bound VAO with current in-use shader
glDrawArrays(GL_TRIANGLES, 0, 3);
/* Swap front and back buffers */
glfwSwapBuffers(Display::window);
/* Poll for and process events */
glfwPollEvents();
Display::GLCheckError();
}
glfwTerminate();
return 0;
}
I also have a Display.cpp file but this is just to resolve the GLFW window symbol.
The coordinates do not define a triangle, but a straight line. For this reason, no triangle primitive is rendered. Change the coordinates:
dis->Create_Buffer(-0.5f, -0.5f, 0.0f, -0.5f, 0.5f, -0.5f, buffer);
dis->Create_Buffer(-0.5f, -0.5f, 0.0f, 0.5f, 0.5f, -0.5f, buffer);
I need to code the fragment shader so that the triangle has a simple gradient effect. That is, so that its transparency decreases from left to right.
I tried this but it fails:
#version 120
uniform float startX = gl_FragCoord.x;
void main(void) {
gl_FragColor[0] = 0.0;
gl_FragColor[1] = 0.0;
gl_FragColor[2] = 1.0;
gl_FragColor[3] = startX / gl_FragCoord.x;
}
The full code:
#include <cstdlib>
#include <iostream>
using namespace std;
#include <GL/glew.h>
#include <SDL.h>
GLuint program;
GLint attribute_coord2d;
bool init_resources(void)
{
GLint compile_ok, link_ok = GL_FALSE;
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
const char* vs_source = R"(
#version 120
attribute vec2 coord2d;
void main(void) {
gl_Position = vec4(coord2d, 0.0, 1.0);
}
)";
glShaderSource(vs, 1, &vs_source, NULL);
glCompileShader(vs);
glGetShaderiv(vs, GL_COMPILE_STATUS, &compile_ok);
if (!compile_ok) {
cerr << "Error in vertex shader" << endl;
return false;
}
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
const char* fs_source = R"(
#version 120
uniform float startX = gl_FragCoord.x;
void main(void) {
gl_FragColor[0] = 0.0;
gl_FragColor[1] = 0.0;
gl_FragColor[2] = 1.0;
gl_FragColor[3] = startX / gl_FragCoord.x;
}
)";
glShaderSource(fs, 1, &fs_source, NULL);
glCompileShader(fs);
glGetShaderiv(fs, GL_COMPILE_STATUS, &compile_ok);
if (!compile_ok) {
cerr << "Error in fragment shader" << endl;
return false;
}
program = glCreateProgram();
glAttachShader(program, vs);
glAttachShader(program, fs);
glLinkProgram(program);
glGetProgramiv(program, GL_LINK_STATUS, &link_ok);
if (!link_ok) {
cerr << "Error in glLinkProgram" << endl;
return false;
}
const char* attribute_name = "coord2d";
attribute_coord2d = glGetAttribLocation(program, attribute_name);
if (attribute_coord2d == -1) {
cerr << "Could not bind attribute " << attribute_name << endl;
return false;
}
return true;
}
void render(SDL_Window* window)
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(program);
glEnableVertexAttribArray(attribute_coord2d);
GLfloat triangle_vertices[] = {
0.0, 0.8,
-0.8, -0.8,
0.8, -0.8,
};
glVertexAttribPointer(attribute_coord2d, 2, GL_FLOAT, GL_FALSE, 0, triangle_vertices);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(attribute_coord2d);
SDL_GL_SwapWindow(window);
}
void free_resources()
{
glDeleteProgram(program);
}
void mainLoop(SDL_Window* window)
{
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
while (true)
{
SDL_Event ev;
while (SDL_PollEvent(&ev))
{
if (ev.type == SDL_QUIT)
return;
}
render(window);
}
}
int main(int argc, char* argv[])
{
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window = SDL_CreateWindow("My First Triangle", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL);
SDL_GL_CreateContext(window);
GLenum glew_status = glewInit();
if (glew_status != GLEW_OK) {
cerr << "Error: glewInit: " << glewGetErrorString(glew_status) << endl;
return EXIT_FAILURE;
}
if (!init_resources())
return EXIT_FAILURE;
mainLoop(window);
free_resources();
return EXIT_SUCCESS;
}
How to do it right?
vYou can not initialize a uniform with gl_FragCoord.x. A uniform initialization is determined at link time.
uniform float startX = gl_FragCoord.x;
uniform float startX;
You have to set the unform with glUniform1f.
gl_FragCoord.xy are not the vertex coordinates. gl_FragCoord.xy are the window coordinate in pixels. You have to divide gl_FragCoord.xy by the size of the viewport:
#version 120
void main(void) {
gl_FragColor = vec4(0.0, 0.0, 0.0, gl_FragCoord.x / 640.0);
}
Or passing coord2d to the fragment shader:
#version 120
attribute vec2 coord2d;
varying vec2 coord;
void main(void) {
coord = coord2d;
gl_Position = vec4(coord2d, 0.0, 1.0);
}
#version 120
varying vec2 coord;
void main(void) {
float alpha = 2.0 / (1.0 - coord.y);
gl_FragColor = vec4(0.0, 0.0, 1.0, alpha);
}
Or use a color attribute:
#version 120
attribute vec2 coord2d;
attribute vec4 attrColor;
varying vec4 color;
void main(void) {
color = attrColor;
gl_Position = vec4(coord2d, 0.0, 1.0);
}
#version 120
varying vec4 color;
void main(void) {
gl_FragColor = color;
}
attribute_color = glGetAttribLocation(program, "attrColor");
GLfloat triangle_colors[] = {
0.0f, 0.0f, 1.0f, 0.5f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f, 1.0f
};
glEnableVertexAttribArray(attribute_color);
glVertexAttribPointer(attribute_color, 4, GL_FLOAT, GL_FALSE, 0, triangle_colors);
I'm currently working on a project involving two cubes. The first cube meant to be static, while the second is going to rotate, or orbit, around the first. I'm very new to OpenGL programming, so I'm having quite a bit of difficulty getting this to work. Can anyone point me in the right direction? So far, I just have two cubes, but I am unable to get them to move as if they were animated. Here is my code if you would like to offer suggestions. Thank you so much!
Main:
#include "vgl.h"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <iostream>
#include <vector>
#include "Box.h"
#include "Camera.h"
#include "VertexBufferData.h"
#include <glm/gtc/quaternion.hpp>
#include <glm/gtx/quaternion.hpp>
using namespace std;
using namespace glm;
#define SPACEBAR_KEY 32
#define ESCAPE_KEY 033
Camera* camera;
vector<Box * > gameObjects;
bool keyDown[255];
void CheckKeyboard(){
if (keyDown['a'])
camera->RotateLeft();
if (keyDown['d'])
camera->RotateRight();
if (keyDown['w'])
camera->MoveForward();
if (keyDown['s'])
camera->MoveBackWard();
if (keyDown['e'])
camera->StrafeRight();
if (keyDown['q'])
camera->StrafeLeft();
}
void closeApp()
{
delete camera;
for (auto it = gameObjects.begin(); it != gameObjects.end(); ++it) {
delete (*it);
}
}
void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case ESCAPE_KEY: // ASCII Escape Key Code
closeApp();
exit(EXIT_SUCCESS);
break;
default:
keyDown[key] = true;
}
glutPostRedisplay();
}
void keyboardUp(unsigned char key, int x, int y)
{
keyDown[key] = false;
}
void mouseWheel(int button, int direction, int x, int y)
{
if (button == 16)
camera->ResetFOV();
else if (direction > 0)
camera->ZoomIn();
else
camera->ZoomOut();
}
void mouseMovement(int x, int y)
{
static bool warp = true;
if (warp)
{
if (x>glutGet(GLUT_WINDOW_WIDTH) / 2)
camera->RotateRight();
else if (x<glutGet(GLUT_WINDOW_WIDTH) / 2)
camera->RotateLeft();
if (y>glutGet(GLUT_WINDOW_HEIGHT) / 2)
camera->RotateUp();
else if (y<glutGet(GLUT_WINDOW_HEIGHT) / 2)
camera->RotateDown();
glutWarpPointer(glutGet(GLUT_WINDOW_WIDTH) / 2, glutGet(GLUT_WINDOW_HEIGHT) / 2);
warp = false;
}
else
warp = true;
}
void timer(int value)
{
glutPostRedisplay();
glutTimerFunc(25, timer, 0);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
CheckKeyboard();
camera->Update();
for (auto it = gameObjects.begin(); it != gameObjects.end(); ++it) {
(*it)->shaderProgram->bindShader();
glm::mat4 MVP = camera->ProjectionMatrix * camera->ViewMatrix * (*it)->getModelMatrix();
(*it)->shaderProgram->sendUniform4x4("MVP", glm::value_ptr(MVP));
(*it)->Draw();
}
glutSwapBuffers();
}
void init()
{
int i = 0, forever = 0;
camera = new Camera();
memset(keyDown, false, sizeof(keyDown));
glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
glutWarpPointer(glutGet(GLUT_WINDOW_WIDTH) / 2, glutGet(GLUT_WINDOW_HEIGHT) / 2);
glutSetCursor(GLUT_CURSOR_FULL_CROSSHAIR);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glEnable(GL_CULL_FACE);
VertexBufferData vertexBufferData = VertexBufferData("Data\\Models\\Objects.xml");
gameObjects.push_back(new Box(vertexBufferData, glm::vec3(-2.0f, 0.0f, 0.0f), "Data\\Shaders\\Vertex.shader", "Data\\Shaders\\Fragment.shader"));
gameObjects.push_back(new Box(vertexBufferData, glm::vec3(2.0f, 0.0f, 0.0f), "Data\\Shaders\\Vertex.shader", "Data\\Shaders\\Fragment.shader"));
gameObjects[1]->setRotation(45.0f, vec3(0.0f, 0.0f, 1.0f));
gameObjects[0]->setScaleMatrix(vec3(2, 2, 2));
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(1024, 768);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutCreateWindow("Satterwhite_Project_3");
if (glewInit())
{
cerr << "Unable to init glew" << endl;
exit(EXIT_FAILURE);
}
init();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutKeyboardUpFunc(keyboardUp);
glutPassiveMotionFunc(mouseMovement);
glutMouseWheelFunc(mouseWheel);
glutTimerFunc(25, timer, 0);
glutMainLoop();
return 0;
}
Box.cpp
#include "Box.h"
Box::Box(VertexBufferData &vbd, vec3 initialPosition,const string vertexShader, const string fragmentShader):GameObject(vertexShader, fragmentShader)
{
Position=initialPosition;
vertexBufferData =vbd;
RotationMatrix=mat4(1.0f);
OrbitMatrix = mat4(1.0f);
ScaleMatrix=mat4(1.0f);
TranslationMatrix =translate(glm::mat4(1.0f), Position);
if (!shaderProgram->initialize())
{
std::cerr << "Could not initialize the shaders" << std::endl;
}
shaderProgram->bindShader();
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, vertexBufferData.verticiesSize, &vertexBufferData.verticies[0], GL_STATIC_DRAW);
glGenBuffers(1, &fragmentBuffer);
glBindBuffer(GL_ARRAY_BUFFER, fragmentBuffer);
glBufferData(GL_ARRAY_BUFFER, vertexBufferData.colorSize, &vertexBufferData.colors[0], GL_STATIC_DRAW);
shaderProgram->linkProgram();
}
void Box::setRotation(float degrees, vec3 axis)
{
RotationMatrix=rotate(glm::mat4(1.0f), degrees, axis);
RotationMatrix=rotate(RotationMatrix, degrees, vec3(1.0f,0.0f,0.0f));
}
void Box::setScaleMatrix(vec3 scale)
{
ScaleMatrix = glm::scale(mat4(1.0f), scale);
}
/*void Box::setOrbitMatrix(vec3 Position, vec3 axis)
{
OrbitMatrix =
}*/
void Box::Draw()
{
shaderProgram->bindShader();
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glVertexAttribPointer(0, 3, GL_FLOAT,GL_FALSE, 0, BUFFER_OFFSET(0));
glBindBuffer(GL_ARRAY_BUFFER, fragmentBuffer);
glVertexAttribPointer(1, 3, GL_FLOAT,GL_FALSE, 0, BUFFER_OFFSET(0));
glDrawArrays(GL_TRIANGLES, 0, vertexBufferData.verticies.size());
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
}
Box.h
#pragma once
#include "GameObject.h"
class Box : public GameObject
{
private:
mat4 ModelMatrix;
mat4 RotationMatrix;
mat4 OrbitMatrix;
mat4 TranslationMatrix;
mat4 ScaleMatrix;
public:
void Draw();
virtual ObjectType getType() const { return BOX; }
mat4 getModelMatrix() {return TranslationMatrix*RotationMatrix*ScaleMatrix;}
mat4 getMRotationMatrix() {return RotationMatrix;}
mat4 getTranslationMatrix() {return TranslationMatrix;}
mat4 getOrbitMatrix(){ return OrbitMatrix; }
mat4 getScaleMatrix() {return ScaleMatrix;}
void setRotation(float degrees, vec3 axis);
void setScaleMatrix(vec3 scale);
void setOrbitMatrix(vec3 Position, vec3 axis);
Box(VertexBufferData &vertexBufferData, vec3 initialPosition, const string vertexShader, const string fragmentShader);
vec3 Position;
mat4 rot;
mat4 scale;
};
I'm having a problem with glCreateShaders. It always returns 0. I'm using Glew with SDL and whenever I run the program, it says:
0(1) : error C0000: syntax error, unexpected '}' at token "}"
Shader Shaders/colorShading.vert failed to compile!
main.cpp:
#include <iostream>
#include "MainGame.h"
int main(int argc, char** argv)
{
MainGame maingame;
maingame.run();
return 0;
}
MainGame.h:
//Core -> initializing glew, sdl etc...
//Just ignore the sprite class
#pragma once
#include <SDL/SDL.h>
#include <GL/glew.h>
#include <iostream>
#include <string>
#include "Errors.h"
#include "GLSLProgram.h"
enum class GameState {PLAY, EXIT};
#include "Sprite.h"
class MainGame
{
public:
MainGame(void);
~MainGame(void);
void run();
private:
void initSystems();
void initShaders();
void gameLoop();
void processInput();
void drawGame();
SDL_Window* _window;
int _screenWidth;
int _screenHeight;
GameState _gameState;
Sprite _sprite;
GLSLProgram _colorProgram;
};
MainGame.cpp:
#include "MainGame.h"
MainGame::MainGame(void)
{
_window = nullptr;
_screenWidth = 1024;
_screenHeight = 700;
_gameState = GameState::PLAY;
}
MainGame::~MainGame(void)
{
}
void MainGame::run()
{
initSystems();
_sprite.init(-1.0f, -1.0f, 1.0f, 1.0f);
gameLoop();
}
void MainGame::initSystems()
{
//Initialize SDL
SDL_Init(SDL_INIT_EVERYTHING);
_window = SDL_CreateWindow("Game Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, _screenWidth, _screenHeight, SDL_WINDOW_OPENGL);
if (_window == nullptr)
{
fatalError("SDL Window could not be created!");
}
SDL_GLContext glContext = SDL_GL_CreateContext(_window);
if (glContext == nullptr)
fatalError("SDL_GL context could not be created!");
GLenum error = glewInit();
if (error != GLEW_OK)
fatalError("Could not initialize glew!");
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
initShaders();
}
void MainGame::initShaders()
{
_colorProgram.compileShaders("Shaders/colorShading.vert", "Shaders/colorShading.frag");
_colorProgram.addAttribute("vertexPosition");
_colorProgram.linkShaders();
}
void MainGame::gameLoop()
{
while (_gameState != GameState::EXIT)
{
processInput();
drawGame();
}
}
void MainGame::processInput()
{
SDL_Event evnt;
while (SDL_PollEvent(&evnt))
{
switch (evnt.type)
{
case SDL_QUIT:
_gameState = GameState::EXIT;
break;
case SDL_MOUSEMOTION:
std::cout << "New Coords: " << evnt.motion.x << " " << evnt.motion.y << std::endl;
break;
}
}
}
void MainGame::drawGame()
{
glClearDepth(1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
_colorProgram.use();
_sprite.draw();
_colorProgram.unUse();
SDL_GL_SwapWindow(_window);
}
GLSLProgram.h:
//Used for
#pragma once
#include <string>
#include <GL/glew.h>
//#include <SDL/SDL.h>
#include "Errors.h"
class GLSLProgram
{
public:
GLSLProgram();
~GLSLProgram();
void compileShaders(const std::string& vertextShaderFilePath, const std::string& fragmentShaderFilePath);
void linkShaders();
void addAttribute(const std::string&);
void use();
void unUse();
private:
int _numAttributes;
void _compileShader(const std::string&, GLuint);
GLuint _programID;
GLuint _vertexShaderID;
GLuint _fragmentShaderID;
};
GLSLProgram.cpp:
//Used for compiling shaders
#include "GLSLProgram.h"
#include <fstream>
#include <vector>
GLSLProgram::GLSLProgram() : _numAttributes(0), _programID(0), _vertexShaderID(0), _fragmentShaderID(0)
{
}
GLSLProgram::~GLSLProgram()
{
}
void GLSLProgram::compileShaders(const std::string& vertexShaderFilePath, const std::string& fragmentShaderFilePath)
{
_vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
if (_vertexShaderID == 0)
{
fatalError("Vertex shader failed to be created!");
SDL_Quit();
}
_fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
if (_fragmentShaderID == 0)
{
fatalError("Fragment shader failed to be created!");
SDL_Quit();
}
_compileShader(vertexShaderFilePath, _vertexShaderID);
_compileShader(fragmentShaderFilePath, _fragmentShaderID);
}
void GLSLProgram::addAttribute(const std::string& attributeName)
{
glBindAttribLocation(_programID, _numAttributes++, attributeName.c_str());
}
void GLSLProgram::use()
{
glUseProgram(_programID);
for (int x = 0; x < _numAttributes; x++)
{
glEnableVertexAttribArray(x);
}
}
void GLSLProgram::unUse()
{
glUseProgram(0);
for (int x = 0; x < _numAttributes; x++)
{
glDisableVertexAttribArray(x);
}
}
void GLSLProgram::linkShaders()
{
//Vertex and fragment shaders are successfully compiled.
//Now time to link them together into a program.
//Get a program object.
_programID = glCreateProgram();
//Attach our shaders to our program
glAttachShader(_programID, _vertexShaderID);
glAttachShader(_programID, _fragmentShaderID);
//Link our program
glLinkProgram(_programID);
//Note the different functions here: glGetProgram* instead of glGetShader*.
GLint isLinked = 0;
glGetProgramiv(_programID, GL_LINK_STATUS, (int *)&isLinked);
if (isLinked == GL_FALSE)
{
GLint maxLength = 0;
glGetProgramiv(_programID, GL_INFO_LOG_LENGTH, &maxLength);
//The maxLength includes the NULL character
std::vector<char> infoLog(maxLength);
glGetProgramInfoLog(_programID, maxLength, &maxLength, &infoLog[0]);
//We don't need the program anymore.
glDeleteProgram(_programID);
//Don't leak shaders either.
glDeleteShader(_vertexShaderID);
glDeleteShader(_fragmentShaderID);
printf("%s\n", &(infoLog[0]));
fatalError("Shaders failed to link!");
}
//Always detach shaders after a successful link.
glDetachShader(_programID, _vertexShaderID);
glDetachShader(_programID, _fragmentShaderID);
glDeleteProgram(_programID);
glDeleteShader(_vertexShaderID);
glDeleteShader(_fragmentShaderID);
}
void GLSLProgram::_compileShader(const std::string &filePath, GLuint id)
{
std::ifstream vertexFile(filePath);
if (vertexFile.fail())
{
perror(filePath.c_str());
fatalError("Failed to open " + filePath);
}
std::string fileContents;
std::string line;
while (std::getline(vertexFile, line));
{
fileContents += line + "\n";
}
vertexFile.close();
const char *contentsPtr = fileContents.c_str();
glShaderSource(id, 1, &contentsPtr, nullptr);
glCompileShader(id);
GLint isCompiled = 0;
glGetShaderiv(id, GL_COMPILE_STATUS, &isCompiled);
if (isCompiled == GL_FALSE)
{
GLint maxLength = 0;
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &maxLength);
//The maxLength includes the NULL character
std::vector<char> errorLog(maxLength);
glGetShaderInfoLog(id, maxLength, &maxLength, &errorLog[0]);
//Provide the infolog in whatever manor you deem best.
//Exit with failure.
glDeleteShader(id); //Don't leak the shader.
printf("%s\n", &(errorLog[0]));
fatalError("Shader" + filePath + "failed to compile!");
}
}
vertex shader:
#version 130
//The vertex shader operates on each vertex
//input data from the VBO. Each vertex is 2 floats
in vec2 vertexPosition;
void main()
{
//Set the x,y position on the screen
gl_Position.xy = vertexPosition;
//the z position is zero since we are in 2D
gl_Position.z = 0.0;
//Indicate that the coordinates are normalized
gl_Position.w = 1.0;
}
and fragment shader:
#version 130
//The fragment shader operates on each pixel in a given polygon
//This is the 3 component float vector that gets outputted to the screen
//for each pixel.
out vec3 color;
void main() {
//Just hardcode the color to red
color = vec3(1.0, 0.0, 1.0);
}
I don't have any idea why it is happening. :(
PS: I'm beginner with glew so please don't answer with some advance stuff:D
Edit 1:
02/11/2014 (11/02/2014 for Americans)
I downloaded the source code from the tutorial I was using and it is working. So there's something with my code. I will edit the post when I find the problem.
Remove semicolon in file GLSLProgram.cpp at line:
while (std::getline(vertexFile, line));
You go through the file and then just add to the empty string fileContents the last line of your shader code, that is '}'.
for the last two days I've been watching following (really great and informative) tutorial series: http://www.youtube.com/playlist?list=PLEETnX-uPtBXT9T-hD0Bj31DSnwio-ywh
Last night I finished the part about meshes and now a red triangle is supposed to appear on my window, but it just doesn't. I don't know if it is a problem with my mesh or with my shader class (for both of them I don't get any errors). I use GLEW 1.10.0 for loading OpenGL, GLM 0.9.5.4 for OpenGL math stuff and SDL 2.0.3 for window stuff. Everything running on Windows 8.1 in Visual Studio 2013 Ultimate with the latest Nvidia graphics drivers.
Edit: I forgot to mention that the screen doesn't stay black, so the display.Clear method works. I hope thats any help.
Here are my shader and mesh files:
Shader.h:
#pragma once
#include <iostream>
#include <fstream>
#include <string>
#include <glew\glew.h>
using namespace std;
class Shader
{
public:
Shader(const string& fileName);
virtual ~Shader();
void Bind();
private:
string LoadShader(const string& fileName);
void CheckShaderError(GLuint shader, GLuint flag, bool isProgram, const string& errorMessage);
GLuint CreateShader(const string& text, GLenum shaderType);
static const unsigned int NUM_SHADERS = 2;
GLuint m_program;
GLuint m_shaders[NUM_SHADERS];
};
Shader.cpp
#include "Shader.h"
Shader::Shader(const string& fileName)
{
m_program = glCreateProgram();
m_shaders[0] = CreateShader(LoadShader(fileName + ".vert"), GL_VERTEX_SHADER);
m_shaders[1] = CreateShader(LoadShader(fileName + ".frag"), GL_FRAGMENT_SHADER);
for (unsigned int i = 0; i < NUM_SHADERS; i++)
glAttachShader(m_program, m_shaders[i]);
glLinkProgram(m_program);
CheckShaderError(m_program, GL_LINK_STATUS, true, "Error: Program linking failed");
glValidateProgram(m_program);
CheckShaderError(m_program, GL_VALIDATE_STATUS, true, "Error: Program is invalid");
}
Shader::~Shader()
{
for (unsigned int i = 0; i < NUM_SHADERS; i++)
{
glDetachShader(m_program, m_shaders[i]);
glDeleteShader(m_shaders[i]);
}
glDeleteProgram(m_program);
}
string Shader::LoadShader(const string& fileName)
{
ifstream file;
file.open(fileName.c_str());
string line;
string output;
if (file.is_open())
{
while (file.good())
{
getline(file, line);
output += (line + "\n");
}
}
else
{
cerr << "Unable to load shader: " << fileName << endl;
}
return output;
}
GLuint Shader::CreateShader(const string& text, GLenum shaderType)
{
GLuint shader = glCreateShader(shaderType);
if (shader == 0)
cerr << "Error: Shader creation failed!";
const GLchar* shaderSourceStrings[1] = { text.c_str() };
GLint shaderSourceStringLengths[1] = { text.length() };
glShaderSource(shader, 1, shaderSourceStrings, shaderSourceStringLengths);
glCompileShader(shader);
CheckShaderError(shader, GL_COMPILE_STATUS, false, "Error: Shader compilation failed");
return shader;
}
void Shader::Bind()
{
glUseProgram(m_program);
}
void Shader::CheckShaderError(GLuint shader, GLuint flag, bool isProgram, const string& errorMessage)
{
GLint success = 0;
GLchar error[1024] = { 0 };
if (isProgram)
glGetProgramiv(shader, flag, &success);
else
glGetShaderiv(shader, flag, &success);
if (success == GL_FALSE)
{
if (isProgram)
glGetProgramInfoLog(shader, sizeof(error), NULL, error);
else
glGetShaderInfoLog(shader, sizeof(error), NULL, error);
cerr << errorMessage << ": '" << error << "'" << endl;
}
}
BasicShader.vert:
#version 420 core
attribute vec3 position;
void main()
{
gl_Position = vec4(position, 1.0);
}
BasicShader.frag:
#version 420 core
void main()
{
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
Mesh.h:
#pragma once
#include <glew\glew.h>
#include "Vertex.h"
class Mesh
{
public:
Mesh(Vertex* vertecies, unsigned int numVertecies);
virtual ~Mesh();
void Draw();
private:
enum
{
POSITION_VB,
NUM_BUFFERS
};
GLuint m_vertexArrayObject;
GLuint m_vertexArrayBuffers[NUM_BUFFERS];
unsigned int m_drawCount;
};
Mesh.cpp:
#include "Mesh.h"
Mesh::Mesh(Vertex* vertecies, unsigned int numVertecies)
{
m_drawCount = numVertecies;
glGenVertexArrays(1, &m_vertexArrayObject);
glBindVertexArray(m_vertexArrayObject);
glGenBuffers(NUM_BUFFERS, m_vertexArrayBuffers);
glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffers[POSITION_VB]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertecies[0]) * numVertecies, vertecies, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindVertexArray(0);
}
Mesh::~Mesh()
{
glDeleteBuffers(NUM_BUFFERS, m_vertexArrayBuffers);
glDeleteVertexArrays(1, &m_vertexArrayObject);
}
void Mesh::Draw()
{
glBindVertexArray(m_vertexArrayObject);
glDrawArrays(GL_TRIANGLES, 0, m_drawCount);
glBindVertexArray(0);
}
Vertex.h:
#pragma once
#include <glm\glm.hpp>
using namespace glm;
class Vertex
{
public:
Vertex(const vec3& pos);
virtual ~Vertex();
private:
vec3 m_pos;
};
Vertex.cpp:
#include "Vertex.h"
Vertex::Vertex(const vec3& pos)
{
m_pos = pos;
}
Vertex::~Vertex()
{
}
main.cpp:
#include "Display.h"
#include "Shader.h"
#include "Mesh.h"
using namespace std;
int main(int argc, char** argv)
{
Display display(800, 600, "Hello World");
Vertex vertecies[] =
{
Vertex(vec3(-0.5, -0.5, 0)),
Vertex(vec3(0, 0.5, 0)),
Vertex(vec3(0.5, -0.5, 0))
};
Mesh mesh(vertecies, sizeof(vertecies) / sizeof(vertecies[0]));
Shader shader(".\\res\\BasicShader");
while (!display.IsClosed())
{
display.Clear(0.0f, 0.15f, 0.3f, 1.0f);
shader.Bind();
mesh.Draw();
display.Update();
}
return 0;
}
I'd be very grateful for any help...
Try changing your vertex program to:
#version 420 core
layout(location = 0) in vec3 position;
void main()
{
gl_Position = vec4(position, 1.0);
}
and your fragment program to:
#version 420 core
out vec4 frag;
void main()
{
frag = vec4(1.0, 0.0, 0.0, 1.0);
}