I am attempting to learn OpenGL 2.0 and I am trying to draw a triangle onto the screen.
When I try to load the vertex shader, it won't compile Here is my code(I am aware that my code is incomplete, I am just running it periodically to make sure I don't get too deep with errors):
#include <iostream>
#include <stdlib.h>
#include <GL/glew.h>
#include <GLUT/glut.h>
int init_resoureces(void){
GLint compile_ok = GL_FALSE, link_ok = GL_FALSE;
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
const char *vs_source =
"#version 120\n"
"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 (0 == compile_ok){
fprintf(stderr, "Error in the vertex shader\n");
return 0;
}
return 1;
}
void onDisplay(){
//fill in later
}
void free_resources(){
}
GLfloat triangle_vertices[] = {
0.0, 0.8,
-0.8, -0.8,
0.8, -0.8
};
int main(int argc, char * argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(640, 480);
glutCreateWindow("My First Triangle");
GLenum glew_status = glewInit();
if (glew_status != GLEW_OK){
fprintf(stderr, "Error: %s\n", glewGetErrorString(glew_status));
return EXIT_FAILURE;
}
if (1 == init_resoureces()) {
glutDisplayFunc(onDisplay);
glutMainLoop();
}
free_resources();
return EXIT_SUCCESS;
}
The program quits and says there is an error in the vertex shader (as it should with the if statement detecting compiling errors). I can't find the error in my shader, though.
Does someone know what I'm doing incorrectly?
If it matters, I am using Xcode and OS X 10.8
Thanks!
The OpenGL Shader Language, GLSL, is case-sensitive.
Make sure you capitalize gl_Position correctly.
Related
I'm trying to make a simple program to work. I am using SD2 + OpenGL Shading Language. Also, I'm trying to this on MacOS, more precisely 10.15.5.
I know Apple is deprecating OpenGL in favor of Metal, but it seems they are still supporting at least up to OpenGL 4.1.
This is a simplified version of the program I'm trying to run:
#include <OpenGL/glext.h>
#include <OpenGL/glu.h>
#include <SDL.h>
#include <iostream>
const int WIDTH = 600;
const int HEIGHT = 600;
const int numVAOs = 1;
GLuint renderingProgram;
GLuint vao[numVAOs];
GLuint createShaderProgram() {
GLint vertCompiled;
GLint fragCompiled;
GLint linked;
const char *vshaderSource =
"#version 410\n"
"void main(void)\n"
"{gl_Position = vec4(0.0, 0.0, 0.0, 1.0); }";
const char *fshaderSource =
"#version 410\n"
"out vec4 color; \n"
"void main(void)\n"
"{color = vec4(1.0, 0.0, 0.0, 1.0);}";
GLuint vShader = glCreateShader(GL_VERTEX_SHADER);
GLuint fShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(vShader, 1, &vshaderSource, NULL);
glShaderSource(fShader, 1, &fshaderSource, NULL);
glCompileShader(vShader);
glCompileShader(fShader);
GLuint vfProgram = glCreateProgram();
glAttachShader(vfProgram, vShader);
glAttachShader(vfProgram, fShader);
glLinkProgram(vfProgram);
return vfProgram;
}
int main() {
SDL_Window *window;
SDL_GLContext gl_context;
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
return -1;
}
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
window = SDL_CreateWindow(
"OpenGL Shaders",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
WIDTH,
HEIGHT,
SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
gl_context = SDL_GL_CreateContext(window);
std::cout << glGetString(GL_VERSION) << std::endl;
renderingProgram = createShaderProgram();
glGenVertexArraysAPPLE(numVAOs, vao);
glBindVertexArrayAPPLE(vao[0]);
bool isRunning = true;
SDL_Event event;
while (isRunning) {
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
isRunning = false;
break;
}
}
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(renderingProgram);
glPointSize(30.0f);
glDrawArrays(GL_POINTS, 0, 1);
SDL_GL_SwapWindow(window);
}
SDL_GL_DeleteContext(gl_context);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
This shows only a blank screen and on the console it prints 4.1 ATI-3.9.15. So I am sure I got the correct profile.
I have tried playing with the headers. It seems i don't need a library to get extension names, seems Apple already has them int heir OpenGL headers.
The full program has some routines which handles errors when creating/linking the shaders and I can tell they are not failing to compile/link.
The program should show a point in the middle of the screen, but is not showing anything but a white screen(the clear color).
Any idea what am I missing or doing wrong?
Ok. I made it work.
The problem was this lines:
glGenVertexArraysAPPLE(numVAOs, vao);
glBindVertexArrayAPPLE(vao[0]);
For some reason, the APPLE version of those doesn't work.
What I did is:
Install GLEW with brew (which I thought I didn't need)
Update my cmake file to link glew correctly
Add glew headers and glew_init()
Replace the APPLE functions with "normal" ones.
glGenVertexArrays(numVAOs, vao);
glBindVertexArray(vao[0]);
Now the program shows points/triangles
Hope this helps anyone else in the future.
I'm trying to write a simple opengl program that cyclically grows and shrinks a single point at the center of the screen using glPointSize() and a variable pointSize. Printing the value of pointSize and stepping through the code with a debugger appears to show that pointSize updates correctly on each iteration. The rendering of the point also appears to be correct when pointSize is increasing, but when pointSize is decreasing the point is still rendered at its maximum size on the screen, and keeps rendering that way no matter how much pointSize grows or shrinks in value - and I cannot figure out why.
#include <GL\glew.h>
#include <GLFW\glfw3.h>
#include <iostream>
#define numVAOs 1
GLuint renderingProgram;
GLuint vao[numVAOs];
GLuint createShaderProgram() {
const char *vshaderSource =
"#version 430 \n"
"void main(void) \n"
"{ gl_Position = vec4(0.0, 0.0, 0.0, 1.0); }";
const char *fshaderSource =
"#version 430 \n"
"out vec4 color; \n"
"void main(void) \n"
"{ color = vec4(0.0, 0.0, 1.0, 1.0); }";
GLuint vShader = glCreateShader(GL_VERTEX_SHADER);
GLuint fShader = glCreateShader(GL_FRAGMENT_SHADER);
GLuint vfprogram = glCreateProgram();
glShaderSource(vShader, 1, &vshaderSource, NULL);
glShaderSource(fShader, 1, &fshaderSource, NULL);
glCompileShader(vShader);
glCompileShader(fShader);
glAttachShader(vfprogram, vShader);
glAttachShader(vfprogram, fShader);
glLinkProgram(vfprogram);
return vfprogram;
}
void init() {
renderingProgram = createShaderProgram();
glGenVertexArrays(numVAOs, vao);
glBindVertexArray(vao[0]);
}
GLfloat pointSize = 100.0f;
GLfloat increment = 1.0f;
void display() {
glUseProgram(renderingProgram);
if (pointSize > 200 || pointSize < 2) increment *= -1.0f;
pointSize += increment;
glPointSize(pointSize);
glDrawArrays(GL_POINTS, 0, 1);
}
int main(void) {
if (!glfwInit()) exit(EXIT_FAILURE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
GLFWwindow* window = glfwCreateWindow(600, 600, "Test", nullptr, nullptr);
glfwMakeContextCurrent(window);
if (glewInit() != GLEW_OK) exit(EXIT_FAILURE);
glfwSwapInterval(1);
init();
while (!glfwWindowShouldClose(window)) {
display();
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
exit(EXIT_SUCCESS);
}
Your code does not clear the screen with glClear. Therefore, when the point grows, you can see it. When it shrinks, it's simply drawn 'inside' the bigger dot, without you noticing it. You could catch that if you changed the color of the dot as it changes the size.
Simply clear the color buffer at the beginning of the frame:
void display() {
// default clear color is black, but you can change it:
//glClearColor(1,0,0,1);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(renderingProgram);
...
I'm drawing a triangle on screen with OpenGL and SDL2.
My problem resides at the shaders compiling stage.
Here are the errors I get :
ERROR::SHADER::VERTEX::COMPILATION_FAILED
0:1(10): error: GLSL 3.30 is not supported. Supported versions are: 1.10, 1.20, 1.30, 1.40, 1.00 ES, and 3.00 ES
ERROR::SHADER::FRAGMENT::COMPILATION_FAILED
0:1(10): error: GLSL 3.30 is not supported. Supported versions are: 1.10, 1.20, 1.30, 1.40, 1.00 ES, and 3.00 ES
ERROR::SHADER::PROGRAM::LINKING_FAILED
error: linking with uncompiled/unspecialized shadererror: linking with uncompiled/unspecialized shader
I saw on some forums people having the same error messages, and they were told that by default MESA will switch to compatibility profile instead of core profile, but the thing is that I've explicitly told SDL2 to create a core context profile :
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
By the way, I've already used OpenGL version 3.30 with GLSL 3.30 on that same computer without any issues by using it with GLFW and GLAD, but now I just want to have it working with SDL2 and GLEW.
Here is the result of the glxinfo | grep "version" command :
server glx version string: 1.4
client glx version string: 1.4
GLX version: 1.4
Max core profile version: 3.3
Max compat profile version: 3.1
Max GLES1 profile version: 1.1
Max GLES[23] profile version: 3.1
OpenGL core profile version string: 3.3 (Core Profile) Mesa 19.1.7
OpenGL core profile shading language version string: 3.30
OpenGL version string: 3.1 Mesa 19.1.7
OpenGL shading language version string: 1.40
OpenGL ES profile version string: OpenGL ES 3.1 Mesa 19.1.7
OpenGL ES profile shading language version string: OpenGL ES GLSL ES 3.10
GL_EXT_shader_implicit_conversions, GL_EXT_shader_integer_mix,
And the content of my main function :
int main(int argc, char* argv[])
{
// =====> INIT VARS
SDL_Window* window;
SDL_Renderer* renderer;
SDL_GLContext glContext;
GLenum err;
// INIT VARS <=====
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
std::cout << SDL_GetError() << std::endl;
return -1;
}
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
window = createWindow(WIDTH, HEIGHT, "TP_PROGRAMMATION_3D");
if(window == nullptr)
{
std::cout << SDL_GetError() << std::endl;
SDL_Quit();
return -1;
}
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if(renderer == nullptr)
{
std::cout << SDL_GetError() << std::endl;
SDL_DestroyWindow(window);
SDL_Quit();
return -1;
}
// OPENGL CONTEXT FOR THE WINDOW
glContext = SDL_GL_CreateContext(window);
// GLEW INIT
err = glewInit();
if(err != GLEW_OK)
{
std::cout << glewGetErrorString(err) << std::endl;
SDL_GL_DeleteContext(glContext);
SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window);
SDL_Quit();
return -1;
}
// EVERYTHING'S FINE : TIME TO PAINT IT GREY
glViewport(0, 0, WIDTH, HEIGHT);
SDL_GL_SetSwapInterval(1);
glClearColor(Color::LIGHT_GREY[0], Color::LIGHT_GREY[1], Color::LIGHT_GREY[2], Color::LIGHT_GREY[3]);
// OPENGL DRAWING CALLS AND MAIN DRAWING LOOP
draw_gl(window);
// END
SDL_GL_DeleteContext(glContext);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
And the shader code:
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"
"}";
const char* fragmentShaderSource = "#version 330 core\n"
"out vec4 color;\n"
"void main()\n"
"{\n"
" color = vec4(1.0f, 1.0f, 0.0f, 1.0f);\n"
"}";
Alright, I got it working, but honestly I don't know why it works now with the little changes I made, since it seems to be nearly the same thing.
Here is the code:
#include <SDL2/SDL.h>
#include <GL/glew.h>
#include <iostream>
#include "color.hpp"
#define WIDTH 800
#define HEIGHT 600
float triangle[] =
{
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f
};
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"
"}";
const char* fragmentShaderSource = "#version 330 core\n"
"out vec4 color;\n"
"void main()\n"
"{\n"
" color = vec4(1.0f, 1.0f, 0.0f, 1.0f);\n"
"}";
SDL_Window* createWindow(int w, int h, std::string title)
{
SDL_Window* window = nullptr;
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
std::cout << SDL_GetError() << std::endl;
return nullptr;
}
// OPENGL VERSION
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
// DOUBLE BUFFER
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
if(w == 0 and h == 0)
{
window = SDL_CreateWindow(
title.c_str(),
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
w,
h,
SDL_WINDOW_OPENGL | SDL_WINDOW_FULLSCREEN);
}
window = SDL_CreateWindow(
title.c_str(),
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
w,
h,
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
if(window == nullptr)
{
std::cout << SDL_GetError() << std::endl;
return nullptr;
}
return window;
}
int processInput()
{
SDL_Event event;
if(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
return -1;
if(event.type == SDL_WINDOWEVENT_RESIZED)
glViewport(0, 0, event.window.data1, event.window.data2);
}
return 1;
}
void draw_gl(SDL_Window* window)
{
// VAO
GLuint vao1;
glGenVertexArrays(1, &vao1);
glBindVertexArray(vao1);
// VBO for a triangle
GLuint vbo1;
glGenBuffers(1, &vbo1);
glBindBuffer(GL_ARRAY_BUFFER, vbo1);
glBufferData(GL_ARRAY_BUFFER, sizeof(triangle), triangle, GL_STATIC_DRAW);
// VertexAttribPointer
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// Vertex shader
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, nullptr);
glCompileShader(vertexShader);
// Fragment shader
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, nullptr);
glCompileShader(fragmentShader);
// Check for compile errors if any
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\n" << infoLog << std::endl;
}
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if(! success)
{
glGetShaderInfoLog(fragmentShader, 512, nullptr, infoLog);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
}
// Shader program
GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if(! success)
{
glGetProgramInfoLog(shaderProgram, 512, nullptr, infoLog);
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
glUseProgram(shaderProgram);
while(true)
{
if(processInput() == -1)
break;
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 3);
SDL_GL_SwapWindow(window);
}
}
int main(int argc, char* argv[])
{
// =====> INIT VARS
SDL_Window* window;
SDL_Renderer* renderer;
SDL_GLContext glContext;
GLenum err;
// INIT VARS <=====
window = createWindow(WIDTH, HEIGHT, "OPENGL");
if(window == nullptr)
{
SDL_Quit();
return -1;
}
// OPENGL CONTEXT FOR THE WINDOW
glContext = SDL_GL_CreateContext(window);
// GLEW INIT
glewExperimental = true;
err = glewInit();
if(err != GLEW_OK)
{
std::cout << glewGetErrorString(err) << std::endl;
SDL_GL_DeleteContext(glContext);
SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window);
SDL_Quit();
return -1;
}
// EVERYTHING'S FINE : TIME TO PAINT IT GREY
glViewport(0, 0, WIDTH, HEIGHT);
SDL_GL_SetSwapInterval(1);
glClearColor(Color::LIGHT_GREY[0], Color::LIGHT_GREY[1], Color::LIGHT_GREY[2], Color::LIGHT_GREY[3]);
// OPENGL DRAWING CALLS AND MAIN DRAWING LOOP
draw_gl(window);
// END
SDL_GL_DeleteContext(glContext);
//SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
I am trying to render a triangle in a QGLWidget (Qt5.7) but by some reason, I am unable to show the triangle on screen, only the blue background is showed.
myapp.pro file:
QT += core gui opengl
CONFIG += c++11
TARGET = myapp
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
INCLUDEPATH += ../../libs/glew/include
SOURCES += main.cpp \
../../libs/glew/src/glew.c \
glwindow.cpp
HEADERS += \
../../libs/glew/include/GL/glew.h \
glwindow.h
This is the main function:
#include <QApplication>
#include <glwindow.h>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
glwindow v;
v.show();
return app.exec();
}
The window header:
#include <QtOpenGL/QGLWidget>
class glwindow : public QGLWidget
{
public:
glwindow();
protected:
void initializeGL() override;
void paintGL() override;
};
The cpp file:
#include <GL/glew.h>
#include <glwindow.h>
GLfloat vertices[] = {
+0.0f, +1.0f,
-1.0f, -1.0f,
+1.0f, -1.0f
};
glwindow::glwindow()
{}
void setupGeometry()
{
GLuint buffer_id;
glGenBuffers(1, &buffer_id);
glBindBuffer(GL_ARRAY_BUFFER, buffer_id);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
}
void glwindow::initializeGL()
{
glewInit();
setupGeometry();
}
void glwindow::paintGL()
{
glClearColor(0.0, 0.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
Update 1:
Added the shader code:
const GLchar *vs = "#version 150 // Specify which version of GLSL we are using."
"in vec2 in_Position;"
"void main() "
"{"
" gl_Position = vec4(in_Position.x, in_Position.y, 0.5, 1.0);"
"}";
const GLchar *fs = "#version 150 // Specify which version of GLSL we are using."
"precision highp float; // Video card drivers require this line to function properly"
"out vec4 fragColor;"
"void main()"
"{"
" fragColor = vec4(1.0,1.0,1.0,1.0); //Set colour of each fragment to WHITE"
"}";
The function that setup shader is:
void checkShader(GLuint ID)
{
GLint compile_status = 0;
glGetShaderiv(ID, GL_COMPILE_STATUS, &compile_status);
if(compile_status != GL_TRUE)
{
GLint info_length;
GLsizei buffer_size;
glGetShaderiv(ID, GL_INFO_LOG_LENGTH, &info_length);
GLchar *message = new GLchar[info_length];
glGetShaderInfoLog(ID, info_length, &buffer_size, message);
delete[] message;
}
}
void initShader()
{
GLuint program_id;
GLuint vs_id, fs_id;
vs_id = glCreateShader(GL_VERTEX_SHADER);
fs_id = glCreateShader(GL_FRAGMENT_SHADER);
const char *adapter[1];
adapter[0] = vs;
glShaderSource(vs_id, 1, adapter, 0);
adapter[1] = fs;
glShaderSource(fs_id, 1, adapter, 0);
glCompileShader(vs_id);
checkShader(vs_id);
glCompileShader(fs_id);
checkShader(fs_id);
program_id = glCreateProgram();
glAttachShader(program_id, vs_id);
glAttachShader(program_id, fs_id);
glLinkProgram(program_id);
glUseProgram(program_id);
}
So, the init function is changed to
void glview::initializeGL()
{
glewInit();
initGeometry();
initShader();
}
The shader initialization is failed with error message :
(GLchar *) 0x7efe21 \":1(10): error: GLSL 1.50 is not supported. Supported versions are: 1.10, 1.20, 1.30, 1.00 ES, and 3.00 ES\n\"
Qt 5.7 only supports to glsl 1.3.0. So if ppl want to use glsl 4.3, try glfw or SDL.
well, I have being working in java and c++ for a while, but Im new to OpenGL, so I started using a library called GLFW, I have being following a book called "OpenGL Super Bible 6th Edition" but in GLFW mode. The problem here is that I have rechecked all and watched other tutorials and my code seams to be alright but nothing from the shaders renders. I don't know if the part where I declare the shader src is okay or even a valid form.
Thank you for even read this :)
NOTE:
I know it will render only a point but I resized it with "glPointSize(40.0f);".
#include <GL/glew.h>
#define GLFW_DLL
#include <GLFW/glfw3.h>
#include <stdio.h>
#include <iostream>
#include "jelly/lua_manager.h"
#include "jelly/keysManager.h"
jelly::keys_buttons::KeysManager km;
GLuint vertex_array_obj;
GLuint program;
GLuint startRender(GLFWwindow* window)
{
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
std::cout << "ASD" << std::endl;
static const GLchar * fragmentShader_src[] =
{
"#version 430 core \n"
" \n"
"void main(void) \n"
"{ \n"
" gl_Position = vec4(0, 0.5, 0.0, 1); \n"
"} \n"
};
static const GLchar * vertexShader_src[] =
{
"#version 430 core \n"
" \n"
"out vec4 color; \n"
" \n"
"void main(void) \n"
"{ \n"
" color = vec4(0.0, 0.8, 1.0, 1.0); \n"
"} \n"
};
glShaderSource(vertexShader, 1, vertexShader_src, NULL);
glCompileShader(vertexShader);
glShaderSource(fragmentShader, 1, fragmentShader_src, NULL);
glCompileShader(fragmentShader);
GLuint tprogram = glCreateProgram();
glAttachShader(tprogram, vertexShader);
glAttachShader(tprogram, fragmentShader);
glLinkProgram(tprogram);
glValidateProgram(tprogram);
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
glGenVertexArrays(1, &vertex_array_obj);
glBindVertexArray(vertex_array_obj);
return tprogram;
}
void render(GLFWwindow* window)
{
glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_POINTS, 0, 1);
glPointSize(40.0f);
}
void mouseCallback(GLFWwindow* window, int button, int action, int mods)
{
km.mouseClick(button, action, mods);
}
void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
km.keyPressed(key, action, mods);
}
int main()
{
jelly::lua::LuaManager lm;
// 0 = Build | 1 = Release | 2 = Alpha | 3 = Beta
int buildType = 0;
std::string title = "Relieved";
if (buildType != 1)
{
switch (buildType) {
case 0 :
title += " | Build Version";
break;
case 2 :
title += " | Alpha Version";
break;
case 3 :
title += " | Beta Version";
break;
default :
break;
}
}
GLFWwindow* window;
if (!glfwInit()) {
glfwTerminate();
return -1;
}
window = glfwCreateWindow(640, 400, title.c_str(), NULL, NULL);
if (!window) {
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
glewInit ();
program = startRender(window);
glUseProgram(program);
glfwSetKeyCallback(window, keyCallback);
glfwSetMouseButtonCallback(window, mouseCallback);
while(!glfwWindowShouldClose(window))
{
render(window);
glfwSwapBuffers(window);
glfwPollEvents();
}
glDeleteVertexArrays(1, &vertex_array_obj);
glDeleteProgram(program);
glDeleteVertexArrays(1, &vertex_array_obj);
glfwTerminate();
return 0;
}
The two variables that contain shaders' sources are named incorrectly. You've misplaced vertex source into fragmentShader_src and fragment source into vertexShader_src.
You would easily found the error if you checked shader compilation and linking status. You should add appropriate ifs and print logs if shader compilation or linking fails.
Also, you're missing an explicit OpenGL version selection. You should ask GLFW to give you 'OpenGL 4.3 compatibility profile' context. ('core profile' works too if you don't need any deprecated features.) Check GLFW docs for information how to do it.