Related
This code is to draw one pyramid and one cube. I am trying to colour them using GL_SMOOTH type by glShadeModel. But the result is that only the pyramid can be smooth while another one cube was only colored like a GL_FLAT one.
It seems that there are some problems with the normal vectors but I was poor at this aspect.
Here are my codes below:
void initGL() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClearDepth(1.0f); // Set background depth to farthest
glEnable(GL_DEPTH_TEST); // Enable depth testing for z-culling
glDepthFunc(GL_LEQUAL); // Set the type of depth-test
glShadeModel(GL_SMOOTH); // Enable smooth shading
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(1.5f, 0.0f, -7.0f);
glBegin(GL_QUADS);
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f( 1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f);
// Bottom face (y = -1.0f)
glColor3f(1.0f, 0.5f, 0.0f); // Orange
glVertex3f( 1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f( 1.0f, -1.0f, -1.0f);
// Front face (z = 1.0f)
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f( 1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f( 1.0f, -1.0f, 1.0f);
// Back face (z = -1.0f)
glColor3f(1.0f, 1.0f, 0.0f); // Yellow
glVertex3f( 1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f( 1.0f, 1.0f, -1.0f);
// Left face (x = -1.0f)
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
// Right face (x = 1.0f)
glColor3f(1.0f, 0.0f, 1.0f); // Magenta
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glEnd(); // End of drawing color-cube
// Render a pyramid consists of 4 triangles
glLoadIdentity(); // Reset the model-view matrix
glTranslatef(-1.5f, 0.0f, -6.0f); // Move left and into the screen
glBegin(GL_TRIANGLES); // Begin drawing the pyramid with 4 triangles
// Front
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f( 0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f(-1.0f, -1.0f, 1.0f);
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(1.0f, -1.0f, 1.0f);
// Right
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(1.0f, -1.0f, 1.0f);
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f(1.0f, -1.0f, -1.0f);
// Back
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f(1.0f, -1.0f, -1.0f);
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(-1.0f, -1.0f, -1.0f);
// Left
glColor3f(1.0f,0.0f,0.0f); // Red
glVertex3f( 0.0f, 1.0f, 0.0f);
glColor3f(0.0f,0.0f,1.0f); // Blue
glVertex3f(-1.0f,-1.0f,-1.0f);
glColor3f(0.0f,1.0f,0.0f); // Green
glVertex3f(-1.0f,-1.0f, 1.0f);
glEnd(); // Done drawing the pyramid
glutSwapBuffers(); // Swap the front and back frame buffers (double buffering)
}
Can someone tell me how could this happen?
When you draw the pyramid, then you've one color associated to each vertex coordinate of the triangle primitives. e.g:
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f( 0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f(-1.0f, -1.0f, 1.0f);
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(1.0f, -1.0f, 1.0f);
This causes that the colors of the vertices (corner) are interpolated on the triangle.
When you draw the cube then you've set one color fro each quad. Each corner of the quad is associated to the same color:
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f( 1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f);
This causes that the entire quad primitiv is uniform colored. If you want a color gradient, then you've to associate different colors to the vertices (corners).
I tried to write an "Engine" to render shapes using OpenGL.
The idea is that you can write "renderers" which are just functions that render shapes and add them to a list for the Engine class to display.
So I added a cube and a pyramid (the code for which I just copied from the internet) - and I made them rotate.
As you can see in the images at the bottom of the question - the shapes acted weird - you could see the back of the shape from the front, etc.
Now, I understand that OpenGL just renders stuff in the order I tell it to render them - causing things that are written first to be rendered first - but I used the glDepthFunc which should make it such that stuff renders by "depth" and not order of writing.
#include <GL/glut.h>
#include <iostream>
#include <list>
namespace Graphics
{
class Engine
{
public:
static void Init();
static void Display();
static void Reshape(GLsizei width, GLsizei height);
static void Timer(int value);
static bool Run(int argc, char** argv);
// A renderer is just a method that does stuff and return a boolean
using renderer_t = bool(*)();
static void AddRenderer(renderer_t renderer);
private:
static std::list<renderer_t> renderers;
};
}
namespace Graphics
{
std::list<Engine::renderer_t> Engine::renderers;
void Engine::Init()
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
glClearDepth(1.0f); // Set background depth to farthest
glEnable(GL_DEPTH_TEST); // Enable depth testing for z-culling
glDepthFunc(GL_LEQUAL); // Set the type of depth-test
glShadeModel(GL_SMOOTH); // Enable smooth shading
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Nice perspective corrections
}
void Engine::Display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear color and depth buffers
glMatrixMode(GL_MODELVIEW); // To operate on model-view matrix
for (auto renderer : Engine::renderers)
{
if (!renderer())
{
std::cout << "A renderer has failed rendering something... :(" << std::endl;
}
}
glutSwapBuffers(); // Swap the front and back frame buffers (double buffering)
}
void Engine::Reshape(GLsizei width, GLsizei height)
{ // GLsizei for non-negative integer
// Compute aspect ratio of the new window
if (height == 0) height = 1; // To prevent divide by 0
GLfloat aspect = (GLfloat)width / (GLfloat)height;
// Set the viewport to cover the new window
glViewport(0, 0, width, height);
// Set the aspect ratio of the clipping volume to match the viewport
glMatrixMode(GL_PROJECTION); // To operate on the Projection matrix
glLoadIdentity(); // Reset
// Enable perspective projection with fovy, aspect, zNear and zFar
gluPerspective(45.0f, aspect, 0.1f, 100.0f);
}
void Engine::Timer(int value)
{
glutPostRedisplay(); // Post re-paint request to activate display()
glutTimerFunc(15, Engine::Timer, 0); // next timer call milliseconds later
}
bool Engine::Run(int argc, char** argv)
{
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode(GLUT_DOUBLE); // Enable double buffered mode
glutInitWindowSize(640, 480); // Set the window's initial width & height
glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
glutCreateWindow("FML"); // Create window with the given title
glutDisplayFunc(Engine::Display); // Register callback handler for window re-paint event
glutReshapeFunc(Engine::Reshape); // Register callback handler for window re-size event
Engine::Init(); // Our own OpenGL initialization
glutTimerFunc(0, Engine::Timer, 0); // Call the next display immediately
glutMainLoop(); // Enter the infinite event-processing loop
return true;
}
void Engine::AddRenderer(renderer_t renderer)
{
Engine::renderers.push_back(renderer);
}
}
using namespace Graphics;
static bool RenderCube()
{
static auto angleCube = 0.0f;
// Render a color-cube consisting of 6 quads with different colors
glLoadIdentity(); // Reset the model-view matrix
glTranslatef(1.5f, 0.0f, -7.0f); // Move right and into the screen
glRotatef(angleCube, 1.0f, 1.0f, 1.0f);
glBegin(GL_QUADS); // Begin drawing the color cube with 6 quads
// Top face (y = 1.0f)
// Define vertices in counter-clockwise (CCW) order with normal pointing out
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f( 1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f);
// Bottom face (y = -1.0f)
glColor3f(1.0f, 0.5f, 0.0f); // Orange
glVertex3f( 1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f( 1.0f, -1.0f, -1.0f);
// Front face (z = 1.0f)
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f( 1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f( 1.0f, -1.0f, 1.0f);
// Back face (z = -1.0f)
glColor3f(1.0f, 1.0f, 0.0f); // Yellow
glVertex3f( 1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f( 1.0f, 1.0f, -1.0f);
// Left face (x = -1.0f)
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
// Right face (x = 1.0f)
glColor3f(1.0f, 0.0f, 1.0f); // Magenta
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glEnd();
angleCube += 0.2f;
return true;
}
static bool RenderPyramid()
{
static auto anglePyramid = 0.0f;
// Render a pyramid consists of 4 triangles
glLoadIdentity(); // Reset the model-view matrix
glTranslatef(-1.5f, 0.0f, -6.0f); // Move left and into the screen
glRotatef(anglePyramid, 1.0f, 1.0f, 1.0f);
glBegin(GL_TRIANGLES); // Begin drawing the pyramid with 4 triangles
// Front
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f( 0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f(-1.0f, -1.0f, 1.0f);
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(1.0f, -1.0f, 1.0f);
// Right
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(1.0f, -1.0f, 1.0f);
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f(1.0f, -1.0f, -1.0f);
// Back
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f(1.0f, -1.0f, -1.0f);
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(-1.0f, -1.0f, -1.0f);
// Left
glColor3f(1.0f,0.0f,0.0f); // Red
glVertex3f( 0.0f, 1.0f, 0.0f);
glColor3f(0.0f,0.0f,1.0f); // Blue
glVertex3f(-1.0f,-1.0f,-1.0f);
glColor3f(0.0f,1.0f,0.0f); // Green
glVertex3f(-1.0f,-1.0f, 1.0f);
glEnd(); // Done drawing the pyramid
anglePyramid += 0.25f;
return true;
}
int main(int argc, char** argv)
{
Engine::AddRenderer(RenderCube);
Engine::AddRenderer(RenderPyramid);
return Engine::Run(argc, argv);
}
Images of the resulting shapes rotating
As you can see the shapes are acting weird - you can see the back of them even though I used the glDepthFunc.
The OS/driver is under no obligation to give you any depth buffer bits unless you specifically request them; zero bits, no depth buffering.
As #BDL pointed out, for GLUT that means ORing in GLUT_DEPTH to your glutInitDisplayMode() parameter.
I'm trying to draw a "transparent" cube (with only edges) and place a pyramid inside this cube, the problem is that the edges of the cube are drawing over pyramid when I rotate it.
Here is an example
I understand why this is happening (because I draw pyramid first and then a cube), but I want that these edges to not be drawn over the pyramid when this is not needed.
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QOpenGLWindow>
#include <QSurfaceFormat>
#include <QOpenGLContext>
#include <QTimer>
#ifdef _DEBUG
# include <QDebug>
#endif
class MainWindow: public QOpenGLWindow
{
Q_OBJECT
public:
MainWindow();
private:
QOpenGLContext *m_context;
QTimer m_timer;
GLfloat m_angle;
private slots:
void rotate();
protected:
void initializeGL() override;
void paintGL() override;
void paintEvent(QPaintEvent *event) override;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
MainWindow::MainWindow():
QOpenGLWindow(),
m_angle(0.0f)
{
QSurfaceFormat sf;
sf.setProfile(QSurfaceFormat::CompatibilityProfile);
sf.setVersion(2, 1);
setFormat(sf);
setSurfaceType(QWindow::OpenGLSurface);
create();
m_context = new QOpenGLContext();
m_context->setFormat(sf);
m_context->create();
m_context->makeCurrent(this);
connect(&m_timer, SIGNAL(timeout()), this, SLOT(rotate()));
m_timer.start(33);
}
void MainWindow::rotate()
{
m_angle += 0.5f;
if (m_angle >= 360.0f) {
m_angle = 0.0f;
}
update();
}
void MainWindow::initializeGL()
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
}
void MainWindow::paintGL()
{
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glRotatef(m_angle, 1.0f, 1.0f, 1.0f);
glScalef(0.3f, 0.3f, 0.3f);
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f( 0.0f, 1.0f, 0.0f);
glVertex3f( 1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f( 1.0f, -1.0f, 1.0f);
glVertex3f( 0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f( 0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glColor3f(1.0f, 1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
// bottom
glColor3f(0.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f( 1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f( 1.0f, -1.0f, -1.0f);
glVertex3f( 1.0f, -1.0f, 1.0f);
glEnd();
// The cube (only edges)
glScalef(1.5f, 1.5f, 1.5f);
glColor3f(0.0f, 0.0f, 0.0f);
glBegin(GL_LINE_LOOP);
glVertex3f( 1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f( 1.0f, -1.0f, -1.0f);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex3f( 1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f( 1.0f, -1.0f, 1.0f);
glEnd();
glBegin(GL_LINES);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glEnd();
glFlush();
}
void MainWindow::paintEvent(QPaintEvent*)
{
paintGL();
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.resize(480, 480);
w.show();
return a.exec();
}
... after that I got vice-versa behavior, take a look at this screenshot It seems like the front edges behave like back edges.
The triangles of your pyramid are set clockwise, but default, counterclockwise polygons are taken to be front-facing. Set glFrontFace(GL_CW) or change the order of glVertex3f.
Could someone explain me why triangle is rendering over quad please ?
When Depth Testing is active, the fragments will be displayed depending on their depth. The depth value from the fragment is compared to the depth value from the matching sample currently in the framebuffer. The conditional test is specified by the function glDepthFunc. The initial value of glDepthFunc is GL_LESS.
In your example the depth of the triangle's fragments is LESS than that of the quad's fragments, so the triangle is over the quad.
Setting minimal depth buffer didn't changed anything for me (likely because platform setting was already 24, but checking those defaults is not without merit). The problem is that displayed order of cube's edges still look wrong: the edge that supposed to be closer is painted over by pyramid.
Because you don't do anything about visibility of edges it causes illusion that cube must be rotating in direction opposite and perpendicular to rotation of pyramid.
The problem is that it's pyramid which is actually wrong. Let me prove that:
glBegin(GL_QUADS);//(GL_LINE_LOOP);
glVertex3f( 1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f( 1.0f, -1.0f, -1.0f);
glEnd();
Resulting image is:
Cube rotates in supposed direction, it isn't inside out. That means that pyramid is. One reason of that is that we actually see back faces, not front ones.
If I convert your code to emulate transparent pyramid, , but save default culling settings:
void MainWindow::initializeGL()
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
/*...*/
glFrontFace(GL_CCW);
glBegin(GL_TRIANGLES);
glColor4f(1.0f, 0.0f, 0.0f, 0.3f);
glVertex3f( 0.0f, 1.0f, 0.0f);
glVertex3f( 1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glColor4f(0.0f, 1.0f, 0.0f, 0.3f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f( 1.0f, -1.0f, 1.0f);
glVertex3f( 0.0f, 1.0f, 0.0f);
glColor4f(0.0f, 0.0f, 1.0f, 0.3f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f( 0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glColor4f(1.0f, 1.0f, 0.0f, 0.3f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
// bottom
glColor4f(0.0f, 1.0f, 1.0f, 0.3f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f( 1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f( 1.0f, -1.0f, -1.0f);
glVertex3f( 1.0f, -1.0f, 1.0f);
glEnd();
glFrontFace(GL_CW);
glBegin(GL_TRIANGLES);
glColor4f(1.0f, 0.0f, 0.0f, 0.6f);
glVertex3f( 0.0f, 1.0f, 0.0f);
glVertex3f( 1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glColor4f(0.0f, 1.0f, 0.0f, 0.6f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f( 1.0f, -1.0f, 1.0f);
glVertex3f( 0.0f, 1.0f, 0.0f);
glColor4f(0.0f, 0.0f, 1.0f, 0.6f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f( 0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glColor4f(1.0f, 1.0f, 0.0f, 0.6f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
// bottom
glColor4f(0.0f, 1.0f, 1.0f, 0.6f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f( 1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f( 1.0f, -1.0f, -1.0f);
glVertex3f( 1.0f, -1.0f, 1.0f);
glEnd();
I get image
I still see how pyramid rotates, all of its edges and that line of cube that is "behind" is clipped (because blending does nothing to depth test).
But now if you look at the "bold" faces of pyramid - I intentionally made faces that are invisible without blending bold by giving them larger alpha value - you'll see that they rotate properly, in same direction as cube does. That means that your mesh, the pyramid, is actually inverted, "inside out". With non-convex figures that would be more obvious, with convex ones that creates illusion of opposite rotation.
All you should do is change order of vertices or switch to GL_CW for pyramid (GL_CCW is default).
I surmise that this is expected look of the pyramid, if the face colors are taken in account. If it isn't, that means that you assumed that vertices with greater Z-coordinate is further away from projection plane. With Identity projection matrix and GL_LESS depth test function it's opposite. Be advised, if you switch direction of only one axis, that literally inverts all rules that related to direction of rotation, counterclockwise becomes clockwise.
NB: Trick I used for transparency illusion doesn't work that well with non-convex figures. Why? To use color blending you have to draw faces in order of their depth test doing this manually. My dirty trick does draw all faces twice, but culling removes those which face camera during first pass, and removes those which face away during second pass. For non-convex that doesn't match the order - there are faces that face camera but are further away than some "hidden" ones, wrong order. More work is required. Usage of flexible pipeline really changes how OpenGL can handle this.
I've finally done it:
#include "mainwindow.h"
MainWindow::MainWindow():
QOpenGLWindow(),
m_angle(0.0f)
{
QSurfaceFormat sf;
sf.setProfile(QSurfaceFormat::CompatibilityProfile);
sf.setDepthBufferSize(24);
sf.setVersion(2, 1);
setSurfaceType(QWindow::OpenGLSurface);
setFormat(sf);
create();
m_context = new QOpenGLContext(this);
m_context->setFormat(sf);
m_context->create();
m_context->makeCurrent(this);
connect(&m_timer, SIGNAL(timeout()), this, SLOT(rotate()));
m_timer.start(33);
}
void MainWindow::rotate()
{
m_angle += 0.5f;
if (m_angle >= 360.0f) {
m_angle = 0.0f;
}
update();
}
void MainWindow::initializeGL()
{
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClearDepth(-1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_GEQUAL);
}
void MainWindow::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// The Pyramid
glLoadIdentity();
glRotatef(m_angle, 1.0f, 1.0f, 1.0f);
glScalef(0.33f, 0.33f, 0.33f);
glEnable(GL_CULL_FACE);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glBegin(GL_TRIANGLES);
// Front
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f( 0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f( 1.0f, -1.0f, 1.0f);
// Back
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f( 0.0f, 1.0f, 0.0f);
glVertex3f( 1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
// Left
glColor3f(1.0f, 1.0f, 0.0f);
glVertex3f( 0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
// Right
glColor3f(1.0f, 0.0f, 1.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
// Bottom
glColor3f(0.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f( 1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f( 1.0f, -1.0f, -1.0f);
glVertex3f( 1.0f, -1.0f, 1.0f);
glEnd();
// The Cube (edges)
glLoadIdentity();
glRotatef(m_angle, 1.0f, 1.0f, 1.0f);
glScalef(0.5f, 0.5f, 0.5f);
glDisable(GL_CULL_FACE);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glColor3f(0.0f, 0.0f, 0.0f);
glBegin(GL_QUADS);
// Front
glVertex3f( 1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f( 1.0f, -1.0f, -1.0f);
// Back
glVertex3f( 1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
// Left
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
// Right
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glEnd();
glFlush();
}
void MainWindow::paintEvent(QPaintEvent*)
{
paintGL();
}
Probably this solution is far from ideal, but it does exactly what I need (any tips would be appreciated).
demo image
I'd like to thank #Swift - Friday Pie and #Asaq for such detailed answers which helped me a bit more to understand how opengl works.
If anyone is interested - I'm on Linux (Fedora 28), graphic card is Intel HD Graphics 530 (Skylake GT2).
I need help. I have found several solutions to draw cube in OpenGl, but I have a problem.
My code is:
wglMakeCurrent(pDC->m_hDC, m_hrc);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(1.5f, 0.0f, -7.0f); // Move right and into the screen
glBegin(GL_QUADS); // Begin drawing the color cube with 6 quads
// Top face (y = 1.0f)
// Define vertices in counter-clockwise (CCW) order with normal pointing out
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
// Bottom face (y = -1.0f)
glColor3f(1.0f, 0.5f, 0.0f); // Orange
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
// Front face (z = 1.0f)
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
// Back face (z = -1.0f)
glColor3f(1.0f, 1.0f, 0.0f); // Yellow
glVertex3f(1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
// Left face (x = -1.0f)
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
// Right face (x = 1.0f)
glColor3f(1.0f, 0.0f, 1.0f); // Magenta
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glEnd(); // End of drawing color-cube
glFlush();
SwapBuffers(pDC->m_hDC);
wglMakeCurrent(NULL, NULL);
Bellow is the image I want to get and the image I get.
expected image and the image I get
How to solve this problem?
You'll have to enable depth testing. Otherwise triangles are drawn in the order the are given in code which leads to overdraws even if a triangle is behind another one. To enable depth-testing call
glEnable(GL_DEPTH_TEST);
before issuing the first draw-call.
I'm a complete beginner with OpenGL, just trying to learn (starting with freeglut for the moment). So far I have the following code that should draw some basic 3D objects. The problem is that whatever I put in the render function (although it does execute), it only displays a blank window.
#include "stdafx.h"
#include <iostream>
#include "dependente\glew\glew.h"
#include "dependente\freeglut\glut.h"
void render()
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_DEPTH_TEST);
glTranslatef(-1.5f, 1.0f, -6.0f); // Translate back and to the left
glPushMatrix(); // Push the current modelview matrix on the matrix // Rotate on all 3 axis
glBegin(GL_TRIANGLES); // Draw a pyramid
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f(0.0f, 1.0f, 0.0f); // Top of front face
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f(-1.0f, -1.0f, 1.0f); // Left of front face
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(1.0f, -1.0f, 1.0f); // Right of front face
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f(0.0f, 1.0f, 0.0f); // Top of right face
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(1.0f, -1.0f, 1.0f); // Left of right face
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f(1.0f, -1.0f, -1.0f); // Right of right face
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f(0.0f, 1.0f, 0.0f); // Top of back face
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f(1.0f, -1.0f, -1.0f); // Left of back face
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(-1.0f, -1.0f, -1.0f); // Right of back face
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f(0.0f, 1.0f, 0.0f); // Top of left face
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(-1.0f, -1.0f, -1.0f); // Left of left face
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f(-1.0f, -1.0f, 1.0f); // Right of left face
glEnd();
// Render a quad for the bottom of our pyramid
glBegin(GL_QUADS);
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f(-1.0f, -1.0f, 1.0f); // Left/right of front/left face
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(1.0f, -1.0f, 1.0f); // Right/left of front/right face
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f(1.0f, -1.0f, -1.0f); // Right/left of right/back face
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(-1.0f, -1.0f, -1.0f); // Left/right of right/back face
glEnd();
glPopMatrix();
glTranslatef(3.0f, 0.0f, 0.0f); // Translate right
glPushMatrix(); // Push the current modelview matrix on the matrix stack // Rotate the primitive on all 3 axis
glBegin(GL_QUADS);
// Top face
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f(1.0f, 1.0f, -1.0f); // Top-right of top face
glVertex3f(-1.0f, 1.0f, -1.0f); // Top-left of top face
glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom-left of top face
glVertex3f(1.0f, 1.0f, 1.0f); // Bottom-right of top face
// Bottom face
glColor3f(1.0f, 0.5f, 0.0f); // Orange
glVertex3f(1.0f, -1.0f, -1.0f); // Top-right of bottom face
glVertex3f(-1.0f, -1.0f, -1.0f); // Top-left of bottom face
glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom-left of bottom face
glVertex3f(1.0f, -1.0f, 1.0f); // Bottom-right of bottom face
// Front face
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f(1.0f, 1.0f, 1.0f); // Top-Right of front face
glVertex3f(-1.0f, 1.0f, 1.0f); // Top-left of front face
glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom-left of front face
glVertex3f(1.0f, -1.0f, 1.0f); // Bottom-right of front face
// Back face
glColor3f(1.0f, 1.0f, 0.0f); // Yellow
glVertex3f(1.0f, -1.0f, -1.0f); // Bottom-Left of back face
glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom-Right of back face
glVertex3f(-1.0f, 1.0f, -1.0f); // Top-Right of back face
glVertex3f(1.0f, 1.0f, -1.0f); // Top-Left of back face
// Left face
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(-1.0f, 1.0f, 1.0f); // Top-Right of left face
glVertex3f(-1.0f, 1.0f, -1.0f); // Top-Left of left face
glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom-Left of left face
glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom-Right of left face
// Right face
glColor3f(1.0f, 0.0f, 1.0f); // Violet
glVertex3f(1.0f, 1.0f, 1.0f); // Top-Right of left face
glVertex3f(1.0f, 1.0f, -1.0f); // Top-Left of left face
glVertex3f(1.0f, -1.0f, -1.0f); // Bottom-Left of left face
glVertex3f(1.0f, -1.0f, 1.0f); // Bottom-Right of left face
glEnd();
glPopMatrix();
glTranslatef(-1.5f, -3.0f, 0.0f); // Back to center and lower screen
glPushMatrix();
glColor3f(1.0f, 1.0f, 0.0f); // Yellow
glutSolidSphere(1.0f, 16, 16); // Use GLUT to draw a solid sphere
glScalef(1.01f, 1.01f, 1.01f);
glColor3f(1.0f, 0.0f, 0.0f); // Red
glutWireSphere(1.0f, 16, 16); // Use GLUT to draw a wireframe sphere
glPopMatrix();
}
void initGlut(int argc, char* argv[]) {
std::cout << "Initialise OpenGL..." << std::endl;
glutInit(&argc, argv);
int iScreenWidth = glutGet(GLUT_SCREEN_WIDTH);
int iScreenHeight = glutGet(GLUT_SCREEN_HEIGHT);
glutInitDisplayMode(GLUT_RGBA | GLUT_ALPHA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowPosition(120, 120);
glutInitWindowSize(600, 600);
glutCreateWindow("OpenGL");
// Register GLUT callbacks
glutDisplayFunc(render);
// Setup initial GL State
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClearDepth(1.0f);
glShadeModel(GL_SMOOTH);
glutMainLoop();
std::cout << "Initialise OpenGL: Success!" << std::endl;
}
int _tmain(int argc, char* argv[])
{
initGlut(argc, argv);
return 0;
}
Hopefully someone with more experience will let me know what obvious thing I'm missing.
Here's how I go about debugging the problem "OpenGL isn't drawing anything":
Add this code to the start of my render() function: glClearColor (1, 1, 0, 1); glClear (GL_COLOR_BUFFER_BIT); If the output turns yellow, it's calling your render() function and clearing the output properly. You can then remove that code or comment it out. If the output doesn't turn yellow, then either your render() function isn't getting called or it is, but your OpenGL state is set up not to draw to the screen. (Perhaps the wrong context is current at the time, or the color attachment for the current FBO isn't what you think it is.)
Attempt to draw a single white triangle, with no textures or shaders, centered at the origin. If it shows up, then the other geometry you're trying to draw could be wrong. If it doesn't show up, the problem could be your matrix calculations (projection or modelview matrix). (Are you pointing the "camera" where you think you are? Are your objects being drawn where you think?) It could also be lighting, blending, or depth testing. I turn all of those off for this sort of test just to be sure. (See glEnable()/glDisable() for how to turn them on and off.)
If that stuff works, I start turning on the things that I turned off above: texturing, shaders, lighting, blending, depth testing. I turn them on one at a time until something goes wrong.
If nothing goes wrong, then probably the geometry for my objects is wrong.