I am working through a book on OpenGL, and I am stuck on the third chapter. The following code should open a window and draw a single dot in the center.
The window shows up, glClearColor and glClear seem to be working, but no dot. This answer leads me do believe I may need to compile and link simple vertex and fragment shaders.
The book has made no mention of them, but do I need shaders? Am I missing something else with how I am drawing the dot? Something with the window? The book uses some WIN32 thing for windowing, I am using Linux Mint 17.1
Code below:
#include <stdlib.h>
#include <stdio.h>
#include <GL/glew.h>
#include <GL/gl.h>
#include <GLFW/glfw3.h>
#include <GL/glut.h>
#define BUFFER_OFFSET(i) ( (char*)NULL + (i) )
static void error_callback(int error, const char* description) {
fputs(description, stderr);
}
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}
int main(void) {
GLFWwindow* window;
glfwSetErrorCallback(error_callback);
if (!glfwInit()) {
exit(EXIT_FAILURE);
}
window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL);
if (!window) {
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
glfwSetKeyCallback(window, key_callback);
if (glewInit()) {
glfwTerminate();
exit(EXIT_FAILURE);
}
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
float ratio;
int width, height;
glfwGetFramebufferSize(window, &width, &height);
ratio = width / (float) height;
glViewport(0, 0, width, height);
GLfloat vertex [] = {0.0f, 0.0f, -2.0f};
GLuint m_vertexBuffer;
glGenBuffers(1, &m_vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3, &vertex[0], GL_STATIC_DRAW);
while (!glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT);
glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer);
glVertexPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0));
glPointSize(10.0f);
glEnableClientState(GL_VERTEX_ARRAY);
glDrawArrays(GL_POINTS, 0, 1);
glDisableClientState(GL_VERTEX_ARRAY);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
exit(EXIT_SUCCESS);
}
Also, I am compiling with g++ main.cpp -lGL -lglfw -lGLU -lGLEW
I have OpenGL 4.5 installed, but am opening a fixed-pipeline context.
From your code:
GLfloat vertex [] = {0.0f, 0.0f, -2.0f};
Since you use the fixed function pipelione and never set any matrices, both the projection and the modelView matrix will be left at the default, and the point (0, 0, -2) is outside fo the viewing volume. Try (0, 0, 0) instead.
If the book uses excatly this code with exactly that vertex coordinates, you'd better search for another one. You should also consider learning "modern" GL instead. The fixed-funciton pipeline basically has been superseeded by shaders in GL2.0 a decade ago.
Related
I have read some tutorials to write the following code.
The only difference is the original tutorials where using SDL instead of GLEW.
I do not understand what is wrong in this code. It compiles but i do not see the triangle. (the tutorial were not using shaders too)
#include <iostream>
#include <GL/glew.h>
#include <GL/gl.h>
#include <GLFW/glfw3.h>
GLFWwindow* window;
int main(int argc, const char * argv[])
{
if (!glfwInit())
{
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
window = glfwCreateWindow(640, 480, "Test", NULL, NULL);
if (window==NULL)
{
return -1;
}
glfwMakeContextCurrent(window);
glewExperimental = true;
if (glewInit() != GLEW_OK)
{
return -1;
}
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
glClearColor(0.0f, 1.0f, 1.0f, 1.0f);
do
{
glfwPollEvents();
float vertices[] = {-0.5, -0.5, 0.0, 0.5, 0.5, -0.5};
glClear(GL_COLOR_BUFFER_BIT);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glEnableVertexAttribArray(0);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
glfwSwapBuffers(window);
}
while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS && glfwWindowShouldClose(window) == 0 );
glfwTerminate();
return 0;
}
If you're using the fixed-function pipeline, you cannot use generic vertex attributes like glVertexAttribPointer.
NVIDIA's implementation, however, illegally aliases between generic attributes and non-generic ones. This is probably why the initial writer of the tutorial got away with it on their machine.
If you want to write this in a cross-platform way, you have to use glVertexPointer and glEnableClientState:
glVertexPointer(2, GL_FLOAT, 0, vertices);
glEnableClientState(GL_VERTEX_ARRAY);
I am trying to make a triangle and I'm using GLEW, GLFW and GLM as extensions of OpenGL.
Here's the code I have:
#include <stdlib.h>
#include <stdio.h>
#include <gl\glew.h>
#include <GLFW\glfw3.h>
#include <glm\glm.hpp>
using namespace glm;
int main()
{
if (!glfwInit())
{
fprintf(stderr, "Failed to initialize GLFW\n");
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // We want OpenGL 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //We don't want the old OpenGL
// Open a window and create its OpenGL context
GLFWwindow* window; // (In the accompanying source code, this variable is global)
window = glfwCreateWindow(1024, 768, "Tutorial 01", NULL, NULL);
if (window == NULL)
{
fprintf(stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n");
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window); // Initialize GLEW
glewExperimental = true; // Needed in core profile
if (glewInit() != GLEW_OK)
{
fprintf(stderr, "Failed to initialize GLEW\n");
return -1;
}
// Ensure we can capture the escape key being pressed below
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);
// An array of 3 vectors which represents 3 vertices
static const GLfloat g_vertex_buffer_data[] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
};
// This will identify our vertex buffer
GLuint vertexbuffer;
// Generate 1 buffer, put the resulting identifier in vertexbuffer
glGenBuffers(1, &vertexbuffer);
// The following commands will talk about our 'vertexbuffer' buffer
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
// Give our vertices to OpenGL.
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
do {
// 1rst attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
// Draw the triangle !
glDrawArrays(GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle
glDisableVertexAttribArray(0);
// Swap buffers
glfwSwapBuffers(window);
glfwPollEvents();
} // Check if the ESC key was pressed or the window was closed
while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS &&
glfwWindowShouldClose(window) == 0);
}
Edit
The code is not displaying the white triangle its just opens the window and shows empty black.
Also at first I added the code I messed around with, which is missing part of the code. However it still does not produce the white triangle.
Do I need to use a shader for OpenGL 3.3?
You need to use a shader or you won't see anything. If you do see something then that is unspecified behavior. Which highly depend on each individual driver.
You could try fiddling with the clear color and clear the screen:
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
The tutorial was able to get a white triangle on a black background. On my computer the opposite was the case.
The tutorial also points out:
If you’re on lucky, you can see the result (don’t panic if you don’t)
So read the rest of the tutorial, it also includes a shader.
I'm very new to OpenGL and at the moment I'm trying to understand VAO and VBO.
The VAO is simply a collection of VBO.
Each VBO is an attribute of an object, coordinates of vertices of the object, color at each object's vertex, etc.
In the code below, the Vertex struct define 2 properties of a vertex, which is position and color of the vertex. Therefore, 2 VBOs are needed to store these information.
My problem is: I'm able to draw the triangle on the screen, but the color at each vertex doesn't seem to be drawn. Why does this happen?
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
// create vertex buffer
GLuint vertexbuffer;
struct Vertex{
GLdouble position[3];
GLfloat color[3];
};
static const GLfloat vertex_data[] = {
-0.7f, -0.7f, 0.0f,
0.7f, -0.7f, 0.0f,
0.0f, 1.0f, 0.0f
};
void SetupGeometry(){
const struct Vertex triangle[3] = {
{{-0.7, -0.7, 0.0}, {1.0f, 0.0f, 0.0f}},
{{0.7, -0.7, 0.0}, {0.0f, 1.0f, 0.0f}},
{{0.0, 1.0, 0.0}, {0.0f, 0.0f, 1.0f}}
};
//GLuint VertexArrayID;
//glGenVertexArrays(1, &VertexArrayID);
//glBindVertexArray(VertexArrayID);
//generate 1 buffer, put the resulting identifier in vertex buffer
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
// give our vertices to OpenGL
glBufferData(GL_ARRAY_BUFFER, 3*sizeof(struct Vertex), triangle, GL_STATIC_DRAW);
// GLuint index, GLuint size, GLenum type, GLboolean normalized, GLsizei stride, const void *offset
glVertexAttribPointer(0, 3, GL_DOUBLE, GL_FALSE, sizeof(struct Vertex), (void*) offsetof(struct Vertex, position));
// any newly created VAO disables array access for all attributes
// array access is enabled by binding the VAO in SetupGeometry and calling:
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(struct Vertex), (void*) offsetof(struct Vertex, color));
glEnableVertexAttribArray(1);
}
void SetupShaders(void){
}
void Render(int i){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawArrays(GL_LINE_LOOP, 0, 3);
}
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods){
if((key == GLFW_KEY_ESCAPE || key == GLFW_KEY_Q) && action != GLFW_PRESS){
glfwSetWindowShouldClose(window, GL_TRUE);
}
}
int main( void ) {
/* Create a windowed mode window and its OpenGL context */
GLFWwindow* window;
if( !glfwInit() ) {
printf("Failed to start GLFW\n");
exit( EXIT_FAILURE );
}
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window) {
glfwTerminate();
printf("GLFW Failed to start\n");
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window); // IMPORTANT: Must be done so glew recognises OpenGL
glfwWindowHint(GLFW_SAMPLES, 4);
int err = glewInit();
if (glewInit() != GLEW_OK) {
/* Problem: glewInit failed, something is seriously wrong. */
fprintf(stderr, "Error initializing GLEW: %s\n", glewGetErrorString(err));
}
fprintf(stderr, "Glew done\n");
fprintf(stderr, "GL VERSION INFO %s\n", glGetString(GL_VERSION));
glfwSetKeyCallback(window, key_callback);
SetupGeometry();
while(!glfwWindowShouldClose(window)){
Render(0);
glfwSwapBuffers(window);
glfwPollEvents();
}
}
You don't seem to have included any shaders. You'll need to use a fragment shader to set the colour values for the triangle. This link should help you.
I have problem in seeing my triangle in a black window. The window though just closes as it opens, and does not allow me to see what's going on inside it. I have seen somewhere on the net that i has something to do with the minor-versioning, of which I have no clue how to check on my VGA card.
Here is my full code:
#define GLEW_STATIC
#include <stdio.h>
#include <GL\glew.h>
#include <GL\GLU.h>
#include <GL\glut.h>
#include <glm.hpp>
#include <GL\gl.h>
#include <GLFW\glfw3.h>
using namespace glm;
using namespace std;
int main()
{
glfwWindowHint(GLFW_SAMPLES, 4); // anti aliasing
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // openGL major version to be 3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); // minor set to 3, which makes the version 3.3
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // for MAC OS only
glfwWindowHint(GLFW_OPENGL_COMPAT_PROFILE, GLFW_OPENGL_CORE_PROFILE); //avoid using old openGL
GLFWwindow* window;
window = glfwCreateWindow(1024, 768, "First Window in OpenGL", NULL, NULL);
if (window == NULL)
{
fprintf(stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n");
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);
static const GLfloat g_vertex_buffer_data[] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0,
0.0f, 1.0f, 0.0f
};
// identifying our vertex buffer
GLuint vertexbuffer;
glGenBuffers(1, &vertexbuffer);
// The following commands will talk about our 'vertexbuffer' buffer
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
// Give our vertices to OpenGL.
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
glewExperimental = true; // Needed in core profile
if (glewInit() != GLEW_OK)
{
fprintf(stderr, "Failed to initialize GLEW\n");
return -1;
}
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
do{
// 1rst attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
glDrawArrays(GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle
glDisableVertexAttribArray(0);
glfwSwapBuffers(window);
glfwPollEvents();
}
while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS &&
glfwWindowShouldClose(window) == 0);
return 0;
}
Can anyone please help me on keeping the window open and just close it with ESC key as it is expected from the code?
The reason the window closes immediately is because you have a segmentation fault.
This is most likely due to failing to initialize things in proper order.
Initialize GLFW before running any glfw function calls, like so:
// Initialise GLFW
if( !glfwInit() ) {
fprintf( stderr, "Failed to initialize GLFW\n" );
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 4); // anti aliasing
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // openGL major version to be 3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); // minor set to 3, which makes the version 3.3
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // for MAC OS only
glfwWindowHint(GLFW_OPENGL_COMPAT_PROFILE, GLFW_OPENGL_CORE_PROFILE); //avoid using old openGL
GLFWwindow* window;
window = gl
Then, move the initialization of glew to right after you create and set the GL context:
if (window == NULL) {
fprintf(stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n");
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE; // Needed in core profile
const GLenum err = glewInit();
if (err != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW\n");
fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
return -1;
}
GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);
// ... rest of code follows
I have tested it, and your code should work fine now, and the window won't close until you hit ESC.
This is my code so far, it's just tutorial code from open.gl
#include <GL/GL.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <thread>
#define GLEW_STATIC
int main(){
//printf("%s\n", glGetString(GL_EXTENSIONS))
glewExperimental = GL_TRUE;
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
GLFWwindow* window = glfwCreateWindow(800, 600, "OpenGL", nullptr, nullptr); // Windowed
//GLFWwindow* window = glfwCreateWindow(1920, 1080, "OpenGL", glfwGetPrimaryMonitor(), nullptr); fs
//typedef void (*GENBUFFERS) (GLsizei, GLuint*);
//GENBUFFERS glGenBuffers = (GENBUFFERS)glfwGetProcAddress("glGenBuffers");
printf("%s\n", glGetString(GL_EXTENSIONS));
glfwMakeContextCurrent(window);
glewInit();
GLuint vertexBuffer;
glGenBuffers(1, &vertexBuffer);
printf("%u\n", vertexBuffer);
float vertices[] = {
0.0f, 0.5f, // Vertex 1 (X, Y)
0.5f, -0.5f, // Vertex 2 (X, Y)
-0.5f, -0.5f // Vertex 3 (X, Y)
};
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
//glfwSetWindowShouldClose(window, GL_FALSE);
while(!glfwWindowShouldClose(window)){
glfwSwapBuffers(window);
glfwPollEvents();
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}
glfwTerminate();
}
returns the exception above... what is going on and what's wrong?
the error drops at glGenBuffers(1,&vertexBuffer);
I'm still very new to opengl programming so please break it down into autismally small pieces for me if you can :)
An error occurring at location 0 suggests you're trying to call a function using a null function pointer. The culprit in your case seems to be glGenBuffers. It's been in the core for a long time, so it's almost certainly supported by your machine. It probably just hasn't been initialised properly.
You can get libraries such as GLEW (GL Extension Wrangler) to help you with that. It's not something you should rely on long-term, but can definitely help you get started without worrying about lots of details.
Alternatively, a very quick-and-dirty solution may be just to call glGenBuffersARB() instead. Again, that's definitely not something to rely on long-term though!
These questions may help:
glGenBuffers not defined?
OpenGL: How to check if the user supports glGenBuffers()?