Some opengl additions

This commit is contained in:
James Grogan 2022-11-15 11:50:24 +00:00
parent 72123bc333
commit 649079a5c7
6 changed files with 163 additions and 17 deletions

View file

@ -51,7 +51,7 @@ In `xdg-shell-protocol.cpp` - we need access to `xdg_wm_base_interface` so expor
#### 3D Rendering #### 3D Rendering
```bash ```bash
sudo apt-get install libgl-dev libegl1-mesa-dev sudo apt-get install libgl-dev libegl1-mesa-dev libglu1-mesa-dev
``` ```
#### fonts #### fonts

View file

@ -18,7 +18,7 @@ list(APPEND graphics_HEADERS
set(OpenGL_GL_PREFERENCE "GLVND") set(OpenGL_GL_PREFERENCE "GLVND")
find_package(OpenGL QUIET) find_package(OpenGL QUIET)
if (OpenGL_FOUND) if (OpenGL_FOUND)
list(APPEND platform_LIBS OpenGL::GL) list(APPEND platform_LIBS OpenGL::GL OpenGL::GLU)
list(APPEND graphics_LIB_INCLUDES opengl/OpenGlPainter.cpp) list(APPEND graphics_LIB_INCLUDES opengl/OpenGlPainter.cpp)
list(APPEND graphics_HEADERS opengl/OpenGlPainter.h) list(APPEND graphics_HEADERS opengl/OpenGlPainter.h)
else() else()

View file

@ -9,22 +9,129 @@
#include <windows.h> #include <windows.h>
#endif #endif
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h> #include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glx.h>
#include <iostream> #include <iostream>
const char* vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\0";
const char* fragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
"FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
"}\n";
OpenGlPainter::OpenGlPainter()
{
}
void OpenGlPainter::initialize()
{
if (auto context = glXGetCurrentContext())
{
std::cout << "has valid context" << std::endl;
}
else
{
std::cout << "no valid context" << std::endl;
}
int major_version{0};
int minor_version{0};
glGetIntegerv(GL_MAJOR_VERSION, &major_version);
glGetIntegerv(GL_MINOR_VERSION, &minor_version);
std::cout << "Using opengl version " << major_version << "|" << minor_version << std::endl;
std::cout << "Creating opengl painter" << std::endl;
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, nullptr);
glCompileShader(vertexShader);
int success;
char infoLog[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if(!success)
{
glGetShaderInfoLog(vertexShader, 512, nullptr, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED: " << std::string(infoLog) << std::endl;
if (auto errCode = glGetError(); errCode != GL_NO_ERROR)
{
auto errString = gluErrorString(errCode);
std::cout << "Got gl error" << errString << std::endl;
}
}
else
{
std::cout << "Shader compiled ok" << std::endl;
if (auto errCode = glGetError(); errCode != GL_NO_ERROR)
{
auto errString = gluErrorString(errCode);
std::cout << "Got gl error after ok" << errString << std::endl;
}
}
unsigned int fragmentShader;
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, nullptr);
glCompileShader(fragmentShader);
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if(!success)
{
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED: " << infoLog << std::endl;
}
mShaderProgram = glCreateProgram();
glAttachShader(mShaderProgram, vertexShader);
glAttachShader(mShaderProgram, fragmentShader);
glLinkProgram(mShaderProgram);
glGetProgramiv(mShaderProgram, GL_LINK_STATUS, &success);
if(!success)
{
glGetProgramInfoLog(mShaderProgram, 512, NULL, infoLog);
std::cout << "Shader linking FAILED\n" << infoLog << std::endl;
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
mInitialized = true;
}
void OpenGlPainter::paint(DrawingContext* context) void OpenGlPainter::paint(DrawingContext* context)
{ {
if (!mInitialized)
{
initialize();
}
auto surface = context->getSurface(); auto surface = context->getSurface();
const auto width = double(surface->getWidth()); const auto width = double(surface->getWidth());
const auto height = double(surface->getHeight()); const auto height = double(surface->getHeight());
const auto num_mesh = context->getScene()->getNumMeshes(); const auto num_mesh = context->getScene()->getNumMeshes();
glViewport(0, 0, width, height); glViewport(0, 0, width, height);
glOrtho(0, width, 0, height, -1.0, 1.0); //glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
glScissor(0, 0, width, height); //glScissor(0, 0, width, height);
glMatrixMode(GL_PROJECTION); //glMatrixMode(GL_PROJECTION);
glLoadIdentity(); //glLoadIdentity();
glClearColor(0.5, 0.5, 1.0, 0.0); glClearColor(0.5, 0.5, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
@ -32,6 +139,30 @@ void OpenGlPainter::paint(DrawingContext* context)
//glMatrixMode(GL_MODELVIEW); //glMatrixMode(GL_MODELVIEW);
//glLoadIdentity(); //glLoadIdentity();
float vertices[] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f
};
unsigned int vbo{0};
glGenBuffers(1, &vbo);
unsigned int VAO;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glUseProgram(mShaderProgram);
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 3);
/*
std::cout << "Bounds are : " << width << " | " << height << std::endl; std::cout << "Bounds are : " << width << " | " << height << std::endl;
for (std::size_t idx=0; idx<num_mesh; idx++) for (std::size_t idx=0; idx<num_mesh; idx++)
@ -50,14 +181,14 @@ void OpenGlPainter::paint(DrawingContext* context)
glColor3f(r, g, b); glColor3f(r, g, b);
glBegin(GL_TRIANGLES); glBegin(GL_TRIANGLES);
double x0 = face[0].getX(); double x0 = 2.0*face[0].getX()/width - 1.0;
double y0 = height -face[0].getY(); double y0 = 1.0 - 2.0*face[0].getY()/height;
double x1 = face[1].getX(); double x1 = 2.0*face[1].getX()/width - 1.0;
double y1 = height -face[1].getY(); double y1 = 1.0 - 2.0*face[1].getY()/height;
double x2 = face[2].getX(); double x2 = 2.0*face[2].getX()/width - 1.0;
double y2 = height -face[2].getY(); double y2 = 1.0 - 2.0*face[2].getY()/height;
glVertex3f(x0, y0, 0); glVertex3f(x0, y0, 0);
glVertex3f(x1, y1, 0); glVertex3f(x1, y1, 0);
@ -71,8 +202,11 @@ void OpenGlPainter::paint(DrawingContext* context)
glEnd(); glEnd();
counter++; counter++;
break;
} }
break;
} }
*/
glFlush(); glFlush();
} }

View file

@ -7,7 +7,16 @@ class DrawingContext;
class OpenGlPainter : public AbstractPainter class OpenGlPainter : public AbstractPainter
{ {
public: public:
OpenGlPainter();
void initialize();
void paint(DrawingContext* context) override; void paint(DrawingContext* context) override;
private:
bool mInitialized{false};
unsigned int mShaderProgram{0};
}; };

View file

@ -1,6 +1,7 @@
#include "XcbGlInterface.h" #include "XcbGlInterface.h"
#include "FileLogger.h" #include "FileLogger.h"
#include <iostream>
#include <GL/gl.h> #include <GL/gl.h>
@ -59,6 +60,7 @@ void XcbGlInterface::setupContext(int default_screen)
glXGetFBConfigAttrib(mDisplay, mConfig, GLX_VISUAL_ID, &visualID); glXGetFBConfigAttrib(mDisplay, mConfig, GLX_VISUAL_ID, &visualID);
/* Create OpenGL context */ /* Create OpenGL context */
std::cout << "Creating opengl context" << std::endl;
mContext = glXCreateNewContext(mDisplay, mConfig, GLX_RGBA_TYPE, 0, True); mContext = glXCreateNewContext(mDisplay, mConfig, GLX_RGBA_TYPE, 0, True);
if (!mContext) if (!mContext)
{ {

View file

@ -54,17 +54,18 @@ bool XcbGlWindowInterface::initialize(xcb_window_t window)
MLOG_ERROR("glXMakeContextCurrent failed"); MLOG_ERROR("glXMakeContextCurrent failed");
return false; return false;
} }
return true; return true;
} }
void XcbGlWindowInterface::resizeViewPort(unsigned width, unsigned height) void XcbGlWindowInterface::resizeViewPort(unsigned width, unsigned height)
{ {
glViewport(0, 0, width, height); //glViewport(0, 0, width, height);
glScissor(0, 0, width, height); //glScissor(0, 0, width, height);
glOrtho(0, width, 0, height, -1.0, 1.0); //glOrtho(0, width, 0, height, -1.0, 1.0);
glMatrixMode(GL_PROJECTION); //glMatrixMode(GL_PROJECTION);
glLoadIdentity(); //glLoadIdentity();
//glMatrixMode(GL_MODELVIEW); //glMatrixMode(GL_MODELVIEW);