Hey everybody,
Recently I have been sparked to try and mess around with OpenGL again. A few years back I made a start as well and managed to draw some objects on the screen and move them around, but now I can’t even draw a single pixel. After consulting many online tutorials I have still not managed to draw a single pixel (except for a clear screen at the assigned colour).
I just can’t find my error and spent so many hours looking for the answer that I allowed myself to post the question here.
I am really just looking for the mistake I am making.
Here is some of the info I print when I run and get the blue screen:
OpenGL version: 4.0.0 NVIDIA 350.12
Supported GLSL version 4.00 NVIDIA via Cg compiler.
GLEW 1.13.0
Not being able to draw a single pixel is really limiting my progress and I really have no clue where to look anymore. The weirdest part is that I have managed to do it the first time I started with OpenGL programming. Personally I think it has something to do with the shaders, but after many hours of trial and error I still get no useful result.
The following code compiles and does not give any errors, but simply gives a blue screen where I believe it should contain a white triangle.
// OPENGL
#include "GL/glew.h"
#include "GL/freeglut.h"
// STANDARD LIBRARIES
#include <iostream>
#include <cstdlib>
// GLM
#include <glm/glm.hpp>
// UNIVERSE
#include "Universe.h" // <string> <vector> <MyTime>
// SPECIAL INCLUDES
#include <stdio.h>
#include "glew.c"
// DEFINTIONS
#define WINDOW_TITLE_PREFIX "My World v2.0"
// Global Constants
int currentWidth = 800;
int currentHeight = 600;
int windowHandle = 0;
const GLchar* vertexSource =
"#version 150 core
"
"in vec2 position"
"void main()"
"{"
" gl_Position = vec4(position, 0.0, 1.0);"
"}";
const GLchar* fragmentSource =
"#version 150 core
"
"out vec4 outColor;"
"void main()"
"{"
" outColor = vec4(1.0, 1.0, 1.0, 1.0);"
"}";
// Function Prototyping
void initialize(int, char* []);
void initWindow(int, char* []);
void resizeFunction(int, int);
void renderFunction(void);
void update() {
//cout << MyTime::getInstance().getSecondOffsetFromStart() << endl;
}
/*
*
*/
int main(int argc, char** argv) {
// Initialize
initialize(argc, argv);
// Vertices
float vertices[] = {
0.0f, 0.5f,
0.5f, -0.5f,
-0.5f, -0.5f
};
// Test
cout << vertexSource << endl;
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexSource, NULL);
glCompileShader(vertexShader);
GLint status;
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &status);
char buffer[512];
glGetShaderInfoLog(vertexShader, 512, NULL, buffer);
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
glCompileShader(fragmentShader);
GLint status2;
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &status2);
char buffer2[512];
glGetShaderInfoLog(fragmentShader, 512, NULL, buffer2);
GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glBindFragDataLocation(shaderProgram, 0, "outColor");
glLinkProgram(shaderProgram);
glUseProgram(shaderProgram);
GLint posAttrib = glGetAttribLocation(shaderProgram, "position");
glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(posAttrib);
// Loop
glutIdleFunc(*update);
glutMainLoop();
// Exit
glDeleteProgram(shaderProgram);
glDeleteShader(fragmentShader);
glDeleteShader(vertexShader);
glDeleteBuffers(1, &vbo);
glDeleteVertexArrays(1, &vao);
exit(EXIT_SUCCESS);
}
void initialize(int argc, char* argv[])
{
initWindow(argc, argv);
fprintf(stdout, "OpenGL version: %s
", glGetString(GL_VERSION));
printf("Supported GLSL version %s.
", (char *)glGetString(GL_SHADING_LANGUAGE_VERSION));
glewExperimental = GL_TRUE;
GLenum err = glewInit();
if (GLEW_OK != err) {
fprintf(stderr, "Error: %s
", glewGetErrorString(err));
}
fprintf(stdout, "GLEW %s
", glewGetString(GLEW_VERSION));
glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
}
void initWindow(int argc, char* argv[]) {
{
glutInit(&argc, argv);
glutInitContextVersion(4, 0);
glutInitContextFlags(GLUT_FORWARD_COMPATIBLE);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS);
glutInitWindowSize(currentWidth, currentHeight);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
windowHandle = glutCreateWindow(WINDOW_TITLE_PREFIX);
if (windowHandle < 1) {
fprintf(stderr, "ERROR: Could not create a new rendering window.
");
exit(EXIT_FAILURE);
}
glutReshapeFunc(*resizeFunction);
glutDisplayFunc(*renderFunction);
}
}
void resizeFunction(int width, int height) {
currentWidth = width;
currentHeight = height;
glViewport(0, 0, currentWidth, currentHeight);
}
void renderFunction() {
// Clearing
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Rendering
glDrawArrays(GL_TRIANGLES, 0, 3);
glutSwapBuffers();
glutPostRedisplay();
}