Index buffers and opengl error handling
This commit is contained in:
parent
3157af8064
commit
edec347ecc
2 changed files with 58 additions and 16 deletions
|
@ -1,7 +1,7 @@
|
||||||
# Search for glfw
|
# Search for glfw
|
||||||
find_package(glfw3 3.3 REQUIRED)
|
find_package(glfw3 3.3 REQUIRED)
|
||||||
# Search for glew
|
# Search for glew
|
||||||
find_package(GLEW REQUIRED)
|
#find_package(GLEW REQUIRED)
|
||||||
# Search for OpenGL
|
# Search for OpenGL
|
||||||
find_package(OpenGL REQUIRED)
|
find_package(OpenGL REQUIRED)
|
||||||
|
|
||||||
|
@ -9,9 +9,9 @@ find_package(OpenGL REQUIRED)
|
||||||
# Add gl1 executable build from gl1.cpp
|
# Add gl1 executable build from gl1.cpp
|
||||||
add_executable(gl1 gl1.cpp)
|
add_executable(gl1 gl1.cpp)
|
||||||
|
|
||||||
|
# Link gl1 with glew
|
||||||
|
#target_link_libraries(gl1 glew)
|
||||||
# Link gl1 with glfw lib
|
# Link gl1 with glfw lib
|
||||||
target_link_libraries(gl1 glfw)
|
target_link_libraries(gl1 glfw)
|
||||||
# Link gl1 with OpenGL
|
# Link gl1 with OpenGL
|
||||||
target_link_libraries(gl1 OpenGL::GL)
|
target_link_libraries(gl1 OpenGL::GL)
|
||||||
# Link gl1 with glew
|
|
||||||
#target_link_libraries(gl1 glew)
|
|
||||||
|
|
|
@ -3,22 +3,46 @@
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <signal.h>
|
||||||
|
|
||||||
typedef unsigned int uint;
|
typedef unsigned int uint;
|
||||||
|
|
||||||
struct ShaderProgramSource{
|
//Only on opengl4
|
||||||
|
static void ErrorGLCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, const void *userParam)
|
||||||
|
{
|
||||||
|
std::cout << "Source: " << source << "Type: " << type << "Id: " << id << "Severity: " << severity << "Length: " << length << message << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Old way to process openGL errors
|
||||||
|
static void GLClearError()
|
||||||
|
{
|
||||||
|
while (glGetError() != GL_NO_ERROR)
|
||||||
|
; //Clear error flags
|
||||||
|
}
|
||||||
|
|
||||||
|
static void GLCheckError(){
|
||||||
|
while(GLenum error = glGetError()){
|
||||||
|
std::cout << "[OpenGL Error] (" << error << ")" << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ShaderProgramSource
|
||||||
|
{
|
||||||
std::string VertexSource;
|
std::string VertexSource;
|
||||||
std::string FragmentSource;
|
std::string FragmentSource;
|
||||||
};
|
};
|
||||||
|
|
||||||
const std::string SHADERS_PATH="src/opengl/res/shaders/Basic.shader";
|
const std::string SHADERS_PATH = "src/opengl/res/shaders/Basic.shader";
|
||||||
|
|
||||||
static ShaderProgramSource ParseShader(const std::string path)
|
static ShaderProgramSource ParseShader(const std::string path)
|
||||||
{
|
{
|
||||||
std::ifstream stream(path);
|
std::ifstream stream(path);
|
||||||
std::string line;
|
std::string line;
|
||||||
enum class ShaderType {
|
enum class ShaderType
|
||||||
NONE=-1,VERTEX=0,FRAGMENT=1
|
{
|
||||||
|
NONE = -1,
|
||||||
|
VERTEX = 0,
|
||||||
|
FRAGMENT = 1
|
||||||
};
|
};
|
||||||
std::stringstream ss[2];
|
std::stringstream ss[2];
|
||||||
ShaderType type = ShaderType::NONE;
|
ShaderType type = ShaderType::NONE;
|
||||||
|
@ -29,19 +53,20 @@ static ShaderProgramSource ParseShader(const std::string path)
|
||||||
if (line.find("vertex") != std::string::npos)
|
if (line.find("vertex") != std::string::npos)
|
||||||
{
|
{
|
||||||
//vertex
|
//vertex
|
||||||
type=ShaderType::VERTEX;
|
type = ShaderType::VERTEX;
|
||||||
}
|
}
|
||||||
else if (line.find("fragment") != std::string::npos)
|
else if (line.find("fragment") != std::string::npos)
|
||||||
{
|
{
|
||||||
//Fragment
|
//Fragment
|
||||||
type=ShaderType::FRAGMENT;
|
type = ShaderType::FRAGMENT;
|
||||||
}
|
}
|
||||||
}else
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
ss[(int)type] << line << '\n';
|
ss[(int)type] << line << '\n';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return {ss[0].str(),ss[1].str()};
|
return {ss[0].str(), ss[1].str()};
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint CompileShader(uint type, const std::string &source)
|
static uint CompileShader(uint type, const std::string &source)
|
||||||
|
@ -99,6 +124,7 @@ static uint CreateShader(const std::string &vertexShader, const std::string &fra
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
GLFWwindow *window;
|
GLFWwindow *window;
|
||||||
|
//glDebugMessageCallback(ErrorGLCallback,0); Enable when glew installed properly
|
||||||
|
|
||||||
/* Initialize the library */
|
/* Initialize the library */
|
||||||
if (!glfwInit())
|
if (!glfwInit())
|
||||||
|
@ -118,20 +144,33 @@ int main(void)
|
||||||
//Print opengl version
|
//Print opengl version
|
||||||
std::cout << glGetString(GL_VERSION) << std::endl;
|
std::cout << glGetString(GL_VERSION) << std::endl;
|
||||||
|
|
||||||
float positions[6] = {-1.0f, -1.0f, 0.0f, 1.0f, 1.0f, -1.0f};
|
float positions[] =
|
||||||
|
{
|
||||||
|
-0.5f, -0.5f,
|
||||||
|
0.5f, -0.5f,
|
||||||
|
0.5f, 0.5f,
|
||||||
|
-0.5f, 0.5f};
|
||||||
|
|
||||||
|
uint indexes[] = {
|
||||||
|
0, 1, 2, 2, 3, 0};
|
||||||
|
|
||||||
//Create buffer
|
//Create buffer
|
||||||
//Buffer id
|
//Buffer id
|
||||||
unsigned int buffer;
|
unsigned int buffer;
|
||||||
glGenBuffers(1, &buffer);
|
glGenBuffers(1, &buffer);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, buffer);
|
glBindBuffer(GL_ARRAY_BUFFER, buffer);
|
||||||
glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), positions, GL_STATIC_DRAW);
|
glBufferData(GL_ARRAY_BUFFER, 10 * sizeof(float), positions, GL_STATIC_DRAW);
|
||||||
glEnableVertexAttribArray(0);
|
glEnableVertexAttribArray(0);
|
||||||
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);
|
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);
|
||||||
|
|
||||||
|
unsigned int index_buffer;
|
||||||
|
glGenBuffers(1, &index_buffer);
|
||||||
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer);
|
||||||
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * sizeof(float), indexes, GL_STATIC_DRAW);
|
||||||
|
|
||||||
ShaderProgramSource source = ParseShader(SHADERS_PATH);
|
ShaderProgramSource source = ParseShader(SHADERS_PATH);
|
||||||
|
|
||||||
std::cout << "Shaders: " << source.VertexSource << std::endl;
|
std::cout << "Shaders: " << source.VertexSource << std::endl;
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
std::cout << source.FragmentSource << std::endl;
|
std::cout << source.FragmentSource << std::endl;
|
||||||
|
|
||||||
|
@ -152,8 +191,11 @@ int main(void)
|
||||||
//glEnd();
|
//glEnd();
|
||||||
|
|
||||||
//Modern OpenGL
|
//Modern OpenGL
|
||||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
//without index buffer
|
||||||
|
//glDrawArrays(GL_TRIANGLES, 0, 12);
|
||||||
|
//GLClearError();
|
||||||
|
glDrawElements(GL_TRIANGLES, 6 * sizeof(uint), GL_UNSIGNED_INT, nullptr);
|
||||||
|
//GLCheckError();
|
||||||
/* Swap front and back buffers */
|
/* Swap front and back buffers */
|
||||||
glfwSwapBuffers(window);
|
glfwSwapBuffers(window);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue