• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
nimnimnim

flip opengl viewport upside-down (-y)

9 posts in this topic

Hello. I'm building a Cairo app using the code from the gl-cairo-simple example found on the Cairo website.
The code where I pass opengl the image buffer is this:
[code]

glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
glClear (GL_COLOR_BUFFER_BIT);

glPushMatrix ();

glBindTexture (GL_TEXTURE_RECTANGLE_ARB, m_texture_id);
glTexImage2D (GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA,
m_width, m_height, 0, GL_BGRA, GL_UNSIGNED_BYTE, m_cairo_surface_data);

glColor3f (0.0, 0.0, 0.0);
glBegin (GL_QUADS);
glTexCoord2f (0.0f, 0.0f);
glVertex2f (0.0f, 0.0f);
glTexCoord2f ((GLfloat) m_width, 0.0f);
glVertex2f (1.0f, 0.0f);
glTexCoord2f ((GLfloat) m_width, (GLfloat) m_height);
glVertex2f (1.0f, 1.0f);
glTexCoord2f (0.0f, (GLfloat) m_height);
glVertex2f (0.0f, 1.0f);
glEnd ();

glPopMatrix ();

SDL_GL_SwapBuffers();
[/code]
how should I flip the entire viewport upside down?

any suggestions on how to change the code alltogether (perhaps glMatrixMode (GL_PROJECTION);?) would also be welcome.
0

Share this post


Link to post
Share on other sites
If you want to control which direction is up, it's usually done with the projection matrix, for example when using glOrtho or gluOrtho2d you can switch the place of the top and bottom arguments (usually 0 and height, or 0 and 1).
If you just want to flip something you draw on the Y-axis, you can use glScalef(1.0f, -1.0f, 1.0f) to manipulate the matrix to flip around the current Y=0 axis.
0

Share this post


Link to post
Share on other sites
[quote name='Erik Rufelt' timestamp='1297761413' post='4774403']
If you want to control which direction is up, it's usually done with the projection matrix, for example when using glOrtho or gluOrtho2d you can switch the place of the top and bottom arguments (usually 0 and height, or 0 and 1).
If you just want to flip something you draw on the Y-axis, you can use glScalef(1.0f, -1.0f, 1.0f) to manipulate the matrix to flip around the current Y=0 axis.
[/quote]

thanks for the reply. however that just made my viewport blank. I tried inserting it everywhere... I've never programmed opengl before so I'm totally lost here and while it'd be good to read up on it this short piece of code is really all the related opengl code in my project and there won't be any more later either I should think... :-/

could you give me some short hints on how to alter the code to use projection instead of modelview and where to use glOrtho and glScalef?
0

Share this post


Link to post
Share on other sites
What is the purpose of flipping the viewport?
If you want to display your texture upside down, then you can just change the tex-coords, so you set the tex-coord y to m_height when the vertex y is 0.0f, and change the m_height to a 0 where the vertex y is 1. This will make your texture flip vertically.
[code]
glTexCoord2f (0.0f, 0.0f/*change to m_height*/);
glVertex2f (0.0f, 0.0f);
[/code]

As for the scaling, when you draw your quad from 0,0 to 1,1 like you do now, does it cover the entire screen your just the top right corner?
When you use glScale(1,-1,1) that quad will turn to 0,0 to 1,-1. Normally 0,0 is in the middle of the screen which makes this work fine, but otherwise your quad might be outside the screen with the scale.
In addition, you might need to use glDisable(GL_CULL_FACE) when scaling like that.

When you use a projection matrix, you can specify your vertex-coordinates in screen pixels instead of from 0.0f to 1.0f.
[code]
// To specify coordinates in exact pixels, have width/height match your window-size
// If you set width to 5.0, then 5.0f is the right side of the screen
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho(0, width, height, 0, 0, 1);

glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
[/code]

In addition, you should only call glTexImage2d in init code, and not every frame, unless the texture is animated and the pixel values update every frame. glTexImage2d sets up the texture, and then it can be used extremely fast from video memory. Every time you call glTexImage2d it resets the texture and loads all the memory to the graphics card again, which is expensive. So once it is set up, you only need to call glBindTexture the next time, as OpenGL will remember the pixels.
0

Share this post


Link to post
Share on other sites
[color="#1C2837"][size="2"]the reason I'm calling glTexImage2d every frame is that I'm using opengl as a backend to a 2D graphics surface (Cairo vector graphics) like they do in [url="http://cairographics.org/OpenGL/"]http://cairographics.org/OpenGL/[/url] gl-cairo-simple example at the Cairo website which is whence all of my gl code came.
the
[color="#1C2837"][size="2"][color="#000000"]glMatrixMode [/color][color="#666600"]([/color][color="#000000"]GL_PROJECTION[/color][color="#666600"]);[/color][color="#000000"]
glLoadIdentity [/color][color="#666600"]();[/color][color="#000000"]
glOrtho[/color][color="#666600"]([/color][color="#006666"]0[/color][color="#666600"],[/color][color="#000000"] width[/color][color="#666600"],[/color][color="#000000"] height[/color][color="#666600"],[/color] [color="#006666"]0[/color][color="#666600"],[/color] [color="#006666"]0[/color][color="#666600"],[/color] [color="#006666"]1[/color][color="#666600"]);[/color][color="#000000"]

glMatrixMode [/color][color="#666600"]([/color][color="#000000"]GL_MODELVIEW[/color][color="#666600"]);[/color][color="#000000"]
glLoadIdentity [/color][/size][/color]
code made my viewport go blank aswell. OTL [/size][/color]
0

Share this post


Link to post
Share on other sites
[quote name='nimnimnim' timestamp='1297775122' post='4774448']
[color="#1c2837"][size="2"]the reason I'm calling glTexImage2d every frame is that I'm using opengl as a backend to a 2D graphics surface (Cairo vector graphics) like they do in [url="http://cairographics.org/OpenGL/"]http://cairographics.org/OpenGL/[/url] gl-cairo-simple example at the Cairo website which is whence all of my gl code came.
the
[color="#1c2837"][size="2"][color="#000000"]glMatrixMode [/color][color="#666600"]([/color][color="#000000"]GL_PROJECTION[/color][color="#666600"]);[/color][color="#000000"]
glLoadIdentity [/color][color="#666600"]();[/color][color="#000000"]
glOrtho[/color][color="#666600"]([/color][color="#006666"]0[/color][color="#666600"],[/color][color="#000000"] width[/color][color="#666600"],[/color][color="#000000"] height[/color][color="#666600"],[/color] [color="#006666"]0[/color][color="#666600"],[/color] [color="#006666"]0[/color][color="#666600"],[/color] [color="#006666"]1[/color][color="#666600"]);[/color][color="#000000"]

glMatrixMode [/color][color="#666600"]([/color][color="#000000"]GL_MODELVIEW[/color][color="#666600"]);[/color][color="#000000"]
glLoadIdentity [/color][/size][/color]
code made my viewport go blank aswell. OTL [/size][/color]
[/quote]

You said earlier that something didn't work, but he gave you more than one option. What were you referring to there? Scaling it by -1 on the y axis should work fine.
0

Share this post


Link to post
Share on other sites
Did you try disabling face culling?
When you switch the Y-axis direction every front-face becomes a back-face, so that means you need to either disable face-culling or change the winding of the front-face.
0

Share this post


Link to post
Share on other sites
I tried glDisable(GL_CULL_FACE); and then glScalef(1,-1,1); but either way my viewport is blank. really appreciate the help :) I wish I'd have a better understanding of it!
0

Share this post


Link to post
Share on other sites
Try the following.
[code]
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho(0.0, 100.0, 100.0, 0.0, -1.0, 1.0);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
glClear (GL_COLOR_BUFFER_BIT);

glPushMatrix ();

glBindTexture (GL_TEXTURE_RECTANGLE_ARB, m_texture_id);
glTexImage2D (GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA,
m_width, m_height, 0, GL_BGRA, GL_UNSIGNED_BYTE, m_cairo_surface_data);

glDisable(GL_CULL_FACE);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glDisable(GL_BLEND);
glDisable(GL_LIGHTING);
glDisable(GL_DEPTH_TEST);
glColor3f (1.0, 1.0, 1.0);
glBegin (GL_QUADS);
glTexCoord2f (0.0f, 0.0f);
glVertex2f (0.0f, 0.0f);
glTexCoord2f ((GLfloat) m_width, 0.0f);
glVertex2f (100.0f, 0.0f);
glTexCoord2f ((GLfloat) m_width, (GLfloat) m_height);
glVertex2f (100.0f, 100.0f);
glTexCoord2f (0.0f, (GLfloat) m_height);
glVertex2f (0.0f, 100.0f);
glEnd ();

glPopMatrix ();

SDL_GL_SwapBuffers();
[/code]
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0