OpenGL internal triangle culling - some tech questions

Hi, everyone!
I am a student of Computer Science, and i’m currently occupied with the following task.
I’m doing some 3D visualization, and i’m supposed to do it both using OpenGL and my own -made graphical routines.
What i’m trying to do is: i’m trying to write my own 3d routines the way that everything would look similar to OpenGL.
So, i made transformation routines, as well as manipulations with my own Model-view and Projection matrixes, - it’s quite clear to me how it works in OpenGL, and i stumbled over a triangle visualization routine.
So, the problem is:
I’ve got 3 vertexes: v1,v2,v3, ModelView matrix: M, and Projection matrix P.
v’1=P*(Mv1);
v’2=P
(Mv2);
v’3=P
(M*v3);
After making these calculations, new coordinates are given in the canonical view volume (x=-1…1 y=-1…1 z=-1…1).
So, i should clip everything, that is out of this cube of visibility. And i’m completely lost - i don’t know how to do that, because i don’t know how OpenGL would do that clipping.
First, i was drawing every point of a triangle (v’1, v’2, v’3) pixel by pixel, checking if a current particular pixel fits a visibility cube, it’s slow, but it works.
But imagine that one vertex of the triangle was BEHIND the camera before the projection. It is projected completely wrong then, and i cannot use the drawing technique i described earlier.
What should i do?
Should i clip my triangle with the frustum planes before applying projection matrix? It’s a complicated task, and it may transform my triangle into quad or even pentagon!

I’m wondering how it is made in OpenGL?

I’d be very pleased, if somebody tells me how to solve this problem, or just gives me any link.

Thanks,
Grigory

In one software enginge that i know of (some friends of mine are working on that) and that now shows the same result in SW and ogl they clip against the frustum and as you said, it can be more triangles back from that clipping than you put in to it, and thats a thing you just have to deal with if you dont want to clip against screenspace (ie one IF before each pixel)

By the way, it might be suitable to clip only with the Front frustum plane, that would make only one or two triangles no more, - and all the rest might be clipped later…

But how does OpenGL works in such cases?
Is it possible to find out it anywhere?

hi!

generally what you should do is to find intersection of

  • frustum built for whole screen AND
  • triangle
    result could be even septa-gon (imagine each triangle’s vert intersects other screen’s edge and one of them intersects 2 screen edges)

but in fact you don’t really need to know it was 5, 6, 7-gon or whatever, what you want to know is to know left and right screen coordinates between which you want ot interpolate texture, color, whatever…

therefore instead od building new polygon from triangle you should make some clever cutting - such that you finally have two arrays:
left[SCREEN_HEIGHT] and right[SCREEN_HEIGHT]
saying what’s visible interval of your triangle on certain screen_Y coordinate, thus you can fill visible (only visible) space of it

hope it helps a bit