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 8932563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:20:28+00:00 2026-06-15T09:20:28+00:00

I mixed Cocoa, GLUT and OpenGL frameworks to draw a teapot. I usually do

  • 0

I mixed Cocoa, GLUT and OpenGL frameworks to draw a teapot.
I usually do this in plain C but I need to mix up Cocoa buttons and stuffs with OpenGL, this is the corrispettive C code:

#import <OpenGL/OpenGL.h>
#import <GLUT/GLUT.h>
#import <math.h>
#include "utility.h"

GLuint width=640, height=480;
GLfloat angle=0.0;


void init()
{

    glEnable(GL_DEPTH_TEST);
    glViewport(-500, -500, 1000, 1000);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45, width/(float)height, 1, 1000);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0, 0, -100, 0, 0, 0, 0, 1, 0);

    // Luci

    glShadeModel(GL_FLAT);

    glLightfv(GL_LIGHT0, GL_AMBIENT, (const GLfloat[]) {0,0,0} );
    glLightfv(GL_LIGHT0, GL_DIFFUSE, (const GLfloat[]) {1,1,0} );
    glLightfv(GL_LIGHT0, GL_SPECULAR, (const GLfloat[]) {0.5,0.5,0} );

    glMaterialfv(GL_FRONT, GL_AMBIENT, (const GLfloat[]) {1,0,0} );
    glMaterialfv(GL_FRONT, GL_DIFFUSE, (const GLfloat[]) {1,0.25,0} );
    glMaterialfv(GL_FRONT, GL_SPECULAR, (const GLfloat[]) {1,0.75,0} );
    glMaterialfv(GL_FRONT, GL_SHININESS, (const GLfloat[]) {1,1,0} );

    glEnable(GL_LIGHT0);
    glEnable(GL_LIGHTING);

}

void display()
{
    glClearColor(BLACK);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();
    glRotatef(angle, 0, 1, 0);
    glutSolidTeapot(10);
    glPopMatrix();

    glutSwapBuffers();
}

void keyboard(unsigned char key, int x, int y)
{
    if(key=='+')
    {
        angle+=5.0;
    }
    else if(key=='-')
    {
        angle-=5.0;
    }
    else
    {
        return;
    }
    makeRound(&angle);
    glutPostRedisplay();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(width, height);
    glutCreateWindow(argv[0]);
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    init();
    glutMainLoop();
    return 0;
}

And this is the result:

enter image description here

Then I’m subclassing a NSOpenGLView and do the same of this C code, just with different sizes, but the meterials and lights are the same:

@implementation MyView

@synthesize angle;

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
    }
    return self;
}


- (void) prepareOpenGL
{
    NSOpenGLContext* context= self.openGLContext;
    GLfloat width= self.bounds.size.width;
    GLfloat height= self.bounds.size.height;

    [context makeCurrentContext];
    glEnable(GL_DEPTH_TEST);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45, width/height, 1, 1000);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0, 0, -100, 0, 0, 0, 0, 1, 0);

    // Lighting

    glShadeModel(GL_FLAT);
    glLightfv(GL_LIGHT0, GL_AMBIENT, (const GLfloat[]) {0,0,0} );
    glLightfv(GL_LIGHT0, GL_DIFFUSE, (const GLfloat[]) {1,1,0} );
    glLightfv(GL_LIGHT0, GL_SPECULAR, (const GLfloat[]) {0.5,0.5,0} );

    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, (const GLfloat[]) {1,0,0} );
    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, (const GLfloat[]) {1,0.25,0} );
    glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, (const GLfloat[]) {1,0.75,0} );
    glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, (const GLfloat[]) {1,1,0} );
    glEnable(GL_LIGHT0);
    glEnable(GL_LIGHTING);

}


- (void)drawRect:(NSRect)dirtyRect
{
    glClearColor(0, 0, 0, 0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();
    glRotatef(angle, 0, 1, 0);
    glutSolidTeapot(20);
    glPopMatrix();

    glFlush();
}

#pragma mark - Actions

- (IBAction) increaseAngle :(id)sender
{
    angle+=5.0;
    if(angle>360.0)
    {
        angle-=360.0;
    }
    [self setNeedsDisplay: YES];
}

- (IBAction) decreaseAngle:(id)sender
{
    angle-=5.0;
    if(angle<0.0)
    {
        angle+=360.0;
    }
    [self setNeedsDisplay: YES];
}

@end

And this is the result:

enter image description here

That’s pretty ugly and considering that I used the same meterials and lights, I think that I’m doing something wrong in the one done with Cocoa and OpenGL mixed.

  • 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-06-15T09:20:29+00:00Added an answer on June 15, 2026 at 9:20 am

    Make sure you request a depth buffer when you create your GL context. You may not get one by default.

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

Sidebar

Related Questions

Finding lots of mixed answers on this from Google - but mostly 2-3 years
I've received Mixed responses on this depending what walk-through I read, I've defined a
I'm writing a mixed C++/CLI DLL with both managed and unmanaged classes. Both need
I'm sending multipart/mixed content message from JSP to servlets but when I used ServletFileUpload.isMultipartContent(request);
The Mixed Authentication Disposition ASP.NET Module (MADAM) is exactly what I need for the
I have heard mixed responses on this topic, so what is a sure fire
Working on an application that contains legacy Carbon code (mixed with some Cocoa). It
This my chat mixed with threads to run server and chat at same time.
This is all mixed up in my head, and I can't wrap my head
I've created a mixed DLL (C++/CLI) and after successfully calling it from a plain

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.