The OpenGL SuperBible 6th Ed (for OpenGL 4.3) has samples that use vertex shaders that have no vertex attribute inputs, but hard coded vertices, e.g.
#version 420 core
void main(void)
{
const vec4 vertices[] = vec4[](vec4( 0.4, -0.4, 0.5, 1.0),
vec4(-0.4, -0.4, 0.5, 1.0),
vec4( 0.4, 0.4, 0.5, 1.0));
gl_Position = vertices[gl_VertexID];
}
When I run the samples, the window is cleared but nothing else happens.
By experimenting I discovered that when binding an empty buffer to the context, the program runs as expected. Below is the sample program:
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h> /
#include <GLFW/glfw3.h>
#define GLM_MESSAGES
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtx/vector_angle.hpp>
int main(int argc, char* argv[]) {
GLuint program;
GLuint vao;
GLuint vbo;
glfwInit();
GLFWwindow* window = glfwCreateWindow(640, 480, "gl_InstanceID", NULL, NULL);
glfwMakeContextCurrent(window);
glewInit();
program = glCreateProgram();
GLuint vs = load("vertex.glsl", GL_VERTEX_SHADER, true);
GLuint fs = load("frag.glsl", GL_FRAGMENT_SHADER, true);
glAttachShader(program, vs);
glAttachShader(program, fs);
glLinkProgram(program);
glDeleteShader(vs);
glDeleteShader(fs);
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
//**************************************
//no triangle drawn when below these lines commented out
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, 4, NULL, GL_DYNAMIC_COPY);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(0);
//***************************************
do {
static const GLfloat green[] = { 0.0f, 0.2f, 0.0f, 1.0f };
glClearBufferfv(GL_COLOR, 0, green);
glUseProgram(program);
glDrawArrays(GL_TRIANGLES, 0, 3); //OR glDrawArraysInstanced(GL_TRIANGLES, 0, 3,1);
glfwSwapBuffers(window);
glfwPollEvents();
if (GLFW_PRESS == glfwGetKey(window, GLFW_KEY_ESCAPE)) {
glfwSetWindowShouldClose(window, 1);
}
} while (!glfwWindowShouldClose(window));
glfwTerminate();
return 0;
}
When adding the code between the stars the triangle is drawn. Without the buffer no triangle is drawn.
Also, when the vertex array object (VAO) is removed (or used as above), the triangle also does not show. Only with the VAO and with the (empty) VBO is the triangle drawn.
Removing the VAO but having the buffer shows the triangle. No buffer no triangle. The VAO doesn't seem to make a difference.
Also note the vertex shader has no inputs.
What is the happening ?
- Compiler / Graphics Card thinks the vertex shader does nothing and removes it ? I have feeling it's the graphics card (driver).
- Is this to be expected from OpenGL or is it because of the graphics card ?
- My understanding of buffers, VAO's and shaders is lacking something ?
Other information that might be useful
- GLEW 1.10.0
- GLFW 3.0.3
Fragment shader:
#version 420 core
out vec4 color;
void main(void)
{
color = vec4(1.0);
}
OS details:
- Linux: 3.2.0-4-amd64
- Distro: Debian 7.3 Wheezy
- uname -m: x86_64
Compiler details
- g++ --version: g++ (Debian 4.7.2-5) 4.7.2
OpenGL details:
- OpenGL Provider: Advanced Micro Devices (from AMD Catalyst Control Center)
- OpenGL Renderer: AMD Radeon HD 7600M Series (from AMD Catalyst Control Center)
- OpenGL Version: 4.2.11762 Compatibility Profile Context (from AMD Catalyst Control Center)
- glxinfo:
- server glx version string: 1.4
- client glx version string: 1.4
- GLX version: 1.4
- OpenGL version string: 4.2.11762 Compatibility Profile Context
- OpenGL shading language version string: 4.20
- server glx vendor string: ATI
- client glx vendor string: ATI
The problem also occurs in Windows using the same graphics card as in Linux, i.e.
- Windows 8
- 64 bit
- Running on a AMD Radeon HD 7670M from ATI Technologies Inc.
- OpenGL version 4.2.11762 Compatibility Profile Context is supported
AMD has a new version 13.12 of their drivers for Linux (I am using 13.4). I'll give that a try when I have some time because the install last time wasn't easy.
I also logged issue 984 on Unofficial AMD Bugzilla. But perhaps it's fixed in 13.12.
However, the program works (both without using an empty buffer and with the empty buffer), running on
- Windows 7 Pro
- 64 bit
- NVIDIA GeForce GT 520M (Driver Date: 2013/10/15, Driver Version: 9.18.13.3158)
and on
- Windows 8
- 64 bit
- Running on a Intel(R) HD Graphics 4000 from Intel,
- OpenGL version 4.0.0 - Build 9.17.10.2849