I've been following the beginners tutorials from opengl-tutorial.org and decided to try and put it in to a class which would show a square with a random texture in the middle of the screen. However I seem to have hit a problem, whenever I run it, it just shows a white screen whereas it should show a square with a texture on a dark blue background. I assume I have missed a function or passed in a variable wrong, however I can't seem to find it. So I would really appreciate an experienced eye taking a look and pointing where I went wrong and other things that I have done badly.
I will post my code first and the example that I modified and am effectively copying from below that.
There may be some random and unused functions and variables about the place since I have been experimenting.
Just previewed and it seems while the code is properly indented in the message editor, it does not seem to be when previewed. Is there anyway to do this?
Display All
VertexShader:
Display All
Fragment Shader:
Display All
I will post my code first and the example that I modified and am effectively copying from below that.
There may be some random and unused functions and variables about the place since I have been experimenting.
Just previewed and it seems while the code is properly indented in the message editor, it does not seem to be when previewed. Is there anyway to do this?
C Source Code
- #include <GL/glew.h>
- #include "glm/glm.hpp"
- #include "glm/gtc/matrix_transform.hpp"
- #include <Windows.h>
- #include "GLFW\glfw.h"
- #include <stdio.h>
- #include <typeinfo>
- #include <string>
- #include <iostream>
- #include <fstream>
- using namespace std;
- class Render;
- static Render* current;
- class Render
- {
- enum State
- {
- RUNNING,
- QUIT
- };
- State m_state;
- GLuint m_ProgramID;
- GLuint m_MatrixID;
- GLuint m_Texture;
- GLuint m_TextureID;
- GLuint m_VertexArrayID;
- GLuint m_VertexBuffer;
- GLuint m_UVBuffer;
- static const long m_Width;
- static const long m_Height;
- static const GLfloat g_vertex_buffer_data[12];
- static const GLfloat g_uv_buffer_data[8];
- glm::mat4 m_Model;
- glm::mat4 m_Projection;
- glm::mat4 m_View;
- glm::mat4 m_MVP;
- glm::vec2 m_speed; //units per sec
- public:
- static void GLFWCALL INPUTCALLBACK(int Key, int Action);
- Render();
- ~Render();
- void Init();
- void Destruct();
- void RenderNow();
- void Update(float deltaTime);
- int Run();
- private:
- GLuint LoadShaders(const char * vertex_file_path,const char * fragment_file_path)
- {
- // Create the shaders
- GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
- GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
- // Read the Vertex Shader code from the file
- std::string VertexShaderCode;
- std::ifstream VertexShaderStream(vertex_file_path, std::ios::in);
- if(VertexShaderStream.is_open())
- {
- std::string Line = "";
- while(getline(VertexShaderStream, Line))
- VertexShaderCode += "\n" + Line;
- VertexShaderStream.close();
- }
- else
- {
- printf("Impossible to open %s. Are you in the right directory ? Don't forget to read the FAQ !\n", vertex_file_path);
- return 0;
- }
- // Read the Fragment Shader code from the file
- std::string FragmentShaderCode;
- std::ifstream FragmentShaderStream(fragment_file_path, std::ios::in);
- if(FragmentShaderStream.is_open())
- {
- std::string Line = "";
- while(getline(FragmentShaderStream, Line))
- FragmentShaderCode += "\n" + Line;
- FragmentShaderStream.close();
- }
- }
- GLuint loadTGA_glfw(const char * imagepath){
- // Create one OpenGL texture
- GLuint textureID;
- glGenTextures(1, &textureID);
- // "Bind" the newly created texture : all future texture functions will modify this texture
- glBindTexture(GL_TEXTURE_2D, textureID);
- // Read the file, call glTexImage2D with the right parameters
- glfwLoadTexture2D(imagepath, 0);
- // Nice trilinear filtering.
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
- glGenerateMipmap(GL_TEXTURE_2D);
- // Return the ID of the texture we just created
- return textureID;
- }
- };
- #pragma region vertexData
- const GLfloat Render::g_vertex_buffer_data[12] =
- {
- -100.0f, 100.0f, 0.0f,
- -100.0f, -100.0f, 0.0f,
- 100.0f, 100.0f, 0.0f,
- 100.0f, -100.0f, 0.0f
- };
- const GLfloat Render::g_uv_buffer_data[8] =
- {
- 0.0f, 1.0f,
- 0.0f, 0.0f,
- 1.0f, 1.0f,
- 1.0f, 0.0f
- };
- #pragma endregion
- const long Render::m_Width = 1024;
- const long Render::m_Height = 768;
- int main()
- {
- current = new Render();
- current->Init();
- while (current->Run() != 0)
- {
- }
- current->Destruct();
- return 0;
- }
- Render::Render()
- {
- m_state = State::RUNNING;
- printf("Render Object created! \n");
- }
- void Render::Init()
- {
- if(!glfwInit())
- {
- fprintf(stderr, "Failed to initialize GLFW \n");
- }
- glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 4);
- glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
- glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 3);
- glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
- if(!glfwOpenWindow(m_Width, m_Height, 0, 0,0,0,32, 0, GLFW_WINDOW))
- {
- fprintf(stderr, "Open window failed :( \n");
- glfwTerminate();
- }
- glewExperimental = true;
- if(glewInit() != GLEW_OK)
- {
- fprintf(stderr, "Failed to initialize GLEW! :( ");
- }
- glfwSetWindowTitle("Render");
- glfwEnable(GLFW_STICKY_KEYS);
- glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
- glEnable(GL_DEPTH_TEST);
- glDepthFunc(GL_LESS);
- glGenVertexArrays(1, &m_VertexArrayID);
- glBindVertexArray(m_VertexArrayID);
- m_ProgramID = LoadShaders("Transform.vertexshader","Texture.fragmentshader");
- m_MatrixID = glGetUniformLocation(m_ProgramID, "MVP");
- m_Texture = loadTGA_glfw("textu.tga");
- m_TextureID = glGetUniformLocation(m_ProgramID, "myTextureSampler");
- glGenBuffers(1, &m_VertexBuffer);
- glBindBuffer(GL_ARRAY_BUFFER, m_VertexBuffer);
- glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
- glGenBuffers(1, &m_UVBuffer);
- glBindBuffer(GL_ARRAY_BUFFER, m_UVBuffer);
- glBufferData(GL_ARRAY_BUFFER, sizeof(g_uv_buffer_data), g_uv_buffer_data, GL_STATIC_DRAW);
- m_Model = glm::mat4(1.0f);
- glfwSetKeyCallback(INPUTCALLBACK);
- m_Projection = glm::ortho(float(-(m_Width/2)),float( m_Width/2), float(-(m_Height/2)), float(m_Height/2), 1.0f, 100.0f);
- m_View = glm::lookAt(
- glm::vec3(0,0,3),
- glm::vec3(0,0,0),
- glm::vec3(0,1,0)
- );
- printf("INIT SUCCEEDED!");
- }
- void Render::Destruct()
- {
- glDeleteBuffers(1, &m_VertexBuffer);
- glDeleteBuffers(1, &m_UVBuffer);
- glDeleteProgram(m_ProgramID);
- glDeleteTextures(1, &m_TextureID);
- glDeleteVertexArrays(1, &m_VertexArrayID);
- }
- void Render::RenderNow()
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glUseProgram(m_ProgramID);
- glUniformMatrix4fv(m_MatrixID, 1, GL_FALSE, &m_MVP[0][0]);
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, m_Texture);
- glUniform1i(m_TextureID, 0);
- glEnableVertexAttribArray(0);
- glBindBuffer(GL_ARRAY_BUFFER, m_VertexBuffer);
- glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,(void*)0);
- glEnableVertexAttribArray(1);
- glBindBuffer(GL_ARRAY_BUFFER, m_UVBuffer);
- glVertexAttribPointer(1,2,GL_FLOAT,GL_FALSE,0,(void*)0);
- glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
- glDisableVertexAttribArray(0);
- glDisableVertexAttribArray(1);
- glfwSwapBuffers();
- }
- void Render::Update(float deltaTime)
- {
- m_Model[3].x += (m_speed.x * deltaTime);
- m_Model[3].y += (m_speed.y * deltaTime);
- m_MVP = m_Projection * m_View * m_Model;
- }
- Render::~Render()
- {
- }
- void Render::INPUTCALLBACK(int Key, int Action)
- {
- if(Key == GLFW_KEY_DOWN && Action == GLFW_PRESS)
- current->m_speed.y += -0.2;
- if(Key == GLFW_KEY_UP && Action == GLFW_PRESS)
- current->m_speed.y += 0.2;
- if(Key == GLFW_KEY_RIGHT && Action == GLFW_PRESS)
- current->m_speed.x += 0.2;
- if(Key == GLFW_KEY_LEFT && Action == GLFW_PRESS)
- current->m_speed.x += -0.2;
- }
- int Render::Run()
- {
- float lastTime = glfwGetTime();
- float deltaTime;
- while(m_state != State::QUIT)
- {
- deltaTime = glfwGetTime() - lastTime;
- lastTime = glfwGetTime();
- Update(deltaTime);
- RenderNow();
- }
- return 0;
- }
VertexShader:
Source Code
- #version 330 core
- // Input vertex data, different for all executions of this shader.
- layout(location = 0) in vec3 vertexPosition_modelspace;
- layout(location = 1) in vec2 vertexUV;
- // Output data ; will be interpolated for each fragment.
- out vec2 UV;
- // Values that stay constant for the whole mesh.
- uniform mat4 MVP;
- void main(){
- // Output position of the vertex, in clip space : MVP * position
- gl_Position = MVP * vec4(vertexPosition_modelspace,1);
- // UV of the vertex. No special space for this one.
- UV = vertexUV;
- }
Fragment Shader:
Source Code
- #version 330 core
- // Interpolated values from the vertex shaders
- in vec2 UV;
- // Ouput data
- out vec3 color;
- // Values that stay constant for the whole mesh.
- uniform sampler2D myTextureSampler;
- void main(){
- // Output color = color of the texture at the specified UV
- color = texture2D( myTextureSampler, UV ).rgb;
- }
The post was edited 1 time, last by olivermd ().