Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 308217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:37:48+00:00 2026-05-12T07:37:48+00:00

I have written some code to experiment with opengl programming on Ubuntu, its been

  • 0

I have written some code to experiment with opengl programming on Ubuntu, its been a little while but I used to have a reasonable understanding of C. Since c++ i’m told is the language of choice for games programming I am trying to develop with it.

This is my first real attempt at opengl with sdl and I have gotten to this far, it compiles and runs but my camera function doesn’t seem to do anything. I know there is probably a lot better ways to do this sort of stuff but I wanted to get the basics before I moved on to more advanced stuff.

main.cpp

#include <iostream>
#include <cmath>
#include "SDL/SDL.h"
#include "SDL/SDL_opengl.h"

int screen_width = 640;
int screen_height = 480;
const int screen_bpp = 32;
float rotqube = 0.9f;

float xpos = 0, ypos = 0, zpos = 0, xrot = 0, yrot = 0, angle=0.0;
float lastx, lasty;

SDL_Surface *screen = NULL; // create a default sdl_surface to render our opengl to

void camera (void) {
glRotatef(xrot,1.0,0.0,0.0);  // x-axis (left and right)
glRotatef(yrot,0.0,1.0,0.0);  // y-axis (up and down)
glTranslated(-xpos,-ypos,-zpos); // translate the screen to the position
SDL_GL_SwapBuffers();
}

int DrawCube(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

glTranslatef(0.0f, 0.0f,-7.0f);
glRotatef(rotqube,0.0f,1.0f,0.0f);
glRotatef(rotqube,1.0f,1.0f,1.0f);
glBegin(GL_QUADS);
    glColor3f(0.0f,1.0f,0.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);
    glColor3f(1.0f,0.5f,0.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);
    glColor3f(1.0f,0.0f,0.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);
    glColor3f(1.0f,1.0f,0.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);
    glColor3f(0.0f,0.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);
    glColor3f(1.0f,0.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();
SDL_GL_SwapBuffers();

rotqube +=0.9f;
return true;
}

bool init_sdl(void)
{
if( SDL_Init( SDL_INIT_EVERYTHING ) != 0 )
{
    return false;
}

SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );

// TODO: Add error check to this screen surface init
screen = SDL_SetVideoMode( screen_width, screen_height, screen_bpp, SDL_OPENGL | SDL_HWSURFACE | SDL_RESIZABLE );

return true;
}

static void init_opengl()
{
float aspect = (float)screen_width / (float)screen_height;
glViewport(0, 0, screen_width, screen_height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, aspect, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
glClearColor(0.0, 0.0 ,0.0, 0);
glEnable(GL_DEPTH_TEST);
}

void heartbeat()
{
float xrotrad, yrotrad;
int diffx, diffy;

SDL_Event event;

while(1)
{
    while(SDL_PollEvent(&event))
    {
        switch(event.type)
        {
            case SDL_KEYDOWN:
                switch(event.key.keysym.sym)
                {
                    case SDLK_ESCAPE:
                        exit(0);
                        break;
                    case SDLK_w:
                        yrotrad = (yrot / 180 * 3.141592654f);
                        xrotrad = (xrot / 180 * 3.141592654f);
                        xpos += (float)sin(yrotrad);
                        zpos -= (float)cos(yrotrad);
                        ypos -= (float)sin(xrotrad);
                        std::cout << "w pressed" << std::endl;
                        break;
                    case SDLK_s:
                        yrotrad = (yrot / 180 * 3.141592654f);
                        xrotrad = (xrot / 180 * 3.141592654f);
                        xpos -= (float)sin(yrotrad);
                        zpos += (float)cos(yrotrad);
                        ypos += (float)sin(xrotrad);
                        break;
                    case SDLK_d:
                        yrotrad = (yrot / 180 * 3.141592654f);
                        xpos += (float)cos(yrotrad) * 0.2;
                        zpos += (float)sin(yrotrad) * 0.2;
                            break;
                        case SDLK_a:
                        yrotrad = (yrot / 180 * 3.141592654f);
                        xpos -= (float)cos(yrotrad) * 0.2;
                        zpos -= (float)sin(yrotrad) * 0.2;
                            break;
                    default:
                        break;
                }
                break;

            case SDL_MOUSEMOTION:
                diffx=event.motion.x-lastx; //check the difference between the current x and the last x position
                diffy=event.motion.y-lasty; //check the difference between the current y and the last y position
                lastx=event.motion.x; //set lastx to the current x position
                lasty=event.motion.y; //set lasty to the current y position
                xrot += (float)diffy; //set the xrot to xrot with the addition of the difference in the y position
                yrot += (float)diffx;    //set the xrot to yrot with the addition of the difference in the x position
                break;

            case SDL_QUIT:
                exit(0);
                break;

            case SDL_VIDEORESIZE:
                screen = SDL_SetVideoMode( event.resize.w, event.resize.h, screen_bpp, SDL_OPENGL | SDL_HWSURFACE | SDL_RESIZABLE );
                screen_width = event.resize.w;
                screen_height = event.resize.h;
                init_opengl();
                std::cout << "Resized to width: " << event.resize.w << " height: " << event.resize.h << std::endl;
                break;

            default:
                break;
        }
    }

    DrawCube();
    camera();

    SDL_Delay( 50 );
}
}

int main(int argc, char* argv[])
{
if( init_sdl() != false )
{
    std::cout << "SDL Init Successful" << std::endl;
}

init_opengl();

std::cout << "Hello World" << std::endl;

heartbeat();    // this is essentially the main loop

SDL_Quit();

return 0;
}

Makefile

all:
g++ -o test main.cpp -lSDL -lGL -lGLU

It compiles and runs, I guess I just need some help with doing the camera translation. Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T07:37:49+00:00Added an answer on May 12, 2026 at 7:37 am

    Remove the glLoadIdentity() call from DrawCube(). Replace it with glPushMatrix() at the beginning and glPopMatrix() at the end. Now pressing ‘w’ does something. (I am not entirely sure what it is supposed to do.)

    The problem is glLoadIdentity clears all the previous transformations set up with glTranslatef and the like. Detailed description: http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/pushmatrix.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 165k
  • Answers 165k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There are a couple of variants of a rename command,… May 12, 2026 at 12:33 pm
  • Editorial Team
    Editorial Team added an answer There is currently no way to build CLS-compliant assemblies from… May 12, 2026 at 12:33 pm
  • Editorial Team
    Editorial Team added an answer You might want to look at Google Protocol Buffers or… May 12, 2026 at 12:33 pm

Related Questions

I'm a C# developer who's fumbling in the first VB code he's written since
I have a class (in C++), call it Data , that has thousands of
I do not currently have this issue , but you never know, and thought
I have written some code to do a bulk update of a gridview. In
In a constructor in Java, if you want to call another constructor (or a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.