C++: Removing Moire effect from openGL - c++

I draw patterns which have detailed pixels and they often face with Moire effect. I am not good at shading and I am not sure if this problem will be solved by shaders. I have not found any basic, understandable and complete example of shaders. Most of the tutorial websites start a program from the middle omitting the header file includes!
This is a MWE of my code. Is it possible to mitigate or remove the Moire effect from it?
#include <cmath>
#include <vector>
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
const int w=640,h=480;
float cam_angle=0.0f;
void init()
{
GLfloat LightAmbient[] = { 0.2f, 0.2f, 0.2f, 1.0f };
GLfloat LightDiffuse[] = { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat LightPosition[] = { 5.0f, 5.0f, -10.0f, 1.0f };
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
glLightfv(GL_LIGHT1, GL_POSITION, LightPosition);
glEnable(GL_LIGHT1);
}
void onDisplay()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective (90, float(w)/float(h), 0.01, 10000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
const double pi=3.1415926;
double cam_x=2.0*cos(pi/4.0)*cos(cam_angle);;
double cam_y=2.0*sin(pi/4.0)*cos(cam_angle);;
double cam_z=2.0*sin(cam_angle);
gluLookAt(cam_x, cam_y, cam_z, 0, 0, 0, 0, 0, 1);
struct Point3D
{
double x, y, z;
unsigned char r, g, b, a=255;
};
for(double r=0.5;r<=1.0;r+=0.03)
{
std::vector<Point3D> points;
for(int i=0;i<1000;i++)
{
double theta=double(i)/1000.0*pi*2.0;
Point3D p;
p.x=r*sin(theta);
p.y=r*cos(theta);
p.z=r;
p.r=128;
p.g=200;
p.b=50;
points.push_back(p);
}
// draw
glPushMatrix();
glColor3ub(255,255,255);
glEnableClientState( GL_VERTEX_ARRAY );
glEnableClientState( GL_COLOR_ARRAY );
glVertexPointer(3, GL_DOUBLE, sizeof(Point3D), &points[0].x );
glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(Point3D), &points[0].r );
// glPointSize( 3.0 );
glLineWidth(2.0);
glDrawArrays( GL_LINE_STRIP, 0, int(points.size()) );
glDisableClientState( GL_VERTEX_ARRAY );
glDisableClientState( GL_COLOR_ARRAY );
glPopMatrix();
}
glFlush();
glutSwapBuffers();
}
void Timer(int /*value*/)
{
cam_angle+=0.01f;
glutPostRedisplay();
// 100 milliseconds
glutTimerFunc(100, Timer, 0);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitWindowSize (w, h);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowPosition (100, 100);
glutCreateWindow ("my window");
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
init();
glutDisplayFunc(onDisplay);
Timer(0);
glutMainLoop();
return 0;
}

If you use Multisampling, then the result will be significantly improved:
glutSetOption(GLUT_MULTISAMPLE, 8);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE | GLUT_MULTISAMPLE);
without GLUT_MULTISAMPLE:
with GLUT_MULTISAMPLE:
See also the answer to GLUT + OpenGL multisampling anti aliasing (MSAA)

Related

Performance drop with GL_LINES?

I tried to draw a simple line in OpenGL:
#include "stdafx.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <freeglut.h>
float startX = -180.0f;
float startZ = -100.0f;
float qtyX = 36;
float qtyZ = 20;
float red = 0.0f, green = 0.0f, blue = 0.0f;
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0.0, 50.0f, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0);
glBegin(GL_LINES);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(50.0f, 50.0f, 50.0f);
glEnd();
//glutWireSphere(20.0f, 20.0f, 20.0f);
glutSwapBuffers();
glFlush();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-64.0, 64.0, -36.0, 36.0, 10, 10000.0);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(1280, 720);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutIdleFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
However, when I run the program, the performance drop significantly then after a few seconds, it stops responding (I have to run task manager and force quit the program). However, when I disable the line and draw the glutWireSphere (or a polygon), the performance is normal.
Is there a problem with my code? Or perhaps GL_LINES is deprecated?
Windows 10, 1070 MaxQ, Visual Studio

Opengl and c++ drawing multiple cubes

So I am able to draw a cube, but I want to draw multiple cubes, and I don't know how. Can you guys help me?
This is the code of how I draw the cube:
#include <GL/freeglut.h>
#include <stdlib.h>
void initGL(int width, int height)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,2.0f,100.0f);
glMatrixMode(GL_MODELVIEW);
const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };
glEnable(GL_LIGHT0);glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,2.0f,100.0f);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_CULL_FACE);glCullFace(GL_BACK);
}
static void display(void)
{
glLoadIdentity();
glPushMatrix();
glTranslatef(0.0,0.0,-10);
glRotatef(60,1,0,0);
glRotatef(60,0,1,0);
glutSolidCube(2);
glLoadIdentity();
glTranslatef(0.0,0.0,-10);
glRotatef(60,1,0,0);
glRotatef(60,0,1,0);
glutSolidCube(2);
glPopMatrix();
glFlush();
}
static void idle(void)
{
glutPostRedisplay();
}
int main(int argc, char *argv[])
{
int width = 640;
int height = 480;
glutInit(&argc, argv);
glutInitWindowSize(width,height);
glutInitWindowPosition(10,10);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutCreateWindow("Lab 1: Hello OpenGL World !");
glutDisplayFunc(display);
glutIdleFunc(idle);
initGL(width, height);
glutMainLoop();
return EXIT_SUCCESS;
}
I need to draw multiple cubes like the one that I am drawing. I tried with a for but I just started opengl today and don't know what I'm doing. I have tried multiple things and got very annoyed. Do you also recommend any book?
"I just started opengl today"
It will take a while to fully understand OpenGL but if you had consulted your study material, you should have spoted a few errors in you code.
You need to define a glutReshapeFunc function: without this your viewport is undefined. #KevinSpaghetti is also right your glutDisplayFunc has a bug: all cubes are drawn at the same position and a model-view matrix was not specified.
Here is a code that actually works.
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <iostream>
/*******************************************************************************
********************************************************************************
********************************************************************************/
struct { int width = 640; int height = 480; } window;
/*******************************************************************************
********************************************************************************
********************************************************************************/
void resize(int width, int height)
{
glViewport(0, 0, (window.width = width), (window.height = height));
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, ((GLdouble)window.width / window.height), 2, 100);
glMatrixMode(GL_MODELVIEW);
}
/*******************************************************************************
********************************************************************************
********************************************************************************/
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, 30, 0, 0, 0, 0, 1, 0);
glRotatef(-30, 0, 1, 0); // <----- rotate entire scene by 30 degrees
glColor3f(0.5f, 0.0f, 0.0f);
for (int i = 0; i < 3; ++i) // <----- draw three cubes
{
glPushMatrix();
glRotatef(60, 1.0, 0.0, 0.0);
glRotatef(60, 0.0, 0.1, 0.0);
glutSolidCube(5);
glPopMatrix();
glTranslatef(0.0, 0.0, -10.0f);
}
glFlush();
}
/*******************************************************************************
********************************************************************************
********************************************************************************/
void initGL(void)
{
glClearColor(0.8f, 0.8f, 0.8f, 1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CCW);
const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glEnable(GL_COLOR_MATERIAL);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
}
/*******************************************************************************
********************************************************************************
********************************************************************************/
int main(int argc, char *argv[])
{
//.....................
glutInit(&argc, argv);
glutInitContextFlags(GLUT_FORWARD_COMPATIBLE);
glutInitContextProfile(GLUT_COMPATIBILITY_PROFILE);
glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS);
glutInitWindowSize(window.width, window.height);
glutInitWindowPosition(10, 10);
glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | GLUT_SINGLE);
glutCreateWindow("Lab 1: Hello OpenGL World !");
if (glewInit() != GLEW_OK) exit(EXIT_FAILURE);
glutReshapeFunc(resize);
glutDisplayFunc(display);
initGL();
glutMainLoop();
std::cout << "...it worked!";
//.....................
return 0;
}
I would recommend: OpenGL Programming Guide. It is old OpenGl but still relevant.
glPushMatrix();
glTranslatef(0.0,0.0,-10);
glRotatef(60,1,0,0);
glRotatef(60,0,1,0);
glutSolidCube(2);
glLoadIdentity();
glTranslatef(0.0,0.0,-10);
glRotatef(60,1,0,0);
glRotatef(60,0,1,0);
glutSolidCube(2);
glPopMatrix();
Here it seems that you are drawing 2 cubes in the same position, so the second is drawn over the first, try changing the code to
glPushMatrix();
glTranslatef(2,0.0,-10);
glRotatef(60,1,0,0);
glRotatef(60,0,1,0);
glutSolidCube(2);
glLoadIdentity();
glTranslatef(0.0,0.0,-10);
glRotatef(60,1,0,0);
glRotatef(60,0,1,0);
glutSolidCube(2);
glPopMatrix();

How to display text over rectangle in OpenGL in C++?

My code looks like this Modification in this code will be appreciated. I tried, only rectangle appears not the text.
#include <GL/glut.h>
#include<bits/stdc++.h>
using namespace std;
void init2D(float r, float g, float b)
{
glClearColor(r, g, b, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 600.0, 0.0, 600.0);
}
void RenderToDisplay()
{
int l,lenghOfQuote, i;
char str[80];
strcpy(str,"Have courage and be kind");
cout<<str;
lenghOfQuote = (int)strlen(str);
for (i = 0; i < lenghOfQuote; i++)
{
glColor3f(1.0, 0.0, 0.0);
glutStrokeCharacter(GLUT_STROKE_ROMAN, str[i]);
}
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glVertex3f(150.0f, 200.0f, 0.0f);
glColor3f(0.940, 0.37, 0.47);
glVertex3f(450.0f, 200.0f, 0.0f);
glColor3f(0.940, 0.37, 0.47);
glVertex3f(450.0f, 400.0f, 0.0f);
glColor3f(0.69, 0.27, 0.57);
glVertex3f(150.0f, 400.0f, 0.0f);
glColor3f(0.69, 0.27, 0.57);
glEnd();
RenderToDisplay();
glFlush();
}
int main(int argc, char *argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(600, 600);
glutInitWindowPosition(0,0);
glLineWidth(3);
glutCreateWindow("Assignment Q2");
init2D(0.0, 0.0, 0.0);
glutDisplayFunc(display);
glutMainLoop();
}
You could tell me the comments if you need to ask anything else. I used codeblocks to test this program.
Okay after a day I got it, I'm so dumb :')
#ifdef __APPLE_CC__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
void init2D(float r, float g, float b)
{
glClearColor(r, g, b, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 600.0, 0.0, 600.0);
}
void rectangle()
{
glBegin(GL_POLYGON);
glColor3f(0.4,0,0.8);
glVertex3f(150.0f, 200.0f, 0.0f);
glColor3f(0.4,0,0.8);
glVertex3f(450.0f, 200.0f, 0.0f);
glColor3f(0.6,0,0.6);
glVertex3f(450.0f, 400.0f, 0.0f);
glColor3f(0.6,0,0.6);
glVertex3f(150.0f, 400.0f, 0.0f);
glEnd();
}
void text()
{
char menu[80];
strcpy(menu,"Have courage and be kind");
int len;
len = strlen(menu);
glColor3f(1,1,1);
glMatrixMode( GL_PROJECTION );
glPushMatrix();
glLoadIdentity();
gluOrtho2D( 0, 600, 0, 600 );
glMatrixMode( GL_MODELVIEW );
glPushMatrix();
glLoadIdentity();
glRasterPos2i(190, 300);
for ( int i = 0; i < len; ++i )
{
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, menu[i]);
}
glPopMatrix();
glMatrixMode( GL_PROJECTION );
glPopMatrix();
glMatrixMode( GL_MODELVIEW );
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
rectangle();
text();
glFlush();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(0, 0);
glutInitWindowSize(600, 600);
glutCreateWindow("Assignment 1 Question 2");
init2D(0.0f, 0.0f, 0.0f);
glutDisplayFunc(display);
glutMainLoop();
}

Texture not displaying in OpenGL

The triangle i drew with OpenGL can't be displayed. When i debug my code it's only the white background appeared. Can anyone help me?
#pragma warning(disable:4996)
#include <Windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <glut.h>
#include <SOIL.h>
const float piOver180 = 0.0174532925f;
float heading;
float xPos;
float zPos;
//declarations
GLuint filter;
GLuint texture[3];
int width = 720;
int height = 540;
void init();
void display();
void drawTriangle();
void reshape(int width, int height);
this is my main function
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(width, height);
glutInitWindowPosition(0, 0);
glutCreateWindow("Muzeum");
init();
glutDisplayFunc(display);
glutIdleFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
void init()
{
texture[0] = SOIL_load_OGL_texture
(
"wallpaper.bmp",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
);
texture[1] = texture[2] = texture[0];
if (texture[0] == 0 || texture[1] == 0 || texture[2] == 0)
{
printf("SOIL loading error: '%s'\n", SOIL_last_result());
exit(0);
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
}
my display function
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST);
GLfloat xTrans = -xPos;
GLfloat yTrans = -walk - 0.25f;
GLfloat zTrans = -zPos;
GLfloat sceneRotY = 360.0f - yRot;
glTranslatef(xTrans, yTrans, zTrans);
drawTriangle();
glFlush();
glutSwapBuffers();
}
where i draw my triangle
void drawTriangle()
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glBegin(GL_TRIANGLES);
glNormal3f(0.0f, 0.0f, 1.0f);
glTexCoord2f(-3.0f, 0.0f); glVertex3f(-3.0f, 0.0f, 6.0f);
glTexCoord2f(-3.0f, 0.0f); glVertex3f(-3.0f, 0.0f, 0.0f);
glTexCoord2f(3.0f, 0.0f); glVertex3f(3.0f, 6.0f, 0.0f);
glTexCoord2f(-3.0f, 0.0f); glVertex3f(-3.0f, 0.0f, 6.0f);
glTexCoord2f(3.0f, 0.0f); glVertex3f(-3.0f, 6.0f, 6.0f);
glTexCoord2f(3.0f, 0.0f); glVertex3f(3.0f, 6.0f, 0.0f);
glEnd();
glDisable(GL_TEXTURE_2D);
}
void reshape(int width, int height)
{
glViewport(0, 0, (GLsizei)width, (GLsizei)height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(40.0f, (GLdouble)width / (GLdouble)height, 0.5f, 20.0f);
glMatrixMode(GL_MODELVIEW);
}
What i want to do is a room with texture-mapped, now this is only my floor part.
The main problem is that your geometry is not in your field of view. Your gluPerspective() call sets up a projection transformation with a camera placed on the origin, and pointing in the negative z-direction. With the values for the near and far planes, it will show z-values in the range from -0.5 to -20.0. All the z-values of your geometry are in the range 0.0 to 6.0, so all your geometry is behind the camera.
I tried most of your code, and I got things showing up by adding a translation to the modelview transformation. For example, adding a line after the first two lines of the display() function:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -10.0f);
A few more things that could get in your way:
No call to glutInit(). I figure you have that in the original code.
GL_LINEAR_MIPMAP_LINEAR is not a legal value for GL_TEXTURE_MAG_FILTER. This must be GL_LINEAR or GL_NEAREST. glGetError() is your friend!
Fortunately you didn't enable blending, because your glBlendFunc() call, in combination with a white clear color, would prevent anything from being drawn. You would add to white, and that whole whiter than white thing only works in detergent commercials. ;)
Clearing alpha to 1.0, as you do with the last argument of glClearColor() is quite unusual. Doesn't really hurt for now, since you only use an RGB framebuffer, and do not blend with destination alpha.
glFlush() before glutSwapBuffers() is redundant, and only hurts performance.

Draw array of points with OpenGL

Why in this program four points drawn by glVertex3f rendered in pointed positions, but instead of four array points rendered only one in the center of the window?
#include <GL/glew.h>
#include <GL/glut.h>
#include <iostream>
#include <vector>
using namespace std;
struct point3 {
GLfloat x, y, z;
};
vector<point3> Vertices(4);
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
GLuint abuffer;
glGenVertexArrays(1, &abuffer);
glBindVertexArray(abuffer);
GLuint buffer;
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
Vertices = {{0.3f, 0.3f, 0.5f}, {0.6f, 0.6f, 0.5f}, {0.6f, 0.3f, 0.5f}, {0.3f, 0.6f, 0.5f}};
glBufferData(GL_ARRAY_BUFFER, Vertices.size() * sizeof(Vertices[0]), &Vertices[0], GL_STATIC_DRAW);
glVertexPointer(3, GL_FLOAT, 0, &Vertices[0]);
glDrawArrays(GL_POINTS, 0, Vertices.size());
glFlush();
glBegin(GL_POINTS);
glVertex3f(0.25f, 0.25f, 0.5f);
glVertex3f(0.25f, 0.75f, 0.5f);
glVertex3f(0.75f, 0.75f, 0.5f);
glVertex3f(0.75f, 0.25f, 0.5f);
glEnd();
glFlush();
}
void init (void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("simple OpenGL example");
glutDisplayFunc(display);
GLenum err = glewInit();
if (GLEW_OK != err) {
cerr << "Error: " << glewGetErrorString(err) << endl;
}
cout << "Status: Using GLEW" << glewGetString(GLEW_VERSION) << endl;
init();
glutMainLoop();
}
You haven't enabled the attribute to actually use the array. You do that by:
(OpenGL 2) glEnableClientState(GL_VERTEX_ARRAY) (or GL_COLOR_ARRAY, etc)
(OpenGL 3) glEnableVertexArray(attributeId)
Without that, what you point to with glVertexPointer (GL2) or glVertexAttribPointer (GL3) won't be actually used.